All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChangeMonitor_test.cc
Go to the documentation of this file.
1 /**
2  * @file ChangeMonitor_test.cc
3  * @brief Unit test for utilities from `ChangeMonitor.h`.
4  * @date September 16, 2020
5  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
6  * @see `icarusalg/Utilities/ChangeMonitor.h`
7  */
8 
9 // Boost libraries
10 #define BOOST_TEST_MODULE ChangeMonitorTest
11 #include <boost/test/unit_test.hpp>
12 
13 // ICARUS libraries
15 
16 
17 //------------------------------------------------------------------------------
19 
20  /*
21  * // starts with no reference by default
22  * icarus::ns::util::ChangeMonitor<int> monitor;
23  *
24  * // first check just establishes the reference
25  * int var = 0;
26  * monitor(var); // this is also a `update()`, which returns no value
27  *
28  * // reference is 0, new value is 1: a change is detected
29  * if (monitor(1)) {
30  * std::cout << "Value has changed!" << std::endl;
31  * }
32  *
33  * var = 5; // this does not change the monitoring
34  * // reference is now 1, new value is 1: no change is detected
35  * if (monitor(1)) {
36  * std::cout << "Value has changed again!" << std::endl;
37  * }
38  *
39  * // more complex syntax (C++17) allows accessing the old reference value;
40  * // reference is now 1, new value is 2: change is detected
41  * if (auto newVal = monitor(2); newVal) {
42  * std::cout << "Value has changed from " << *newVal << " to 2!"
43  * << std::endl;
44  * }
45  */
46 
48  BOOST_CHECK((!monitor.hasReference()));
49 
50  // first check just establishes the reference
51  int var = 0;
52  auto&& res1 = monitor(var); // this is also a `update()`, which returns no value
53  BOOST_CHECK((!res1));
54  BOOST_CHECK((monitor.hasReference()));
55  BOOST_TEST((monitor.reference() == var));
56 
57  // reference is 0, new value is 1: a change is detected
58  auto&& res2 = monitor(1);
59  BOOST_CHECK((!!res2));
60  BOOST_TEST((res2.value() == 0));
61  BOOST_CHECK((monitor.hasReference()));
62  BOOST_TEST((monitor.reference() == 1));
63 
64  var = 5; // this does not change the monitoring
65  // reference is now 1, new value is 1: no change is detected
66  auto&& res3 = monitor(1);
67  BOOST_CHECK((!res3));
68  BOOST_CHECK((monitor.hasReference()));
69  BOOST_TEST((monitor.reference() == 1));
70 
71  bool detected = false;
72  if (auto prevVal = monitor(2); prevVal) {
73  detected = true;
74  BOOST_CHECK((!!prevVal));
75  BOOST_TEST((prevVal.value() == 1));
76  BOOST_CHECK((monitor.hasReference()));
77  BOOST_TEST((monitor.reference() == 2));
78  }
79  BOOST_CHECK((detected));
80 
81 } // documentationTest()
82 
83 
84 //------------------------------------------------------------------------------
86 
87  // same test as ChangeMonitor_documentationTest()
88 
90  BOOST_CHECK((!monitor.hasReference()));
91 
92  // first check just establishes the reference
93  int var = 0;
94  auto&& res1 = monitor(var); // this is also a `update()`, which returns no value
95  BOOST_CHECK((!res1));
96  BOOST_CHECK((monitor.hasReference()));
97  BOOST_TEST((monitor.reference() == var));
98 
99  // reference is 0, new value is 1: a change is detected
100  auto&& res2 = monitor(1);
101  BOOST_CHECK((!!res2));
102  BOOST_TEST((res2.value() == 0));
103  BOOST_CHECK((monitor.hasReference()));
104  BOOST_TEST((monitor.reference() == 1));
105 
106  var = 5; // this does not change the monitoring
107  // reference is now 1, new value is 1: no change is detected
108  auto&& res3 = monitor(1);
109  BOOST_CHECK((!res3));
110  BOOST_CHECK((monitor.hasReference()));
111  BOOST_TEST((monitor.reference() == 1));
112 
113  bool detected = false;
114  if (auto prevVal = monitor(2); prevVal) {
115  detected = true;
116  BOOST_CHECK((!!prevVal));
117  BOOST_TEST((prevVal.value() == 1));
118  BOOST_CHECK((monitor.hasReference()));
119  BOOST_TEST((monitor.reference() == 2));
120  }
121  BOOST_CHECK((detected));
122 
123 } // ThreadSafeChangeMonitor_documentationTest()
124 
125 
126 //------------------------------------------------------------------------------
127 //--- The tests
128 //---
129 BOOST_AUTO_TEST_CASE( ChangeMonitorTestCase ) {
130 
132 
133 } // BOOST_AUTO_TEST_CASE( ChangeMonitorTestCase )
134 
135 
136 BOOST_AUTO_TEST_CASE( ThreadSafeChangeMonitorTestCase ) {
137 
139 
140 } // BOOST_AUTO_TEST_CASE( ThreadSafeChangeMonitorTestCase )
141 
142 
BOOST_AUTO_TEST_CASE(AllTests)
void documentationTest()
bool hasReference() const
Returns whether a reference value is present.
Helper to check if an object has changed. Thread-safe.
bool hasReference() const
Returns whether a reference value is present.
void ThreadSafeChangeMonitor_documentationTest()
Classes to detect the change of object values.
Helper to check if an object has changed.
Definition: ChangeMonitor.h:85
Data_t const & reference() const
Returns the reference value; undefined if hasReference() is false.