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

r7xajy2e  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(158)

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

  1. // source code
  2. void test_string_plus(const string& local ,const string& remote)
  3. {
  4. static string recv_msg;
  5. static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
  6. }
  7. // test code
  8. TEST(teststring, teststringplus)
  9. {
  10. string local = "test1";
  11. string remote = "test12";
  12. EXPECT_NO_THROW(test_string_plus(local, remote));
  13. local = "test21";
  14. remote = "test2";
  15. EXPECT_NO_THROW(test_string_plus(local, remote));
  16. }

字符串
它在lcov中报告如下

  1. 281 : 2 : void test_string_plus(const string& local ,const string& remote)
  2. 282 : : {
  3. 283 : 2 : static string recv_msg;
  4. 284 [ + + + - : 2 : static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
  5. + - + - +
  6. - + - + +
  7. + + - - -
  8. - ]
  9. 285 : 2 : }


gcov文件显示,这段代码包含了大量未覆盖的throw分支。

  1. 2: 284: static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
  2. call 0 returned 2
  3. branch 1 taken 1 (fallthrough)
  4. branch 2 taken 1
  5. call 3 returned 1
  6. branch 4 taken 1 (fallthrough)
  7. branch 5 taken 0 (throw)
  8. call 6 returned 1
  9. branch 7 taken 1 (fallthrough)
  10. branch 8 taken 0 (throw)
  11. call 9 returned 1
  12. branch 10 taken 1 (fallthrough)
  13. branch 11 taken 0 (throw)
  14. call 12 returned 1
  15. branch 13 taken 1 (fallthrough)
  16. branch 14 taken 0 (throw)
  17. call 15 returned 2
  18. branch 16 taken 2 (fallthrough)
  19. branch 17 taken 0 (throw)
  20. call 18 returned 2
  21. call 19 returned 2
  22. call 20 returned 2
  23. branch 21 taken 1 (fallthrough)
  24. branch 22 taken 1
  25. call 23 returned 1
  26. branch 24 taken 1 (fallthrough)
  27. branch 25 taken 1
  28. call 26 returned 1
  29. call 27 never executed
  30. branch 28 never executed
  31. branch 29 never executed
  32. call 30 never executed
  33. branch 31 never executed
  34. branch 32 never executed
  35. call 33 never executed
  36. 2: 285:}


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

  • g++/gcov(Ubuntu 11.4.0-1ubuntu1~22.04)11.4.0
  • gtest 1.12.x
  • lcov/genhtml:LCOV版本2.0-1
  1. g++ -std=c++17 test.cpp -o test -lgtest -lgtest_main -pthread -fprofile-arcs -ftest-coverage -fprofile-update=atomic && \
  2. ./test && \
  3. gcov -b -c -o . test.cpp && \
  4. lcov --capture \
  5. --rc branch_coverage=1 \
  6. --directory . \
  7. --filter branch \
  8. --output-file coverage_all.info \
  9. --ignore-errors mismatch && \
  10. genhtml coverage_all.info \
  11. --rc branch_coverage=1 \
  12. --output-directory coverage_report && \
  13. rm *.info


我已经使用--filter branch忽略了一些无法访问的分支
我应该如何减少这里未覆盖的分支?或者我可以忽略它们吗?
https://legacy.cplusplus.com/reference/string/string/operator+/
我在std::string中检出了operator+()的引用,似乎只有在尝试添加超过长度限制的字符串或者内存应用失败时才会抛出异常,但在实际项目中,构造这样的测试条件是非常不合理的。
我试着修改?:为if else,那些由string引起的分支可以被过滤。

  1. 281 : 2 : void test_string_plus(const string& local ,const string& remote)
  2. 282 : : {
  3. 283 : 2 : static string recv_msg;
  4. 284 : 2 : string target("/");
  5. 285 [ + + ]: 2 : if(local<remote)
  6. 286 : : {
  7. 287 : 1 : target += local + "_" + remote;
  8. 288 : : }
  9. 289 : : else{
  10. 290 : 1 : target += remote + "_" + local;
  11. 291 : : }
  12. 292 : 2 : static_cast<void>(recv_msg.assign(target));

d4so4syb

d4so4syb1#

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

  1. 281 : 2 : void test_string_plus(const string& local ,const string& remote)
  2. 282 : : {
  3. 283 : 2 : static string recv_msg;
  4. 284 : 2 : string target("/");
  5. 285 [ + + ]: 2 : if(local<remote)
  6. 286 : : {
  7. 287 : 1 : target += local + "_" + remote;
  8. 288 : : }
  9. 289 : : else{
  10. 290 : 1 : target += remote + "_" + local;
  11. 291 : : }
  12. 292 : 2 : static_cast<void>(recv_msg.assign(target));

字符串

相关问题