博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
阅读量:6312 次
发布时间:2019-06-22

本文共 3278 字,大约阅读时间需要 10 分钟。

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 
#include
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 = 0; i < ((66000ul / 26) + 1); 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 = 0; i < ((66000ul / 4) + 1); i++) fputs(ws, f); fclose(f); /* 3. input file contains 66000 newlines */ f = fopen("test3", "w"); assert(f != NULL); for (i = 0; i < 66000; 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 = 0; i < ((66000ul / 26) + 1); 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 = 0; i < 1000; i++) fputs(i5, f); fclose(f); /* 6. input file contains 66000 words without any newlines */ f = fopen("test6", "w"); assert(f != NULL); for (i = 0; i < 66000; i++) fputs("word ", f); fclose(f); return 0;}

 

转载地址:http://bfhxa.baihongyu.com/

你可能感兴趣的文章
Django 文件下载功能
查看>>
走红日本 阿里云如何能够赢得海外荣耀
查看>>
在市场营销中使用敏捷方法:过程、团队与成功案例
查看>>
新书问答:Agile Management
查看>>
苹果将iOS应用带入macOS
查看>>
react入门
查看>>
VUE高仿饿了么app
查看>>
针对Kubernetes软件栈有状态服务设计的思考
查看>>
你的可用性达标了吗?云端业务性能高可用的深度实践
查看>>
linux yum清缓存脚本
查看>>
基于epoll封装的事件回调miniserver
查看>>
天猫高管全面解读大快消2018新零售打法
查看>>
idea springboot热部署无效问题
查看>>
第八章 进程间通信
查看>>
「镁客早报」AI可预测心脏病人死亡时间;机器人开始在美国送外卖
查看>>
MoQ(基于.net3.5,c#3.0的mock框架)简单介绍
查看>>
物联网全面升级,十年内推动工业进入智能化新阶段
查看>>
spring-通过ListFactory注入List
查看>>
一种基于SDR实现的被动GSM嗅探
查看>>
阿里云ECS每天一件事D1:配置SSH
查看>>