Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The EXPECT_NEAR assertion will be presented first. It is very elegant because it allows the determination of the expected absolute error between the values. It was mentioned earlier that the function returns a number with high decimal precision. However, a precision of up to 7 digits is expected. It is enough to provide the expected value and the maximum absolute error. 

Code Block
languagecpp
titleTEST(GetPiOperation, AbsoluteError)
linenumberstrue
TEST(GetPiOperation, AbsoluteError) {
    EXPECT_NEAR(get_pi(), 3.14159265, 4e1e-98);
    //EXPECT_NEAR(get_pi(), 3.14159265, 3.5e1e-9);  // Should FAIL
}

If a given absolute error is not selected correctly, as in the commented line above, the assertion will fail with the message shown below:

Code Block
languagebash
titleResult of the assertion: EXPECT_NEAR(get_pi(), 3.14159265, 1e-9)
linenumberstrue
The difference between get_pi() and 3.14159265 is 3.5897929073769319e-09, which exceeds 1e-9, where
get_pi() evaluates to 3.1415926535897931,
3.14159265 evaluates to 3.1415926500000002, and
1e-9 evaluates to 1.0000000000000001e-09.
[  FAILED  ] GetPiOperation.AbsoluteError (0 ms)

...