NumericUtils_test.cc
Go to the documentation of this file.
1 /**
2  * @file NumericUtils_test.cc
3  * @brief Unit test for NumericUtils functions.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date March 30, 2018
6  * @see NumericUtils.h
7  */
8 
9 // Boost libraries
10 #define BOOST_TEST_MODULE ( NumericUtils_test )
11 #include <boost/test/unit_test.hpp>
12 
13 // LArSoft libraries
16 
17 // C/C++ standard libraries
18 #include <limits> // std::numeric_limits<>
19 #include <type_traits> // std::declval(), std::is_same<>, ...
20 
21 
22 //------------------------------------------------------------------------------
23 template <typename A, typename B, typename D = A>
24 void test_absDiff() {
25 
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."
30  );
31 
32  A const a = 5;
33  B const b = 6;
34 
35  auto absDeltaAB = util::absDiff(a, b);
36  auto absDeltaBA = util::absDiff(b, a);
37 
38  static_assert(std::is_same<decltype(absDeltaAB), decltype(absDeltaBA)>(),
39  "Results of |a-b| and |b-a| have different type!");
40 
41  static_assert(std::is_same<decltype(absDeltaAB), D>(),
42  "Results of |a-b| has unexpected type!");
43 
44  BOOST_TEST(absDeltaAB == D(1));
45  BOOST_TEST(absDeltaBA == D(1));
46 
47  // test that a very large B compares kind-of-correctly with a small A
48  // (the difference needs to be large)
49  B const m = std::numeric_limits<B>::max() - b;
50 
51  auto absDeltaAM = util::absDiff(a, m);
52  auto absDeltaMA = util::absDiff(m, a);
53 
54  BOOST_TEST(absDeltaAM > D(2*(a + b)));
55  BOOST_TEST(absDeltaMA > D(2*(a + b)));
56 
57 } // test_absDiff()
58 
59 
60 template <typename A, typename B, typename D = std::add_const_t<A>>
62 
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."
67  );
68 
69  constexpr A a = 5;
70  constexpr B b = 6;
71 
72  constexpr auto absDeltaAB = util::absDiff(a, b);
73  constexpr auto absDeltaBA = util::absDiff(b, a);
74 
75  static_assert(std::is_same<decltype(absDeltaAB), decltype(absDeltaBA)>(),
76  "Results of |a-b| and |b-a| have different type!");
77 
78  static_assert(std::is_same<decltype(absDeltaAB), D>(),
79  "Results of |a-b| has unexpected type!");
80 
81  static_assert(absDeltaAB == 1, "|5-6| != 1?!?");
82  static_assert(absDeltaBA == 1, "|6-5| != 1?!?");
83 
84 } // test_constexpr_absDiff()
85 
86 
87 //------------------------------------------------------------------------------
88 BOOST_AUTO_TEST_CASE(absDiffTestCase) {
89 
90  BOOST_TEST_INFO("Testing <int,int>");
91  test_absDiff<int, int>();
92  test_constexpr_absDiff<int, int>();
93 
94  BOOST_TEST_INFO("Testing <unsigned int,unsigned int>");
95  test_absDiff<unsigned int, unsigned int>();
96  test_constexpr_absDiff<unsigned int, unsigned int>();
97 
98 } // BOOST_AUTO_TEST_CASE(absDiffTestCase)
99 
100 //------------------------------------------------------------------------------
101 
102 // fun with C!!
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");
107 // static_assert(5U > -6, "ERROR 5"); // !!!
108 static_assert(std::is_unsigned<decltype(5U - 6)>(), "ERROR 6");
109 
110 //------------------------------------------------------------------------------
Functions to help with numbers.
void test_absDiff()
Basic C++ metaprogramming utilities.
#define D
Debug message.
Definition: tclscanner.cpp:775
const double a
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.
Definition: NumericUtils.h:43
BOOST_AUTO_TEST_CASE(absDiffTestCase)
void test_constexpr_absDiff()
static bool * b
Definition: config.cpp:1043