ProteoWizard
Classes | Functions | Variables
IterationListenerTest.cpp File Reference
#include "Std.hpp"
#include "IterationListener.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include <cstring>
#include <ctime>

Go to the source code of this file.

Classes

class  TestListener
 
class  CancelListener
 
class  BadListener
 

Functions

void nullDeallocate (IterationListener *s)
 
void test ()
 
void testCancel ()
 
void testRemove ()
 
void testTime ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 

Function Documentation

◆ nullDeallocate()

void nullDeallocate ( IterationListener s)

Definition at line 83 of file IterationListenerTest.cpp.

84{
85 // do nothing
86}

Referenced by test(), testCancel(), testRemove(), and testTime().

◆ test()

void test ( )

Definition at line 88 of file IterationListenerTest.cpp.

89{
90 if (os_) *os_ << "test()\n";
91
93
94 TestListener test3("test3");
95 TestListener test4("test4");
96 TestListener test5("test5");
97 TestListener test6("test6");
98
101 registry.addListener(IterationListenerPtr(&test5, nullDeallocate), 5);
102 registry.addListener(IterationListenerPtr(&test6, nullDeallocate), 6);
103
104 size_t iterationCount = 24;
105 for (size_t i=0; i<iterationCount; i++)
106 registry.broadcastUpdateMessage(IterationListener::UpdateMessage(i, iterationCount));
107
108 // repeating last iteration should not create new messages
109 registry.broadcastUpdateMessage(IterationListener::UpdateMessage(iterationCount - 1, iterationCount));
110 registry.broadcastUpdateMessage(IterationListener::UpdateMessage(iterationCount - 1, iterationCount));
111 registry.broadcastUpdateMessage(IterationListener::UpdateMessage(iterationCount - 1, iterationCount));
112
113 // validate
114
115 unit_assert_operator_equal(9, test3.count()); // 0 2 5 8 11 14 17 20 23
117 unit_assert_operator_equal(6, test5.count());
118 unit_assert_operator_equal(5, test6.count());
119
120 if (os_) *os_ << endl;
121}
void test4()
void test3()
void nullDeallocate(IterationListener *s)
ostream * os_
handles registration of IterationListeners and broadcast of update messages
void addListener(const IterationListenerPtr &listener, size_t iterationPeriod)
IterationListener::Status broadcastUpdateMessage(const IterationListener::UpdateMessage &updateMessage) const
boost::shared_ptr< IterationListener > IterationListenerPtr
#define unit_assert_operator_equal(expected, actual)
Definition unit.hpp:92

References pwiz::util::IterationListenerRegistry::addListener(), pwiz::util::IterationListenerRegistry::broadcastUpdateMessage(), TestListener::count(), nullDeallocate(), os_, test3(), test4(), and unit_assert_operator_equal.

Referenced by main().

◆ testCancel()

void testCancel ( )

Definition at line 124 of file IterationListenerTest.cpp.

125{
126 if (os_) *os_ << "testCancel()\n";
127
129
130 CancelListener cancelListener(12);
131 TestListener test3("test3");
132 TestListener test4("test4");
133 TestListener test6("test6");
134
135 registry.addListener(IterationListenerPtr(&cancelListener, nullDeallocate), 1);
138 registry.addListener(IterationListenerPtr(&test6, nullDeallocate), 5);
139
140 // typical use of IterationListenerRegistry, with proper Status_Cancel handling
141
142 bool canceled = false;
143
144 size_t iterationCount = 24;
145 for (size_t i=0; i<iterationCount; i++)
146 {
148 registry.broadcastUpdateMessage(IterationListener::UpdateMessage(i, iterationCount));
149
150 // handle Status_Cancel
152 {
153 canceled = true;
154 break;
155 }
156 }
157
158 // implementations should send a final update on completion of the iteration
159
160 if (!canceled)
161 registry.broadcastUpdateMessage(IterationListener::UpdateMessage(iterationCount, iterationCount));
162
163 // validate
164
165 unit_assert(test3.count() == 5);
166 unit_assert(test4.count() == 4);
167 unit_assert(test6.count() == 3);
168
169 if (os_) *os_ << endl;
170}
#define unit_assert(x)
Definition unit.hpp:85

References pwiz::util::IterationListenerRegistry::addListener(), pwiz::util::IterationListenerRegistry::broadcastUpdateMessage(), TestListener::count(), nullDeallocate(), os_, pwiz::util::IterationListener::Status_Cancel, test3(), test4(), and unit_assert.

Referenced by main().

◆ testRemove()

void testRemove ( )

Definition at line 184 of file IterationListenerTest.cpp.

185{
186 if (os_) *os_ << "testRemove()\n";
187
189
190 BadListener bad;
191 TestListener test3("test3");
192 TestListener test4("test4");
193
195
197 registry.addListener(badPtr, 1);
199
200 // sanity check -- verify that broadcast throws if BadListener is in the registry
201
202 bool caught = false;
203
204 try
205 {
207 }
208 catch (exception& e)
209 {
210 if (e.what() == string("bad")) caught = true;
211 }
212
213 unit_assert(caught);
214
215 // remove BadListener -- broadcast will throw if not removed properly
216
217 registry.removeListener(badPtr);
219
220 if (os_) *os_ << endl;
221}
void removeListener(const IterationListenerPtr &listener)

References pwiz::util::IterationListenerRegistry::addListener(), pwiz::util::IterationListenerRegistry::broadcastUpdateMessage(), nullDeallocate(), os_, pwiz::util::IterationListenerRegistry::removeListener(), test3(), test4(), and unit_assert.

Referenced by main().

◆ testTime()

void testTime ( )

Definition at line 224 of file IterationListenerTest.cpp.

225{
226 if (os_) *os_ << "testTime()\n";
227
229
230 TestListener test_iteration("test_iteration");
231 TestListener test_time("test_time");
232
233 registry.addListener(IterationListenerPtr(&test_iteration, nullDeallocate), 1000000);
234 registry.addListenerWithTimer(IterationListenerPtr(&test_time, nullDeallocate), 1.0);
235
236 time_t start;
237 time(&start);
238
239 const double iterationDuration = 5.0;
240 for (int i=0; ; i++)
241 {
242 time_t now;
243 time(&now);
244 if (difftime(now, start) > iterationDuration) break;
245
247 }
248
249 if (os_) *os_ << endl;
250}
void addListenerWithTimer(const IterationListenerPtr &listener, double timePeriod)

References pwiz::util::IterationListenerRegistry::addListener(), pwiz::util::IterationListenerRegistry::addListenerWithTimer(), pwiz::util::IterationListenerRegistry::broadcastUpdateMessage(), nullDeallocate(), and os_.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 253 of file IterationListenerTest.cpp.

254{
255 TEST_PROLOG(argc, argv)
256
257 try
258 {
259 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
260 test();
261 testCancel();
262 testRemove();
263 testTime();
264 }
265 catch (exception& e)
266 {
267 TEST_FAILED(e.what())
268 }
269 catch (...)
270 {
271 TEST_FAILED("Caught unknown exception.")
272 }
273
275}
void testTime()
void testRemove()
void test()
void testCancel()
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175

References os_, test(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testCancel(), testRemove(), and testTime().

Variable Documentation

◆ os_

ostream* os_ = 0