提问人:musnow 提问时间:11/14/2023 最后编辑:musnow 更新时间:11/14/2023 访问量:69
如何减少此代码中包含std::string operator+的未覆盖分支?
How could I reduce uncovered branches in this code contains std::string operator+?
问:
我正在使用 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+()
我试图修改为,如果不然,那些由字符串引起的分支可以被过滤。?:
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));
答: 暂无答案
评论
std::string
--filter branch
using namespace std;
std::
using String = std::string;
std::string