commit cabdebd2847ad5dcba6532e027e4aacedc7aaaec
parent 397998fe12390cab951c81a9be41eaf22eca5db9
Author: d.levin256@gmail.com <d.levin256@gmail.com>
Date: Mon, 25 Nov 2019 08:30:49 +0000
Avoid undefined behavior in absmin and absmax
Diffstat:
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/tests/unit/math/min_max.cpp b/tests/unit/math/min_max.cpp
@@ -26,6 +26,21 @@ TEST(max)
[](auto x, auto y) -> common_type<decltype(x), decltype(y)> { return x >= y ? x : y; });
}
+struct IsNotMinInt
+{
+ template <typename T>
+ bool operator()(ctype_t<T>, identity<T> x, identity<T> y) const
+ {
+ return is_floating_point<T> || is_unsigned<T> ||
+ (x != std::numeric_limits<T>::min() && y != std::numeric_limits<T>::min());
+ }
+ template <typename T, size_t N>
+ bool operator()(ctype_t<vec<T, N>>, identity<T> x, identity<T> y) const
+ {
+ return operator()(ctype<T>, x, y);
+ }
+};
+
TEST(absmin)
{
test_function2(
@@ -34,7 +49,8 @@ TEST(absmin)
x = x >= 0 ? x : -x;
y = y >= 0 ? y : -y;
return x <= y ? x : y;
- });
+ },
+ IsNotMinInt{});
}
TEST(absmax)
@@ -45,7 +61,8 @@ TEST(absmax)
x = x >= 0 ? x : -x;
y = y >= 0 ? y : -y;
return x >= y ? x : y;
- });
+ },
+ IsNotMinInt{});
}
} // namespace CMT_ARCH_NAME
} // namespace kfr