word count

github 项目地址:https://github.com/liuqiang666/wordCount

PSP表格

PSP2.1  PSP阶段  预估耗时(小时)  实际耗时(小时)
 Planning  计划 0.5  0.5 
 Estimate  估计任务需要多少时间 0.5  0.5 
 Development  开发 2.5 
 Analysis  需求分析 0.5  0.5 
 Design Spec  生成设计文档 0.5 
 Design Review  设计复审 0.5 0
 Coding Standard  代码规范 0.5 
 Design  具体设计  0.5 0.5
Coding  具体编码  3 3.5 
 Code Review  代码复审 0.5 0.5
 Test  测试  2 2.5
 Reporting  报告  0.5 0.5 
 Test Report  测试报告 1 0.5 
 Size Measurement  计算工作量  0.5 0.5 
 Postmortem  总结 0.5  0.5 
   合计  13.5 13

解题思路

  1. 首先考虑如何统计文件单词数,可以将一个非分隔符(如‘,’,‘ ’)作为一个新单词计数的开始,分隔符作为该单词的结束,行数的统计可以根据‘\n'的数量来统计。参考了博客【1】的部分思想。
  2. 考虑如何获取命令行的参数,百度了一下C++中main函数参数的获取,便大致知道main函数参数中的argc 代表参数数量,argv为参数的字符串数组。将argv数组中的参数逐个取出判别,便可获得命令行输入参数以及指定的输入文件名和指定的输出文件名。
  3. 考虑如何利用C++相关函数读取文件内容,以及如何写入文件内容。

程序设计实现

  • wordCount函数:用于实现单词计数的核心功能
  • count函数:负责读取分析命令行参数,以及获得输入文件名,读取输入文件,将统计结果写入结果文件。
  • isSparator函数:判断字符是否是分隔符(如‘,’,‘  ’, '\n'等)。
  • count函数为程序入口函数,调用wordCount函数进行单词计数。isSparator函数在单词计数判断过程中被调用。流程如下图:

  

代码说明

void wordCount()//单词统计函数
{
chars = ;
words = ;
lines = ;
while ((c = fgetc(file)) != EOF)
{
chars++;
if (!isSeparator(c)) //若不是分隔符则是组成单词的一个字符
{
words++;
while ((c = fgetc(file)) != EOF)//继续向后读
{
chars++;
if (!isSeparator(c))//若不是分隔符则是组成单词的一个字符,
{
}
else if (isNewline(c))//若是新行,行数+1
{
lines++;
break;
}
else if (isSpace(c) || isComma(c) || isTab(c))//单词结束
{
break;
}
}
}
else if (isNewline(c))
lines++;
}
if (chars !=)//若该行为结束行,无换行符,行数也应+1
lines++;
}

测试设计过程

  • 如何测试设计用例

  测试时应重点考虑边界值以及特殊情况,使测试用例最大程度覆盖代码。例如在本程序测试时,命令行输入参数时,应考虑输入文件未指定,·-o·参数独立出现,输入错误参数,输入参数重复等错误,应测试当输入文件为空文件或者全为分隔符的文件时统计结果是否正确等。

  • 哪些地方会导致程序高风险

  程序中的空指针,输入文件的内容为空或输入大文件内容等边界会导致程序高风险。

  • 测试代码如何设计 

  测试时应重点考虑边界值以及特殊情况并且使使测试用例最大程度覆盖代码,为此针对wordCount函数重点考虑边界值以及特殊情况和使用例最大程度覆盖代码产生test1, test9, test10共三个测试用例。同样地,针对Count函数产生test2, test3, test4,test5,test6,test7,test8共七个测试用例。测试代码如下:

void test1()//测试正确输入
{
cout << "testcase1:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l", "D:\\wordCount\\test.txt" };
int result = count(argc, argv);
assert(result == );
} void test2()//测试指定输出文件
{
cout << "testcase2:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l", "D:\\wordCount\\test.txt" , "-o", "outfile.txt"};
int result = count(argc, argv);
assert(result == );
} void test3()//测试错误参数
{
cout << "testcase3:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-b", "-w", "-l", "D:\\wordCount\\test.txt" , "-o", "outfile.txt" };
int result = count(argc, argv);
assert(result == );
} void test4()//测试不指定输入文件
{
cout << "testcase4:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "-o", "outfile.txt" };
int result = count(argc, argv);
assert(result == );
} void test5()//测试不指定输出文件,即'-o'单独出现
{
cout << "testcase5:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "D:\\wordCount\\test.txt", "-o"};
int result = count(argc, argv);
assert(result == );
} void test6()//测试输入文件指定错误,即指定为打不开的文件
{
cout << "testcase6:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "D:\\wordCount\\tes.txt", "-o" , "outfile.txt" };
int result = count(argc, argv);
assert(result == );
} void test7()//测试参数重复
{
cout << "testcase7:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-c", "D:\\wordCount\\test.txt" };
int result = count(argc, argv);
assert(result == );
} void test8()//'-o'参数出现在输入文件之前
{
cout << "testcase8:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-o", "-l" , "D:\\wordCount\\test.txt" , "outfile.txt" };
int result = count(argc, argv);
assert(result == );
} void test9()//测试空文件
{
cout << "testcase9:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "D:\\wordCount\\test9.txt", "-o" , "outfile.txt" };
int result = count(argc, argv);
assert(result == );
} void test10()//测试只含有',', ' ' 的文件
{
cout << "testcase10:" << endl;
int argc = ;
char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "D:\\wordCount\\test10.txt", "-o" , "outfile.txt" };
int result = count(argc, argv);
assert(result == );
}
  • 测试结果

  用于测试的输入文件内容如下:

  test.txt:

  

  test9.txt 为空文件

  test10.txt:

  

  运行测试代码,测试全部通过,符合预期输出,结果如图:

  

参考文献链接:

博客【1】:http://blog.csdn.net/flyyyri/article/details/5084981

最新文章

  1. python笔记
  2. Python之路【第二篇】python基础 之基本数据类型
  3. Emit学习(4) - Dapper解析之数据对象映射(一)
  4. Unity3D ShaderLab 各向异性高光
  5. webservice wsdl 生成服务
  6. Android Studio创建项目
  7. 更改 Tomcat 日志路径
  8. [BZOJ 1502] [NOI2005] 月下柠檬树 【Simpson积分】
  9. poj 1265 Area(pick定理)
  10. PHP实现中文截取无乱码
  11. MMA
  12. mybatis系列笔记(4)---输入输出映射
  13. Tomcat 请求处理流程详解
  14. https浅析
  15. windows 下mysq安装之后的数据恢复案例
  16. vue 关于数组和对象的更新
  17. Nginx 如何减轻高流量下的压力
  18. 英语口语练习系列-C30-生日-年历的周日和月份-如果白昼落进
  19. centos7下安装docker(13.3volume生命周期管理)
  20. RocketMQ的使用

热门文章

  1. FastAdmin 中的 formatter flag 分析
  2. apache配置详解 apache安装路径
  3. 在Mac系统下使用自己安装的PHP
  4. 学习Linux相关书籍
  5. Mybatis多参数查询映射
  6. 线程及同步的性能 – 线程池/ ThreadPoolExecutors/ ForkJoinPool
  7. SpringBoot自动化配置之三:深入SpringBoot:自定义EnableAutoConfiguration
  8. Servlet3.0之八:基于Servlet3.0的文件上传@MultipartConfig
  9. Oracle RMAN 学习:演练进阶篇
  10. mjpg-streamer在Ubuntu下编译,运行