Tutorial on Testing your code

A crucial step in programming is to test the code we write. In this tutorial we will see a simple way to test our code. Lets say we want to write two functions:
  • int find(int[] array,int value) A function that returns the first index in the given array in which the given value is found. In case the value does not appear in the array, the result will be -1.
  • int[] minAndMax(int[] array) A function that finds the minimal and maximal numbers in a given array. The result array will be of size 2, with the minimal number in the first index, and the maximal in the second.

  1. public static int find(int[] arrayint value) {
  2.         int position = -1;
  3.         boolean found = false;
  4.         for (int i=0i < array.length && !foundi=i+1) {
  5.             if (array[i] == value) {
  6.                 position = i;
  7.             }
  8.         }
  9.         return position;
  10.     }

  1. public static int[] minAndMaX(int[] array) {
  2.         //assuming the first number in the array is both the minimal and the maximal
  3.         int min = array[0];
  4.         int max = array[0];
  5.         for (int i=0i < array.lengthi=i+1) {
  6.             if (array[i] < min) {
  7.                 min = array[i];
  8.             }
  9.  
  10.             if (array[i] > max) {
  11.                 max = array[i];
  12.             }
  13.         }
  14.         int[] res = {min,max};
  15.         return res;
  16.     }

After we wrote the code, we need to check if its working. In java, the main function is the entry point of the application and will invoke all the commands required by your program.

Inside the main function we need to write tests (or as we will see, usually call test functions) for our functions. It is important to think about tests that cover a wide range of cases, do not just write trivial tests. Lets see an example for testing the first function (find)

  1. public static void main(String[] args) {
  2.         int[] array = {11, -4321912};
  3.         System.out.println("running test 1 for function find");
  4.         int val1 = 21;
  5.         int res1 = find(array,val1);
  6.         int expected1 = 2;
  7.         System.out.println("looked for " + val1 + ". got: " + res1 + " expected: " + expected1);
  8.  
  9.         System.out.println("running test 2 for function find");
  10.         int val2 = 31;
  11.         int res2 = find(array,val2);
  12.         int expected2 = -1;
  13.         System.out.println("looked for " + val2 + ". got: " + res2 + " expected: " + expected2);
  14.     }

If we'll compile the code and run it, the results of our tests should be displayed on the screen. An example of the output:

Running test 1 for function find
looked for 21. got: 2 expected: 2
Running test 2 for function find
looked for 31. got: -1 expected: -1

Now we would like to add tests for the second function (minAndMax), but what about the tests for the find function?
we can delete them / comment them - the problem with deletion or commenting is that maybe later we would modify the function and still want to know if we passed our tests! To avoid this situation, we can just add the tests for minAndMax after the tests for the previous function.

The problem is that after adding code to test several functions, the main will be very long and un-readable. An elegant solution for this problem is to write function tests for each function in a separate test-function and call it from main only when we want to.
For example, we can create a function testFind and move the code from main.

  1. public static void testFind() {
  2.         int[] array = {11, -4321912};
  3.         System.out.println("running test 1 for function find");
  4.         int val1 = 21;
  5.         int res1 = find(arrayval1);
  6.         int expected1 = 2;
  7.         System.out.println("looked for " + val1 + ". got: " + res1 + " expected: " + expected1);
  8.  
  9.         System.out.println("running test 2 for function find");
  10.         int val2 = 31;
  11.         int res2 = find(arrayval2);
  12.         int expected2 = -1;
  13.         System.out.println("looked for " + val2 + ". got: " + res2 + " expected: " + expected2);
  14.     }

The main function will now look like this:

  1. public static void main(String[] args) {
  2.         testFind();
  3.     }

The next step is to create a function to test minAndMax and invoke it from main, but if we want to print arrays we should also add a helping function that prints an array:

  1. public static void printArray(int[] array) {
  2.         if (array == null) {
  3.             System.out.println("null");
  4.         } else {
  5.             for (int i = 0i < array.lengthi++) {
  6.                 System.out.print(array[i] + " ");
  7.             }
  8.             System.out.println();
  9.         }
  10.     }

The testMinAndMax function may look like this:

  1. public static void testMinAndMax() {
  2.         System.out.println("Running test 1 for function minAndMax");
  3.         int[] array1 = {316110, -1315341932};
  4.         int[] res = minAndMaX(array1);
  5.         int[] expected = {-1361};
  6.         System.out.print("Input array = ");
  7.         printArray(array1);
  8.         System.out.print("Result: ");
  9.         printArray(res);
  10.         System.out.print("Expected: ");
  11.         printArray(expected);
  12.     }

And we can add the invoke of the test function in main

  1. public static void main(String[] args) {
  2.         testFind();
  3.         testMinAndMax();
  4.     }

The output will now be all the output from both tests:

Running test 1 for function find
looked for 21. got: 2 expected: 2
Running test 2 for function find
looked for 31. got: -1 expected: -1
Running test 1 for function minAndMax
Input array = 31 61 10 -13 15 34 19 32 
Result: -13 61 
Expected: -13 61 

We don't really want to run the tests for all our functions every time, so we can comment in main the lines for all the test we don't want to execute.