Macros | Functions
NumericUtils_test.cc File Reference

Unit test for NumericUtils functions. More...

#include <boost/test/unit_test.hpp>
#include "larcorealg/CoreUtils/MetaUtils.h"
#include "larcorealg/CoreUtils/NumericUtils.h"
#include <limits>
#include <type_traits>

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( NumericUtils_test )
 

Functions

template<typename A , typename B , typename D = A>
void test_absDiff ()
 
template<typename A , typename B , typename D = std::add_const_t<A>>
void test_constexpr_absDiff ()
 
 BOOST_AUTO_TEST_CASE (absDiffTestCase)
 

Detailed Description

Unit test for NumericUtils functions.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
March 30, 2018
See also
NumericUtils.h

Definition in file NumericUtils_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( NumericUtils_test )

Definition at line 10 of file NumericUtils_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( absDiffTestCase  )

Definition at line 88 of file NumericUtils_test.cc.

88  {
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)
template<typename A , typename B , typename D = A>
void test_absDiff ( )

Definition at line 24 of file NumericUtils_test.cc.

24  {
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()
#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
static bool * b
Definition: config.cpp:1043
template<typename A , typename B , typename D = std::add_const_t<A>>
void test_constexpr_absDiff ( )

Definition at line 61 of file NumericUtils_test.cc.

61  {
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()
const double a
constexpr auto absDiff(A const &a, B const &b)
Returns the absolute value of the difference between two values.
Definition: NumericUtils.h:43
static bool * b
Definition: config.cpp:1043