From 4188d35cafca3f1540276514f2d8d286029adf8d Mon Sep 17 00:00:00 2001 From: Crzyrndm Date: Sun, 19 Aug 2018 15:11:57 +1200 Subject: [PATCH] Add tests for the other small utilities --- source/detail/numeric_utils.hpp | 8 +-- tests/detail/numeric_util_test_suite.cpp | 67 ++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/source/detail/numeric_utils.hpp b/source/detail/numeric_utils.hpp index a50ea802..30f49f89 100644 --- a/source/detail/numeric_utils.hpp +++ b/source/detail/numeric_utils.hpp @@ -58,8 +58,8 @@ constexpr Number abs(Number val) /// /// constexpr max /// -template -constexpr Number max(Number lval, Number rval) +template +constexpr typename std::common_type::type max(NumberL lval, NumberR rval) { return (lval < rval) ? rval : lval; } @@ -67,8 +67,8 @@ constexpr Number max(Number lval, Number rval) /// /// constexpr min /// -template -constexpr Number min(Number lval, Number rval) +template +constexpr typename std::common_type::type min(NumberL lval, NumberR rval) { return (lval < rval) ? lval : rval; } diff --git a/tests/detail/numeric_util_test_suite.cpp b/tests/detail/numeric_util_test_suite.cpp index 2e9489fb..a6bf810a 100644 --- a/tests/detail/numeric_util_test_suite.cpp +++ b/tests/detail/numeric_util_test_suite.cpp @@ -31,6 +31,9 @@ public: { register_test(test_float_equals_zero); register_test(test_float_equals_large); + register_test(test_min); + register_test(test_max); + register_test(test_abs); } void test_float_equals_zero() @@ -108,5 +111,69 @@ public: // all the same options are available for increasing/decreasing the precision of the comparison // however the the epsilon source should always be of equal or lesser precision than the arguments when away from zero } + + void test_float_equals_nan() + { + const float nan = std::nan(""); + // nans always compare false + xlnt_assert(!xlnt::detail::float_equals(nan, 0.f)); + xlnt_assert(!xlnt::detail::float_equals(nan, nan)); + xlnt_assert(!xlnt::detail::float_equals(nan, 1000.f)); + } + + void test_min() + { + // simple + xlnt_assert(xlnt::detail::min(0, 1) == 0); + xlnt_assert(xlnt::detail::min(1, 0) == 0); + xlnt_assert(xlnt::detail::min(0.0, 1) == 0.0); // comparisons between different types just work + xlnt_assert(xlnt::detail::min(1, 0.0) == 0.0); + // negative numbers + xlnt_assert(xlnt::detail::min(0, -1) == -1.0); + xlnt_assert(xlnt::detail::min(-1, 0) == -1.0); + xlnt_assert(xlnt::detail::min(0.0, -1) == -1.0); + xlnt_assert(xlnt::detail::min(-1, 0.0) == -1.0); + // no zeroes + xlnt_assert(xlnt::detail::min(10, -10) == -10.0); + xlnt_assert(xlnt::detail::min(-10, 10) == -10.0); + xlnt_assert(xlnt::detail::min(10.0, -10) == -10.0); + xlnt_assert(xlnt::detail::min(-10, 10.0) == -10.0); + + static_assert(xlnt::detail::min(-10, 10.0) == -10.0, "constexpr"); + } + + void test_max() + { + // simple + xlnt_assert(xlnt::detail::max(0, 1) == 1); + xlnt_assert(xlnt::detail::max(1, 0) == 1); + xlnt_assert(xlnt::detail::max(0.0, 1) == 1.0); // comparisons between different types just work + xlnt_assert(xlnt::detail::max(1, 0.0) == 1.0); + // negative numbers + xlnt_assert(xlnt::detail::max(0, -1) == 0.0); + xlnt_assert(xlnt::detail::max(-1, 0) == 0.0); + xlnt_assert(xlnt::detail::max(0.0, -1) == 0.0); + xlnt_assert(xlnt::detail::max(-1, 0.0) == 0.0); + // no zeroes + xlnt_assert(xlnt::detail::max(10, -10) == 10.0); + xlnt_assert(xlnt::detail::max(-10, 10) == 10.0); + xlnt_assert(xlnt::detail::max(10.0, -10) == 10.0); + xlnt_assert(xlnt::detail::max(-10, 10.0) == 10.0); + + static_assert(xlnt::detail::max(-10, 10.0) == 10.0, "constexpr"); + } + + void test_abs() + { + xlnt_assert(xlnt::detail::abs(0) == 0); + xlnt_assert(xlnt::detail::abs(1) == 1); + xlnt_assert(xlnt::detail::abs(-1) == 1); + + xlnt_assert(xlnt::detail::abs(0.0) == 0.0); + xlnt_assert(xlnt::detail::abs(1.5) == 1.5); + xlnt_assert(xlnt::detail::abs(-1.5) == 1.5); + + static_assert(xlnt::detail::abs(-1.23) == 1.23, "constexpr"); + } }; static numeric_test_suite x; \ No newline at end of file