gcc 中参数-ftest-coverage -fprofile-arcs 使用

来源:互联网 发布:麒麟与ubuntu的区别 编辑:程序博客网 时间:2024/06/11 15:05

利用gcov检测覆盖率

例子:

#include <stdio.h>




int _abs(int a)
{
if (a < 0) {
a = 0-a;
}
return a;
}


int _max(int a, int b)
{
int max = a;
if (a < b) {
max = b;
}
return max;
}


void sort( int list[], int size)
{
int i, j, temp, swap = 1;
while (swap) {
swap = 0;
for ( i = (size-1) ; i >= 0 ; i-- ) {
for ( j = 1 ; j <= i ; j++ ) {
if ( list[j-1] > list[j] ) {
temp = list[j-1];
list[j-1] = list[j];
list[j] = temp;
swap = 1;
}
}
}
}
}


int main() 
{
int x = 4;
int a = 15,b = 20;


int theList[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int i;
sort( theList, 10 );

for (i = 0 ; i < 10 ; i++) { 
printf("%d\n", theList[i]);
}


printf("Hello Ubuntu!!\n");



printf("abs(%d) is %d\n",x,_abs(x));
printf("max(%d,%d) is %d\n",a,b,_max(a,b));


return 0;
}


编译

gcc -fprofile-arcs -ftest-coverage Hello.c


运行:

./a

1
2
3
4
5
6
7
8
9
10
Hello Ubuntu!!
abs(4) is 4
max(15,20) is 20


$ gcov -b Hello.c

File 'Hello.c'
Lines executed:96.88% of 32
Branches executed:100.00% of 14
Taken at least once:85.71% of 14
Calls executed:100.00% of 7
Creating 'Hello.c.gcov'


$ gcov  Hello.c

File 'Hello.c'
Lines executed:96.88% of 32
Creating 'Hello.c.gcov'


以上是执行的效果,自己作为以后查看备份在这里。

0 0
原创粉丝点击