我使用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好吧,我會改變標記 - 不確定,因爲我對單元測試是新手。但是,代碼確實運行,所以爲什麼在測試時掛起? –
函數是否可以在單元測試之外正確地調用? –