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\t\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\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 = ; i < ((66000ul / ) + ); 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 = ; i < ((66000ul / ) + ); i++)
fputs(ws, f);
fclose(f); /* 3. input file contains 66000 newlines */
f = fopen("test3", "w");
assert(f != NULL);
for (i = ; i < ; i++)
fputc('\n', 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 = ; i < ((66000ul / ) + ); 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 = ; i < ; i++)
fputs(i5, f);
fclose(f); /* 6. input file contains 66000 words without any newlines */
f = fopen("test6", "w");
assert(f != NULL);
for (i = ; i < ; i++)
fputs("word ", f);
fclose(f); return ;
}

c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件的更多相关文章

  1. c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出

    Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...

  2. c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号

    Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...

  3. c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)

      fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...

  4. c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()

    The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...

  5. c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。

    Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...

  6. c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行

    Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...

  7. c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图

    Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...

  8. c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格

    Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...

  9. 《JAVA程序设计》_第七周学习总结

    一.学习内容 1.String类--8,1知识 Java专门提供了用来处理字符序列的String类.String类在java.lang包中,由于java.lang包中的类被默认引入,因此程序可以直接使 ...

随机推荐

  1. 学习笔记---C++虚函数,纯虚函数

    1 .虚函数 假设people是man的父类,people类和man类都定义了实函数walk() people* p = new man(); p->walk(); 这里P执行的是people类 ...

  2. java 中hashcode和equals 总结

    一.概述            在Java中hashCode的实现总是伴随着equals,他们是紧密配合的,你要是自己设计了其中一个,就要设计另外一个.当然在多数情况下,这两个方法是不用我们考虑的,直 ...

  3. C语言使用中的细节问题总结

    1.在使用bool关键字时,出现"error:'bool' undeclared(first use in this function)"的错误,原因为C语言本身是没有bool关键 ...

  4. 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(四)

    这一节,是要把AS库和Android的jar包及相关配置文件打成一个ane包. 首先先建一个build目录,里面文件目录结构如下: 然后用打开压缩包的方式打开ServiceLib.swc, 把其中的l ...

  5. MySql启动提示:The server quit without updating PID file(…)失败

    在网上找了很多 1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的权限解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data”  ...

  6. PHOTOSHOP 半透明方格

    1.新建60*60的透明文档,在左方和上方用直线工具画白边,存储为图案(编辑/定义图案) 2.新建图层,用油漆桶填充图案 3. 选择若干小方格,填充白色后设置不透明度50%

  7. LinqToExcel常用对象

    1.ExcelQueryFactory对象(1)获取工作表名集合IEnumerable<string> GetWorksheetNames() //获取工作薄中的工作表名 foreach ...

  8. 、Dll文件的编写 调用 说明

    1>新建Dll文件TestLib.dll 新建Unit文件U_TestFunc U_TestFunc代码如下: unit U_TestFunc; interface uses //尽可能的少us ...

  9. 恢复Delphi XE2的Library Path

    Delphi XE2好好的,手贱乱删,结果新建一个工程都不能编译了,出现:DELPHI X2 [DCC Fatal Error] KjcxClient.dpr(1): F1027 Unit not f ...

  10. CODEVS 2994 超级弹珠

    题目描述 Description 奶牛们最近从著名的奶牛玩具制造商Tycow那里,买了一套仿真版彩蛋游戏设备.Bessie把她们玩游戏的草坪划成了N*N单位的矩阵,同时列出了她的K个对手在草地上的位置 ...