Thoughts on Unit Testing

The java community is very fortunate to have a standard unit testing tool in JUnit. In order to demonstrate how to use the tool, the JUnit documentation illustrated it using simple examples. Now that we know how to use the tool, it now remains for us to use it effectively.

Perhaps the first thing we need to know is how do we make sure we have tested the possible scenarios of our code. In order to answer this question, we need to know the control flow of our code. In basic programming, we know that controls flows are of the form

if(statement){
doSomething();
}
while(condition){
doSomething();
}
for(...){
doSomething();
}

We can visualize these control structures using a graph. The if structure can be drawn like the figure below:

if_control_flow1.jpg

The if block is shown in the colored box. The node labeled 1 is a statement before the if block and the node labeled 4 is the statement after the if block. The two arrows from node 2 to node 3 represent the two possibilities, namely true and false.

The do and for have a similar representation. We can convert a for statement to a do statement. This is how to convert a do or for

do_control_flow.jpg

The do ... while graph is different. The loop will have to be entered first before a check is made for the loop to terminate. Here is how the do ... while graph looks like:

if_control_flow1.jpg

The next thing we want to do is to illustrate these graphical flow construction. Here is an algorithm i took from a book.

if(values.length>1){
for(int i=0;i0)
sum+=values[i];
}
} else {
sum=values[0];
}

The flow graph of this code is shown below:

sum.jpg

The for loop is highlighted. With a digram like this, it would be easy for us the determine the paths we want to exercise in our unit testing.

The possible paths of this flow graph are:

  1. b->c->d->e->f->g->h->i
  2. b->c->e->f->g->h->i
  3. a->i

To test the first flow, assuming that the path d is the false part of the if statement, a vector values whose length is greater than 1 with all entries negative.

For the second path, we need a vector with more than 1 entry and all entries positive. To test the third path, we need a vector of length 1. What would happen if the length of the vector is zero?

Syntax Highlighting code

I’m using the Code Prettify by Dean Lee.

To syntax highlight a code you have to put the code between the

<pre class="prettyprint">...</pre>

tags.

For example, a java code can be pretty-printed like this:

package test;

public class Test {
public static void main(String[] args){
System.out.println("Hello, World!");
}
}

In order to make it work, I have to put the following code to the header template of the theme i’m using.

<script type="text/javascript">
window.onload = function(){prettyPrint();};
</script>

Here is another example of a bash shell script. I modified the .css file in order to make the code bold-face.

#!/bin/bash

# Fibonacci numbers
# Writes an infinite series to stdout,
# one entry per line
function fib() {
local a=1
local b=1
while true ; do
echo $a
local tmp=$a
a=$(( $a + $b ))
b=$tmp
done
}

# output the 10th element of the series and halt
fib | head -10 | tail -1

If Einstein was a Manager

I don’t think he will be effective. He is much better as a scientist. That is his strength.

I believe that there are people really much suited for the job. They have the skills necessary to interact with people and to coordinate.

I am a technically inclined person. Being in management is something very hard to imagine. I might make a very bad job out of it.