当前位置:嗨网首页>书籍在线阅读

15-测试队列

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

17.4.4 测试队列

在重要程序中使用一个新的设计(如,队列包)之前,应该先测试该设计。测试的一种方法是,编写一个小程序。这样的程序称为驱动程序(driver),其唯一的用途是进行测试。例如,程序清单17.8使用一个添加和删除整数的队列。在运行该程序之前,要确保 queue.h 中包含下面这行代码:

typedef int item;

记住,还必须链接 queue.cuse_q.c

程序清单17.8  use_q.c 程序

/* use_q.c -- 驱动程序测试 Queue 接口    */
/* 与 queue.c 一起编译                   */
#include <stdio.h>
#include "queue.h"  /* 定义Queue、Item   */
int main(void)
{
     Queue line;
     Item temp;
     char ch;
     InitializeQueue(&line);
     puts("Testing the Queue interface. 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("Removing %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;
}

下面是一个运行示例。除了这样测试,还应该测试当队列已满后,实现是否能正常运行。

Testing the Queue interface. Type a to add a value,
type d to delete a value, and type q to quit.
a
Integer to add: 40
Putting 40 into queue
1 items in queue
Type a to add, d to delete, q to quit:
a
Integer to add: 20
Putting 20 into queue
2 items in queue
Type a to add, d to delete, q to quit:
a
Integer to add: 55
Putting 55 into queue
3 items in queue
Type a to add, d to delete, q to quit:
d
Removing 40 from queue
2 items in queue
Type a to add, d to delete, q to quit:
d
Removing 20 from queue
1 items in queue
Type a to add, d to delete, q to quit:
d
Removing 55 from queue
0 items in queue
Type a to add, d to delete, q to quit:
d
Nothing to delete!
0 items in queue
Type a to add, d to delete, q to quit:
q
Bye!