Code Coverage
For those that know I work for Crossroads Systems as an intern. My job is mostly to write unit tests for their code. Unit testing code is a very very useful thing to do. I’ve primarily deal with code that was never designed to ever be tested. So I am quite good at dealing with code with high complexities, calls to exit(), and system calls. So I can really deal with just about any code.
So what I want to say about code coverage of unit tests is this. It is useful in helping find holes in your unit tests where there is not a test case. While code coverage tools are not perfect in finding all holes it does help to find the ones where you may not see them.
Unit tests should try to cover nearly all possibilities that a function can do. When writing them, it is best to try to break the code and designing edge cases. For example when you have a function that tries to find the minimum element in the array and you are sure that it can never be 0, set it up so one of the elements is zero to see how the code deals with it.
But I digress. I’m going to show you how to run GNU’s code coverage tool on C/C++ programs. As an example I will be show how to do it with main.c++.
On the linux machines when you have all of the tests running and hopefully passing run these commands:
g++ -ansi -pedantic -fprofile-arcs -ftest-coverage -lcppunit -ldl -Wall -DTEST main.c++ -o main.app