首页 > 技术文章 > 2020年秋第四五周-单元测试

HanYG 2020-10-05 23:45 原文

此作业要求参见https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11243
测试使用cppUnit框架
结对伙伴是[Nicole]

要求一

1、功能一
测试用例如下

1+2*3+5=      12
1+2/4-5=      -3.5

测试编码如下

TEST_METHOD(TestOne)
{
	pEqua pequ = create_equa_array(10);
	pEqua p = pequ;
	strcpy_s(p->equa[0], DATA_SIZE, "1\0");
	strcpy_s(p->equa[1], DATA_SIZE, "+\0");
	strcpy_s(p->equa[2], DATA_SIZE, "2\0");
	strcpy_s(p->equa[3], DATA_SIZE, "*\0");
	strcpy_s(p->equa[4], DATA_SIZE, "3\0");
	strcpy_s(p->equa[5], DATA_SIZE, "+\0");
	strcpy_s(p->equa[6], DATA_SIZE, "5\0");
	p = p->next;
	strcpy_s(p->equa[0], DATA_SIZE, "1\0");
	strcpy_s(p->equa[1], DATA_SIZE, "+\0");
	strcpy_s(p->equa[2], DATA_SIZE, "2\0");
	strcpy_s(p->equa[3], DATA_SIZE, "/\0");
	strcpy_s(p->equa[4], DATA_SIZE, "4\0");
	strcpy_s(p->equa[5], DATA_SIZE, "-\0");
	strcpy_s(p->equa[6], DATA_SIZE, "5\0");
	char pChr1[DATA_SIZE];
	char pChr2[DATA_SIZE];
	strcpy_s(pChr1, DATA_SIZE, ((fraction)calculate_equa(pequ)).print_res(false));
	strcpy_s(pChr2, DATA_SIZE, ((fraction)calculate_equa(p)).print_res(false));
	Assert::AreEqual(pChr1, "12");
	Assert::AreEqual(pChr2, "-3.5");
}

测试结果如下图

2、功能二
测试用例如下

1+2*(3+4)=      15
((1/2)-4)*5=    -17.5

测试编码如下

TEST_METHOD(TestTwo)
{
	pEqua pequ = create_equa_array(10);
	pEqua p = pequ;
	strcpy_s(p->equa[0], DATA_SIZE, "1\0");
	strcpy_s(p->equa[1], DATA_SIZE, "+\0");
	strcpy_s(p->equa[2], DATA_SIZE, "2\0");
	strcpy_s(p->equa[3], DATA_SIZE, "*\0");
	strcpy_s(p->equa[4], DATA_SIZE, "(\0");
	strcpy_s(p->equa[5], DATA_SIZE, "3\0");
	strcpy_s(p->equa[6], DATA_SIZE, "+\0");
	strcpy_s(p->equa[7], DATA_SIZE, "4\0");
	strcpy_s(p->equa[8], DATA_SIZE, ")\0");
	p = p->next;
	strcpy_s(p->equa[0], DATA_SIZE, "(\0");
	strcpy_s(p->equa[1], DATA_SIZE, "(\0");
	strcpy_s(p->equa[2], DATA_SIZE, "1\0");
	strcpy_s(p->equa[3], DATA_SIZE, "/\0");
	strcpy_s(p->equa[4], DATA_SIZE, "2\0");
	strcpy_s(p->equa[5], DATA_SIZE, ")\0");
	strcpy_s(p->equa[6], DATA_SIZE, "-\0");
	strcpy_s(p->equa[7], DATA_SIZE, "4\0");
	strcpy_s(p->equa[8], DATA_SIZE, ")\0");
	strcpy_s(p->equa[9], DATA_SIZE, "*\0");
	strcpy_s(p->equa[10], DATA_SIZE, "5\0");
	char pChr1[DATA_SIZE];
	char pChr2[DATA_SIZE];
	strcpy_s(pChr1, DATA_SIZE, ((fraction)calculate_equa(pequ)).print_res(false));
	strcpy_s(pChr2, DATA_SIZE, ((fraction)calculate_equa(p)).print_res(false));
	Assert::AreEqual(pChr1, "15");
	Assert::AreEqual(pChr2, "-17.5");
}

测试结果如下图

3、功能三和功能四一起测试
测试用例如下

1/3+2/3+1+1=      3
7/5+3/4*2-3=      -1/10
1/2+2/3+1+2=      4 1/6

测试编码如下

TEST_METHOD(TestThree)
{
	pEqua pequ = create_equa_array(10);
	pEqua p = pequ;
	strcpy_s(p->equa[0], DATA_SIZE, "1/3\0");
	strcpy_s(p->equa[1], DATA_SIZE, "+\0");
	strcpy_s(p->equa[2], DATA_SIZE, "2/3\0");
	strcpy_s(p->equa[3], DATA_SIZE, "+\0");
	strcpy_s(p->equa[4], DATA_SIZE, "1\0");
	strcpy_s(p->equa[5], DATA_SIZE, "+\0");
	strcpy_s(p->equa[6], DATA_SIZE, "1\0");
	p = p->next;
	strcpy_s(p->equa[0], DATA_SIZE, "7/5\0");
	strcpy_s(p->equa[1], DATA_SIZE, "+\0");
	strcpy_s(p->equa[2], DATA_SIZE, "3/4\0");
	strcpy_s(p->equa[3], DATA_SIZE, "*\0");
	strcpy_s(p->equa[4], DATA_SIZE, "2\0");
	strcpy_s(p->equa[5], DATA_SIZE, "-\0");
	strcpy_s(p->equa[6], DATA_SIZE, "3\0");
	p = p->next;
	strcpy_s(p->equa[0], DATA_SIZE, "1/2\0");
	strcpy_s(p->equa[1], DATA_SIZE, "+\0");
	strcpy_s(p->equa[2], DATA_SIZE, "2/3\0");
	strcpy_s(p->equa[3], DATA_SIZE, "+\0");
	strcpy_s(p->equa[4], DATA_SIZE, "1\0");
	strcpy_s(p->equa[5], DATA_SIZE, "+\0");
	strcpy_s(p->equa[6], DATA_SIZE, "2\0");

	char pChr1[DATA_SIZE];
	char pChr2[DATA_SIZE];
	char pChr3[DATA_SIZE];

	strcpy_s(pChr1, DATA_SIZE, ((fraction)calculate_equa(pequ)).print_res(true));
	strcpy_s(pChr2, DATA_SIZE, ((fraction)calculate_equa(pequ->next)).print_res(true));
	strcpy_s(pChr3, DATA_SIZE, ((fraction)calculate_equa(p)).print_res(true));

	Assert::AreEqual(pChr1, "3");
	Assert::AreEqual(pChr2, "-1/10");
	Assert::AreEqual(pChr3, "4 1/6\0");
}

测试结果如下图

要求二

功能一的测试中,全部fail。原因在于((fraction)calculate_equa(p)).print_res()这句代码中的print_res()返回的是分数形式,而不是小数形式。修改print_res()函数为print_res(bool bol),当参数为true时输出分数,参数为false是输出小数。当参数为false是的部分代码如下

char pChr[CHAR_SIZE];
if (this->numerator == 0)
{
	//printf("0");
	sprintf_s(pChr, "0\0");
}
else
{
	if (this->denominator == 1)
	{
		//printf("%d", this->numerator);
		sprintf_s(pChr, "%d\0", this->numerator);
	}
	else
	{
		float nume = abs(this->numerator);	//获取分子
		float deno = abs(this->denominator);	//获取分母
		float res = nume / deno;
		if (this->numerator < 0)
		{
			//printf("-%d %d/%d", nume / deno, nume % deno, deno);
			sprintf_s(pChr, "-%.1f\0", res);
		}
		else
		{
			//printf("%d %d/%d", nume / deno, nume % deno, deno);
			sprintf_s(pChr, "%.1f\0", res);
		}

	}
}
return pChr;

功能二测试全部pass
功能三测试fail,失败原因在于存储运算结果的三个变量pChr1、pChr2、pChr3出现数组越界问题,没有获得真是的结果,调试测试如下图

反复调试发现是在函数((fraction)calculate_equa(p)).print_res(true)中的print_res()函数中出现的数组越界,扩大函数print_res()中存储返回值的数组pChr,由CHAR_SIZE改为CHAR_SIZE*4。代码如下

char pChr[CHAR_SIZE * 4];
if (this->numerator == 0)
{
	//printf("0");
	sprintf_s(pChr, "0\0");
}
else
{
	if (this->denominator == 1)
	{
		//printf("%d", this->numerator);
		sprintf_s(pChr, "%d\0", this->numerator);
	}
	else
	{
		int nume = abs(this->numerator);	//获取分子
		int deno = abs(this->denominator);	//获取分母
		if (nume / deno == 0)
		{
			//printf("%d/%d", this->numerator, this->denominator);
			sprintf_s(pChr, "%d/%d\0", this->numerator, this->denominator);
		}
		else
		{
			if (this->numerator < 0)
			{
				//printf("-%d %d/%d", nume / deno, nume % deno, deno);
				sprintf_s(pChr, "-%d %d/%d\0", nume / deno, nume % deno, deno);
			}
			else
			{
				//printf("%d %d/%d", nume / deno, nume % deno, deno);
				sprintf_s(pChr, "%d %d/%d\0", nume / deno, nume % deno, deno);
			}
		}
	}
}
return pChr;

要求三

已做好准备

要求四

单元测试的代码文件名为[f4_UnitTest.cpp],所在的coding地址为https://e.coding.net/nenuwork/ourteam/arithmetic_operation.git

推荐阅读