Looking at the answers here, I really think the most-beautiful and perfect answer is a blend between @Billy Donahue's answer and @Alexander Voitenko's answer.
So, here's what I recommend. I think this should become part of the official googletest/googlemock code base:
Quick summary
// 1. definitions
#define EXPECT_RANGE(val, min, max) EXPECT_THAT((val), \
::testing::AllOf(::testing::Ge((min)), ::testing::Le((max))))
#define ASSERT_RANGE(val, min, max) ASSERT_THAT((val), \
::testing::AllOf(::testing::Ge((min)), ::testing::Le((max))))
// 2. usage
EXPECT_RANGE(value, min, max);
ASSERT_RANGE(value, min, max);
Details
Here is a more-complete picture:
#include "gmock/gmock.h"
/// Expect or assert that value `val` is within the range of `min` to `max`,
/// inclusive. ie: `val` is tested to be >= `min` and <= `max`.
/// See:
/// 1. googletest `matchers.md` document under the "Composite Matchers" section,
/// here:
/// https://github.com/google/googletest/blob/main/docs/reference/matchers.md#composite-matchers
/// 1. [My answer with this code] https://mcmap.net/q/480645/-expect-a-value-within-a-given-range-using-google-test
#define EXPECT_RANGE(val, min, max) EXPECT_THAT((val), \
::testing::AllOf(::testing::Ge((min)), ::testing::Le((max))))
#define ASSERT_RANGE(val, min, max) ASSERT_THAT((val), \
::testing::AllOf(::testing::Ge((min)), ::testing::Le((max))))
TEST(Simulation, TrivialEndToEnd)
{
// ...test setup stuff goes here...
// Usage example: expect that the `distance_traveled_miles` value is within
// the range 1151.77 to 1151.97, inclusive.
EXPECT_RANGE(stats->distance_traveled_miles, 1151.77, 1151.97);
}
The error output if the test fails is really nice too. Here's what is printed in the event of a failure:
src/main_unittest.cpp:194: Failure
Value of: (stats->distance_traveled_miles)
Expected: (is >= 1151.77) and (is <= 1151.97)
Actual: 1151.6666666667204 (of type double)
[ FAILED ] Simulation.TrivialEndToEnd (0 ms)
You can also add C++-style prints to the error message if desired, using the <<
output print operator, like this:
EXPECT_RANGE(stats->distance_traveled_miles, 1151.77, 1151.97)
<< "Your custom failure message goes here.\n";
The line just above will produce this output in the event of a failure:
src/main_unittest.cpp:194: Failure
Value of: (stats->distance_traveled_miles)
Expected: (is >= 1151.77) and (is <= 1151.97)
Actual: 1151.6666666667204 (of type double)
Your custom failure message goes here.
[ FAILED ] Simulation.TrivialEndToEnd (0 ms)
References:
- I first saw the
EXPECT_THAT(x, AllOf(Ge(1),Le(3)));
usage in @Billy Donahue's answer here.
- https://github.com/google/googletest/blob/main/docs/reference/matchers.md
- Learn about the
AllOf(m1, m2, ..., mn)
composite matcher here under the section "Composite Matchers"
- Learn about the
Ge()
("greater than or equal to") and Le()
("less than or equal to") matchers here under the section "Multi-argument Matchers"
EXPECT_RANGE(value, min, max);
andASSERT_RANGE(value, min, max);
– Improvised