From: Andrei Pavel Date: Mon, 29 Apr 2024 09:28:08 +0000 (+0300) Subject: [#3163] add EXPECT_EQ_MARGIN and EXPECT_IN_RANGE X-Git-Tag: Kea-2.6.0~93 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55b6da5b9eb05b03b39a2674ac221cf46bee94e8;p=thirdparty%2Fkea.git [#3163] add EXPECT_EQ_MARGIN and EXPECT_IN_RANGE --- diff --git a/src/lib/testutils/gtest_utils.h b/src/lib/testutils/gtest_utils.h index 76afb1e22d..2d5c35e6a6 100644 --- a/src/lib/testutils/gtest_utils.h +++ b/src/lib/testutils/gtest_utils.h @@ -118,7 +118,55 @@ namespace test { } #endif -}; // end of isc::test namespace -}; // end of isc namespace +/// @brief Expect two values to be equal with a given margin of error. +/// +/// Output is similar to official gtest expect macro outputs. +/// Static casts avoid comparison of integers of different signs. +/// +/// @param val1 the first value being tested +/// @param val2 the second value being tested +/// @param margin the allowed margin of error +#define EXPECT_EQ_MARGIN(val1_statement, val2_statement, margin_statement) \ + { \ + auto const val1(val1_statement); \ + auto const val2(static_cast(val2_statement)); \ + auto const margin(static_cast(margin_statement)); \ + if (val1 < val2 && val1 + margin < val2 || val2 < val1 && val2 + margin < val1) { \ + ADD_FAILURE() << "Expected equality of these values:\n" \ + << " " << #val1_statement << "\n" \ + << " Which is: " << val1 << "\n" \ + << " " << #val2_statement << "\n" \ + << " Which is: " << val2 << "\n" \ + << "With a margin of error of:\n" \ + << " " << #margin_statement << "\n" \ + << " Which is: " << margin << ""; \ + } \ + } + +/// @brief Expect a value to be between two other given values. +/// +/// Output is similar to official gtest expect macro outputs. +/// Static casts avoid comparison of integers of different signs. +/// +/// @param value_statement the statement for the value being tested +/// @param low_statement the low reference value being tested against +/// @param high_statement the high reference value being tested against +#define EXPECT_IN_RANGE(value_statement, low_statement, high_statement) \ + { \ + auto const value(value_statement); \ + auto const low(static_cast(low_statement)); \ + auto const high(static_cast(high_statement)); \ + if (value < low || high < value) { \ + ADD_FAILURE() << "Expected this value:\n" \ + << " " << #value_statement << "\n" \ + << " Which is: " << value << "\n" \ + << "To be in range:\n" \ + << " [" << #low_statement << ", " << #high_statement << "]\n" \ + << " Which is: [" << low << ", " << high << "]"; \ + } \ + } + +} // namespace test +} // namespace isc #endif // GTEST_UTILS_H