我使用Visual Studio 2015的内置测试框架对以下C++代码进行单元测试。当我运行下面的测试时,没有发生错误(代码编译),但测试只是挂起。它仅在我已将注释号averageGradient
运行的注释掉的行上执行。为什么是这样?单元测试没有失败,但挂在子程序调用
float averageGradient(int x1, int x2) {
int i = 0, y1 = 0, y2 = 0;
while (i < graph.size() && (y1 == 0 || y2 == 0)) { //if both y values haven't been solved then keep trying
if (x1 >= graph[i][0] && x1 < graph[i][1]) { // if x1 lies in the first straight line segment
y1 = (graph[i][2] * x1) + graph[i][2]; // then calculate it's y value (y1)
}
else if (x2 >= graph[i][0] && x2 < graph[i][1]) { //try the same thing for x2
y2 = (graph[i][2] * x2) + graph[i][3]; //calculate its y value (y2)
}
else { i++; } //otherwise incriment i to check the next straight line segment
}
float m = (y2 - y1)/(x2 - x1);
return m;
};
单元测试:
TEST_METHOD(Average_Gradient) {
int x1 = 683675;
int x2 = x1 + 86400;
//float gradient = averageGradient(x1, x2);
float answer = 0.0000895684639;
//Assert::AreEqual(answer, gradient);
}
单元测试没有任何关系。简单地说,你的平均梯度计算函数挂起。 –
@YeldarKurmangaliyev好吧,我会改变标记 - 不确定,因为我对单元测试是新手。但是,代码确实运行,所以为什么在测试时挂起? –
函数是否可以在单元测试之外正确地调用? –