代码覆盖率测试

上一篇 / 下一篇  2008-12-10 10:52:27 / 个人分类:单元测试

昨天开始研究了一下阿LTP测试工具,这个工具可以做linux kernel测试,也可以用来做代码覆盖测试,重点介绍gcov和lcov这两个工具

测试代码在附件中,大家可以尝试一下。

在编译的时候要加上-fprofile-arcs -ftest-coverage
例如:gcc -o test queue.h  queue.c use.c -fprofile-arcs -ftest-coverage

执行以后,能看到生成这些文件:

test mymemory.log    queue.c  queue.c.gcov  queue.gcda  queue.gcno  queue.h  queue.h.gch     use.c     use.gcda  use.gcno

如果安装了,ggcov的话,直接在当前目录下./ggcov . 就行了,你可以看到每个文件的覆盖程度,也可以看到每个函数的覆盖程度

如果没有ggcov的话,也没关系,就做个手动的,拿上面的use.c来举个例子,只要执行gcov use.c就行了,你ls一下,会看到多出这样一个文件夹 use.c.gcov
打开use.c.gcov

        -:    0:Source:use.c
        -:    0:Graph:use.gcno
        -:    0:Data:use.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <stdlib.h>
        -:    2:#include <stdio.h>
        -:    3:#include <mcheck.h>
        -:    4:#include "queue.h"
        -:    5:
        -:    6:
        -:    7:int main()
function main called 1 returned 100% blocks executed 97%
        1:    8:{
        -:    9:    Queue line;
        -:   10:    Item temp;
        -:   11:    char ch;
        -:   12:   
        1:   13:    mtrace();
call    0 returned 100%
        -:   14:
        1:   15:    InitializeQueue(&line);
call    0 returned 100%
        -:   16:
        1:   17:    puts("testing the queue .Type a to add a value");
call    0 returned 100%
        1:   18:    puts("type d to delete a value , and type q to quit");
call    0 returned 100%
        -:   19:
       19:   20:    while((ch = getchar()) != 'q')
call    0 returned 100%
branch  1 taken 94%
branch  2 taken 6% (fallthrough)
        -:   21:    {
       17:   22:        if(ch != 'a' && ch != 'd')
branch  0 taken 76% (fallthrough)
branch  1 taken 24%
branch  2 taken 62% (fallthrough)
branch  3 taken 38%
        8:   23:            continue;
        -:   24:       
        -:   25:
        9:   26:        if(ch == 'a')
branch  0 taken 44% (fallthrough)
branch  1 taken 56%
        -:   27:        {
        4:   28:           printf("Integer to add: ");
call    0 returned 100%
        4:   29:           scanf("%d",&temp);
call    0 returned 100%
        -:   30:
        4:   31:           if(!QueueIsFull(&line))
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0%
        -:   32:           {
        4:   33:               printf("putting %d into queue \n", temp);
call    0 returned 100%
        4:   34:               EnQueue(temp, &line);
call    0 returned 100%
        -:   35:           }
        -:   36:           else
    #####:   37:               puts("Queue is full");
call    0 never executed
        -:   38:        }
        -:   39:        else
        -:   40:        {
        5:   41:            if (QueueIsEmpty(&line))
call    0 returned 100%
branch  1 taken 20% (fallthrough)
branch  2 taken 80%
        1:   42:                puts ("Nothing to delete");
call    0 returned 100%
        -:   43:            else
        -:   44:            {
        4:   45:                DeQueue(&temp, &line);
call    0 returned 100%
        4:   46:                printf("Removeing %d from queue \n",temp);
call    0 returned 100%
        -:   47:            }
        -:   48:        }
        -:   49:
        -:   50:
        9:   51:        printf("%d items in queue \n",QueueItemCount(&line));
call    0 returned 100%
call    1 returned 100%
        9:   52:        puts("Type a to add . d to delete, q to quit:");
call    0 returned 100%
        -:   53:    }
        1:   54:        EmptyTheQueue(&line);
call    0 returned 100%
        1:   55:        puts("Bye!");
call    0 returned 100%
        -:   56:
        1:   57:        return 0;
        -:   58:}

凡是带有“######”,说明该代码没有被覆盖,要看看你的测试用例了
居然不让我上传附件,大家就看看源代码吧!
queue.h

#pragma c9x on
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include <stdbool.h>

#define MAXQUEUE 5


typedef int Item;

typedef struct node
{
    Item item;
    struct node *next;
}Node;

typedef struct queue
{
    Node * front;
    Node * rear;
    int items;
}Queue;

void InitializeQueue(Queue *pq);
bool QueueIsFull (const Queue *pq);
bool QueueIsEmpty (const Queue *pq);
int  QueueItemCount(const Queue *pq);
bool EnQueue(Item item,Queue *pq);
bool DeQueue(Item *pitem, Queue *pq);
void EmputyTheQueue(Queue *pq);

#endif




queue.c 源代码

#include <stdio.h>
#include <stdlib.h>
#include <mcheck.h>
#include "queue.h"

static void CopyToNode (Item item, Node *pn);
static void CopyToItem (Node *pn, Item *pi);

void InitializeQueue(Queue *pq)
{
    pq -> front = pq ->rear = NULL;
    pq ->items = 0;
}

bool QueueIsFull (const Queue *pq)
{
    return pq ->items == MAXQUEUE;
}

bool QueueIsEmpty(const Queue *pq)
{
    return pq->items == 0;
}

int QueueItemCount(const Queue *pq)
{
    return pq ->items;
}

bool EnQueue(Item item, Queue *pq)
{
    Node *pnew;

    if(QueueIsFull(pq))
        return false;
    pnew = (Node *)malloc(sizeof (Node));

    if(pnew == NULL)
    {
        fprintf(stderr, "Unable to allocate memory! \n");
        exit(1);
    }
   
    CopyToNode (item,pnew);
    pnew ->next =NULL;
   
    if(QueueIsEmpty(pq))
        pq ->front = pnew;
    else
        pq ->rear->next = pnew;
    pq -> rear = pnew;
    pq -> items++;
    return true;
}

bool DeQueue(Item * pitem, Queue *pq)
{
    Node *pt;

    if(QueueIsEmpty(pq))
        return false;
    CopyToItem(pq -> front,pitem);
    pt = pq ->front;
    pq -> front = pq ->front->next;
    free(pt);
    pq ->items --;

    if(pq -> items ==0)
        pq ->rear = NULL;
    return true;
}

void EmptyTheQueue (Queue *pq)
{
    Item dummy;
    while (!QueueIsEmpty(pq))
        DeQueue(&dummy,pq);
}

static void CopyToNode(Item item,Node *pn)
{
    pn ->item = item;
}

static void CopyToItem(Node *pn, Item *pi)
{
    *pi = pn ->item;
}


USE.c 源代码
#include <stdlib.h>
#include <stdio.h>
#include <mcheck.h>
#include "queue.h"


int main()
{
    Queue line;
    Item temp;
    char ch;
   
    mtrace();

    InitializeQueue(&line);

    puts("testing the queue .Type a to add a value");
    puts("type d to delete a value , and type q to quit");

    while((ch = getchar()) != 'q')
    {
        if(ch != 'a' && ch != 'd')
            continue;
       

        if(ch == 'a')
        {
           printf("Integer to add: ");
           scanf("%d",&temp);

           if(!QueueIsFull(&line))
           {
               printf("putting %d into queue \n", temp);
               EnQueue(temp, &line);
           }
           else
               puts("Queue is full");
        }
        else
        {
            if (QueueIsEmpty(&line))
                puts ("Nothing to delete");
            else
            {
                DeQueue(&temp, &line);
                printf("Removeing %d from queue \n",temp);
            }
        }


        printf("%d items in queue \n",QueueItemCount(&line));
        puts("Type a to add . d to delete, q to quit:");
    }
        EmptyTheQueue(&line);
        puts("Bye!");

        return 0;
}








TAG: 单元测试

 

评分:0

我来说两句

Open Toolbar