How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?
你会如何测试前面的字符统计程序呢?什么样的测试输入,最能揭示你程序中的bug呢?
It sounds like they are really trying to get the programmers
to learn how to do a unit test.
这听起来,似乎要让程序员如何学习做单元测试。
I would submit the following:
对于我,我想出了下面这些具有代表性的测试输入:
0. input file contains zero words 1. input file contains 1 enormous word without any newlines 2. input file contains all white space without newlines 3. input file contains 66000 newlines 4. input file contains word/{huge sequence of whitespace of different kinds}/word 5. input file contains 66000 single letter words, 66 to the line 6. input file contains 66000 words without any newlines 7. input file is /usr/dict contents (or equivalent) 8. input file is full collection of moby words 9. input file is binary (e.g. its own executable) 10. input file is /dev/nul (or equivalent)
66000 is chosen to check for integral overflow on small
integer machines.
这里的 66000代表机器的整型溢出的上限值,根据不同机器字长进行设定。
Dann
suggests a followup exercise 1-11a: write a program to generate inputs
(0,1,2,3,4,5,6)
Dann 建议再加一个训练:就是自动生成上面所列出10个极端情况中六个输入。
I guess it was inevitable that I'd receive a
solution for this followup exercise! Here is Gregory Pietsch's program to
generate Dann's suggested inputs:
#include <assert.h> #include <stdio.h> int main(void) { FILE *f; unsigned long i;
//这里定义的变量都是static静态变量 static char *ws = " f v"; static char *al = "abcdefghijklmnopqrstuvwxyz"; static char *i5 = "a b c d e f g h i j k l m " "n o p q r s t u v w x y z " "a b c d e f g h i j k l m " "n o p q r s t u v w x y z " "a b c d e f g h i j k l m " "n "; /* Generate the following: 生成测试输入文件,但是请注意,这里主要是从linux系统上测试,所以文件没有后缀名;在windows上,如果要加后缀名的话,加'.txt'就好了。 */ /* 0. input file contains zero words */ f = fopen("test0", "w"); assert(f != NULL); fclose(f); /* 1. input file contains 1 enormous word without any newlines */ f = fopen("test1", "w"); assert(f != NULL); for (i = 0; i < ((66000ul / 26) + 1); i++) fputs(al, f); fclose(f); /* 2. input file contains all white space without newlines */ f = fopen("test2", "w"); assert(f != NULL);
//66000ul 代表这是无符号长整型 for (i = 0; i < ((66000ul / 4) + 1); i++) fputs(ws, f); fclose(f); /* 3. input file contains 66000 newlines */ f = fopen("test3", "w"); assert(f != NULL); for (i = 0; i < 66000; i++) fputc(' ', f); fclose(f); /* 4. input file contains word/ * {huge sequence of whitespace of different kinds} * /word */ f = fopen("test4", "w"); assert(f != NULL); fputs("word", f); for (i = 0; i < ((66000ul / 26) + 1); i++) fputs(ws, f); fputs("word", f); fclose(f); /* 5. input file contains 66000 single letter words, * 66 to the line */ f = fopen("test5", "w"); assert(f != NULL); for (i = 0; i < 1000; i++) fputs(i5, f); fclose(f); /* 6. input file contains 66000 words without any newlines */ f = fopen("test6", "w"); assert(f != NULL); for (i = 0; i < 66000; i++) fputs("word ", f); fclose(f); return 0; }