All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rounding_test.cc
Go to the documentation of this file.
1 /**
2  * @file rounding_test.cc
3  * @brief Unit test for utilities from `rounding.h`.
4  * @date April 17, 2020
5  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
6  * @see `icarusalg/Utilities/rounding.h`
7  */
8 
9 // Boost libraries
10 #define BOOST_TEST_MODULE RoundingTest
11 #include <boost/test/unit_test.hpp>
12 
13 // ICARUS libraries
15 
16 
17 //------------------------------------------------------------------------------
18 void roundupTest() {
19 
20  BOOST_CHECK_EQUAL(icarus::ns::util::roundup(23.0, 2.5), 25.0);
21  BOOST_CHECK_EQUAL(icarus::ns::util::roundup(22.0, 2.5, 1.0), 23.5); // 21.0 + 1.0 => 22.5 + 1.0
22  BOOST_CHECK_EQUAL(icarus::ns::util::roundup(23.0, 2.5, 1.0), 23.5); // 22.0 + 1.0 => 22.5 + 1.0
23  BOOST_CHECK_EQUAL(icarus::ns::util::roundup(24.0, 2.5, 1.0), 26.0); // 23.0 + 1.0 => 25.0 + 1.0
24 
25  BOOST_CHECK_EQUAL(icarus::ns::util::roundup(23, 5), 25);
26  BOOST_CHECK_EQUAL(icarus::ns::util::roundup(22, 5, 3), 23); // 19 + 3 => 20 + 3
27  BOOST_CHECK_EQUAL(icarus::ns::util::roundup(23, 5, 3), 23); // 20 + 3 => 20 + 3
28  BOOST_CHECK_EQUAL(icarus::ns::util::roundup(24, 5, 3), 28); // 21 + 3 => 25 + 3
29 
30 } // roundupTest()
31 
32 
33 //------------------------------------------------------------------------------
34 void rounddownTest() {
35 
36  BOOST_CHECK_EQUAL(icarus::ns::util::rounddown(23.0, 2.5), 22.5);
37  BOOST_CHECK_EQUAL(icarus::ns::util::rounddown(23.0, 2.5, 1.0), 21.0); // 21.0 + 1.0 => 20.0 + 1.0
38  BOOST_CHECK_EQUAL(icarus::ns::util::rounddown(23.0, 2.5, 1.0), 21.0); // 22.0 + 1.0 => 20.0 + 1.0
39  BOOST_CHECK_EQUAL(icarus::ns::util::rounddown(24.0, 2.5, 1.0), 23.5); // 23.0 + 1.0 => 22.5 + 1.0
40 
41  BOOST_CHECK_EQUAL(icarus::ns::util::rounddown(23, 5), 20);
42  BOOST_CHECK_EQUAL(icarus::ns::util::rounddown(22, 5, 3), 18); // 19 + 3 => 15 + 3
43  BOOST_CHECK_EQUAL(icarus::ns::util::rounddown(23, 5, 3), 23); // 20 + 3 => 20 + 3
44  BOOST_CHECK_EQUAL(icarus::ns::util::rounddown(24, 5, 3), 23); // 21 + 3 => 20 + 3
45 
46 } // rounddownTest()
47 
48 
49 //------------------------------------------------------------------------------
50 //--- The tests
51 //---
52 BOOST_AUTO_TEST_CASE( RoundUpTestCase ) {
53 
54  roundupTest();
55 
56 } // BOOST_AUTO_TEST_CASE( RoundUpTestCase )
57 
58 
59 BOOST_AUTO_TEST_CASE( RoundDownTestCase ) {
60 
61  rounddownTest();
62 
63 } // BOOST_AUTO_TEST_CASE( RoundDownTestCase )
64 
BOOST_AUTO_TEST_CASE(AllTests)
constexpr T roundup(T const value, U const quantum, T const offset=T{})
Returns the value, rounded up.
Definition: rounding.h:112
Utilities for numerical rounding.
void roundupTest()
constexpr T rounddown(T const value, U const quantum, T const offset=T{})
Returns the value, rounded down.
Definition: rounding.h:68
void rounddownTest()