...
2. Testing whether the result is within range
Checking whether the obtained result is within the expected range, it will be presented in two ways. This will be shown in the example get_pi()
function, which task is to return only Pi with high decimal precision of 36 digits. The get_pi()
function body:
Code Block |
---|
language | cpp |
---|
title | double get_pi() |
---|
linenumbers | true |
---|
|
double get_pi(){
return 3.141592653589793238462643383279502884L;
} |
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.
Code Block |
---|
language | cpp |
---|
title | TEST(GetPiOperation, AbsoluteError) |
---|
|
TEST(GetPiOperation, AbsoluteError) {
EXPECT_NEAR(get_pi(), 3.14159265, 4e-9);
//EXPECT_NEAR(get_pi(), 3.14159265, 3.5e-9); // Should FAIL
} |
Code Block |
---|
language | bash |
---|
title | Result of the assertion: EXPECT_NEAR(get_pi(), 3.14159265, 1e-9) |
---|
linenumbers | true |
---|
|
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) |
3. Testing boundary conditions
...