jeudi 23 juin 2016

Making sense of gcov branch-coverage

I am currently trying to get sensible code-coverage results for a C++ project with gcov, but the results I've got until now are quite confusing. I have implemented a class to read a configuration from a file, the implementation of the methods is quite basic

FileConfigurationProvider::FileConfigurationProvider(std::string file_name)
{
    m_file_name = file_name;
}

std::string FileConfigurationProvider::get_configuration()
{
    std::ifstream t(m_file_name);
    return std::string((std::istreambuf_iterator<char>(t)),
        std::istreambuf_iterator<char>());
}

void FileConfigurationProvider::set_configuration(std::string configuration)
{
    std::ofstream t(m_file_name);
    t << configuration;
}

bool FileConfigurationProvider::exists()
{
    return access(m_file_name.c_str(), F_OK) != -1;
}

My unit-tests cover all of these methods, but when I run gcov with the branches option I get the following results

File 'src/FileConfigurationProvider.cpp'
Lines executed:100.00% of 12
Branches executed:100.00% of 10
Taken at least once:50.00% of 10
Calls executed:80.95% of 21
Creating 'FileConfigurationProvider.cpp.gcov'

Apart from the fact that I have not been able to distinguish between Branches executed and Taken at least once the Taken at least once value does not make any sense to me. There is no branching in the code, hence I would count 4 branches of which 4 have been taken (the 4 methods). I'd guess that the issue is somehow related with the usage of the STL-classes and inlining of code, but I do not have any clue how to work this around. I've tried the -O0 option, but this did not change anything.

I wouldn't want to renounce the usage of the STL, but the results are of little use to me. Line coverage could be an option, but I'd really prefer branch coverage.

Aucun commentaire:

Enregistrer un commentaire