代码覆盖率分析(gcov)(转)

上一篇 / 下一篇  2008-08-11 12:48:40 / 个人分类:SoftTest

我的栏目
  • 栏目:

为什么需要代码覆盖率分析?51Testing软件测试网+@ P%s p\O;gX
51Testing软件测试网Xn9B3yf1l
在发布代码的时候,我们常常会对其进行一系列的测试来协调软件的性能和功能,使他们和预计的相同。但是检验通常都是相当的困难,即使程序相当的简单。开发者常常会借助一些测试工具(test suite)来模拟或者重建执行脚本。如果测试程序组是彻底的,那么程序的各个功能都将被测试到并且都可以证明是可以工作的。51Testing软件测试网k9iZ Ie]@e8V
51Testing软件测试网(q/hR)p"E1@8rjU3u
但是怎样才算彻底呢?简单点说就是测试程序的每一条路径,验证每一个结果,执行每一条语句,证明没一句语句是没用的。gcov就是一个用来检验你的每一句语句是否都执行了的工具。51Testing软件测试网k~!V}oV
51Testing软件测试网]#JcW7f@ IX S^2y
什么是代码覆盖率分析?
m#NfT\(\0代码覆盖率分析就是找到定位没用的或者不执行的代码的过程。没用的代码不会存在什么问题,但是他们会影响程序的可读性;不执行的代码则可能是未来bug的所在。所以找到他们,把他们从你的程序中移处是大有裨益的。51Testing软件测试网f%bix T#pe
覆盖率分析主要有下面的几个过程:
7o!]L"R7s+a+rE/Q0    通过测试程序组找到不执行的程序段;
G.]&t"MZW`$X5z0    添加额外测试程序组,以便增加代码覆盖率;51Testing软件测试网6e!?M7w{5M*Rd b:q:[.H
    决定代码覆盖率的定量测度,他也是程序质量的间接测度。51Testing软件测试网xUg*@%L

代码覆盖率分析的缺陷
X-l {Zdc.i0代码覆盖率分析不能找出程序的逻辑错误。考虑一下下面的代码51Testing软件测试网A __!M*o
10:  rc = call_to_xx ();51Testing软件测试网@C XP5Tf ?t{
11:  if (rc == ERROR_FATAL)51Testing软件测试网YoU\\)II
12:    exit(2);    /* exit with error code 2 */51Testing软件测试网:@Fk b9?}c U
13:  else
$pq \.p1X J014:    /* continue on */51Testing软件测试网3H!q3[I4V8gdrKI*HP
当测试程序段运行到11行时,11行始终都不能为真。call_to_xx返回了另外的一个错误比如ERROR_HANDLE,除非我们加入这种错误的处理方式的代码。51Testing软件测试网WI vd'c
代码覆盖测试工具不会告诉你什么是必须的,他们只能显示已经存在的代码的覆盖率。
Sd] yO Q;fO0~D0
代码覆盖率的类型
J&ty0HI8[$M$L0gcov可以用来测量各种形式的代码覆盖率。最常见最有用的两种是分支覆盖(branch coverage)和循环覆盖(loop coverage)
dgO9JZ e#JB0分支覆盖证明各个方向的每一条分支都被执行到了。循环覆盖试图证明循环内部的每一条路径都被测试到了。循环覆盖似乎非常的复杂,但基本上只要满足下面的三个状况,就可以作了。51Testing软件测试网z0K D.T c
    1。循环条件不满足,循环没有内部没有执行;
:[z%c3c1\ T Q-D]0    2。循环条件就满足了一次,循环内部就执行了一次;
gIr GP'P2bD*lp0    3。循环条件至少满足了两次,循环至少执行了两次。
,n+k/z.Fq a0举个例子
x1|+ES0wn2J*a0void  function(int number)51Testing软件测试网$E1VV+fS+uZ
{
~JWlN#k0  if (number % 2) == 0)51Testing软件测试网x7Q@8C2So%W-n k#ENP9K
    printf("even \n");51Testing软件测试网9e:UT5QNu
  for (;number < 9; number++){
? _~BL&v0    printf("number is %d\n", number);
)[ EA9RS+t$x6zzi0  }
2d$N)^8qo Y$X2[W.p0}51Testing软件测试网M'd2q^ B{7x/z?Q
51Testing软件测试网&F#Rjb Au {;j,S
function(11);   满足状况一51Testing软件测试网`*D9Y ] d x/MI&T
function(8);    满足状况二
;gFJ!y`g:{:oz{0function(6);    满足状况三   51Testing软件测试网[^zG7ZP2Qi

代码覆盖率工具gcov的使用51Testing软件测试网 r2Y;i7t na9q8M+Jr
要使用gcov,需要在我们用gcc编译程序时加入两个参数fprofile-arcsftest-coverage.51Testing软件测试网\"yt1^.pfCS
fprofile-arcs参数使gcc创建一个程序的流图,之后找到适合图的生成树。只有不在生成树中的弧被操纵(instrumented):gcc添加了代码来清点这些弧执行的次数。当这段弧是一个块的唯一出口或入口时,操纵工具代码(instrumentation code)将会添加到块中,否则创建一个基础块来包含操纵工具代码。51Testing软件测试网(b~'YA7I.K
gcov主要使用.gcno和.gcda两个文件51Testing软件测试网 k KD{3Pv
.gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息。51Testing软件测试网.sv)nZQH n
.gcda是由加了-fprofile-arcs编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息。51Testing软件测试网2?n7@ wK6J2V2}J

2u OI.V ?&~ A0下面是一个简要的范例:51Testing软件测试网"@0Kj.k^6L-J
  1 #include <stdlib.h>51Testing软件测试网'[EF r,f,o
  2 #include <stdio.h>51Testing软件测试网s,~D6A]$zi
  3
(DVn#}'Q0  4 int main(int argc,char** argv)51Testing软件测试网 G%Q kA+r v f
  5 {51Testing软件测试网 u5iLjrm
  6     int x,y;
~&@/gk:UI.K$O.e0  7     int arraysize;
{'e[Q f|y0  8     int **array;51Testing软件测试网(zSYW&j
  951Testing软件测试网3J U,\noa
 10     if(argc!=2)
"Uc8OF-|w6Z)U7~,@1x3d0 11     {51Testing软件测试网qrG"pq Eld3_
 12         printf("Usage: %s Enter arraysize value\n;",argv[0]);51Testing软件测试网*|_.K6h9EM;Q(X;yW,R
 13         exit(-1);
&}4{rD+_5LPJ0 14     }51Testing软件测试网;I H*O\FJ6r v
 15     else
9V6EkGl%Cy I8A0 16     {
wx4qm2I7ReX0 17         arraysize = atoi(argv[1]);51Testing软件测试网Hj ` W9M%Af
 18         if(arraysize <=0)51Testing软件测试网v)u)h(~*U2y pRK:~
 19         {51Testing软件测试网r*K/d;v/pz n Rc[1g#F
 20             printf("Array size must be larger than 0\n;");51Testing软件测试网1o3PP3r*K2l*`1b
 21             exit(-1);
|z%a*y:H'zF0 22         }
v$p6zz Y;EW0 23     }
_9g]$dE'cc+K*fHP0 2451Testing软件测试网~ ~.DrR$^Jm.Y
 25     array = (int**) malloc( arraysize*sizeof(int*));
*Pz tZt N0 2651Testing软件测试网vXuC$X8b"g
 27     printf("Creating an %d by %d array \n",arraysize,arraysize);
@,Z@2W0Lk0 28
.c,Xcg+V0 29     if(array == NULL)
_ Ak4I O9S [0 30     {51Testing软件测试网 Bn;C7iPs
 31         printf("Malloc failed for array size %d \n",arraysize);51Testing软件测试网)I2dxgU l\
 32         exit(-1);51Testing软件测试网i']tJ$}v%t
 33     }
5w8sBaf?8d0 34
7{c2j~?)d0 35     for (x=0;x<arraysize;x++)
d ^'ewp` b}0 36     {
%Q^-XG _0 37         array[x] = (int*) malloc (arraysize*sizeof(int));
O QCONk!h0 38
0FA)T;x+Z+a.[b:[1l0 39         if(array[x] == NULL)51Testing软件测试网3t:WDc5B8L"^b
 40         {51Testing软件测试网5r Nid I VY%@NSZ
 41             printf("Failed malloc for array size %d\n",arraysize);
H b)f9u%Az KNH0 42             exit(-1);
!L,ts[q%FI Tzi[0 43         }51Testing软件测试网%b5n8v-W'_h3G0A/q
 44     }51Testing软件测试网^uWi%f t{ [
 4551Testing软件测试网8w'qpg'KzG!gV)Z
 46     exit(0);51Testing软件测试网,[z:O.U:mK"R Fl d
 47 }51Testing软件测试网.{a}$sf}#Y5L

(mOA%u2h!@l.Uj0x0$ gcc -fprofile-arcs -ftest-coverage -g -o sample test.c51Testing软件测试网s s5uY,Y
$ ./sample 10
wa7Z C'wU'Aie LI0Creating an 10 by 10 array
n+jkof0k(}E UM1~0 $ gcov test.c
V"V ~%CE [0File ''/usr/include/sys/sysmacros.h''51Testing软件测试网n4C {;M2H)[[y
Lines executed:0.00% of 6
}*oR4_;V0/usr/include/sys/sysmacros.h:creating ''sysmacros.h.gcov''51Testing软件测试网n!]Rl&K"we:i)zs
51Testing软件测试网qc#c/w5g)d%g
File ''test.c''
O#k'H&R At0Lines executed:57.89% of 1951Testing软件测试网{#A/};d;K~S
test.c:creating ''test.c.gcov''
'j5O(P$Mk:R0$ cat test.c.gcov51Testing软件测试网*RxV*d`$Pu%^7B
        -:    0:Source:test.c
Vdg'{)Sw!X0        -:    0:Graph:test.gcno
w;w v7p{7[ ^n L@0        -:    0:Data:test.gcda51Testing软件测试网IF2YC$N
        -:    0:Runs:151Testing软件测试网8m1}t w'fncv
        -:    0:Programs:151Testing软件测试网#?:m%y,v!~

-:    1:#include <stdlib.h>
+kR)Z`+qQ2o1kH0        -:    2:#include <stdio.h>51Testing软件测试网Z:Q#mY+i m!G
        -:    3:51Testing软件测试网6W:PU&n5c ]'x
        -:    4:int main(int argc,char** argv)
3i(i|VxLj MALHc0        1:    5:{
/c"Q:xp D2iH z0        -:    6:        int x,y;
h}7`#qHlc0        -:    7:        int arraysize;51Testing软件测试网O*jRm&~ Hx
        -:    8:        int **array;
&e1^bvug/}7r0        -:    9:51Testing软件测试网8y](H&u^5bN5}ctb#t
        1:   10:        if(argc!=2)
%rz3s,m*r&V7a`0        -:   11:        {
8et|7K0ZR |D'cH0    #####:   12:                printf("Usage: %s Enter arraysize value;\n",argv[0]);
#o3R7fJ B fTiO0    #####:   13:                exit(-1);
mC e D.U/g;t0        -:   14:        }
0^*l bi!F1F0        -:   15:        else
#Mxp`&TQ0        -:   16:        {
"A,v{&G K"y0        1:   17:                arraysize = atoi(argv[1]);
b-`(`)J }9d+s0        1:   18:                if(arraysize <=0)
;zN&FN [9tn \A0        -:   19:                {
8u;@+}5AT o1s0    #####:   20:                        printf("Array size must be larger than 0;\n");
8p#{VLgLHC/Wo0    #####:   21:                        exit(-1);
!ig)oKuG[tg0u0        -:   22:                }
)o#m5A&~ V |px0        -:   23:        }
1G^ A.G6r0        -:   24:
vm&Zh9G1` U@m[;q0        1:   25:        array = (int**) malloc( arraysize*sizeof(int*));
i*g9h4d1B r2zk0        -:   26:51Testing软件测试网6l3n5{U^ b J&`
        1:   27:        printf("Creating an %d by %d array \n",arraysize,arraysize);51Testing软件测试网f6Q \.RT*jrF
        -:   28:51Testing软件测试网SS[`YN6hm&Fj
        1:   29:        if(array == NULL)51Testing软件测试网E ^$e:| M0I S'Pp"Q I'F
        -:   30:        {51Testing软件测试网V L*X+E6\:CYp
    #####:   31:                printf("Malloc failed for array size %d \n",arraysize);51Testing软件测试网(P aKi{;KSuR
    #####:   32:                exit(-1);
7v'RK \ J&Uj+W _0        -:   33:        }51Testing软件测试网,H5L:nmf8k;Yz
        -:   34:
@8X1LS6b)gI0       11:   35:        for (x=0;x<arraysize;x++)51Testing软件测试网}+h X*P%gXX8B
        -:   36:        {51Testing软件测试网G(}3qi*~ob7K9[O
       10:   37:                array[x] = (int*) malloc (arraysize*sizeof(int));51Testing软件测试网;i]Ri V/v+? W {
        -:   38:51Testing软件测试网x,gq@X PC"hOA)\
       10:   39:                if(array[x] == NULL)
c/d6B iA;V0        -:   40:                {
/zM(~K1E9p"Y~0    #####:   41:                        printf("Failed malloc for array size %d\n",arraysize);51Testing软件测试网)b ta-Jr$kH({V
    #####:   42:                        exit(-1);
^,@%dljG0        -:   43:                }51Testing软件测试网-f5i(i0U m\\
        -:   44:        }
y1p0g.y@]Y0        -:   45:
^.T.HKa W7\0        1:   46:&n
%B+l(@)?/q#j B8v0     51Testing软件测试网B:M,f1G!MT6}

N{)yBs!Q.n0bsp;       exit(0);51Testing软件测试网(ds^v.JYw
        -:   47:}
%kPPqFl o051Testing软件测试网 ff_7Pjhb
正如我们看到的,我们现在的覆盖率是57.89%,#####后面的语句是现在没有够执行到的。
oWyl:[pr;c&cL051Testing软件测试网~,z4_.A vp+~6`{
$ ./sample51Testing软件测试网_;M2m.x:G Ge
Usage: ./sample Enter arraysize value;51Testing软件测试网mMyo5F aV:g9\
$ gcov test.c
}o/~6^&BA p }k0File ''/usr/include/sys/sysmacros.h''
"e+| ]['C4[0Lines executed:0.00% of 651Testing软件测试网gp/m-dqe&s
/usr/include/sys/sysmacros.h:creating ''sysmacros.h.gcov''
.fX|(yt9z051Testing软件测试网2jT6YH4FBq v2z9Z0b
File ''test.c''51Testing软件测试网&@"vX$yF
Lines executed:68.42% of 1951Testing软件测试网"mGOm%c*a6f
test.c:creating ''test.c.gcov''51Testing软件测试网)M^!P({"iV-]C
现在运行了没有参数的情况,这种情况占了10.53%(68.42-57.89)51Testing软件测试网^B2}9D0^{,B!t
51Testing软件测试网*bc5Uo+n2Bu9d
$ ./sample 0
FU2q0@ND"}0Array size must be larger than 0;51Testing软件测试网1_9bl H;DBk3]/u
$ gcov test.c51Testing软件测试网Oe9]/[-Q
File ''/usr/include/sys/sysmacros.h''51Testing软件测试网;l`v,O'O[F:q
Lines executed:0.00% of 6
!Ud0}~ g6`P8F0/usr/include/sys/sysmacros.h:creating ''sysmacros.h.gcov''51Testing软件测试网z)N+v+S2v _}3cF
51Testing软件测试网z^tqS/c7B7`H
File ''test.c''51Testing软件测试网 d5Z R5V(J*N
Lines executed:78.95% of 1951Testing软件测试网dB:|}oR
test.c:creating ''test.c.gcov''51Testing软件测试网h P_-N n4i"w
有测试了一种情况,那就是arraysize=0现在的覆盖率已经达到了78.95%51Testing软件测试网P5T/s8\ QjI$D}[

5IW X6hJ@R8Y}0还有两种情况,那就是二维数组的第一维和第二维内存空间的申请了。在现在的状况下,malloc()不太可能分配失败,所以我们借助工具gdb来模拟malloc失败时的情况。51Testing软件测试网B x?7y`"H
$ gdb sample51Testing软件测试网?*qcY8h"o&}6UM
GNU gdb Red Hat Linux (6.6-16.fc7rh)51Testing软件测试网x}|,I8Pc
Copyright (C) 2006 Free Software Foundation, Inc.
{E"EkKl0GDB is free software, covered by the GNU General Public License, and you are51Testing软件测试网&_ b4p+w#XS,au
welcome to change it and/or distribute copies of it under certain conditions.51Testing软件测试网M J)c"X#g"WY.O2i0A
Type "show copying" to see the conditions.
"U;A#S cj+Tq\0There is absolutely no warranty for GDB.  Type "show warranty" for details.51Testing软件测试网 E4swJo,o TBV7V
This GDB was configured as "i386-redhat-linux-gnu"...51Testing软件测试网.w)E W](Ot
Using host libthread_db library "/lib/libthread_db.so.1".51Testing软件测试网'G QitQ
(gdb) l
]!Wj|m!}01       #include <stdlib.h>51Testing软件测试网 T!ZD.R3[{
2       #include <stdio.h>51Testing软件测试网,rB2[vVpS0Ba
351Testing软件测试网TA dd!pZ
4       int main(int argc,char** argv)51Testing软件测试网)g h;a)a,P(_2L
5       {51Testing软件测试网Ph(ht]3K m7u*S
6               int x,y;
/{ NB6`*[1U07               int arraysize;51Testing软件测试网!n"SeXjd&N5s
8               int **array;51Testing软件测试网qp$?CYM1n
951Testing软件测试网@,a0?!tG }f ~F;v D
10              if(argc!=2)
&H)c6vO*W#x`.a3P0(gdb) l51Testing软件测试网m^\2my }
11              {
?/v5U!m.z;_*B012                      printf("Usage: %s Enter arraysize value\n;",argv[0]);
(~G'{/\OVv013                      exit(-1);51Testing软件测试网 s f*I7sP/o&k#@#pK
14              }
"g6qNk(H+lG(V&w015              else
1j:M7SR5w2a(kn016              {
z%zu:pZ^#g X017                      arraysize = atoi(argv[1]);51Testing软件测试网N H;N {Al g&h:z
18                      if(arraysize <=0)
Cr i w(mit/[019                      {
A,Y1m,X1R8C5HK020                              printf("Array size must be larger than 0\n;");
3Gl;C I*Y(v$Xqh,r0(gdb) l51Testing软件测试网#[-CN*?'U3k
21                              exit(-1);
z9n,Td K2q022                      }
yN9J|,_"v.z+IxU023              }
_O9J t A/y[4[y Q02451Testing软件测试网'b"U.xi E5hy [
25              array = (int**) malloc( arraysize*sizeof(int*));
jMQO2Y02651Testing软件测试网{ D}B a;p2Wu8_u
27              printf("Creating an %d by %d array \n",arraysize,arraysize);51Testing软件测试网?${[8w1c*I3r
2851Testing软件测试网P/v5do Z,B1i
29              if(array == NULL)51Testing软件测试网c,aA$VQd-iZ"]D
30              {51Testing软件测试网{#O#p6RRO!SN|
(gdb) b 2951Testing软件测试网v3R$s;t!i I t
Breakpoint 1 at 0x8048ada: file test.c, line 29.51Testing软件测试网 de"E N c(A'u^
(gdb) r 10
M@-b)}6b*?#g0Starting program: /home/secularbird/Templates/test/sample 1051Testing软件测试网!tG _2X q7N4g:ma
Creating an 10 by 10 array51Testing软件测试网w\ICL mt;wT

,Au#u-c8q\5|&B%E0Breakpoint 1, main (argc=2, argv=0xbf9b59f4) at test.c:2951Testing软件测试网 V7r(U`/f0l!Bqv5]Lu
29              if(array == NULL)
:g];fMwbhmV H0(gdb) print array
4|5c9g L'I\0$1 = (int **) 0x90af00851Testing软件测试网N$]7N%cyM5e&v| lNh
(gdb) set array=0
j @@["X.q qC0(gdb) print array51Testing软件测试网2M)l ?q!{M7MY
$2 = (int **) 0x051Testing软件测试网$u Ks6U'r0DF
(gdb) step51Testing软件测试网(xj kI7@s
31                      printf("Malloc failed for array size %d \n",arraysize);51Testing软件测试网HUG)\YM-a6t
(gdb) cont
{u I(B&Sa\h0Continuing.51Testing软件测试网$icKh8z8J
Malloc failed for array size 1051Testing软件测试网"Jl/W2D#r!bo
51Testing软件测试网4zHX z~#s?y U JT
Program exited with code 0377.51Testing软件测试网 oeZX7LQl
(gdb) quit51Testing软件测试网Ap"T/mdYdt I%P R
l命令是list的简写,显示了程序的源代码。
:qbKR2AG/u3z0b 29是在29行设置一个断点,之所以这样设置是因为array空间申请过了,我们为了模拟需要改变它的值。
$k3k d}?$@:[0r 10是run 10,也就是运行程序,10是传递的命令行参数。51Testing软件测试网$uSB%zeN S%^s.j
print array打印array的地址
3jm-@#t Z kM0set array=0将array指向空地址
3Y-F|q9r P1m-e0step一步一步的运行程序,简写是s51Testing软件测试网gz:| Q t!m
cont继续运行程序51Testing软件测试网?1E;G!i!zK6J/M
quit退出gdb。51Testing软件测试网1Z-q2|8r] yf
51Testing软件测试网!o#A)}y|,~m
接下来做类似的工作产生另一个malloc fail。
!Z5S0h8MD \{FM0$ gdb sample
V)f*W3X W+pH4p0GNU gdb Red Hat Linux (6.6-16.fc7rh)51Testing软件测试网[p!SJK*|
Copyright (C) 2006 Free Software Foundation, Inc.
A,?}!T`5P:D(y ~0GDB is free software, covered by the GNU General Public License, and you are51Testing软件测试网.rsE)]b2fOe&?bT!yI.^
welcome to change it and/or distribute copies of it under certain conditions.
)Z0k*oa\ lA0Type "sh ow copying" to see the conditions.51Testing软件测试网l'a[@8w+z
There is absolutely no warranty for GDB.  Type "show warranty" for details.51Testing软件测试网0k.~Nu*|^
This GDB was configured as "i386-redhat-linux-gnu"...51Testing软件测试网,O*?5mk5c M1Z}
Using host libthread_db library "/lib/libthread_db.so.1".
;p;J&v1OXf5m0(gdb) l
J1R j/d&B{01       #include <stdlib.h>51Testing软件测试网*|8n k*@4k%H.i
2       #include <stdio.h>
6SaTS@6C0351Testing软件测试网1PU%e7} JLP U)d
4       int main(int argc,char** argv)
%`$rH$},Td%c05       {51Testing软件测试网E}Mw5er0j
6               int x,y;51Testing软件测试网0C6mjn&^(]~g-^
7               int arraysize;
)^\ug7^08               int **array;51Testing软件测试网@G"F(X UZ9Biv
9
'\dl.]\010              if(argc!=2)51Testing软件测试网D|eRO5d,W4~L
(gdb) l51Testing软件测试网6v:D+Kf-P
11              {
*ViT]"b.K4DY!pD012                      printf("Usage: %s Enter arraysize value\n;",argv[0]);
r%R(M J7C4i013                      exit(-1);
i*^!cHQAh)iq"O9Kk014              }
aK y9o@8G9WT015              else
&f3UZ5yz9eGt ^)N*o016              {
J {'m"E0{)i:tU017                      arraysize = atoi(argv[1]);
{/NV$T|2^hg018                      if(arraysize <=0)51Testing软件测试网'j/?%_J|YU$F
19                      {51Testing软件测试网.Xf1L MDA
20                              printf("Array size must be larger than 0\n;");51Testing软件测试网'r+CVz ZHYk
(gdb) l51Testing软件测试网&vr1R3l-P,IAU`0r
21                              exit(-1);
d S E?k af2_n022                      }
[1m5R&Y \4Q k?023              }51Testing软件测试网e8ib0`8U DS0J;u
24
fqn;A'Q+gT025              array = (int**) malloc( arraysize*sizeof(int*));51Testing软件测试网/XuW&|'g0{O#q
26
6E*d GW,b"`9yD027              printf("Creating an %d by %d array \n",arraysize,arraysize);51Testing软件测试网 QC0](T ~P
28
}p2e8S9L['yh1h029              if(array == NULL)
L'`5r$Ha030              {
N@R5@^y"`R0(gdb) l51Testing软件测试网MTYLa&`ey2j7]
31                      printf("Malloc failed for array size %d \n",arraysize);
;nmh&_jN I,}X032                      exit(-1);51Testing软件测试网,Z q},De sSg0qL
33              }
(d6y1L#wl\;}G03451Testing软件测试网oe,r @.|6x _9~
35              for (x=0;x<arraysize;x++)
{o0y;^%q~\036              {51Testing软件测试网6} ^U7};PQ
37                      array[x] = (int*) malloc (arraysize*sizeof(int));51Testing软件测试网UBUgX9pq"yV
3851Testing软件测试网G xTV iU"K3N
39                      if(array[x] == NULL)51Testing软件测试网ruFQ,p#YD1l Vq(IO
40                      {
o Dg S.E)yS0(gdb) b 3951Testing软件测试网2B6@m0Rv,ZGe
Breakpoint 1 at 0x8048b7a: file test.c, line 39.51Testing软件测试网3` cgPG&|-k8^
(gdb) r 1051Testing软件测试网m2W0Jz.Y1i
Starting program: /home/secularbird/Templates/test/sample 10
7m r V6LJ0L vg~i0Creating an 10 by 10 array
5L4C/x4e3n)j6r/S0
?N,@R&\-t0Breakpoint 1, main (argc=2, argv=0xbfdcae04) at test.c:39
5Qo L'~x?039                      if(array[x] == NULL)
v$X(a&mP(|Fx0(gdb) print array[0]51Testing软件测试网Ed8D(D4^
$1 = (int *) 0x9bc2038
@ ?.g5k-TC P[0(gdb) set array[0]=0
b#\ J&o"oVI Kc Xx.a0(gdb) step
Nde(b\C041                              printf("Failed malloc for array size %d\n",arraysize);
HgHd'r,p;N0(gdb) cont
VPv;VY!GAS!_%U@%ER0Continuing.
s5jS.M:I mrA/} M]0Failed malloc for array size 1051Testing软件测试网 {7x)VF[(on

:c$v"usbS4g Y0Program exited with code 0377.
r`p?Aq4_0(gdb) quit51Testing软件测试网$O2Q7h%q1oE!n

mv6Zl/@ u1w2n&S0$ gcov test.c
!A/_,?fAdV0File ''/usr/include/sys/sysmacros.h''51Testing软件测试网^u-{1\7q
Lines executed:0.00% of 651Testing软件测试网8k ?%ZT0x Vq
/usr/include/sys/sysmacros.h:creating ''sysmacros.h.gcov''51Testing软件测试网H!\uz'Z7x

]|x+U%x0File ''test.c''
N6G gmc0N8}fL0Lines executed:100.00% of 1951Testing软件测试网uka_#B{e
test.c:creating ''test.c.gcov''
j5i C+C [g0到现在为止每条路径都测试过了,代码覆盖率也已经达到了100%,也可以查看一下test.c.gcov文件
WGr%PDf*d2L0里面已经没有#####了。
x!et:py P0
+R6q8uRJ _ ?4y0参考资料:
,L@.NQK0Linux® Debugging and Performance Tuning: Tips and Techniques By Steve Best
Tl8Tm]f4jg d0man gcov51Testing软件测试网!Y"u|A(t\P
http://gcc.gnu.org/onlinedocs/gcc/Gcov-Data-Files.html#Gcov-Data-Files
3pzOdw-S7Deg~051Testing软件测试网h6Xn([-f ah4i9B6_ ?9Q

#`E'c ^i.E:c6s0

TAG: Linux Codec SoftTest

 

评分:0

我来说两句

Open Toolbar