Changes in src/unittests/ObserverTest.cpp [033a05:7a176b]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/unittests/ObserverTest.cpp
r033a05 r7a176b 11 11 #include <cppunit/extensions/TestFactoryRegistry.h> 12 12 #include <cppunit/ui/text/TestRunner.h> 13 #include <set> 13 14 14 15 #include "Patterns/Observer.hpp" 16 #include "Patterns/ObservedIterator.hpp" 15 17 #include "Helpers/Assert.hpp" 16 18 … … 31 33 public: 32 34 UpdateCountObserver() : 35 Observer("UpdateCountObserver"), 33 36 updates(0) 34 37 {}; … … 43 46 class SimpleObservable : public Observable { 44 47 public: 48 SimpleObservable() : 49 Observable("SimpleObservable") 50 {} 51 45 52 void changeMethod() { 46 53 OBSERVE; … … 52 59 class CallObservable : public Observable { 53 60 public: 61 CallObservable() : 62 Observable("CallObservable") 63 {} 64 54 65 void changeMethod1() { 55 66 OBSERVE; … … 68 79 class BlockObservable : public Observable { 69 80 public: 81 BlockObservable() : 82 Observable("BlockObservable") 83 {} 84 70 85 void changeMethod1(){ 71 86 OBSERVE; … … 102 117 class SuperObservable : public Observable { 103 118 public: 104 SuperObservable(){ 119 SuperObservable(): 120 Observable("SuperObservable") 121 { 105 122 subObservable = new SimpleObservable(); 106 123 subObservable->signOn(this); … … 121 138 public: 122 139 NotificationObservable() : 123 notification1(new Notification(this)), 124 notification2(new Notification(this)) 140 Observable("NotificationObservable"), 141 notification1(new Notification(this)), 142 notification2(new Notification(this)) 125 143 {} 126 144 … … 147 165 public: 148 166 NotificationObserver(Notification_ptr notification) : 167 Observer("NotificationObserver"), 149 168 requestedNotification(notification), 150 169 wasNotified(false) … … 162 181 bool wasNotified; 163 182 }; 183 184 class ObservableCollection : public Observable { 185 public: 186 typedef std::set<SimpleObservable*> set; 187 typedef ObservedIterator<set> iterator; 188 typedef set::const_iterator const_iterator; 189 190 ObservableCollection(int _num) : 191 Observable("ObservableCollection"), 192 num(_num) 193 { 194 for(int i=0; i<num; ++i){ 195 SimpleObservable *content = new SimpleObservable(); 196 content->signOn(this); 197 theSet.insert(content); 198 } 199 } 200 201 ~ObservableCollection(){ 202 set::iterator iter; 203 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){ 204 delete (*iter); 205 } 206 } 207 208 iterator begin(){ 209 return iterator(theSet.begin(),this); 210 } 211 212 iterator end(){ 213 return iterator(theSet.end(),this); 214 } 215 216 const int num; 217 218 private: 219 set theSet; 220 }; 221 164 222 165 223 /******************* actuall tests ***************/ … … 173 231 blockObservable = new BlockObservable(); 174 232 notificationObservable = new NotificationObservable(); 233 collection = new ObservableCollection(5); 175 234 176 235 observer1 = new UpdateCountObserver(); … … 181 240 notificationObserver1 = new NotificationObserver(notificationObservable->notification1); 182 241 notificationObserver2 = new NotificationObserver(notificationObservable->notification2); 183 184 242 } 185 243 … … 191 249 delete blockObservable; 192 250 delete notificationObservable; 251 delete collection; 193 252 194 253 delete observer1; … … 279 338 } 280 339 340 void ObserverTest::iteratorTest(){ 341 int i = 0; 342 // test the general iterator methods 343 for(ObservableCollection::iterator iter=collection->begin(); iter!=collection->end();++iter){ 344 CPPUNIT_ASSERT(i< collection->num); 345 i++; 346 } 347 348 i=0; 349 for(ObservableCollection::const_iterator iter=collection->begin(); iter!=collection->end();++iter){ 350 CPPUNIT_ASSERT(i<collection->num); 351 i++; 352 } 353 354 collection->signOn(observer1); 355 { 356 // we construct this out of the loop, so the iterator dies at the end of 357 // the scope and not the end of the loop (allows more testing) 358 ObservableCollection::iterator iter; 359 for(iter=collection->begin(); iter!=collection->end(); ++iter){ 360 (*iter)->changeMethod(); 361 } 362 // At this point no change should have been propagated 363 CPPUNIT_ASSERT_EQUAL( 0, observer1->updates); 364 } 365 // After the Iterator has died the propagation should take place 366 CPPUNIT_ASSERT_EQUAL( 1, observer1->updates); 367 368 // when using a const_iterator no changes should be propagated 369 for(ObservableCollection::const_iterator iter = collection->begin(); iter!=collection->end();++iter); 370 CPPUNIT_ASSERT_EQUAL( 1, observer1->updates); 371 collection->signOff(observer1); 372 } 373 281 374 void ObserverTest::CircleDetectionTest() { 282 375 cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl; … … 289 382 // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE 290 383 simpleObservable1->signOn(simpleObservable1); 384 #ifndef NDEBUG 291 385 CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure); 386 #else 387 simpleObservable1->changeMethod(); 388 #endif 292 389 293 390 // more complex test … … 295 392 simpleObservable1->signOn(simpleObservable2); 296 393 simpleObservable2->signOn(simpleObservable1); 394 #ifndef NDEBUG 297 395 CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure); 396 #else 397 simpleObservable1->changeMethod(); 398 #endif 399 400 298 401 simpleObservable1->signOff(simpleObservable2); 299 402 simpleObservable2->signOff(simpleObservable1);
Note:
See TracChangeset
for help on using the changeset viewer.