1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

gcovrのサンプル

Last updated at Posted at 2023-12-08

Gcovrのサンプル

sample.cppのカバレッジをどのように測定しているかわかるかもしれないサンプル

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char * argv[]) {
    int i1 = atoi(argv[1]);
    int i2 = atoi(argv[1]);
#if 0
    printf("i1 = %d, i2 = %d\n", i1, i2);
    if(i1 == 1 && i2 == 1){
        printf("argv[1] = 1, argv[2] = 1\n");
    } else {
        printf("argv[1] != 1\n");
    }
#endif

    printf("i1 = %d\n", i1);
    if(i1 == 1){
        printf("argv[1] = 1\n");
    } else {
        printf("argv[1] != 1\n");
    }

    for(int j = i1; j < 3; j++)
    {
        if(j == 2){
            printf("j = 2\n");
        } else {
            printf("j != 2\n");
        }
        printf("j=%d\n", j);
       
    }

    return 0;
}


install gcovr

This is an example.

pip install gcovr

Build sample.cpp

g++ sample.cpp -o sample -fprofile-arcs -ftest-coverage -fPIC

Clean gcov files

rm sample.gcda
rm sample.gcno

Execute sample

This is an example. Please change parameter and execute several times.

./sample 100

Create gcov report

gcovr -v -b --html-details repo/ -r .

Watch gcov report

Please see 'repo/coverage_details.functions.html'.

image.png

  • if (i1 == 1) では2分岐
  • if ((i1 == 1) && (i2 == 1)) では4分岐

となる
gcovrでは単純に条件文の数 x 2 が分岐数の総数になっているように思える。

image.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?