Tuesday, April 10, 2012

Trying out Mosh

Found another tool from HN with over 700 upvotes. Why not give it a try?!?

Mosh is another terminal application which handles unstable internet connection. It also employs a local buffer so that you get instant response after each keystroke. The latter is what I have always wanted to see for a very long time. I have a VPS account with RackSpace, and with crappy internet connection here it took very long before each character appears on my screen after each keystroke, making it very hard to work with.

Notes for the problems I encountered while trying to install and use Mosh:
  • I have to install Mosh on both the server and the client. I installed Mosh on both my the OS X and my Ubuntu 64-bit server.
  • OS X's terminal is, by default, sending its LC_* environment variable when you SSH to another machine. LC_CTYPE is set to UTF-8 by default and it is fine under OS X Lion. But when this is send to my Ubuntu server, it caused a problem with setlocale call. Must comment out SendEnv line in /etc/ssh_config on my Mac. Thanks to tip from this page! - http://thegreyblog.blogspot.com/2012/02/fixing-mac-os-x-lions-ssh-utf-8-issues.html
With this, I can mosh to my Ubuntu server, the same way as I do with ssh:

$ mosh m3rlinez@gantserver.com

I got the response from each keystroke almost instantly as advertised, albeit there was usually an initial long lag before I could type smoothly. This was impressive as the response time was waaaay better than a normal ssh. Way to go Mosh!


Sunday, September 25, 2011

Reply: Some thoughts on tests' maintainability

This was to reply to an interesting post of my friend. Unfortunately, Blogger is not able to handle my long comment. Maybe it is too long, or may Blogger think I am a spammer. Anyway, the comment is long enough for me to dedicate one of my post here for it:


Interesting post. I'm in my first years practicing unit testing too. Won't miss leaving a long comment here!

It seems the very problem about unit testing for you is about maintainability of the test cases. It was a question for me too about how many tests to write for particular class. The lower number of tests you write, the lower maintainance problems, right?

There are some occassions I avoid writing tests for. I am not claiming these are right or wrong but it seems to work for me.

#1: During our undergrad years, I was introduced to unit testing (with JUnit.) What I remembered are that we are encouraged to write tests for as many methods as possible. We even generated the tests for plain getter/setters. Come to think of it now, that was totally unpractical!

So now I just skip the simple methods I am sure there are only silly test cases for it.

#2: Another thing is about writing test for private methods. When you design a class, you are giving your users some kind of contract by providng public methods. These are something that should be broken or be changed often. If you need to change these often, you may have to admit the class is incorrectly designed and thus rewrite/add the tests.

However, I do not test private methods. This give me some room of flexiblity where I can change/optimize/organize the inner working of the class. I can change the signature/working of private methods any way I would like to, but I just need to make sure the public methods do pass the tests.


#3: I do have some tests you referred to as 'small acceptance tests.' However, to perform 'unit' testing, you test one and only one class. If the class depends on some other concrete classes then you have to change them to abstract/interface instead. If you are testing more than one classes than that may be considered as 'integration' testing, or some other names.

That was quite hard core! While I know this, I don't mock out some dependencies for small classes. That would require some efforts. But still, I would write integration tests for it.


Clearly, innovations in our world do not start with requirements from users. So all these models starting from requirement gathering are all WRONG. If you want to introduce something now, use whatever you like and do whatever you want to. But if you are building something big, you must also need to look into the rear mirrors and you do need some confidence going forward. Unit tests, test coverage, etc. are some of the answers to that.



Sunday, July 10, 2011

Generating Permutation with Javascript

I got hit by an interesting problem of “Zebra Puzzle” the other day. It is a well known logic puzzle believed to be first invented by Albert Einstein.

The question here is, how do we use our favorite programming language to solve this problem?

The first obstacle I found is that we need to generate all permutation of given list. Say if we have [1,2,3], we have a total of 3! = 6 which are [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. I couldn’t find simple Javascript code for this so I ended up writing one. Reinventing the wheel is fun, isn’t it? :))

Wikipedia suggests the following algorithm for generating all permutation systematically.

The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place.

  1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
  2. Find the largest index l such that a[k] < a[l]. Since k + 1 is such an index, l is well defined and satisfies k < l.
  3. Swap a[k] with a[l].
  4. Reverse the sequence from a[k + 1] up to and including the final element a[n].

Here is my PermutationGenerator module implemented in Javascript.

/////////////////////////////////////////////
// Permutation Generator
// - generate all permutation of given string
// array
// - Natthawut Kulnirundorn <m3rlinez at email by google>
// http://www.solidskill.net 10 July 2011
/////////////////////////////////////////////

var PermutationGenerator = (function () {
var self = {};

// Get start sequence of given array
self.getStartSequence = function (list) {
return list.slice(0).sort();
};

// Get next sequence from given array
// Ref: http://en.wikipedia.org/wiki/Permutation#Systematic_generation_of_all_permutations
self.getNextSequence = function (list) {
// Make clone
var a = list.slice(0);

//The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place.
// 1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
var k = -1;
for (var i = 0; i < a.length - 1; ++i) {
if (a[i] < a[i + 1]) { k = i; }
}
if (k == -1) return null; // means this is the last one

// 2. Find the largest index l such that a[k] < a[l]. Since k + 1 is such an index, l is well defined and satisfies k < l.
var l = -1;
for (var i = 0; i < a.length; ++i) {
if (a[k] < a[i]) { l = i };
}
if (l == -1) return null; // impossible

// 3. Swap a[k] with a[l].
var tmp = a[k]; a[k] = a[l]; a[l] = tmp;

// 4. Reverse the sequence from a[k + 1] up to and including the final element a[n].
var next = a.slice(0, k + 1).concat(a.slice(k + 1).reverse());

return next;
};

return self;
} ());





Test cases, also serve as a user guide :)



/////////////////////////////////////////////
// Test Cases
/////////////////////////////////////////////

var PermutationGeneratorTest = (function () {
var self = {};

function getStartSequence_Test1() {
var a = ['red', 'white', 'green', 'yellow', 'blue'];
var res = PermutationGenerator.getStartSequence(a);
log('input = ' + a);
log('output = ' + res);
}

function generateSequence(list) {
var current = PermutationGenerator.getStartSequence(list);
var count = 1;
log('start = ' + current);
while (true) {
current = PermutationGenerator.getNextSequence(current);
if (current == null) { break; }
log('next' + (++count) + ' = ' + current);
}
}

function getNextSequence_TestNormal() {
generateSequence(['English', 'Spanish', 'Japanese']);
}

function getNextSequence_TestEmpty() {
generateSequence([]);
}

function getNextSequence_TestRepeat() {
generateSequence(['gant', 'gant', 'korkore', 'jan']);
}

self.runTests = function () {
log("=== getStartSequence 1 ===");
getStartSequence_Test1();
log("=== getNextSequence Normal ===");
getNextSequence_TestNormal();
log("=== getNextSequence Empty ===");
getNextSequence_TestEmpty();
log("=== getNextSequence Repeat ===");
getNextSequence_TestRepeat();
};

return self;
} ());







I have put the working example on http://www.solidskill.net/ZebraPuzzle.htm. There are other parts of code that search through the solution space, setup the constraints for Zebra puzzle. But they are not discussed here.



There are some things to note about this implementation though. First, it is not very efficient. You can see in the getNextSequence(..) loop that there are loops used to search for k and l that satisfy the conditions.



Second, there could be problem with list of integers. The Array.sort() in Javascript sort using string interpretation by default. So [4, –1, 100, 500].sort() would return [-1, 100, 4, 500] instead of expected [-1, 4, 100, 500]. One must give Array.sort() function the comparer in order to properly sort list of integer. I design this module to work with list of strings initially.



Hope this helps those who are looking for permutation generation code.