首先,需要包含gtest头文件,并使用TEST宏定义一个测试用例。在测试用例中,可以使用EXPECT_DOUBLE_EQ或ASSERT_DOUBLE_EQ断言函数来比较双精度的值是否相等。 EXPECT_DOUBLE_EQ和ASSERT_DOUBLE_EQ的用法类似,区别在于EXPECT_DOUBLE_EQ在断言失败时会继续执行后续的代码,而ASSERT_DOUBLE_EQ在断言失败时会立即退出当...
EXPECT_DOUBLE_EQ(expected, actual):(double)expected == actual EXPECT_NEAR(var1, var2, tol):abs(var1 - var2) <= tol 当判断浮点数时,推荐使用后三个。 3、大于小于 EXPECT_LT(var1, var2):var1 < var2 EXPECT_GT(var1, var2):var1 > var2 EXPECT_LE(var1, var2):var1 <= var2 E...
// 下面三个 TEST 都是属于同一个 test suite,即 FactorialTest// 正数为一组TEST(FactorialTest,Negative){EXPECT_EQ(1,Factorial(-5));EXPECT_EQ(1,Factorial(-1));EXPECT_GT(Factorial(-10),0);}// 0TEST(FactorialTest,Zero){EXPECT_EQ(1,Factorial(0));}// 负数为一组TEST(FactorialTest,Positiv...
ASSERT_DOUBLE_EQ(expected, actual); EXPECT_DOUBLE_EQ(expected, actual); the two double values are almost equal 对相近的两个数比较: Fatal assertionNonfatal assertionVerifies ASSERT_NEAR(val1, val2, abs_error); EXPECT_NEAR(val1, val2, abs_error); the difference between val1 and val2 doesn...
EXPECT_DOUBLE_EQ(val1, val2); the two double values are almost equal almost euqal表示两个数只是近似相似,默认的是是指两者的差值在4ULP之内(Units in the Last Place)。我们还可以自己制定精度 Fatal assertion Nonfatal assertion Verifies ASSERT_NEAR(val1, val2, abs_error); EXPECT_NEAR(val1, ...
ASSERT_FLOAT_EQ和ASSERT_DOUBLE_EQ是在比较的基础上允许一定的浮点数精度误差值。如果你需要比较两个浮点数,请使用对应的浮点数类型比较宏,而不要直接使用ASSERT_EQ; 4.6 ASSERT_GE/LE 注意,大小写比较都是左和右直接按顺序比较的,比如GE是判断左边是否大于右边(不要搞反顺序了) TEST(EXPTEST, EXPTEST_GE) ...
EXPECT_EQ(4, st.pop());//使用Google Test宏进行测试(非致命断言) } //测试实例2 TEST(testStack, testAll) { MyStack st; st.push(9); st.push(28); int val = st.pop(); EXPECT_EQ(28, val);//28等于val则测试通过(非致命断言) ...
EXPECT_DOUBLE_EQ(expected, actual); the two double values are almost equal 对相近的两个数比较: Fatal assertion Nonfatal assertion Verifies ASSERT_NEAR(val1, val2, abs_error); EXPECT_NEAR(val1, val2, abs_error); the difference between val1 and val2 doesn't exceed the given ...
EXPECT_DOUBLE_EQ(expected, actual); the two double values are almost equal 对相近的两个数比较: Fatal assertion Nonfatal assertion Verifies ASSERT_NEAR(val1, val2, abs_error); EXPECT_NEAR(val1, val2, abs_error); the difference between val1 and val2 doesn't exceed the given ...
EXPECT_DOUBLE_EQ(val1, val2); the two double values are almost equal ASSERT_NEAR(val1, val2, abs_error); EXPECT_NEAR(val1, val2, abs_error); the difference between val1 and val2 doesn’t exceed the given absolute error ASSERT_THROW(statement, exception_type); EXPECT_THROW(statement,...