From faa7d8a3f7cba02eca7e247108a6b98ea9daf373 Mon Sep 17 00:00:00 2001 From: MarcoFalke <falke.marco@gmail.com> Date: Tue, 1 Feb 2022 13:43:42 +0100 Subject: util: Add SaturatingAdd helper --- src/test/fuzz/addition_overflow.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src/test/fuzz') diff --git a/src/test/fuzz/addition_overflow.cpp b/src/test/fuzz/addition_overflow.cpp index cfad41659e..372c1a370e 100644 --- a/src/test/fuzz/addition_overflow.cpp +++ b/src/test/fuzz/addition_overflow.cpp @@ -26,6 +26,12 @@ void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider) const T i = fuzzed_data_provider.ConsumeIntegral<T>(); const T j = fuzzed_data_provider.ConsumeIntegral<T>(); const bool is_addition_overflow_custom = AdditionOverflow(i, j); + const auto maybe_add{CheckedAdd(i, j)}; + const auto sat_add{SaturatingAdd(i, j)}; + assert(is_addition_overflow_custom == !maybe_add.has_value()); + assert(is_addition_overflow_custom == AdditionOverflow(j, i)); + assert(maybe_add == CheckedAdd(j, i)); + assert(sat_add == SaturatingAdd(j, i)); #if defined(HAVE_BUILTIN_ADD_OVERFLOW) T result_builtin; const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin); @@ -33,11 +39,14 @@ void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider) if (!is_addition_overflow_custom) { assert(i + j == result_builtin); } -#else - if (!is_addition_overflow_custom) { - (void)(i + j); - } #endif + if (is_addition_overflow_custom) { + assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max()); + } else { + const auto add{i + j}; + assert(add == maybe_add.value()); + assert(add == sat_add); + } } } // namespace -- cgit v1.2.3