gcov and switch statements

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-05-11T19:04:42Z Indexed on 2010/05/11 19:44 UTC
Read the original article Hit count: 329

Filed under:
|
|
|

I'm running gcov over some C code with a switch statement. I've written test cases to cover every possible path through that switch statement, but it still reports a branch in the switch statement as not taken and less than 100% on the "Taken at least once" stat.

Here's some sample code to demonstrate:

#include "stdio.h"

void foo(int i)
{
    switch(i)
    {
        case 1:printf("a\n");break;
        case 2:printf("b\n");break;
        case 3:printf("c\n");break;
        default: printf("other\n");
    }
}

int main()
{
    int i;
    for(i=0;i<4;++i)
        foo(i);
    return 0;
}

I built with "gcc temp.c -fprofile-arcs -ftest-coverage", ran "a", then did "gcov -b -c temp.c". The output indicates eight branches on the switch and one (branch 6) not taken.

What are all those branches and how do I get 100% coverage?

© Stack Overflow or respective owner

Related posts about c++

Related posts about gcc