mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
util: Add SaturatingAdd helper
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user