Testing Code


TESTING: process of attempting to crash your code

What should you test?

POINT 1:  Test code at "boundaries"

1. Full array
2. Overfull array
3. Empty array
4. A single input item
5. Wrong kind of input
6. No input
7. Malicious input

Code boundaries:

LOOPs - carefully check first and last iterations of each loop.

POINT 2: Test PreConditions and PostConditions

	double average(double arr[], int size)
	{
	double sum=0.0;

	// test for zero to avoid a possible DIVIDE by ZERO

	if (size < =  0) return 0;

	for (int i=0;i < size;i++)
	   sum+=arr[i];
	return sum/size;
	}


POINT 3: Test small pieces of code.

NEVER test more than 1 function at a time.

You can always write a "dummy function" that calls this function just for testing.

Modular programming - write small pieces of code independent of other parts of the program.


POINT 4: Automated Testing

Write special functions that loop through input data, calling the functin that you are trying to test.

Example: you wrote a binary search function on an array of integers.
Here is a "test driver":

	for (int i=0;i<1000;i++)
	    arr[i]=i;
	for (int i=0;i<1000;i++)
	    binary_search(arr, i); // where i is the key

Test Stubs- before writing a function, you write only the header, and inside the function simply print: "so and so function was called and these are the parameters".  

POINT 5: The single most important point about testing is TO DO IT!