Visual Studio 单元测试的简单应用

写在前面

笔者根据作业的介绍以及Visual Studio 2017 文档的相关说明，进行了Visual Studio 单元测试的简单应用。

单元测试代码

单元测试从以下三方面展开：

1. 四则运算正确性；
2. 四则运算组合后计算的正确性；
3. 要求范围内所有整数运算的正确性；
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../Calculator/Calculator.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace CalculatorUnitTest
{
TEST_CLASS(Test)
{
public:
Calculator* calc;

TEST_METHOD_INITIALIZE(initCal)
{
calc = new Calculator();
}

TEST_METHOD_CLEANUP(CleanCal)
{
delete calc;
calc = NULL;
}

/*
简单测试
*/

{
string ret = calc->Solve("11+22");
Assert::AreEqual(ret, (string)"11+22=33");
}

TEST_METHOD(SimpleSUB)
{
string ret = calc->Solve("77-33");
Assert::AreEqual(ret, (string)"77-33=44");
}

TEST_METHOD(SimpleMUL)
{
string ret = calc->Solve("55*66");
Assert::AreEqual(ret, (string)"55*66=3630");
}

TEST_METHOD(SimpleDIV)
{
string ret = calc->Solve("99/33");
Assert::AreEqual(ret, (string)"99/33=3");
}

/*
组合测试
*/

{
string ret = calc->Solve("22+33+11");
Assert::AreEqual(ret, (string)"22+33+11=66");
}

{
string ret = calc->Solve("22+33-11");
Assert::AreEqual(ret, (string)"22+33-11=44");
}

{
string ret = calc->Solve("22+33*11");
Assert::AreEqual(ret, (string)"22+33*11=385");
}

{
string ret = calc->Solve("22+33/11");
Assert::AreEqual(ret, (string)"22+33/11=25");
}

{
string ret = calc->Solve("99-66+77");
Assert::AreEqual(ret, (string)"99-66+77=110");
}

TEST_METHOD(SUB_SUB)
{
string ret = calc->Solve("99-22-55");
Assert::AreEqual(ret, (string)"99-22-55=22");
}

TEST_METHOD(SUB_MUL)
{
string ret = calc->Solve("77-6*11");
Assert::AreEqual(ret, (string)"77-6*11=11");
}

TEST_METHOD(SUB_DIV)
{
string ret = calc->Solve("77-88/2");
Assert::AreEqual(ret, (string)"77-88/2=33");
}

{
string ret = calc->Solve("22*33+11");
Assert::AreEqual(ret, (string)"22*33+11=737");
}

TEST_METHOD(MUL_SUB)
{
string ret = calc->Solve("22*33-11");
Assert::AreEqual(ret, (string)"22*33-11=715");
}

TEST_METHOD(MUL_MUL)
{
string ret = calc->Solve("22*33*99");
Assert::AreEqual(ret, (string)"22*33*99=71874");
}

TEST_METHOD(MUL_DIV)
{
string ret = calc->Solve("22*33/11");
Assert::AreEqual(ret, (string)"22*33/11=66");
}

{
string ret = calc->Solve("99/9+11");
Assert::AreEqual(ret, (string)"99/9+11=22");
}

TEST_METHOD(DIV_SUB)
{
string ret = calc->Solve("99/9-10");
Assert::AreEqual(ret, (string)"99/9-10=1");
}

TEST_METHOD(DIV_MUL)
{
string ret = calc->Solve("99/9*22");
Assert::AreEqual(ret, (string)"99/9*22=242");
}

TEST_METHOD(DIV_DIV)
{
string ret = calc->Solve("99/9/11");
Assert::AreEqual(ret, (string)"99/9/11=1");
}

/*
运算数长度
*/
TEST_METHOD(Length_1)
{
string ret = calc->Solve("9/3*2+8");
Assert::AreEqual(ret, (string)"9/3*2+8=14");
}

TEST_METHOD(Length_2)
{
string ret = calc->Solve("90/3*22+8");
Assert::AreEqual(ret, (string)"90/3*22+8=668");
}

TEST_METHOD(Length_3)
{
string ret = calc->Solve("90/30*2+89");
Assert::AreEqual(ret, (string)"90/30*2+89=95");
}
};

}



Bug修复

经过调试，对源程序的以下三个部分进行修复。

string Calculator::Solve(string formula) {
vector<string>* tempStack = new vector<string>();
stack<char>* operatorStack = new stack<char>();
int len = formula.length();
int k = 0;
for (int j = -1; j < len - 1; j++) {
char formulaChar = formula[j + 1];
if (j == len - 2 || formulaChar == '+' || formulaChar == '-' ||
formulaChar == '*' || formulaChar == '/') {
if (j == len - 2) {
tempStack->push_back(formula.substr(k));
}
else {
if (k <= j) {	// 判断条件错误：k < j ===> k <= j
/*
substr 使用错误:
tempStack->push_back(formula.substr(k, j + 1));
*/
/*----------修改后----------*/
tempStack->push_back(formula.substr(k, j - k + 1));
/*--------------------------*/
}
if (operatorStack->empty()) {
operatorStack->push(formulaChar);
}
else {
char stackChar = operatorStack->top();
if ((stackChar == '+' || stackChar == '-')
&& (formulaChar == '*' || formulaChar == '/')) {
operatorStack->push(formulaChar);
}
else {
/*
to_string 使用错误:
tempStack->push_back(to_string(operatorStack->top()));
*/
/*----------修改后----------*/
string tempOp(1, operatorStack->top());
tempStack->push_back(tempOp);
/*--------------------------*/
operatorStack->pop();
operatorStack->push(formulaChar);
}
}
}
k = j + 2;
}
}
// 以下略。

• 错误一导致一位数计算时无法将字符串中的数字截取插入临时栈(tempStack)。
• 错误二导致截取数字时截取到错误的长度的子串。
• 错误三会将运算符转换为相应的ASCII码，导致插入临时栈的为数字字符串而非运算符字符串。
posted @ 2019-02-26 20:47  Cookize  阅读(128)  评论(0编辑  收藏  举报