在网上学习了vs测试,发现都是以类为单位来测试类中的方法、属性。
然而我的四则运算代码,没有类,是照着C的风格写的。
于是我将代码中需要测试的方法 在测试类中进行重写。
测试有点牵强。
测试目标 后缀表达式求值函数
测试结果正确
代码:
#include "stdafx.h" using namespace System; using namespace System::Text; using namespace System::Collections::Generic; using namespace Microsoft::VisualStudio::TestTools::UnitTesting; namespace TestProject2 { struct Number { double a; bool b;//0数字 1字符 }; Number s3[100]; [TestClass] public ref class UnitTest { private: TestContext^ testContextInstance; public: property Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ TestContext { Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ get() { return testContextInstance; } System::Void set(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ value) { testContextInstance = value; } }; #pragma region Additional test attributes #pragma endregion double qiuzhi() { //stack<Number>s3; //stack<double>s4; int tou1 = 2; double s4[100]; int tou2 = 0; //while (!s3.empty()) while (tou1 != -1) { //Number c1 = s3.top(); Number c1 = s3[tou1--]; //s3.pop(); if (c1.b == 0) s4[tou2++] = c1.a; //s4.push(c1.a); else { //double c2 = s4.top(); double c2 = s4[--tou2]; //s4.pop(); //double c3 = s4.top(); ///s4.pop(); double c3 = s4[--tou2]; double c4; switch ((int)c1.a) { case '+':c4 = c3 + c2; break; case '-':c4 = c3 - c2; break; case '*':c4 = c3*c2; break; case '/':c4 = c3 / c2; break; } s4[tou2] = c4; } } return s4[tou2]; } [TestMethod] void TestMethod1() { s3[0].a = 43; s3[0].b = 1; s3[1].a = 1; s3[1].b = 0; s3[2].a = 2; s3[2].b = 0; double s=qiuzhi(); Assert::AreEqual(s, 3.0); }; }; }
测试结果: