Skip to content

Commit 55b6da5

Browse files
committed
[#3163] add EXPECT_EQ_MARGIN and EXPECT_IN_RANGE
1 parent 2552391 commit 55b6da5

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/lib/testutils/gtest_utils.h

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,55 @@ namespace test {
118118
}
119119
#endif
120120

121-
}; // end of isc::test namespace
122-
}; // end of isc namespace
121+
/// @brief Expect two values to be equal with a given margin of error.
122+
///
123+
/// Output is similar to official gtest expect macro outputs.
124+
/// Static casts avoid comparison of integers of different signs.
125+
///
126+
/// @param val1 the first value being tested
127+
/// @param val2 the second value being tested
128+
/// @param margin the allowed margin of error
129+
#define EXPECT_EQ_MARGIN(val1_statement, val2_statement, margin_statement) \
130+
{ \
131+
auto const val1(val1_statement); \
132+
auto const val2(static_cast<decltype(val1)>(val2_statement)); \
133+
auto const margin(static_cast<decltype(val1)>(margin_statement)); \
134+
if (val1 < val2 && val1 + margin < val2 || val2 < val1 && val2 + margin < val1) { \
135+
ADD_FAILURE() << "Expected equality of these values:\n" \
136+
<< " " << #val1_statement << "\n" \
137+
<< " Which is: " << val1 << "\n" \
138+
<< " " << #val2_statement << "\n" \
139+
<< " Which is: " << val2 << "\n" \
140+
<< "With a margin of error of:\n" \
141+
<< " " << #margin_statement << "\n" \
142+
<< " Which is: " << margin << ""; \
143+
} \
144+
}
145+
146+
/// @brief Expect a value to be between two other given values.
147+
///
148+
/// Output is similar to official gtest expect macro outputs.
149+
/// Static casts avoid comparison of integers of different signs.
150+
///
151+
/// @param value_statement the statement for the value being tested
152+
/// @param low_statement the low reference value being tested against
153+
/// @param high_statement the high reference value being tested against
154+
#define EXPECT_IN_RANGE(value_statement, low_statement, high_statement) \
155+
{ \
156+
auto const value(value_statement); \
157+
auto const low(static_cast<decltype(value)>(low_statement)); \
158+
auto const high(static_cast<decltype(value)>(high_statement)); \
159+
if (value < low || high < value) { \
160+
ADD_FAILURE() << "Expected this value:\n" \
161+
<< " " << #value_statement << "\n" \
162+
<< " Which is: " << value << "\n" \
163+
<< "To be in range:\n" \
164+
<< " [" << #low_statement << ", " << #high_statement << "]\n" \
165+
<< " Which is: [" << low << ", " << high << "]"; \
166+
} \
167+
}
168+
169+
} // namespace test
170+
} // namespace isc
123171

124172
#endif // GTEST_UTILS_H

0 commit comments

Comments
 (0)