views:

265

answers:

5

Let's say I have this code:

int function(bool b)
{
    // execution path 1
    int ret = 0;
    if(b)
    {
        // execution path 2
        ret = 55;
    }
    else
    {
        // execution path 3
        ret = 120;
    }
    return ret;
}

I need some sort of a mechanism to make sure that the code has gone in any possible path, i.e execution paths 1, 2 & 3 in the code above.

I thought about having a global function, vector and a macro.
This macro would simply call that function, passing as parameters the source file name and the line of code, and that function would mark that as "checked", by inserting to the vector the info that the macro passed.

The problem is that I will not see anything about paths that did not "check".
Any idea how do I do this? How to "register" a line of code at compile-time, so in run-time I can see that it didn't "check" yet?

I hope I'm clear.

+1  A: 

Quite often there will be a utility supplied with your compiler to do this sort of code coverage analysis. For example, GCC has the gcov utility.

Greg Hewgill
Ok, and what about VC9?
Poni
+4  A: 

Usually coverage utilities (such as gcov) are supplied with compiler. However please note that they will usually give you only C0 coverage. I.e.

  • C0 - every line is executed at least once. Please note that a ? b : c is marked as executed even if only one branch have been used.
  • C1 - every branch is executed at least once.
  • C2 - every path is executed at least once

So even if your tests shows 100% C0 coverage you may not catch every path in code - and probably you don't have time to do it (number of paths grows exponentially with respect to branches). However it is good to know if you have 10% C2 or 70% C2 (or 0.1% C2).

Maciej Piechotka
Hi Maciej, thanks for a really informative answer. My problem with it is that, and I quote from gcov's manual, "gcov works only on code compiled with GCC". You see I use VC9, Windows. Do you have any advice?
Poni
http://www.google.co.uk/search?q=coverage+Visual+Studion+C%2B%2B , http://stackoverflow.com/questions/616977/whats-a-good-code-coverage-tool-for-use-with-visual-studio-c . I don't use VS so I cannot recommend anything specific.
Maciej Piechotka
A: 

You can use FILE and LINE preprocessor directives:

#define TRACE(msg) MyTraceNotify(msg,__FILE__,__LINE__)

Just insert TRACE(msg) macro in your code at the places you want to track, with your custom message, and write your MyTraceNotify function.

void MyTraceNotify(const char *msg, const char *filename, ULONG line)
{
    /* Put your code here... */    
}
Jorg B Jorge
+1  A: 

You need a code coverage program (gcov, bullseye, dev partner) and unit-testing (unittest++, cppunit, etc.). You write test that will test that function.

TEST( UnitTestFunction )
{
    CHECK( function(true) == 55 );
    CHECK( function(false) == 120 );
}

Then unit tests in this case do not just check for integrity (though they still do) but they also test for coverage.

graham.reeds
A: 

Try SD C++ TestCoverage for a VisualStudio compatible test coverage tool. I believe that it in fact actually will tell you about test coverage of a?b:c, too.

Ira Baxter