首先,在專案按下右鍵,點選管理方案的NuGet套件
接著的操作如下圖
下載完後會有會顯示已完成,畫面如下
接著我們就開始寫一個函式計算內積運算,以及進行單元測試
程式碼如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gtest/gtest.h> | |
double computeInnerProduct(double v1[], double v2[], int d1, int d2) | |
{ | |
if (d1 != d2) | |
throw "Vectors of different dimension!"; | |
double r = 0; | |
for (int i = 0; i<d1; ++i) { | |
r += v1[i] * v2[i]; | |
} | |
return r; | |
} | |
int main(int argc, char *argv[]) { | |
testing::InitGoogleTest(&argc, argv); | |
RUN_ALL_TESTS(); | |
system("pause"); | |
return 0; | |
} | |
TEST(computeInnerProduct, computeValue) | |
{ | |
double u[2] = { 1,0 }; | |
double v[2] = { 1,1 }; | |
/*將u和v兩個陣列丟到computeInnerProduct函數進行內積運算 | |
而我們期望回傳的值是1 */ | |
EXPECT_EQ(1, computeInnerProduct(u, v, 2, 2)); | |
} | |
TEST(computerInnerProduct, dim_error) | |
{ | |
double x[2] = { 1,0 }; | |
double y[3] = { 1,1,1 }; | |
try { | |
computeInnerProduct(x, y, 2, 3); | |
} | |
catch (char const * s) { | |
//strcmp() 比較兩個字串是否相等,相等就回傳 0 | |
//An assert is a statment that is either true or false | |
//assert can be used to check whether or not your code satisffies the assertion | |
assert(0 == strcmp("Vectors of different dimension!", s)); | |
} | |
} |
得到的結果為
文章標籤
全站熱搜