WordCount测试项目小结

一、本文对应项目GitHub地址

https://github.com/ReWr1te/WCProject
请参照最新版本(WCProject4.0)

二、项目PSP表格

PSP2.1 PSP阶段 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 5 10
· Estimate · 估计这个任务需要多少时间 5 10
Development 开发 355 600
· Analysis · 需求分析 (包括学习新技术) 50 100
· Design Spec · 生成设计文档 10 10
· Design Review · 设计复审 (和同事审核设计文档) 25 50
· Coding Standard · 代码规范 (为目前的开发制定合适的规范) 10 20
· Design · 具体设计 100 100
· Coding · 具体编码 100 200
· Code Review · 代码复审 10 20
· Test · 测试(自我测试,修改代码,提交修改) 50 100
Reporting 报告 40 40
· Test Report · 测试报告 25 25
· Size Measurement · 计算工作量 5 5
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划 10 10
合计 400 650

三、简单解题思路

  1. 需求分析:仔细阅读给出需求并细化,重新陈述以及思考解决难度;
  2. 设计文档:基本设计思路整理;
  3. 制定代码规范:根据指定的语言(Java)指定相关规范;
  4. 具体设计:核心算法设计,6个功能主要分为6部分,同时注意衔接;
  5. 具体编码;
  6. 代码复审:经验排错;
  7. 测试:撰写测试用例并完善,同时修改代码以提高质量;
  8. 总结和撰写报告;

四、程序设计实现流程

本项目Java代码只有一个wc类,4个函数:

  1. 主函数:包含核心算法流程和输出类测试用例;
  2. getFile()函数:递归获取当前文件夹内所有文件(排除文件夹);
  3. chars()函数:计算字符数;
  4. words()函数:计算单词数;
    主函数调用其余函数完成功能,部分功能在主函数内单独实现。

五、代码说明

Java类代码分为主函数和几个功能函数,但是并非所有功能都在功能函数里面实现。具体核心代码及说明参见以下代码和注释(更加详细的解释等待补充):

// 计算字符数

public static int chars(String str) {
    char[] charArray = str.toCharArray();
    int c_num = 0;
    for (int i = 0; i < charArray.length; i++)
    {
        c_num++;
    }
    return c_num;
}

// 计算单词数

public static int words(String str) {
	char[] charArray = str.toCharArray();
    int w_num = 0;
	for (int i = 0; i < charArray.length; i++)
	{
		if (charArray[i] == ' ' || charArray[i] == ',' || charArray[i] == '\n')
		{
			w_num++;
			if (charArray[i] == ' ' && charArray[i - 1] == ',')
				w_num--;
			if (charArray[i] == '\n' && 
				(charArray[i - 1] == '\n' || 
					charArray[i - 1] == '{' || charArray[i - 1] == '}'))
				w_num--;
		}
	}
    return w_num;
}

// 得到当前目录内所有文件(本程序中并没有使用这个方法遍历文件,但是这个方法证实可行)

public static void getFile(File file) {
    File[] files = file.listFiles();
    for (File a:files) {
        if (!a.isDirectory())
            arr_files.add(a.getAbsolutePath());
        else
            getFile(a);
    }
}

// 参数判断

for (int i = 0; i < args.length; i++)
{
	if (args[i].indexOf(".") != -1)
	{
        if (args[i - 1].indexOf(".") != -1)
        {
            arr_inputs.add(args[i]);
            allfile = i;
            file_count++;
        }
        else if (args[i].indexOf("stopList.txt") != -1)
            exclude = i;
        else if (input == -1)
        {
            arr_inputs.add(args[i]);
            input = i;
            file_count++;
        }
        else
            output = i;
	}
    if (file_count >= 2)
        allfile = 1;
}

// stopList扣词

for (int i = 0; i < args.length; i++)
{
    //System.out.println(args[i]);
    if (args[i].equals(str_e))
    {
        if (exclude == -1)
        {
            System.out.println("\n不能单独使用-e参数!\n");
            out = false;
        }
        else
        {
            String[] strArray = str_read.split(" ");
            String tempStr = e_sb.toString();
            String[] e_str = tempStr.split(" ");
            for (int j = 0; j < strArray.length; j++)
            {
                for (int k = 0; k < e_str.length; k++)
                {
                    //System.out.println(strArray[j] + e_str[k]);
                    if (strArray[j].indexOf(e_str[k]) != -1)
                        w_num--;
                }

            }
        }
    }
}

// 输出准备+测试用例

for (int i = 0; i < args.length; i++)
{
    if (args[i].equals(str_c))
    {
        // System.out.println("\n字符数:" + c_num);
        str_output = str_output + this_file + "," 
        + "字符数:" + c_num + "\r\n";
    }
    if (args[i].equals(str_w))
    {
        // System.out.println("\n单词数:" + w_num);
        str_output = str_output + this_file + ", " 
        + "单词数:" + w_num + "\r\n";
    }
    if (args[i].equals(str_l))
    {
        // System.out.println("\n行数: " + line);
        str_output = str_output + this_file + "," 
        + "行数:" + line + "\r\n";
    }
    if (args[i].equals(str_o))
    {
        if (output == -1)
        {
            System.out.println("\n不能单独使用-o参数!\n");
            out = false;
        }
        else
            filename = args[output];
    }
    if (args[i].equals(str_a))
    {
        // System.out.println("\n代码行/空行/注释行:" 
        //     + line_code + "/" 
        //     + line_null + "/" + line_comment);
        str_output = str_output + this_file + "," 
            + "代码行/空行/注释行: " + line_code + "/" 
            + line_null + "/" + line_comment + "\r\n";
    }
}

// 输出(若没有指定文件名则输出到result.txt,若没有文件则创建文件)

if (out == true)
{
    File file = new File(filename);
    if (!file.exists())
        file.createNewFile();
    FileWriter fileWriter = new FileWriter(file.getName(), true);
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    bufferedWriter.write(str_output);
    bufferedWriter.close();
}

改进之后能够在.exe文件形式下处理一般通配符:

// 得到当前目录内所有文件

public static void getFiles(File file) {
    File[] files = file.listFiles();
    for (File a:files) {
        if (!a.isDirectory())
            arr_files.add(a.getName());
        else
            getFiles(a);
    }
}

//参数判断

for (int i = 0; i < args.length; i++) {
    if (args[i].indexOf(".") != -1) {
        if (args[i].indexOf("*.") != -1) {
            type = args[i].replace("*", "");
            allfile = i;
        }
        else if (args[i].indexOf("stopList.txt") != -1)
            exclude = i;
        else if (input == -1)
            input = i;
        else
            output = i;
    }
}

六、测试设计

本来的思路是按照白盒测试设计各个路径的测试用例,但是实际上因为程序比较简单,所以最后应该是黑盒测试的设计思路,尽量覆盖所有功能:

(功能衔接处会有高风险,多设计两个测试用例)

  1. wc.exe -c -w -l test1.cpp
    同时测试-c -w -l三个基础的功能
  2. wc.exe -c -w -l test2.c
    在用例1的基础上改变文件类型
  3. wc.exe -c -w -l -a test1.cpp
    在用例1的基础上添加-a参数,测试输出行类型信息
  4. wc.exe -c -w -l -a test2.c -e stopList.txt
    在用例3的基础上添加-e参数,测试停用词表
  5. wc.exe -c -w -l -a test2.c -o outputFile.txt
    在用例3的基础上添加-o参数,测试指定输出文件的功能
  6. wc.exe -c -w -l -a test2.c -e stopList.txt -o outputFile.txt
    在用例3的基础上添加-e和-o参数,同时测试停用词表和指定输出文件功能
  7. wc.exe -s *.cpp
    测试-s参数,处理多个文件的功能
  8. wc.exe -s *.c
    在用例8的基础上改变输入文件的类型
  9. wc.exe -c -w -l -a -s *.cpp -e stopList.txt -o outputFile.txt
    结合测试用例6,7,同时处理基础功能,行类型信息,多文件,停用词表和指定输出文件名
  10. wc.exe -c -w -l -a test1.cpp test2.c -e stopList.txt -o outputFile.txt
    在用例9的基础上改变多文件处理的类型

七、参考文献

本次项目无参考文献。

posted @ 2018-03-20 22:58  PaulTian  阅读(356)  评论(2编辑  收藏  举报