Add tests for the other small utilities

This commit is contained in:
Crzyrndm 2018-08-19 15:11:57 +12:00
parent 9565377963
commit 4188d35caf
2 changed files with 71 additions and 4 deletions

View File

@ -58,8 +58,8 @@ constexpr Number abs(Number val)
/// <summary>
/// constexpr max
/// </summary>
template <typename Number>
constexpr Number max(Number lval, Number rval)
template <typename NumberL, typename NumberR>
constexpr typename std::common_type<NumberL, NumberR>::type max(NumberL lval, NumberR rval)
{
return (lval < rval) ? rval : lval;
}
@ -67,8 +67,8 @@ constexpr Number max(Number lval, Number rval)
/// <summary>
/// constexpr min
/// </summary>
template <typename Number>
constexpr Number min(Number lval, Number rval)
template <typename NumberL, typename NumberR>
constexpr typename std::common_type<NumberL, NumberR>::type min(NumberL lval, NumberR rval)
{
return (lval < rval) ? lval : rval;
}

View File

@ -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;