代码覆盖度测试,gcov

copy from gcc manual. 

gcov is a tool you can use in conjunction with GCC to test code coverage in your programs.

gcov is a test coverage program. Use it in concert with GCC to analyze your programs
to help create more efficient, faster running code and to discover untested parts of your
program. You can use gcov as a profiling tool to help discover where your optimization
efforts will best affect your code. You can also use gcov along with the other profiling tool,
gprof, to assess which parts of your code use the greatest amount of computing time.
Profiling tools help you analyze your code’s performance. Using a profiler such as gcov
or gprof, you can find out some basic performance statistics, such as:
 • how often each line of code executes
 • what lines of code are actually executed
 • how much computing time each section of code uses

Running the program will cause profile output to be generated. For each source file
compiled with ‘-fprofile-arcs’, an accompanying ‘.gcda’ file will be placed in the object
file directory.

Running gcov with your program’s source file names as arguments will now produce a
listing of the code along with frequency of execution for each line. For example, if your
program is called ‘tmp.c’, this is what you see when you use the basic gcov facility:

$gcc -fprofile-arcs -ftest-coverage tmp.c (1)
$ ./a.out (2)
$ gcov tmp.c (3)
90.00% of 10 source lines executed in file tmp.c

Creating tmp.c.gcov.

The file ‘tmp.c.gcov’ contains output from gcov. Here is a sample:

-: 0:Source:tmp.c
-: 0:Graph:tmp.gcno
-: 0:Data:tmp.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:
-: 3:int main (void)
1: 4:{
1: 5: int i, total;
-: 6:
1: 7: total = 0;
-: 8:
11: 9: for (i = 0; i < 10; i++)
10: 10: total += i;
-: 11:
1: 12: if (total != 45)
#####: 13: printf ("Failure\n");
-: 14: else
1: 15: printf ("Success\n");
1: 16: return 0;
-: 17:}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值