WordCount 编码与测试

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 = 0; 
    words = 0; 
    lines = 0;
    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 !=0)//若该行为结束行,无换行符,行数也应+1
        lines++;
}

测试设计过程

  • 如何测试设计用例

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

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

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

  • 测试代码如何设计 

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

void test1()//测试正确输入
{
    cout << "testcase1:" << endl;
    int argc = 5;
    char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l", "D:\\wordCount\\test.txt" };
    int result = count(argc, argv);
    assert(result == 0);
}

void test2()//测试指定输出文件
{
    cout << "testcase2:" << endl;
    int argc = 7;
    char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l", "D:\\wordCount\\test.txt" , "-o", "outfile.txt"};
    int result = count(argc, argv);
    assert(result == 0);
}

void test3()//测试错误参数
{
    cout << "testcase3:" << endl;
    int argc = 7;
    char *argv[] = { "WordCountTest.exe", "-b", "-w", "-l", "D:\\wordCount\\test.txt" , "-o", "outfile.txt" };
    int result = count(argc, argv);
    assert(result == 4);
}

void test4()//测试不指定输入文件
{
    cout << "testcase4:" << endl;
    int argc = 6;
    char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "-o", "outfile.txt" };
    int result = count(argc, argv);
    assert(result == 1);
}

void test5()//测试不指定输出文件,即'-o'单独出现
{
    cout << "testcase5:" << endl;
    int argc = 6;
    char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "D:\\wordCount\\test.txt", "-o"};
    int result = count(argc, argv);
    assert(result == 2);
}

void test6()//测试输入文件指定错误,即指定为打不开的文件
{
    cout << "testcase6:" << endl;
    int argc = 7;
    char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "D:\\wordCount\\tes.txt", "-o" , "outfile.txt" };
    int result = count(argc, argv);
    assert(result == 3);
}

void test7()//测试参数重复
{
    cout << "testcase7:" << endl;
    int argc = 4;
    char *argv[] = { "WordCountTest.exe", "-c", "-c", "D:\\wordCount\\test.txt" };
    int result = count(argc, argv);
    assert(result == 5);
}

void test8()//'-o'参数出现在输入文件之前
{
    cout << "testcase8:" << endl;
    int argc = 6;
    char *argv[] = { "WordCountTest.exe", "-c", "-o", "-l" , "D:\\wordCount\\test.txt" , "outfile.txt" };
    int result = count(argc, argv);
    assert(result == 6);
}

void test9()//测试空文件
{
    cout << "testcase9:" << endl;
    int argc = 7;
    char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "D:\\wordCount\\test9.txt", "-o" , "outfile.txt" };
    int result = count(argc, argv);
    assert(result == 0);
}

void test10()//测试只含有',', ' ' 的文件
{
    cout << "testcase10:" << endl;
    int argc = 7;
    char *argv[] = { "WordCountTest.exe", "-c", "-w", "-l" , "D:\\wordCount\\test10.txt", "-o" , "outfile.txt" };
    int result = count(argc, argv);
    assert(result == 0);
}
  • 测试结果

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

  test.txt:

  

  test9.txt 为空文件

  test10.txt:

  

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

  

 

参考文献链接:

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

posted on 2018-03-19 16:14  liuqsw  阅读(635)  评论(2编辑  收藏  举报