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. 伪分布式环境下命令行正确运行hadoop示例wordcount

    首先确保hadoop已经正确安装.配置以及运行. 1.     首先将wordcount源代码从hadoop目录中拷贝出来. [root@cluster2 logs]# cp /usr/local/h ...

  2. <<深入Java虚拟机>>-第二章-Java内存区域-学习笔记

    Java运行时内存区域 Java虚拟机在运行Java程序的时候会将它所管理的内存区域划分为多个不同的区域.每个区域都有自己的用途,创建以及销毁的时间.有的随着虚拟机的启动而存在,有的则是依赖用户线程来 ...

  3. iOS 分类思想(1)

    1.需求:如果对一个类在不更改代码的基础上要为它再扩充额外的方法可以使用继承和分类 2.分类 作用:可以在不修改原来类代码的基础上,给某一个类扩充一些对象方法或者类方法,因此一个类可以有多个分类 实现 ...

  4. centos6.5 安装python2.7.5

    1. 下载python2.7.5,保存到 /data/http://www.python.org/ftp/python/ 2. 解压文件tar xvf Python-2.7.5.tar.bz2 3. ...

  5. 第二章 Qt常用工具的介绍

    第二章 Qt常用工具的介绍 (1)No.1 qmake 相信编写过Makefile的开发人员,随着工程中源码的级数递增和以类型.功能.模块组织源码的子目录的增多,都不愿意重复机械地手工编写这个工程管理 ...

  6. Asp.net自带导出方法

    ///datatable数据源 filename绝对路径 如:E:\\***.xls DataTable.WriteXml(fileName)

  7. thymeleaf 基本语法

    四.标准表达式语法 · 简单表达式 (simple expressions) ${...}  变量表达式 *{...}  选择变量表达式 #{...}  消息表达式 @{...}  链接url表达式 ...

  8. NFS挂载及写入故障

    最近在做架构时,分离出来一台图片服务器,图片服务器是通过NFS(网络文件系统)给两台web服务器提供图片存储的,在编辑NFS配置文件(/etc/exports)时:想了一下,允许访问NFS共享目录的范 ...

  9. MVC+EF 随笔小计——NuGet程序包管理

    安装EF 打开 工具-库程序包管理器-程序包管理器控制台 输入 install-package entityframework 去MSDN上查看下EF的架构图:http://msdn.microsof ...

  10. java 动态代理理解

    动态代理,顾名思义就是动态创建一个代理对象,无需手动为被代理类创建一个代理类,java的动态代理通过Proxy类和Invocation接口实现,代码如下: //被代理接口 public interfa ...