c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
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_学习单元测试,自己生成测试输入文件的更多相关文章
- c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...
- c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...
- c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...
- c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()
The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...
- 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 ...
- c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...
- 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 ...
- c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格
Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...
- 《JAVA程序设计》_第七周学习总结
一.学习内容 1.String类--8,1知识 Java专门提供了用来处理字符序列的String类.String类在java.lang包中,由于java.lang包中的类被默认引入,因此程序可以直接使 ...
随机推荐
- Python快速入门学习笔记(二)
注:本学习笔记参考了廖雪峰老师的Python学习教程,教程地址为:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb49318210 ...
- Windows Phone中用到的类名及对应的命名控件及引用
//INotifyPropertyChanged using System.ComponentModel; //ICommand using System.Windows.Input; //Actio ...
- Ubuntu 14.04为浏览器添加Flash插件
在刚安装好到Ubuntu操作系统中默认是没有flash支持到,因此,当我们使用浏览器查看很多视频网页到时候,会导致网页上到视频无法播放.然而,这个问题我们也不能够通过“软件中心”来解决,这时候需要我们 ...
- 详解MySQL中EXPLAIN解释命令(转)
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: expla ...
- 为WPF版的GridControl控件添加行序号功能
废话不多数,先上效果图和代码: 包装GridControl控件 cs using DevExpress.Xpf.Data; using DevExpress.Xpf.Grid; using Syste ...
- sublime c++ builder
rt, mark { "cmd": ["g++", "${file}", "-o", "${file_path ...
- Oracle 排序分析函数之ROW_NUMBER、RANK和DENSE_RANK
我们都知道分析函数功能很强大,可能需要写很复杂的标准SQL才能办到或不可能办到的事,使用分析函数却能很容易完成.我们经常会用到排序分析函数,如ROW_NUMBER,RANK,DENSE_RANK.这三 ...
- AVQueuePlayer,备用
想要视频一个接一个的无缝连续播放么?还在用二逼的mpmovieplayercontroller么?太out了!本大仙介绍一个可以实现无缝连续播放视频的东西-------AVQueuePlayer ! ...
- 数据结构-------单链表(C++)
相关信息: /** * @subject 数据结构 实验2 * @author 信管1142班 201411671210 赖俊杰 * @project 单链表 * @time 2015年10月29日1 ...
- js复制黏贴
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...