如何减少此代码中包含 std::string operator+ 的未覆盖分支?

问题描述 投票:0回答:1

我正在使用lcov2.0检查我的测试分支覆盖率,遇到这个问题。

// 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));
}

它在 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 文件显示此代码包含许多带有 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:}

我使用的工具版本和命令。

  • g++/gcov (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
  • gtest 1.12.x
  • lcov/genhtml:LCOV 版本 2.0-1
    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

我已经使用

--filter branch
忽略了一些无法到达的分支


如何减少这里未覆盖的树枝?或者我可以忽略它们吗?

https://legacy.cplusplus.com/reference/string/string/operator+/

我在 std::string 中查看了

operator+()
的参考。 看起来只有在尝试添加超过长度限制的字符串或者内存申请失败时才会抛出异常,但在实际项目中,构建这样的测试条件是非常不合理的。


我尝试修改

?:
为if else,那些由字符串引起的分支可以被过滤。

    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));
c++ googletest gcov lcov
1个回答
0
投票

我尝试修改

?:
为if else,那些由字符串引起的分支可以通过lcov过滤
--filter branch
选项

    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));
© www.soinside.com 2019 - 2024. All rights reserved.