aboutsummaryrefslogtreecommitdiff
path: root/src/test/fuzz
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2022-02-01 13:43:42 +0100
committerMarcoFalke <falke.marco@gmail.com>2022-02-21 14:32:53 +0100
commitfaa7d8a3f7cba02eca7e247108a6b98ea9daf373 (patch)
tree8826fb85e35da9fb2bee74a5da27cbf39b0c1b2a /src/test/fuzz
parent1337b93f50d9874fa3bce7c0b5bb141aad9b1c49 (diff)
util: Add SaturatingAdd helper
Diffstat (limited to 'src/test/fuzz')
-rw-r--r--src/test/fuzz/addition_overflow.cpp17
1 files changed, 13 insertions, 4 deletions
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