I'm using lcov2.0 to check my test branch coverage, with encounter this issue.
// source code
void test_string_plus(const string& local ,const string& remote)
{
static string recv_msg;
static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
}
// test code
TEST(teststring, teststringplus)
{
string local = "test1";
string remote = "test12";
EXPECT_NO_THROW(test_string_plus(local, remote));
local = "test21";
remote = "test2";
EXPECT_NO_THROW(test_string_plus(local, remote));
}
It reports like this in lcov
281 : 2 : void test_string_plus(const string& local ,const string& remote)
282 : : {
283 : 2 : static string recv_msg;
284 [ + + + - : 2 : static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
+ - + - +
- + - + +
+ + - - -
- ]
285 : 2 : }
gcov file shows that this code contains a lot of uncovered branches with throw.
2: 284: static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
call 0 returned 2
branch 1 taken 1 (fallthrough)
branch 2 taken 1
call 3 returned 1
branch 4 taken 1 (fallthrough)
branch 5 taken 0 (throw)
call 6 returned 1
branch 7 taken 1 (fallthrough)
branch 8 taken 0 (throw)
call 9 returned 1
branch 10 taken 1 (fallthrough)
branch 11 taken 0 (throw)
call 12 returned 1
branch 13 taken 1 (fallthrough)
branch 14 taken 0 (throw)
call 15 returned 2
branch 16 taken 2 (fallthrough)
branch 17 taken 0 (throw)
call 18 returned 2
call 19 returned 2
call 20 returned 2
branch 21 taken 1 (fallthrough)
branch 22 taken 1
call 23 returned 1
branch 24 taken 1 (fallthrough)
branch 25 taken 1
call 26 returned 1
call 27 never executed
branch 28 never executed
branch 29 never executed
call 30 never executed
branch 31 never executed
branch 32 never executed
call 33 never executed
2: 285:}
tools version and command I used.
g++ -std=c++17 test.cpp -o test -lgtest -lgtest_main -pthread -fprofile-arcs -ftest-coverage -fprofile-update=atomic && \
./test && \
gcov -b -c -o . test.cpp && \
lcov --capture \
--rc branch_coverage=1 \
--directory . \
--filter branch \
--output-file coverage_all.info \
--ignore-errors mismatch && \
genhtml coverage_all.info \
--rc branch_coverage=1 \
--output-directory coverage_report && \
rm *.info
I already used --filter branch
to ignore some unreachable branches
How should I reduce the uncovered branches here? Or can I just ignore them?
https://legacy.cplusplus.com/reference/string/string/operator+/
I checkout reference of operator+()
in std::string.
It seems that an exception will only be thrown when attempting to add a string that exceeds the length limit or when the memory application fails, but in practical projects, constructing such test conditions is very unreasonable.
I tried to modify ?:
to if else, those branches caused by string can be filter.
281 : 2 : void test_string_plus(const string& local ,const string& remote)
282 : : {
283 : 2 : static string recv_msg;
284 : 2 : string target("/");
285 [ + + ]: 2 : if(local<remote)
286 : : {
287 : 1 : target += local + "_" + remote;
288 : : }
289 : : else{
290 : 1 : target += remote + "_" + local;
291 : : }
292 : 2 : static_cast<void>(recv_msg.assign(target));
I tried to modify ?:
to if else, those branches caused by string can be filter by lcov --filter branch
options
281 : 2 : void test_string_plus(const string& local ,const string& remote)
282 : : {
283 : 2 : static string recv_msg;
284 : 2 : string target("/");
285 [ + + ]: 2 : if(local<remote)
286 : : {
287 : 1 : target += local + "_" + remote;
288 : : }
289 : : else{
290 : 1 : target += remote + "_" + local;
291 : : }
292 : 2 : static_cast<void>(recv_msg.assign(target));