当前位置: 动力学知识库 > 问答 > 编程问答 >

visual studio - Unit Tests for C++ with VS2015 to test Vector Population

问题描述:

I am trying to write Unit Test for a C++ routine, using Visual Studio 2015 and it's test framework. I have followed a tutorial and set up the test project, but the first test I am running keeps failing.

My Routine I want to test:

vector< vector<int> > history;

vector<int> values;

void populateArray(int input, int time) {

values.push_back(time);

values.push_back(input);

if (history.size() > 0) {

values.push_back(values[0] - history.back()[0]);

values.push_back(values[1] - history.back()[1]);

values.push_back(values[3] / values[2]);

}

history.push_back(values);

};

My Test Code:

TEST_CLASS(MyTests)

{

public:

TEST_METHOD(Array_Populates)

{

int i = 0;

while (i < 2) {

populateArray(dummyData[i][0], dummyData[i][1]);

Assert::AreEqual(history[i][0], dummyData[i][1]);

i++;

}

int j = 0;

while (j < history.size()) {

j++;

}

}

int const MyTests::dummyData[15][2] = {

{ 95, 86400 },

{ 77, 172800 },

{ 76, 259200 },

{ 75, 345600 },

{ 65, 432000 },

{ 65, 518400 },

{ 65, 604800 },

{ 60, 691200 },

{ 52, 777600 },

{ 51, 864000 },

{ 50, 950400 },

{ 40, 1036800 },

{ 40, 1123200 },

{ 40, 1209600 },

{ 35, 1296000 }

};

};

I am trying to populate the vector row by row with a line of the Dummy Array. The after that is done, I would like to check that every line is correct. However, if I run the test, where the loop goes more than once, it fails, saying that the expected value was 95, but actual value was 77. I am not sure if this is an error with the vector code, but I can't see one. Or if it is with my understanding of the Unit Test code.

Thanks For Your Help.

分享给朋友:
您可能感兴趣的文章:
随机阅读: