All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Macros | Functions
ChangeMonitor_test.cc File Reference

Unit test for utilities from ChangeMonitor.h. More...

#include <boost/test/unit_test.hpp>
#include "icarusalg/Utilities/ChangeMonitor.h"

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ChangeMonitorTest
 

Functions

void documentationTest ()
 
void ThreadSafeChangeMonitor_documentationTest ()
 
 BOOST_AUTO_TEST_CASE (ChangeMonitorTestCase)
 
 BOOST_AUTO_TEST_CASE (ThreadSafeChangeMonitorTestCase)
 

Detailed Description

Unit test for utilities from ChangeMonitor.h.

Date
September 16, 2020
Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.slac..nosp@m.stan.nosp@m.ford..nosp@m.edu)
See Also
icarusalg/Utilities/ChangeMonitor.h

Definition in file ChangeMonitor_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ChangeMonitorTest

Definition at line 10 of file ChangeMonitor_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( ChangeMonitorTestCase  )

Definition at line 129 of file ChangeMonitor_test.cc.

129  {
130 
132 
133 } // BOOST_AUTO_TEST_CASE( ChangeMonitorTestCase )
void documentationTest()
BOOST_AUTO_TEST_CASE ( ThreadSafeChangeMonitorTestCase  )

Definition at line 136 of file ChangeMonitor_test.cc.

136  {
137 
139 
140 } // BOOST_AUTO_TEST_CASE( ThreadSafeChangeMonitorTestCase )
void ThreadSafeChangeMonitor_documentationTest()
void documentationTest ( )

Definition at line 18 of file ChangeMonitor_test.cc.

18  {
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()
bool hasReference() const
Returns whether a reference value is present.
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.
void ThreadSafeChangeMonitor_documentationTest ( )

Definition at line 85 of file ChangeMonitor_test.cc.

85  {
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()
bool hasReference() const
Returns whether a reference value is present.
Helper to check if an object has changed. Thread-safe.