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: 

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

1
2
3
4
5
6
7
8
9
10
11
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 = 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://www.cnblogs.com/haore147/p/3647921.html,如需转载请自行联系原作者
相关文章
|
22天前
|
机器学习/深度学习 算法 异构计算
m基于FPGA的多通道FIR滤波器verilog实现,包含testbench测试文件
本文介绍了使用VIVADO 2019.2仿真的多通道FIR滤波器设计。展示了系统RTL结构图,并简述了FIR滤波器的基本理论,包括单通道和多通道的概念、常见结构及设计方法,如窗函数法、频率采样法、优化算法和机器学习方法。此外,还提供了Verilog核心程序代码,用于实现4通道滤波器模块,包含时钟、复位信号及输入输出接口的定义。
40 7
|
3天前
|
移动开发 JavaScript 前端开发
学习vue3使用在线官方开发环境play.vuejs.org进行测试
学习vue3使用在线官方开发环境play.vuejs.org进行测试
|
5天前
|
测试技术
测试基础 Junit单元测试框架
测试基础 Junit单元测试框架
11 2
测试基础 Junit单元测试框架
|
5天前
|
运维 安全 测试技术
测试基础 学习测试你必须要知道的基础知识
测试基础 学习测试你必须要知道的基础知识
9 3
|
11天前
|
算法 异构计算
基于直方图的图像曝光量分析FPGA实现,包含tb测试文件和MATLAB辅助验证
该内容包括了算法的运行效果展示、软件版本信息、理论概述和核心程序代码。在正常图像中,`checkb`位于`f192b`和`f250b`之间,而多度曝光图像中`checkb`超出此范围,判断为曝光过度。使用的软件为Vivado 2019.2和MATLAB 2022a。理论依据未详细给出,但提及主要方法。提供的Verilog代码段用于处理图像数据,包括读取文件、时钟控制及图像histogram计算等,其中模块`im_hist`似乎是关键部分。
|
13天前
|
安全 测试技术 Go
Golang深入浅出之-Go语言单元测试与基准测试:testing包详解
【4月更文挑战第27天】Go语言的`testing`包是单元测试和基准测试的核心,简化了测试流程并鼓励编写高质量测试代码。本文介绍了测试文件命名规范、常用断言方法,以及如何进行基准测试。同时,讨论了测试中常见的问题,如状态干扰、并发同步、依赖外部服务和测试覆盖率低,并提出了相应的避免策略,包括使用`t.Cleanup`、`t.Parallel()`、模拟对象和检查覆盖率。良好的测试实践能提升代码质量和项目稳定性。
17 1
|
13天前
|
监控 JavaScript 前端开发
【TypeScript技术专栏】TypeScript的单元测试与集成测试
【4月更文挑战第30天】本文讨论了在TypeScript项目中实施单元测试和集成测试的重要性。单元测试专注于验证单个函数、类或模块的行为,而集成测试关注不同组件的协作。选用合适的测试框架(如Jest、Mocha),配置测试环境,编写测试用例,并利用模拟和存根进行隔离是关键。集成测试则涉及组件间的交互,需定义测试范围,设置测试数据并解决可能出现的集成问题。将这些测试整合到CI/CD流程中,能确保代码质量和快速响应变化。
|
14天前
|
算法 TensorFlow 算法框架/工具
基于直方图的图像阈值计算和分割算法FPGA实现,包含tb测试文件和MATLAB辅助验证
这是一个关于图像处理的算法实现摘要,主要包括四部分:展示了四张算法运行的效果图;提到了使用的软件版本为VIVADO 2019.2和matlab 2022a;介绍了算法理论,即基于直方图的图像阈值分割,通过灰度直方图分布选取阈值来区分图像区域;并提供了部分Verilog代码,该代码读取图像数据,进行处理,并输出结果到&quot;result.txt&quot;以供MATLAB显示图像分割效果。
|
15天前
|
弹性计算 运维 Shell
测试文件是否存在
【4月更文挑战第29天】
10 1
|
16天前
|
IDE 测试技术 持续交付
【专栏】利用Python自动化测试与单元测试框架提升代码质量与效率
【4月更文挑战第27天】本文探讨了Python自动化测试与单元测试框架在提升代码质量与效率中的作用。Selenium、Appium用于Web和移动应用自动化测试,pytest提供强大、易扩展的测试支持。unittest是Python标准的单元测试框架,支持结构化测试用例和丰富的断言。实践中,应制定测试计划,编写高质量测试用例,实行持续集成与测试,并充分利用测试报告。这些工具和策略能有效保障代码质量和提升开发效率。