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包中的类被默认引入,因此程序可以直接使 ...
随机推荐
- WPF 关于XDocument(xml) 的部分操作记录
(1)删除xml文件中的一个结点的方法,有如下两种方式(只有存在数据绑定的情况下才会有第二种情况,否则一般是第一种情况): private void DeletePacsNode() { //从xml ...
- angular中的orderBy过滤器使用
一 orderBy过滤器 AngularJS中orderBy进行排序,第一个参数可以有三种类型,分别为:function,string,array: 第一种:function,如果是func ...
- Redis和Memcache的区别分析 [转]
1. Redis中,并不是所有的数据都一直存储在内存中的,这是和Memcached相比一个最大的区别. 2. Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构 ...
- js实现克隆一个对象
var app={}; app.cloneobj= function(obj){ var o; if(typeof obj == "object"){ if(obj===null) ...
- Win7 钩子 超时 失效
这段时间在程序中,使用了全局钩子,但是在测试时发现,会偶尔失效. 在网上搜索到了这两篇文章: VC底层钩子程序在Win7/Vista下无效 Hooking problem in Windows 7 这 ...
- 机器学习实战:数据预处理之独热编码(One-Hot Encoding)
问题由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑一下的三个特征: ["male", "female"] ["from ...
- Project not selected to build for this solution configuration.
Project not selected to build for this solution configuration. When you upgrade your older solutio ...
- STL容器的本质
http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...
- GPU CUDA常量内存使用
#include <cuda.h> #include <stdio.h> int getMulprocessorCount(){ cudaDeviceProp prop; cu ...
- OpenGL列主元矩阵的运算
大学线性代数课本是用的行主元矩阵,OpenGL使用的是列主元矩阵.有什么差别呢,对于线性代数学得不够扎实的孩纸来说,还挺烦的. 其实明白了就觉得很简单: 从课本的角度来看,平移矩阵是诸如这样的: 平移 ...