WordCount编码和测试
WordCount编码和测试
项目地址:https://github.com/handsomesnail/WordCount
PSP表格
| PSP2.1 | PSP阶段 | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 20 | 10 |
| Estimate | 估计任务需要多少时间 | 20 | 10 |
| Development | 开发 | 150 | 140 |
| Analysis | 需求分析 | 10 | 10 |
| Design Spec | 生成设计文档 | 10 | 0 |
| Design Review | 设计复审 | 10 | 0 |
| Coding Standard | 代码规范 | 10 | 0 |
| Design | 具体设计 | 20 | 20 |
| Coding | 具体编码 | 60 | 80 |
| Code Review | 代码复审 | 10 | 10 |
| Test | 测试 | 20 | 20 |
| Reporting | 报告 | 60 | 90 |
| Test Report | 测试报告 | 30 | 60 |
| Size Measurement | 计算工作量 | 20 | 20 |
| Postmortem | 总结 | 10 | 10 |
| 合计 | 230 | 240 |
解题思路
将问题分解为如下模块:
解析命令行参数(将输入转化为路径以及各种操作选项如-c -w -l等)I/O操作(将路径转化为相应路径下文件的字符串内容)字符串统计(将各种操作选项抽象为各类处理字符串的方法)
程序设计实现
Program类
- void Main(string[] args)
Main函数作为程序入口 - int Entrance(string[] args)
测试入口, 返回值为错误码,正确执行返回0 - string Read(string fileName)
读取相对路径下名为fileName的文件并返回字符串 - string Read(out string fileName)
调用win32 api选取文件并读取返回字符串,同时返回选取的文件名 - void Write(string fileName, string content)
在相对路径下向名为fileName的文件追加内容为content的字符串 - void LogArgumentError(int code)
打印错误信息 - int CountChar(string s)
统计并返回字符串s的字符数 - int CountWord(string s)
统计并返回字符串s的单词数 - int CountWord(string s,HashSet stopList)
根据停用词表stopList统计并返回字符串s的单词数 - int CountLine(string s)
统计并返回字符串s的行数 - void CountMoreAboutLine(string s, out int codeLineCount, out int emptyLineCount, out int commentLineCount)
统计并返回行的详细信息(代码行/空行/注释行)
OpenFileName类
初始化打开或另存为对话框的信息,系统返回关于用户的选择信息到这个结构中
LocalDialog类
- extern bool GetOpenFileName([In, Out] OpenFileName ofn)
链接Comdlg32.dll中打开文件的系统函数
代码说明
/// <summary>统计字符数并打印</summary>
private static int CountChar(string s) {
return s.Length; //直接返回字符串长度
}
/// <summary>统计单词数并打印</summary>
private static int CountWord(string s) {
return CountWord(s, new HashSet<string>());
}
/// <summary>根据停用词表统计单词数并打印</summary>
private static int CountWord(string s,HashSet<string> stopList) {
if (s.Length == 0)
return 0;
int count = 1;
int i = -1;
StringBuilder sb = new StringBuilder();
foreach (char c in s) {
i++;
if (splitCharDic.Contains(c)) {
if (i < s.Length - 1 && !splitCharDic.Contains(s[i + 1])) {
if (!stopList.Contains(sb.ToString())) {
count++;
//Console.WriteLine(count + ":" + sb.ToString());
sb.Clear();
}
else {
sb.Clear();
}
}
}
else {
sb.Append(c);
}
}
return count;
}
/// <summary>统计行数并打印</summary>
private static int CountLine(string s) {
int count = 0;
//遍历直接统计'\n'数量
foreach (char c in s) {
if (count == 0) {
count++;
continue;
}
if (c == '\n') {
count++;
}
}
return count;
}
/// <summary>统计行的详细信息</summary>
private static void CountMoreAboutLine(string s, out int codeLineCount, out int emptyLineCount, out int commentLineCount) {
codeLineCount = 0;//代码行
emptyLineCount = 0;//空行
commentLineCount = 0;//注释行
int charInALine = 0; //当前行可见字符数
int i = -1;
CommentLineType CommentLine = CommentLineType.NonCommentLine;
bool cancelCommentLine = false;
foreach (char c in s) {
i++;
if (c == '\n'||i==s.Length-1) {
if (CommentLine==CommentLineType.SingleCommentLine) {
commentLineCount++;
CommentLine = CommentLineType.NonCommentLine;
}
else if ((CommentLine == CommentLineType.SeveralCommentLine|| cancelCommentLine )&& charInALine<=1) {
commentLineCount++;
}
else if (charInALine > 1) {
codeLineCount++;
}
else emptyLineCount++;
if (cancelCommentLine) {
cancelCommentLine = false;
}
charInALine = 0;
}
if (charInALine <= 1 && c == '/' && i > 0 && s[i - 1] == '/') {
CommentLine = CommentLineType.SingleCommentLine;
}
else if (c == '*' && i > 0 && s[i - 1] == '/') {
CommentLine = CommentLineType.SeveralCommentLine;
}
else if (c == '/' && i > 0 && s[i - 1] == '*') {
cancelCommentLine = true;
CommentLine = CommentLineType.NonCommentLine;
}
else if (CommentLine == CommentLineType.NonCommentLine && !displayedCharDic.Contains(c)) {
charInALine++;
}
}
}
/// <summary>通过图形界面读取文件 </summary>
public static string Read(out string fileName) {
OpenFileName ofn = new OpenFileName();
ofn.structSize = Marshal.SizeOf(ofn);
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.title = "选择文件";
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (LocalDialog.GetOpenFileName(ofn)) {
fileName = ofn.fileTitle;
using (FileStream fs = new FileStream(ofn.file, FileMode.Open, FileAccess.Read)) {
using (StreamReader sw = new StreamReader(fs, Encoding.UTF8)) {
return sw.ReadToEnd();
}
}
}
throw new System.Exception("用户没有选择文件");
}
测试设计过程
- 如何设计测试用例
保证设计的测试用例应至少覆盖函数中所有的可执行语句 - 哪些地方会导致程序高风险
边界条件,未捕获的异常,未正确初始化的内存空间,空引用等等 - 测试代码如何设计
通过验证测试入口函数和相应分支的返回码覆盖代码的所有分支
namespace UnitTest {
[TestClass]
public class UnitTest1 {
[TestMethod]
public void TestMethod1() {
string[] input = new string[] { };
Assert.AreEqual(Program.Entrance(input), -1);
}
[TestMethod]
public void TestMethod2() {
string[] input = new string[] {"-q", "input.txt" };
Assert.AreEqual(Program.Entrance(input), 2);
}
[TestMethod]
public void TestMethod3() {
string[] input = new string[] {"-c", "-c" ,"input.txt"};
Assert.AreEqual(Program.Entrance(input), 1);
}
[TestMethod]
public void TestMethod4() {
string[] input = new string[] { "-c", "input.txt", "-a" };
Assert.AreEqual(Program.Entrance(input), 7);
}
[TestMethod]
public void TestMethod5() {
string[] input = new string[] { "-c", "input.txt", "-o", "output.txt", "-c" };
Assert.AreEqual(Program.Entrance(input), 4);
}
[TestMethod]
public void TestMethod6() {
string[] input = new string[] { "-c", "input.txt","-o" };
Assert.AreEqual(Program.Entrance(input), 5);
}
[TestMethod]
public void TestMethod7() {
string[] input = new string[] { "-c", "-w", "-l" ,"example.txt" };
Assert.AreEqual(Program.Entrance(input), 6);
}
[TestMethod]
public void TestMethod8() {
string[] input = new string[] { "-c", "-w", "-l", "input.txt" };
Assert.AreEqual(Program.Entrance(input), 0);
}
[TestMethod]
public void TestMethod9() {
string[] input = new string[] { "-l", "-w", "-c", "input.txt", "-o" ,"output.txt"};
Assert.AreEqual(Program.Entrance(input), 0);
}
[TestMethod]
public void TestMethod10() {
string[] input = new string[] { "-l", "-w", "-c", "-a", "input.txt", "-e", "stopList.txt", "-o", "output.txt" };
Assert.AreEqual(Program.Entrance(input), 0);
}
[TestMethod]
public void TestMethod11() {
string[] input = new string[] { "-l", "-w", "-c", "-a", "-s", "*.txt", "-e", "stopList.txt" };
Assert.AreEqual(Program.Entrance(input), 0);
}
[TestMethod]
public void TestMethod12() {
string[] input = new string[] { "-x" };
Assert.AreEqual(Program.Entrance(input), 0);
}
}
}
- 测试结果

参考文献链接:
[1]http://www.cnblogs.com/xinz/archive/2011/10/22/2220872.html
[2]http://www.cnblogs.com/xinz/p/5044037.html
[3]http://http
WordCount编码和测试的更多相关文章
- WordCount 编码与测试
word count github 项目地址:https://github.com/liuqiang666/wordCount PSP表格 PSP2.1 PSP阶段 预估耗时(小时) 实际耗时( ...
- WordCount编码与测试
1. github项目地址:https://github.com/wwwwu/WordCount 2.PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning ...
- 软件质量与测试——WordCount编码实现及测试
1.GitHub地址 https://github.com/noblegongzi/WordCount 2.PSP表格 PSP2.1 PSP 阶段 预估耗时 (分钟) 实际耗时 (分钟) ...
- WordCount编码测试
Github项目地址:https://github.com/LantyrLYL/WordCount PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计 ...
- 软件测试第2周个人作业:WordCount编码测试
一.Github地址 https://github.com/zhouyubei/WordCount 二.PSP表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning ...
- WordCount程序与测试
Github地址: https://github.com/hcy6668/wordCount PSP表格: PSP PSP阶段 预估耗时(分钟) 实际耗时(分钟) Planning 计划 60 40 ...
- WordCountPro 编码与测试
WordCountPro github项目地址:https://github.com/handsomesnail/WordCountPro PSP表格 PSP2.1 PSP阶段 预估耗时(小时) ...
- WordCount程序及测试
Github地址:https://github.com/CG0317/WordCount PSP表: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划 30 ...
- mysql字符集编码乱码测试如下
创建三个表tb_latin1,tb_utf8,tb_gbk,编码分别为latin1/utf8/gbk “你好a”字符串编码如下GBK : %C4%E3 %BA%C3 %61UTF-8 : %E4%BD ...
随机推荐
- swing之flowlayout
import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; //1.继承 JFrame类 // ...
- 使用 DOM对象,控制HTML元素 来制作的一个简单的表格
制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 red,移开鼠标时则恢复为原背景色 white 2. 点击添加按钮,能动态在最后添加一行 3. 点击删除按钮,则删 ...
- 日志收集系统搭建-BELK
前言 日志是我们分析系统运行情况.问题定位.优化分析等主要数据源头.目前,主流的业务系统都采用了分布式.微服务的形式.如果想要查看日志,就需要从不同的节点上去查看,而且对于整个业务链路也非常不清晰.因 ...
- FPGA中竞争冒险问题的研究
什么是竞争冒险? 1 引言 现场可编程门阵列(FPGA)在结构上由逻辑功能块排列为阵列,并由可编程的内部连线连接这些功能块,来实现一定的逻辑功能. FPGA可以替代其他PLD或者各种中小规模数 ...
- 解决ListView 缓存机制带来的显示不正常问题
ListView加载数据原理:系统绘制ListView时,首先会用getCount()函数得到要绘制的这个列表的长度,然后开始逐行绘制.然后调用getView()函数,在这个函数里面首先获得一个Vie ...
- Azure上采用Json Template从已有的VHD创建VM
从已有的VHD创建VM是使用Azure中经常要操作的内容. 本文将介绍如何采用Json Template从已经有的VHD创建VM. 一.准备VHD 在我的Azure账户中选择一台VM,如下图: 查看其 ...
- Nginx解决错误413 Request Entity Too Large
最近一个项目当中,要求上传图片,并且限制图片大小,虽然在laravel当中已经添加了相关的表单验证来阻止文件过大的上传,然而当提交表单时,还没轮到laravel处理,nginx就先报错了.当你仔细看报 ...
- n年的一次聚会
今日聚会有a,b,b1,c,d 五人一起吃饭. 先谈谈各自的变化吧. a 毕业之后,他爸给他买了一个京a的牌照,然后出印度留学,然后回到北京,现在算一个中层领导,不过比较忙,刚刚聚餐完毕就立马回去加班 ...
- java代码实现通讯录实例,我不知道这有什么用。,
运行显示: Friend:zl,Address:武大樱花美Colleagues:蔡依林,Department:麻城市人民政府 题目: 1.任务描述 完善上面通讯录名片的例子. 2.技能要点 掌握类继承 ...
- C# 获取图片某像素点RGB565值
Project Source Download: http://download.csdn.net/detail/mostone/6360007 [csharp] view plain copy pu ...