代码覆盖率测试
2007-8-21
larkguo@
摘要:同gprof一样gcov是 GNU 编译器工具包所提供的一种测试工具,用来检测代码的覆盖率,能标识出每个函数甚至代码行被执行了多少次。本文以C语言实例讲解linux下gcov用法,相关代码见《C语言多线程内存管理模块》。
编译
编译时使用-fprofile-arcs -ftest-coverage选项:
[******@asterisk1 mm]# make CFLAGS+=-fprofile-arcs CFLAGS+=-ftest-coverage LDFLAGS+=-fprofile-arcs LDFLAGS+=-ftest-coverage
cc -fprofile-arcs -ftest-coverage -c -o
cc -fprofile-arcs -ftest-coverage -c -o
gcc -o mm -fprofile-arcs -ftest-coverage
:
[******@asterisk1 mm]# ll
total 76
-rw-r--r-- 1 root root 311 Aug 16 18:00
-rw-r--r-- 1 root root 580 Aug 21 19:24
-rw-r--r-- 1 root root 1884 Aug 21 19:24
-rw-r--r-- 1 root root 415 Aug 21 19:23 Makefile
-rwxr-xr-x 1 root root 20299 Aug 21 19:24 mm
-rw-r--r-- 1 root root 8829 May 18 10:14
-rw-r--r-- 1 root root 9692 Aug 21 19:24
-rw-r--r-- 1 root root 2132 Jun 19 14:43
-rw-r--r-- 1 root root 9888 Aug 21 19:24
执行
[******@asterisk1 mm]# ./mm
LEAK DETAIL:
6 bytes allocated in main at line 15 of
......
6 bytes allocated in main at line 15 of
6010 bytes allocated in 1001 allocations
LEAK SUMMARY:
6010 bytes in 1001 allocations in file ''
6010 bytes allocated in 1001 allocations
:
[******@asterisk1 mm]# ll
total 84
-rw-r--r-- 1 root root 311 Aug 16 18:00
-rw-r--r-- 1 ro
代码覆盖率测试 来自淘豆网m.daumloan.com转载请标明出处.