RealComparisons_test.cc
Go to the documentation of this file.
1 /**
2  * @file RealComparisons_test.cc
3  * @brief Unit test for `RealComparisons` class
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date November 4th, 2016
6  * @see larcorealg/CoreUtils/RealComparisons.h
7  */
8 
9 // Boost libraries
10 #define BOOST_TEST_MODULE ( RealComparisons_test )
11 #include <boost/test/unit_test.hpp>
12 
13 // LArSoft libraries
15 
16 // C/C++ standard libraries
17 #include <ostream>
18 #include <cmath>
19 
20 
21 BOOST_AUTO_TEST_CASE(test_RealComparisons) {
22 
23  // instantiate a RealComparisons with two classes
25 
26  double const sqrt2 = std::sqrt(2);
27 
28  // check zero()
29  double const epsilon = 2.0 - (sqrt2 * sqrt2);
30  BOOST_TEST( check.zero(epsilon));
31  BOOST_TEST( check.zero(0.0));
32  BOOST_TEST( check.zero(1e-5));
33  BOOST_TEST( check.zero(-1e-5));
34  BOOST_TEST(!check.zero(1.01e-5));
35  BOOST_TEST(!check.zero(-1.01e-5 * 1.01));
36 
37  // check nonZero()
38  BOOST_TEST(!check.nonZero(epsilon));
39  BOOST_TEST(!check.nonZero(0.0));
40  BOOST_TEST(!check.nonZero(1e-5));
41  BOOST_TEST(!check.nonZero(-1e-5));
42  BOOST_TEST( check.nonZero(1.01e-5));
43  BOOST_TEST( check.nonZero(-1.01e-5 * 1.01));
44 
45  // check equal()
46  BOOST_TEST(!check.equal(sqrt2, 1.4142));
47  BOOST_TEST( check.nonEqual(sqrt2, 1.4142));
48  BOOST_TEST( check.equal(sqrt2, 1.414213));
49  BOOST_TEST(!check.nonEqual(sqrt2, 1.414213));
50 
51  // check strictlyNegative()
52  BOOST_TEST(!check.strictlyNegative(+1e-5 + 1e-7)); // outside tolerance
53  BOOST_TEST(!check.strictlyNegative(+1e-5 - 1e-7)); // within tolerance
54  BOOST_TEST(!check.strictlyNegative(0.0)); // zero
55  BOOST_TEST(!check.strictlyNegative(-1e-5 + 1e-7)); // within tolerance
56  BOOST_TEST( check.strictlyNegative(-1e-5 - 1e-7)); // outside tolerance
57 
58  // check strictlyPositive()
59  BOOST_TEST( check.strictlyPositive(+1e-5 + 1e-7)); // outside tolerance
60  BOOST_TEST(!check.strictlyPositive(+1e-5 - 1e-7)); // within tolerance
61  BOOST_TEST(!check.strictlyPositive(0.0)); // zero
62  BOOST_TEST(!check.strictlyPositive(-1e-5 + 1e-7)); // within tolerance
63  BOOST_TEST(!check.strictlyPositive(-1e-5 - 1e-7)); // outside tolerance
64 
65  // check nonNegative()
66  BOOST_TEST( check.nonNegative(+1e-5 + 1e-7)); // outside tolerance
67  BOOST_TEST( check.nonNegative(+1e-5 - 1e-7)); // within tolerance
68  BOOST_TEST( check.nonNegative(0.0)); // zero
69  BOOST_TEST( check.nonNegative(-1e-5 + 1e-7)); // within tolerance
70  BOOST_TEST(!check.nonNegative(-1e-5 - 1e-7)); // outside tolerance
71 
72  // check nonPositive()
73  BOOST_TEST(!check.nonPositive(+1e-5 + 1e-7)); // outside tolerance
74  BOOST_TEST( check.nonPositive(+1e-5 - 1e-7)); // within tolerance
75  BOOST_TEST( check.nonPositive(0.0)); // zero
76  BOOST_TEST( check.nonPositive(-1e-5 + 1e-7)); // within tolerance
77  BOOST_TEST( check.nonPositive(-1e-5 - 1e-7)); // outside tolerance
78 
79  // check strictlySmaller()
80  BOOST_TEST(!check.strictlySmaller(1.0, 1.0 - 1e-4)); // outside tolerance
81  BOOST_TEST(!check.strictlySmaller(1.0, 1.0 - 1e-7)); // within tolerance
82  BOOST_TEST(!check.strictlySmaller(1.0, 1.0)); // equal
83  BOOST_TEST(!check.strictlySmaller(1.0, 1.0 + 1e-7)); // within tolerance
84  BOOST_TEST( check.strictlySmaller(1.0, 1.0 + 1e-4)); // outside tolerance
85 
86  // check nonSmaller()
87  BOOST_TEST( check.nonSmaller(1.0, 1.0 - 1e-4)); // outside tolerance
88  BOOST_TEST( check.nonSmaller(1.0, 1.0 - 1e-7)); // within tolerance
89  BOOST_TEST( check.nonSmaller(1.0, 1.0)); // equal
90  BOOST_TEST( check.nonSmaller(1.0, 1.0 + 1e-7)); // within tolerance
91  BOOST_TEST(!check.nonSmaller(1.0, 1.0 + 1e-4)); // outside tolerance
92 
93  // check strictlyGreater()
94  BOOST_TEST( check.strictlyGreater(1.0, 1.0 - 1e-4)); // outside tolerance
95  BOOST_TEST(!check.strictlyGreater(1.0, 1.0 - 1e-7)); // within tolerance
96  BOOST_TEST(!check.strictlyGreater(1.0, 1.0)); // equal
97  BOOST_TEST(!check.strictlyGreater(1.0, 1.0 + 1e-7)); // within tolerance
98  BOOST_TEST(!check.strictlyGreater(1.0, 1.0 + 1e-4)); // outside tolerance
99 
100  // check nonGreater()
101  BOOST_TEST(!check.nonGreater(1.0, 1.0 - 1e-4)); // outside tolerance
102  BOOST_TEST( check.nonGreater(1.0, 1.0 - 1e-7)); // within tolerance
103  BOOST_TEST( check.nonGreater(1.0, 1.0)); // equal
104  BOOST_TEST( check.nonGreater(1.0, 1.0 + 1e-7)); // within tolerance
105  BOOST_TEST( check.nonGreater(1.0, 1.0 + 1e-4)); // outside tolerance
106 
107  // check within()
108  BOOST_TEST(!check.within(sqrt2, 0., 1.41420));
109  BOOST_TEST( check.within(sqrt2, 0., 1.41421));
110  BOOST_TEST( check.within(sqrt2, 1.41422, 2.));
111  BOOST_TEST(!check.within(sqrt2, 1.41423, 2.));
112 
113  // check inverted limits
114  BOOST_TEST(!check.within(sqrt2, 1.41421, 0.));
115  BOOST_TEST(check.withinSorted(sqrt2, 1.41421, 0.));
116 
117 } // BOOST_AUTO_TEST_CASE(test_RealComparisons)
constexpr bool nonEqual(Value_t a, Value_t b) const
Returns whether a and b are farther than the threshold.
Provides simple real number checks.
constexpr bool zero(Value_t value) const
Returns whether the value is no farther from 0 than the threshold.
Class for approximate comparisons.
bool check(const std::vector< std::vector< float > > &outputs)
BOOST_AUTO_TEST_CASE(test_RealComparisons)
constexpr bool withinSorted(Value_t value, Value_t lower, Value_t upper) const
Returns whether value is between bounds (included); bounds are sorted.
constexpr bool strictlySmaller(Value_t a, Value_t b) const
Returns whether a is strictly smaller than b.
constexpr bool strictlyGreater(Value_t a, Value_t b) const
Returns whether a is strictly greater than b.
const double e
constexpr bool strictlyNegative(Value_t value) const
Returns whether value is larger than zero beyond tolerance.
constexpr bool nonGreater(Value_t a, Value_t b) const
Returns whether a is smaller than (or equal to) b.
constexpr bool strictlyPositive(Value_t value) const
Returns whether value is smaller than zero beyond tolerance.
constexpr bool nonNegative(Value_t value) const
Returns whether value is larger than or equal() to zero.
constexpr bool nonZero(Value_t value) const
Returns whether the value is farther from 0 than the threshold.
constexpr bool nonSmaller(Value_t a, Value_t b) const
Returns whether a is greater than (or equal to) b.
constexpr bool equal(Value_t a, Value_t b) const
Returns whether a and b are no farther than the threshold.
constexpr bool nonPositive(Value_t value) const
Returns whether value is smaller than or equal() to zero.
constexpr bool within(Value_t value, Value_t lower, Value_t upper) const
Returns whether value is between the bounds (included)