10 #define BOOST_TEST_MODULE ( NumericUtils_test ) 11 #include <boost/test/unit_test.hpp> 19 #include <type_traits> 23 template <
typename A,
typename B,
typename D = A>
26 static_assert(std::is_same<
27 decltype(std::declval<A>() - std::declval<B>()),
28 decltype(std::declval<B>() - std::declval<A>())
29 >(),
"Difference between types is asymmetric." 38 static_assert(std::is_same<decltype(absDeltaAB), decltype(absDeltaBA)>(),
39 "Results of |a-b| and |b-a| have different type!");
41 static_assert(std::is_same<decltype(absDeltaAB),
D>(),
42 "Results of |a-b| has unexpected type!");
44 BOOST_TEST(absDeltaAB ==
D(1));
45 BOOST_TEST(absDeltaBA ==
D(1));
54 BOOST_TEST(absDeltaAM >
D(2*(a + b)));
55 BOOST_TEST(absDeltaMA >
D(2*(a + b)));
60 template <
typename A,
typename B,
typename D = std::add_const_t<A>>
63 static_assert(std::is_same<
64 decltype(std::declval<A>() - std::declval<B>()),
65 decltype(std::declval<B>() - std::declval<A>())
66 >(),
"Difference between types is asymmetric." 75 static_assert(std::is_same<decltype(absDeltaAB), decltype(absDeltaBA)>(),
76 "Results of |a-b| and |b-a| have different type!");
78 static_assert(std::is_same<decltype(absDeltaAB),
D>(),
79 "Results of |a-b| has unexpected type!");
81 static_assert(absDeltaAB == 1,
"|5-6| != 1?!?");
82 static_assert(absDeltaBA == 1,
"|6-5| != 1?!?");
90 BOOST_TEST_INFO(
"Testing <int,int>");
91 test_absDiff<int, int>();
92 test_constexpr_absDiff<int, int>();
94 BOOST_TEST_INFO(
"Testing <unsigned int,unsigned int>");
95 test_absDiff<unsigned int, unsigned int>();
96 test_constexpr_absDiff<unsigned int, unsigned int>();
103 static_assert(5U - 6U >= 0,
"ERROR 1");
104 static_assert(5U - 6 >= 0,
"ERROR 2");
105 static_assert(5U < 6U,
"ERROR 3");
106 static_assert(5U < 6,
"ERROR 4");
108 static_assert(std::is_unsigned<decltype(5U - 6)>(),
"ERROR 6");
Functions to help with numbers.
static int max(int a, int b)
constexpr auto absDiff(A const &a, B const &b)
Returns the absolute value of the difference between two values.
BOOST_AUTO_TEST_CASE(absDiffTestCase)
void test_constexpr_absDiff()