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:

对于我,我想出了下面这些具有代表性的测试输入:

  1. 0. input file contains zero words
  2. 1. input file contains 1 enormous word without any newlines
  3. 2. input file contains all white space without newlines
  4. 3. input file contains 66000 newlines
  5. 4. input file contains word/{huge sequence of whitespace of different kinds}/word
  6. 5. input file contains 66000 single letter words, 66 to the line
  7. 6. input file contains 66000 words without any newlines
  8. 7. input file is /usr/dict contents (or equivalent)
  9. 8. input file is full collection of moby words
  10. 9. input file is binary (e.g. its own executable)
  11. 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:

  1.  
  1. #include <assert.h>
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6. FILE *f;
  7. unsigned long i;
  8.  
  9.    //这里定义的变量都是static静态变量
  10. static char *ws = " \f\t\v";
  11. static char *al = "abcdefghijklmnopqrstuvwxyz";
  12. static char *i5 = "a b c d e f g h i j k l m "
  13. "n o p q r s t u v w x y z "
  14. "a b c d e f g h i j k l m "
  15. "n o p q r s t u v w x y z "
  16. "a b c d e f g h i j k l m "
  17. "n\n";
  18.  
  19. /* Generate the following: 生成测试输入文件,但是请注意,这里主要是从linux系统上测试,所以文件没有后缀名;在windows上,如果要加后缀名的话,加'.txt'就好了。 */
  20. /* 0. input file contains zero words */
  21. f = fopen("test0", "w");
  22. assert(f != NULL);
  23. fclose(f);
  24.  
  25. /* 1. input file contains 1 enormous word without any newlines */
  26. f = fopen("test1", "w");
  27. assert(f != NULL);
  28. for (i = ; i < ((66000ul / ) + ); i++)
  29. fputs(al, f);
  30. fclose(f);
  31.  
  32. /* 2. input file contains all white space without newlines */
  33. f = fopen("test2", "w");
  34. assert(f != NULL);
  35.  
  36.   //66000ul 代表这是无符号长整型
  37. for (i = ; i < ((66000ul / ) + ); i++)
  38. fputs(ws, f);
  39. fclose(f);
  40.  
  41. /* 3. input file contains 66000 newlines */
  42. f = fopen("test3", "w");
  43. assert(f != NULL);
  44. for (i = ; i < ; i++)
  45. fputc('\n', f);
  46. fclose(f);
  47.  
  48. /* 4. input file contains word/
  49. * {huge sequence of whitespace of different kinds}
  50. * /word
  51. */
  52. f = fopen("test4", "w");
  53. assert(f != NULL);
  54. fputs("word", f);
  55. for (i = ; i < ((66000ul / ) + ); i++)
  56. fputs(ws, f);
  57. fputs("word", f);
  58. fclose(f);
  59.  
  60. /* 5. input file contains 66000 single letter words,
  61. * 66 to the line
  62. */
  63. f = fopen("test5", "w");
  64. assert(f != NULL);
  65. for (i = ; i < ; i++)
  66. fputs(i5, f);
  67. fclose(f);
  68.  
  69. /* 6. input file contains 66000 words without any newlines */
  70. f = fopen("test6", "w");
  71. assert(f != NULL);
  72. for (i = ; i < ; i++)
  73. fputs("word ", f);
  74. fclose(f);
  75.  
  76. return ;
  77. }

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. MySQL二进制文件规范安装

    演示环境介绍 操作系统:CentOS 6.7  (64位) 二进制包:mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz MySQL 下载地址:http://dev.m ...

  2. NFS vs. CIFS

    1.  CIFS协议分析 CIFS(Common Internet File System,公共互联网文件系统)是当前主流异构平台共享文件系统之一.主要应用在NT/Windows环境下,是由Micro ...

  3. 实时监听input输入框value值的变化

    1.js 的 oninput & onpropertychange JS中的 oninput 事件在 IE9 以下版本不支持,需要使用 IE 特有的 onpropertychange 事件替代 ...

  4. python(一)入门

    1.软件环境安装和配置 首先下载属于你的操作系统的对应的python安装包 2.傻瓜化下一步下一步 我直接勾选了配置python到path变量 然后完成 3.cmd命令行中测试一把 表示环境配置成功 ...

  5. HttpWebRequest中的KeepAlive

    一直不是非常理解.NET中HttpWebRequest的KeepAlive属性有何用处,看了这篇文章就清楚了! http://www.cnblogs.com/lwzz/archive/2011/08/ ...

  6. m2e插件的新下载地址

    今天在按照<Maven实战>这本书给eclipse配置maven的m2eclipse插件的时候发现,书中写的老的下载地址http://m2eclipse.sonatype.org/site ...

  7. js电话号码正则校验--座机和手机号

    1.最新的电话号码段: 移动:134(1349除外)135 136 137 138 139 147 150 151 152 157 158 159 182 183 184 187 188 联通: 13 ...

  8. ECSHOP 订单状态 记录

    记录订单状态 order_status /* 订单状态 */ define(‘OS_UNCONFIRMED’,            0); // 未确认 define(‘OS_CONFIRMED’, ...

  9. 制作Mac OS X Mavericks 安装U盘

    1. 8G+ U盘一个. 2. App Store 下载Maverics到本地(默认会下载到Applications) 2. 打开Mac OS 磁盘工具(Disk Utility),左侧选中U盘,在右 ...

  10. 批量将MP4 转换为 MP3

    0 需要先下载VLC 软件 1 win+R 运行 "CMD" 2 CD mp4目录 3 复制 并运行下面代码 for %%a in (*.mp4) do "C:\Prog ...