编译执行:gcc-o testgcov -ftest-coverage -fprofile-arcs testgcov.c

  ./testgcov--->执行程序,各种执行语句将会被写到testgcov.gcda和testgcov.gcno中

  gcov-b testgcov.c --->testgcov.c.gcov详细的覆盖信息

  testgcov.c.gcov 详细信息:

-: 0:Source:testgcov.c
-: 0:Graph:testgcov.gcno
-: 0:Data:testgcov.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:/*Program: testgcov.c -- interpert the workflow of gcov */
-: 2:#include<stdio.h>
1: 3:int main(){
1: 4: printf("Hello,World ");
1: 5:}

  注:gcov -b查看分支执行的频率,-f查看每个函数执行的情况。

  2.3.3gcov覆盖测试应用举例

/*Program:autosell.c -- design the input test case and get the code coverage of program */
#include<stdio.h>
void welcome();
void nochange(int num5coins);
void getcoin(int *coin);
void pushbutton(int *button);
void process(int* coin,int* button,int* num5coins);
int main(){
 int coin=0,button=0,num5coins=2;
 int i;
 for(i=0;i<10;++i){
  welcome();
  nochange(num5coins);
  getcoin(&coin);
  pushbutton(&button);
  process(&coin,&button,&num5coins);
 }
 return 0;
}
void welcome(){
 system("clear");//library function,which
 printf("welcome to this auto selling machine! ");
}
void nochange(int num5coins){
 if(num5coins == 0)
  printf("No change now! ");
}
void getcoin(int* coin){
 int flagredo;
 do{
  printf("Please pitch your coin(5-5角,10-1元):");
  scanf("%d",coin);
  if(*coin!=5 && *coin!=10){
   printf("Wrong coin!Return this coin. ");
   flagredo=1;
  } else
   flagredo=0;
 }while(flagredo);
}
void pushbutton(int *button){
 int flagredo;
 do{
  printf("Please select your drink(1-orange juice,2-beer):");
  scanf("%d",button);
  if(*button!=1 && *button!=2){
   printf("Wrong input,Please re-select. ");
   flagredo=1;
  } else
   flagredo=0;
 } while(flagredo);
}
void process(int* coin,int* button,int* num5coins){
 if(*coin == 10 && *num5coins ==0){
  printf("No change! ");
   printf("Return 1 yuan coin. ");
 } else {
  if(*coin == 10){
   if(*button ==1)
    printf("Please take your orange juice. ");
   else
    printf("Please take you beer ");
   (*num5coins)--;
   printf("Return 5 jiao coin. ");
  }
  if(*coin == 5){
   if(*button ==1)
    printf("Please take your orange juice. ");
   else
    printf("Please take you beer ");
   (*num5coins)++;
  }
 }
 printf(" Press Enter to continue");
 getchar();getchar();

 *coin =0;
 *button=0;
}