1

I have created a simple example to illustrate the issue:

test.h

#ifndef TEST_H
#define TEST_H

class TestCoverage{
public:
/*
 *
 *
 *
 */
    int a = 5;
    int b = 10;
    void testfunc();
    explicit TestCoverage();
};

#endif // TEST_H

test.cpp

#include "test.h"
#include <iostream>

TestCoverage::TestCoverage()
{
    a=15;
    b=20;
}

void TestCoverage::testfunc(){
/*
 *
 * */
    std::cout<<"hey";
}

And added a simple test case (which checks nothing) using catch2:

test_test.cpp

#include "test.h"
#include <catch2/catch.hpp>

TEST_CASE("test", "[whatever]")
{
    auto testCoverage = new TestCoverage();
    testCoverage->testfunc();
}

Afterwards I generated a code coverage report using lcov and generated html file. Html file shows some strange results:

lcov report

The question is: why lines 11 and 12 are displayed as covered? It looks like they are completely mirroring the class member initialization in the header file (a and b initialization). If I move initialization across the header file - the covered lines in the cpp file also move. Is this a bug in lcov or am I missing something?

UPD: If I build coverage with slightly updated header file, e.g.:

#ifndef TEST_H
#define TEST_H

class TestCoverage{
public:
/*
 *
 *
 *
 */
    int a = 5;
    void testfunc();
    int b = 10;
    explicit TestCoverage();
};

#endif // TEST_H

I get report like this: lcov report upd

Note that I swapped lines 12 and 13 in the header file and it is reflected in the report file. In the report file line 12 appears as not covered and line 13 as covered

ymochurad
  • 941
  • 7
  • 15
  • On an unrelated note, default-constructors don't need to be marked as `explicit`. – Some programmer dude Mar 13 '19 at 10:13
  • In both the code you have shown and in the linked image representing the lcov report, lines 11 and 12 are comments. It is a bit of a philosophical question whether a coverage report should count comments in called functions as covered or not. – Peter Mar 13 '19 at 10:22
  • I added it as comments just to clarify the issue. If you have a look at the screenshot - part of the comment is displayed as covered and part is not. And I also had same problem when part of the function was displayed as covered and part is not while the whole function should have been displayed as not covered – ymochurad Mar 13 '19 at 11:55
  • Can you post another picture demonstrating the "*move initialization across the header file - the covered lines in the cpp file also move*" – rustyx Mar 13 '19 at 13:45
  • Sure, I just updated the original post with new screenshot – ymochurad Mar 13 '19 at 13:55
  • How did you compile? It's likely that those redundant assignments are optimised out. – Toby Speight Mar 13 '19 at 13:58
  • I am compiling with `-O0` which should disable optimisations, isn't it? – ymochurad Mar 13 '19 at 14:13

0 Answers0