20180925-4 单元测试,结对

此作业的要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2146

我们使用catch进行单元测试,参考了此篇博客:https://www.cnblogs.com/onduty/p/8399666.html

这里主要用到了match.hpp这个文件,单元测试的代码如下:

#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include <vector>
#include"test01.h"
using namespace std;


TEST_CASE( "four program", "[test01]" ) {
        REQUIRE( FunOne() == 1);
        REQUIRE( FunTwo() == 2);
        REQUIRE( FunThree(4) == 3 );
        REQUIRE( FunFour(4) == 4 );
        //REQUIRE( Factorial(10) == 3628800 );
}

最后通过的单元测试:

我们写产生随机字符串时发生除零异常,程序崩溃,经过修改后代码如下:

for( i=0; i<7; i++)
    {
        if(i%2==0)
        {
            if(F)
            {
                str1[i]=rand()%10+'0';
            }
            else
            {
                while(1)
                {
                    tmp=rand()%10;
                    if(tmp)
                    {
                        str1[i]=tmp+'0';
                        break;
                    }
                }
            }

        }
        else
        {
            str1[i]=Flag();
            if(str1[i]=='/')
                F=false;
            else
                F=true;
        }
    }

编写功能四时,输出格式错误,修改之后:

        flag=false;
        GetStr();
        if(rand()%2==1)
        {
            int KH=rand()%5;
            switch(KH)
            {
            case 0:
                for(i=7; i>0; i--)
                    str1[i]=str1[i-1];
                for(i=8; i>4; i--)
                    str1[i]=str1[i-1];
                str1[0]='(';
                str1[4]=')';
                str1[9]='\0';
                break;
            case 1:
                for(i=7; i>0; i--)
                    str1[i]=str1[i-1];
                for(i=8; i>6; i--)
                    str1[i]=str1[i-1];
                str1[0]='(';
                str1[6]=')';
                str1[9]='\0';
                break;
            case 2:
                for(i=7; i>2; i--)
                    str1[i]=str1[i-1];
                for(i=8; i>6; i--)
                    str1[i]=str1[i-1];
                str1[2]='(';
                str1[6]=')';
                str1[9]='\0';
                break;
            case 3:
                for(i=7; i>2; i--)
                    str1[i]=str1[i-1];
                str1[2]='(';
                str1[8]=')';
                str1[9]='\0';
                break;
            case 4:
                for(i=7; i>4; i--)
                    str1[i]=str1[i-1];
                str1[4]='(';
                str1[8]=')';
                str1[9]='\0';
                break;
            default:
                break;
            }
        }


        float Tans=Arithmetic(str1);
        //printf("2");
        for(i=0; i<j; i++)
        {
            if(Tans==QE[i])
            {
                flag=true;
                break;
            }
        }
        if(flag)
        {
            n++;
            continue;
        }
        QE[j++]=Tans;

        int len=strlen(str1);
        for(i=0; i<len; i++)
        {
            fprintf(fp,"%c",str1[i]);
        }
        fprintf(fp,"=");
        if(len==7)
            fprintf(fp,"  ");
        fprintf(fp,"%12.3f\n",Tans);

 

posted @ 2018-10-08 17:43  王玉潘  阅读(133)  评论(0编辑  收藏  举报