您的位置:首页 > 编程语言

代码覆盖度测试,gcov

2011-07-07 12:24 211 查看
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:}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: