It is also important to note that gcov_scan also results in an annotated source file in which it is specified which line or block has not been executed. This is needful in order to design effective test cases that achieves more coverage.
/* * * This tag must be somewhere in the code * * * GCOV EXPECT 60 * */The regular expression of the GCOV expect tag is as follows :
".*\/*GCOV\ *EXPECT\ *[0-9]+\ *(BRANCH)?.*"
When BRANCH is specified, branch coverage will be in effect for the scan.
..... catch( ... ) { // <INFEASIBLE_COVERAGE> // <REASON> We don't know the type of exception to expect here </REASON> ...... (code that cannot be tested) // </INFEASIBLE_COVERAGE> }When the code of a class contains infeasible coverage parts, these must documenten as shown by the previous example and discussed with another designer/implementer.
erwinn>> more test.cpp #include <iostream> using std::cout; using std::endl; // QACPP EXPECT 4327; argv will not be used int main ( int argc, const char* argv ) { cout << "hello nerd" << endl; switch( argc ) { case 1: { cout << "nice" << endl; break; } case 2: { cout << "also nice" << endl; break; } default: { cout << "something is wrong" << endl; break; } } return 0; }Compiling this code using the gcov_compiler will prepare the application for coverage analysis:
erwinn>> gcov_compiler test.cpp erwinn>> ls a.out test.bb test.bbg test.cpp test.o erwinn>>Typically, files with extension .bb and .bbg are generated. These file are needed by the coverage analysis. Executing the program will result in a .da file which contains coverage information collected during the execution:
erwinn>> gcov_compiler test.cpp erwinn>> ls a.out test.bb test.bbg test.cpp test.o erwinn>> ./a.out hello nerd nice erwinn>> ls a.out test.bb test.bbg test.cpp test.da test.o erwinn>>The results can be analysed by the gcov_scan tool:
uns67c86-di502> gcov_scan In file : test.cpp 72% (statement) coverage Verdict : fail erwinn>>gcov_scan will automatically generate a file named test.cpp.gcov containing annotated source code. This file show which part of the source has not been covered yet. This helps to come up with new ideas for test input.
erwinn>> more test.cpp.gcov #include <iostream> using std::cout; using std::endl; 1 int main ( int argc, const char* argv ) 1 { 1 cout << "hello nerd" << endl; switch( argc ) { case 1: 1 { 1 cout << "nice" << endl; break; } case 2: ###### { ###### cout << "also nice" << endl; break; } default: ###### { 1 cout << "something is wrong" << endl; break; } } 1 1 return 0; } erwinn>>By default 80% statment coverage must be achieved, therefore gcov_scan issues this warning. If we would add an extra argmument on the command line, we will achieve more coverage:
erwinn>> ./a.out try2 hello nerd also nice erwinn>> gcov_scan Verdict : success Now, more than 80\% of statement coverage is achieved. The test.cpp.gcov file shows which part has still not been excersized: \begin{verbatim} erwinn>> more test.cpp.gcov #include <iostream> using std::cout; using std::endl; // QACPP EXPECT 4327; argv will not be used 2 int main ( int argc, const char* argv ) 2 { 2 cout << "hello nerd" << endl; switch( argc ) { case 1: 1 { 1 cout << "nice" << endl; break; } case 2: 1 { 1 cout << "also nice" << endl; break; } default: ###### { 2 cout << "something is wrong" << endl; break; } } 2 2 return 0; } erwinn>>When this would not be a piece of safety critical software, this coverage would be sufficient. From this we conclude that two test cases are needed to achieved the required percentage of coverage.