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. FlatBuffers

    1 What is FlatBuffers. FlatBuffers is a serialization library for games and other memory constrained ...

  2. 【转】SQL删除重复数据方法

    例如: id           name         value 1               a                 pp 2               a           ...

  3. read/write数据读写传输方式(转)

    前言 笔者本打算撰写一篇讲解标准I/O(缓存I/O)的博文,但是发现已经有网友做过同样的工作,并且工作质量上乘,特转载于此. 原文地址http://lenky.info/archives/2012/0 ...

  4. 【GPS】 数据围栏

    1.记录gps信息,定位类型  gps  agps ,偏移量 2.根据id检索用户 gps 历史记录 3.创建围栏 4.围栏内用户检索(先实现 圆形和矩形) 5.判断一个点是否进出围栏 应用场景: o ...

  5. button的相关属性

    设置自定义按钮的文字大小 [submit.titleLabel setFont:[UIFont boldSystemFontOfSize:16]]; 设置按钮选中状态的颜色 [btn setTintC ...

  6. userInteractionEnabled

    NO -------是自身View下面的按钮之类的能点 YES------是View自身的按钮能点击,他下面的不能点击

  7. windows azure programing

    http://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started-vs2012/ http:// ...

  8. 统计 iOS 设备锁定、解锁次数-b

    今天下了个软件,可以记录手机解锁的次数和使用时间,当然啦,App 必须在后台运行着.当时比较纳闷的是有什么 API 可以接收设备解锁事件或通知的,Google 了下,还真有哎——我是链接:http:/ ...

  9. winform 项目获取app.config 中appSettings节点数据

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...

  10. java Arrays.asList()和Collections.addAll()

    java中的方法Arrays.asList(arg1,arg2,arg3...),经常用在将多个元素或数组转化为List中的元素,但是在使用的时候,应该注意: arg1决定返回list的元素类型(即第 ...