软件质量测试第二周作业WordCount

https://github.com/aftermath51/wCount

一.PSP表格

 

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 15  20

· Estimate

· 估计这个任务需要多少时间

 3天  3天

Development

开发

 3天 3天 

· Analysis

· 需求分析 (包括学习新技术)

 45  40

· Design Spec

· 生成设计文档

 60  70

· Design Review

· 设计复审 (和同事审核设计文档)

 30  40

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 45  60

· Design

· 具体设计

 60  60

· Coding

· 具体编码

 400 500

· Code Review

· 代码复审

 30  25

· Test

· 测试(自我测试,修改代码,提交修改)

 150  180

Reporting

报告

45  50

· Test Report

· 测试报告

 30  30

· Size Measurement

· 计算工作量

 20  20

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 30  30
 

合计

1000

 1100

二.解题思路

 1.先完成基本功能,再完成扩展功能。

基本功能

wc.exe -c file.c     //返回文件 file.c 的字符数
wc.exe -w file.c     //返回文件 file.c 的单词总数
wc.exe -l file.c     //返回文件 file.c 的总行数
wc.exe -o outputFile.txt     //将结果输出到指定文件outputFile.txt

扩展功能

wc.exe -s            //递归处理目录下符合条件的文件
wc.exe -a file.c     //返回更复杂的数据(代码行 / 空行 / 注释行)
wc.exe -e stopList.txt  // 停用词表,统计文件单词总数时,不统计该表中的单词

2、先在IDE中实现对电脑中指定目录下的文件的读写操作;

3、编写程序,实现可以在Main函数的调用下分别正确运行各个基础功能;

4、编写程序,实现在终端执行该程序时,程序可以获取到终端输入的全部参数,并能对终端所在的目录下的文档进行读写;

5、调整Main函数中对各个功能的调用机制,使得在终端输入相应的参数时,程序可以正常执行所对应的功能;

6、确定程序可以正确运行所有功能后,把工程打包成.jar文件,再将.jar文件转换成.exe文件,在终端中运行.exe文件,确保.exe可以正确执行所有功能;

三.程序设计实现过程

1.写Main.java

单词统计:按字符流读取文件,对每一个字符做判断,如果不是换行符或空格则继续往下读取;当读取到换行符或者空格是,将前面读到的字符拼作一个单词

,单词计数加一

字符计数:每读入一个字符,判断是不是回车换行符,不是则字符计数器加一

递归获取文件:

获取文件目录,判断是不是目录,若符合要求的文件,则先将文件名存储,与之后的执行一同进行

2.写Test.java

编写测试,覆盖每一个函数及功能,每一个的形参包括:读取的文件名,预期的输入;
每一个调用的输出结果是:
测试名称、预期结果、测试输出结果、是否符合预期结果;

public class Test {

    public boolean Test_CharCount(int expResult, String path) {
        return PrintErrorDetail(GetMethodName(), expResult, Main.ReadChar(path));
    }

    public boolean Test_WordCount(int expResult, String path) {
        return PrintErrorDetail(GetMethodName(), expResult, Main.ReadWord(path));
    }

    public boolean Test_LineCount(int expResult, String path) {
        return PrintErrorDetail(GetMethodName(), expResult, Main.ReadLine(path));
    }

    public boolean Test_ReadDiffLine(int emptyLine, int noteLine, int codeLine, String path) {
        int[] expResult = new int[3];
        expResult[0] = codeLine;
        expResult[1] = emptyLine;
        expResult[2] = noteLine;

        String[] lineNames = {"codeLine", "emptyLine", "noteLine"};

        int[] testResult = Main.GetDifferentLine(path);
        for (int i = 0; i < 3; i++) {
            PrintErrorDetail(GetMethodName() + " " + lineNames[i], expResult[i], testResult[i]);
            if (expResult[i] != testResult[i])
                return false;
        }
        return true;
    }

    public boolean Test_StopList(int expResult, String stoplistPath, String readFilePath) {
        int testResult = Main.StopWordTable(stoplistPath, readFilePath);
        return PrintErrorDetail(GetMethodName(), expResult, testResult);
    }

    public boolean Test_Recursion(int expFileCount, String path, String formateName) {
        Main.fomatName = formateName;
        return PrintErrorDetail(GetMethodName(), expFileCount, Main.FindFile(path));
    }

    public boolean Test_OutputFile(StringBuilder sb, String outputPath) {
        System.out.println("test: " + GetMethodName());
        if (Main.OutPutFile(outputPath, sb)) {
            System.out.println("out put file success");
            return true;
        }
        System.out.println("out put file failed");
        return false;
    }

    public boolean Test_Recursion_StopList(String formatName, String stopListName, int[] expCount) {
        Main.fomatName = formatName;
        Main.FindFile("./");
        boolean result = true;
        for (int i = 0; i < Main.canBeFoundFile.size(); i++) {
            if (!Test_StopList(expCount[i], stopListName, Main.canBeFoundFile.get(i))) {
                result = false;
            }
        }

        return result;
    }

    public boolean Test_Recusion_WordRead_OutputFile(String formatName, String outputFileName, int[] expResult) {
        Main.fomatName = formatName;
        Main.FindFile(Main.fileDir);
        boolean result = true;

        for (int i = 0; i < Main.canBeFoundFile.size(); i++) {
            if (expResult[i] != Main.ReadWord(Main.canBeFoundFile.get(i))) {
                result = false;
            }
        }
        if (result) {
            result = Main.OutPutFile(outputFileName, Main.sb);
        }
        return result;
    }

    private static String GetMethodName() {
        String funName = new Throwable().getStackTrace()[1].getMethodName();
        return funName;
    }

    private boolean PrintErrorDetail(String testName, int expResult, int testResult) {
        System.out.println("test: " + testName);
        System.out.println("exp result: " + expResult);
        System.out.println("test result: " + testResult);
        if (expResult == testResult)
            return true;
        return false;
    }
}

参考文献链接:

Git教程 - 廖雪峰的官方网站

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

java获取当前路径的几种方法 - CSDN博客

http://blog.csdn.net/chengzhaoan2010/article/details/78344791

java.io.File类基本使用——遍历某路径的所有文件夹及文件 - CSDN博客

http://blog.csdn.net/u012325167/article/details/50856515

手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件 - CSDN博客

http://blog.csdn.net/sunkun2013/article/details/13167099

 

PS:参考了17047同学的代码:

http://www.cnblogs.com/Donoth/p/8597025.html

 

 

posted @ 2018-03-20 23:58  Becky_51  阅读(154)  评论(2编辑  收藏  举报