| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [63c1f6] | 8 | /* | 
|---|
|  | 9 | * ObserverTest.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Jan 19, 2010 | 
|---|
|  | 12 | *      Author: crueger | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [63c1f6] | 20 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 21 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 22 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| [bd58fb] | 23 | #include <set> | 
|---|
| [63c1f6] | 24 |  | 
|---|
|  | 25 | #include "Patterns/Observer.hpp" | 
|---|
| [bd58fb] | 26 | #include "Patterns/ObservedIterator.hpp" | 
|---|
| [4fb5a3] | 27 | #include "Helpers/Assert.hpp" | 
|---|
| [63c1f6] | 28 |  | 
|---|
|  | 29 | #include <iostream> | 
|---|
|  | 30 |  | 
|---|
| [a2c4f3] | 31 | #include "ObserverUnitTest.hpp" | 
|---|
| [63c1f6] | 32 |  | 
|---|
| [9b6b2f] | 33 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 34 | #include "UnitTestMain.hpp" | 
|---|
|  | 35 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 36 |  | 
|---|
| [63c1f6] | 37 | // Registers the fixture into the 'registry' | 
|---|
|  | 38 | CPPUNIT_TEST_SUITE_REGISTRATION( ObserverTest ); | 
|---|
|  | 39 |  | 
|---|
|  | 40 | /******************* Test stubs ************************/ | 
|---|
|  | 41 |  | 
|---|
|  | 42 | class UpdateCountObserver : public Observer { | 
|---|
|  | 43 | public: | 
|---|
|  | 44 | UpdateCountObserver() : | 
|---|
| [cd5047] | 45 | Observer("UpdateCountObserver"), | 
|---|
| [63c1f6] | 46 | updates(0) | 
|---|
|  | 47 | {}; | 
|---|
|  | 48 | void update(Observable *publisher){ | 
|---|
|  | 49 | updates++; | 
|---|
|  | 50 | } | 
|---|
|  | 51 | void subjectKilled(Observable *publisher) { | 
|---|
|  | 52 | } | 
|---|
|  | 53 | int updates; | 
|---|
|  | 54 | }; | 
|---|
|  | 55 |  | 
|---|
|  | 56 | class SimpleObservable : public Observable { | 
|---|
|  | 57 | public: | 
|---|
| [cd5047] | 58 | SimpleObservable() : | 
|---|
|  | 59 | Observable("SimpleObservable") | 
|---|
|  | 60 | {} | 
|---|
|  | 61 |  | 
|---|
| [63c1f6] | 62 | void changeMethod() { | 
|---|
| [317df8] | 63 | OBSERVE; | 
|---|
| [5e5283] | 64 | int i = 0; | 
|---|
| [63c1f6] | 65 | i++; | 
|---|
|  | 66 | } | 
|---|
|  | 67 | }; | 
|---|
|  | 68 |  | 
|---|
|  | 69 | class CallObservable : public Observable { | 
|---|
|  | 70 | public: | 
|---|
| [cd5047] | 71 | CallObservable() : | 
|---|
|  | 72 | Observable("CallObservable") | 
|---|
|  | 73 | {} | 
|---|
|  | 74 |  | 
|---|
| [63c1f6] | 75 | void changeMethod1() { | 
|---|
| [317df8] | 76 | OBSERVE; | 
|---|
| [5e5283] | 77 | int i = 0; | 
|---|
| [63c1f6] | 78 | i++; | 
|---|
|  | 79 | } | 
|---|
|  | 80 |  | 
|---|
|  | 81 | void changeMethod2() { | 
|---|
| [317df8] | 82 | OBSERVE; | 
|---|
| [5e5283] | 83 | int i = 0; | 
|---|
| [63c1f6] | 84 | i++; | 
|---|
|  | 85 | changeMethod1(); | 
|---|
|  | 86 | } | 
|---|
|  | 87 | }; | 
|---|
|  | 88 |  | 
|---|
| [4fb5a3] | 89 | class BlockObservable : public Observable { | 
|---|
|  | 90 | public: | 
|---|
| [cd5047] | 91 | BlockObservable() : | 
|---|
|  | 92 | Observable("BlockObservable") | 
|---|
|  | 93 | {} | 
|---|
|  | 94 |  | 
|---|
| [4fb5a3] | 95 | void changeMethod1(){ | 
|---|
|  | 96 | OBSERVE; | 
|---|
|  | 97 | // test if we report correctly as blocked | 
|---|
|  | 98 | CPPUNIT_ASSERT(isBlocked()); | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
|  | 101 | void changeMethod2(){ | 
|---|
|  | 102 | OBSERVE; | 
|---|
|  | 103 | internalMethod1(); | 
|---|
|  | 104 | internalMethod2(); | 
|---|
|  | 105 | } | 
|---|
|  | 106 |  | 
|---|
|  | 107 | void internalMethod1(){ | 
|---|
|  | 108 | // we did not block, but our caller did... | 
|---|
|  | 109 | // see if this is found | 
|---|
|  | 110 | CPPUNIT_ASSERT(isBlocked()); | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 | void internalMethod2(){ | 
|---|
|  | 114 | OBSERVE; | 
|---|
|  | 115 | // Both this method and the caller do block | 
|---|
|  | 116 | // Does the reporting still work as expected? | 
|---|
|  | 117 | CPPUNIT_ASSERT(isBlocked()); | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | void noChangeMethod(){ | 
|---|
|  | 121 | // No Block introduced here | 
|---|
|  | 122 | // reported correctely? | 
|---|
|  | 123 | CPPUNIT_ASSERT(!isBlocked()); | 
|---|
|  | 124 | } | 
|---|
|  | 125 | }; | 
|---|
|  | 126 |  | 
|---|
| [63c1f6] | 127 | class SuperObservable : public Observable { | 
|---|
|  | 128 | public: | 
|---|
| [cd5047] | 129 | SuperObservable(): | 
|---|
|  | 130 | Observable("SuperObservable") | 
|---|
|  | 131 | { | 
|---|
| [63c1f6] | 132 | subObservable = new SimpleObservable(); | 
|---|
|  | 133 | subObservable->signOn(this); | 
|---|
|  | 134 | } | 
|---|
|  | 135 | ~SuperObservable(){ | 
|---|
|  | 136 | delete subObservable; | 
|---|
|  | 137 | } | 
|---|
|  | 138 | void changeMethod() { | 
|---|
| [317df8] | 139 | OBSERVE; | 
|---|
| [5e5283] | 140 | int i = 0; | 
|---|
| [63c1f6] | 141 | i++; | 
|---|
|  | 142 | subObservable->changeMethod(); | 
|---|
|  | 143 | } | 
|---|
|  | 144 | SimpleObservable *subObservable; | 
|---|
|  | 145 | }; | 
|---|
|  | 146 |  | 
|---|
| [ccacba] | 147 | class NotificationObservable : public Observable { | 
|---|
|  | 148 | public: | 
|---|
|  | 149 | NotificationObservable() : | 
|---|
| [cd5047] | 150 | Observable("NotificationObservable"), | 
|---|
|  | 151 | notification1(new Notification(this)), | 
|---|
|  | 152 | notification2(new Notification(this)) | 
|---|
| [ccacba] | 153 | {} | 
|---|
|  | 154 |  | 
|---|
|  | 155 | ~NotificationObservable(){ | 
|---|
|  | 156 | delete notification1; | 
|---|
|  | 157 | delete notification2; | 
|---|
|  | 158 | } | 
|---|
|  | 159 |  | 
|---|
|  | 160 | void operation1(){ | 
|---|
|  | 161 | OBSERVE; | 
|---|
|  | 162 | NOTIFY(notification1); | 
|---|
|  | 163 | } | 
|---|
|  | 164 |  | 
|---|
|  | 165 | void operation2(){ | 
|---|
|  | 166 | OBSERVE; | 
|---|
|  | 167 | NOTIFY(notification2); | 
|---|
|  | 168 | } | 
|---|
|  | 169 |  | 
|---|
|  | 170 | Notification_ptr notification1; | 
|---|
|  | 171 | Notification_ptr notification2; | 
|---|
|  | 172 | }; | 
|---|
|  | 173 |  | 
|---|
|  | 174 | class NotificationObserver : public Observer { | 
|---|
|  | 175 | public: | 
|---|
|  | 176 | NotificationObserver(Notification_ptr notification) : | 
|---|
| [cd5047] | 177 | Observer("NotificationObserver"), | 
|---|
| [ccacba] | 178 | requestedNotification(notification), | 
|---|
|  | 179 | wasNotified(false) | 
|---|
|  | 180 | {} | 
|---|
|  | 181 |  | 
|---|
|  | 182 | void update(Observable*){} | 
|---|
|  | 183 | void subjectKilled(Observable*){} | 
|---|
|  | 184 | void recieveNotification(Observable *publisher, Notification_ptr notification){ | 
|---|
| [033a05] | 185 | ASSERT(requestedNotification==notification,"Notification received that was not requested"); | 
|---|
| [ccacba] | 186 | wasNotified = true; | 
|---|
|  | 187 | } | 
|---|
|  | 188 |  | 
|---|
|  | 189 | Notification_ptr requestedNotification; | 
|---|
|  | 190 |  | 
|---|
|  | 191 | bool wasNotified; | 
|---|
|  | 192 | }; | 
|---|
|  | 193 |  | 
|---|
| [8774c5] | 194 | class ObservableSet : public Observable { | 
|---|
| [bd58fb] | 195 | public: | 
|---|
|  | 196 | typedef std::set<SimpleObservable*> set; | 
|---|
|  | 197 | typedef ObservedIterator<set> iterator; | 
|---|
| [a7b761b] | 198 | typedef set::const_iterator const_iterator; | 
|---|
| [bd58fb] | 199 |  | 
|---|
| [8774c5] | 200 | ObservableSet(int _num) : | 
|---|
| [cd5047] | 201 | Observable("ObservableCollection"), | 
|---|
|  | 202 | num(_num) | 
|---|
| [a7b761b] | 203 | { | 
|---|
| [bd58fb] | 204 | for(int i=0; i<num; ++i){ | 
|---|
|  | 205 | SimpleObservable *content = new SimpleObservable(); | 
|---|
|  | 206 | content->signOn(this); | 
|---|
|  | 207 | theSet.insert(content); | 
|---|
|  | 208 | } | 
|---|
|  | 209 | } | 
|---|
|  | 210 |  | 
|---|
| [8774c5] | 211 | ~ObservableSet(){ | 
|---|
| [bd58fb] | 212 | set::iterator iter; | 
|---|
|  | 213 | for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){ | 
|---|
|  | 214 | delete (*iter); | 
|---|
|  | 215 | } | 
|---|
|  | 216 | } | 
|---|
|  | 217 |  | 
|---|
|  | 218 | iterator begin(){ | 
|---|
|  | 219 | return iterator(theSet.begin(),this); | 
|---|
|  | 220 | } | 
|---|
|  | 221 |  | 
|---|
|  | 222 | iterator end(){ | 
|---|
|  | 223 | return iterator(theSet.end(),this); | 
|---|
|  | 224 | } | 
|---|
|  | 225 |  | 
|---|
| [a7b761b] | 226 | const int num; | 
|---|
|  | 227 |  | 
|---|
| [bd58fb] | 228 | private: | 
|---|
|  | 229 | set theSet; | 
|---|
|  | 230 | }; | 
|---|
|  | 231 |  | 
|---|
| [8774c5] | 232 | class ObservableMap : public Observable { | 
|---|
|  | 233 | public: | 
|---|
|  | 234 | typedef std::map<int,SimpleObservable*> set; | 
|---|
|  | 235 | typedef ObservedIterator<set> iterator; | 
|---|
|  | 236 | typedef set::const_iterator const_iterator; | 
|---|
|  | 237 |  | 
|---|
|  | 238 | ObservableMap(int _num) : | 
|---|
|  | 239 | Observable("ObservableCollection"), | 
|---|
|  | 240 | num(_num) | 
|---|
|  | 241 | { | 
|---|
|  | 242 | for(int i=0; i<num; ++i){ | 
|---|
|  | 243 | SimpleObservable *content = new SimpleObservable(); | 
|---|
|  | 244 | content->signOn(this); | 
|---|
| [a2c4f3] | 245 | theSet.insert(std::make_pair(i,content)); | 
|---|
| [8774c5] | 246 | } | 
|---|
|  | 247 | } | 
|---|
|  | 248 |  | 
|---|
|  | 249 | ~ObservableMap(){ | 
|---|
|  | 250 | set::iterator iter; | 
|---|
|  | 251 | for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){ | 
|---|
|  | 252 | delete iter->second; | 
|---|
|  | 253 | } | 
|---|
|  | 254 | } | 
|---|
|  | 255 |  | 
|---|
|  | 256 | iterator begin(){ | 
|---|
|  | 257 | return iterator(theSet.begin(),this); | 
|---|
|  | 258 | } | 
|---|
|  | 259 |  | 
|---|
|  | 260 | iterator end(){ | 
|---|
|  | 261 | return iterator(theSet.end(),this); | 
|---|
|  | 262 | } | 
|---|
|  | 263 |  | 
|---|
|  | 264 | const int num; | 
|---|
|  | 265 |  | 
|---|
|  | 266 | private: | 
|---|
|  | 267 | set theSet; | 
|---|
|  | 268 | }; | 
|---|
|  | 269 |  | 
|---|
| [a7b761b] | 270 |  | 
|---|
| [63c1f6] | 271 | /******************* actuall tests ***************/ | 
|---|
|  | 272 |  | 
|---|
|  | 273 | void ObserverTest::setUp() { | 
|---|
| [4fb5a3] | 274 | ASSERT_DO(Assert::Throw); | 
|---|
| [d5f216] | 275 | simpleObservable1 = new SimpleObservable(); | 
|---|
|  | 276 | simpleObservable2 = new SimpleObservable(); | 
|---|
| [63c1f6] | 277 | callObservable = new CallObservable(); | 
|---|
|  | 278 | superObservable = new SuperObservable(); | 
|---|
| [4fb5a3] | 279 | blockObservable = new BlockObservable(); | 
|---|
| [ccacba] | 280 | notificationObservable = new NotificationObservable(); | 
|---|
| [8774c5] | 281 | obsset = new ObservableSet(5); | 
|---|
|  | 282 | obsmap = new ObservableMap(5); | 
|---|
| [63c1f6] | 283 |  | 
|---|
|  | 284 | observer1 = new UpdateCountObserver(); | 
|---|
|  | 285 | observer2 = new UpdateCountObserver(); | 
|---|
|  | 286 | observer3 = new UpdateCountObserver(); | 
|---|
| [d5f216] | 287 | observer4 = new UpdateCountObserver(); | 
|---|
| [ccacba] | 288 |  | 
|---|
|  | 289 | notificationObserver1 = new NotificationObserver(notificationObservable->notification1); | 
|---|
|  | 290 | notificationObserver2 = new NotificationObserver(notificationObservable->notification2); | 
|---|
| [63c1f6] | 291 | } | 
|---|
|  | 292 |  | 
|---|
|  | 293 | void ObserverTest::tearDown() { | 
|---|
| [d5f216] | 294 | delete simpleObservable1; | 
|---|
|  | 295 | delete simpleObservable2; | 
|---|
| [63c1f6] | 296 | delete callObservable; | 
|---|
|  | 297 | delete superObservable; | 
|---|
| [033a05] | 298 | delete blockObservable; | 
|---|
| [ccacba] | 299 | delete notificationObservable; | 
|---|
| [8774c5] | 300 | delete obsset; | 
|---|
|  | 301 | delete obsmap; | 
|---|
| [63c1f6] | 302 |  | 
|---|
|  | 303 | delete observer1; | 
|---|
|  | 304 | delete observer2; | 
|---|
|  | 305 | delete observer3; | 
|---|
| [d5f216] | 306 | delete observer4; | 
|---|
| [ccacba] | 307 | delete notificationObserver1; | 
|---|
|  | 308 | delete notificationObserver2; | 
|---|
| [63c1f6] | 309 | } | 
|---|
|  | 310 |  | 
|---|
|  | 311 | void ObserverTest::doesUpdateTest() | 
|---|
|  | 312 | { | 
|---|
| [d5f216] | 313 | simpleObservable1->signOn(observer1); | 
|---|
|  | 314 | simpleObservable1->signOn(observer2); | 
|---|
|  | 315 | simpleObservable1->signOn(observer3); | 
|---|
| [9b6b2f] | 316 |  | 
|---|
| [d5f216] | 317 | simpleObservable2->signOn(observer2); | 
|---|
|  | 318 | simpleObservable2->signOn(observer4); | 
|---|
| [63c1f6] | 319 |  | 
|---|
| [61ea5b] | 320 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
|  | 321 | CPPUNIT_ASSERT_EQUAL( 0, observer2->updates ); | 
|---|
|  | 322 | CPPUNIT_ASSERT_EQUAL( 0, observer3->updates ); | 
|---|
|  | 323 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates ); | 
|---|
|  | 324 |  | 
|---|
|  | 325 |  | 
|---|
| [d5f216] | 326 | simpleObservable1->changeMethod(); | 
|---|
| [63c1f6] | 327 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
|  | 328 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates ); | 
|---|
|  | 329 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates ); | 
|---|
| [d5f216] | 330 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates ); | 
|---|
| [63c1f6] | 331 |  | 
|---|
| [d5f216] | 332 | simpleObservable1->signOff(observer3); | 
|---|
| [63c1f6] | 333 |  | 
|---|
| [d5f216] | 334 | simpleObservable1->changeMethod(); | 
|---|
| [63c1f6] | 335 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); | 
|---|
|  | 336 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates ); | 
|---|
|  | 337 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates ); | 
|---|
| [d5f216] | 338 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates ); | 
|---|
|  | 339 |  | 
|---|
|  | 340 | simpleObservable2->changeMethod(); | 
|---|
|  | 341 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); | 
|---|
|  | 342 | CPPUNIT_ASSERT_EQUAL( 3, observer2->updates ); | 
|---|
|  | 343 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates ); | 
|---|
|  | 344 | CPPUNIT_ASSERT_EQUAL( 1, observer4->updates ); | 
|---|
| [63c1f6] | 345 | } | 
|---|
|  | 346 |  | 
|---|
|  | 347 |  | 
|---|
|  | 348 | void ObserverTest::doesBlockUpdateTest() { | 
|---|
|  | 349 | callObservable->signOn(observer1); | 
|---|
| [61ea5b] | 350 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
| [63c1f6] | 351 |  | 
|---|
|  | 352 | callObservable->changeMethod1(); | 
|---|
|  | 353 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
|  | 354 |  | 
|---|
|  | 355 | callObservable->changeMethod2(); | 
|---|
|  | 356 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); | 
|---|
|  | 357 | } | 
|---|
|  | 358 |  | 
|---|
|  | 359 | void ObserverTest::doesSubObservableTest() { | 
|---|
|  | 360 | superObservable->signOn(observer1); | 
|---|
|  | 361 | superObservable->subObservable->signOn(observer2); | 
|---|
|  | 362 |  | 
|---|
|  | 363 | superObservable->subObservable->changeMethod(); | 
|---|
|  | 364 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
|  | 365 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates ); | 
|---|
|  | 366 |  | 
|---|
|  | 367 | superObservable->changeMethod(); | 
|---|
|  | 368 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); | 
|---|
|  | 369 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates ); | 
|---|
|  | 370 | } | 
|---|
|  | 371 |  | 
|---|
| [61ea5b] | 372 | void ObserverTest::outsideLockTest(){ | 
|---|
|  | 373 | callObservable->signOn(observer1); | 
|---|
|  | 374 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
|  | 375 |  | 
|---|
|  | 376 | { | 
|---|
|  | 377 | LOCK_OBSERVABLE(*callObservable); | 
|---|
|  | 378 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
|  | 379 | } | 
|---|
|  | 380 | // lock is gone now, observer should have notified | 
|---|
|  | 381 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
|  | 382 | } | 
|---|
|  | 383 |  | 
|---|
| [ccacba] | 384 | void ObserverTest::doesNotifyTest(){ | 
|---|
|  | 385 | notificationObservable->signOn(notificationObserver1, | 
|---|
|  | 386 | notificationObservable->notification1); | 
|---|
|  | 387 | notificationObservable->signOn(notificationObserver2, | 
|---|
|  | 388 | notificationObservable->notification2); | 
|---|
|  | 389 |  | 
|---|
|  | 390 | notificationObservable->operation1(); | 
|---|
|  | 391 | CPPUNIT_ASSERT(notificationObserver1->wasNotified); | 
|---|
|  | 392 | CPPUNIT_ASSERT(!notificationObserver2->wasNotified); | 
|---|
|  | 393 |  | 
|---|
|  | 394 | notificationObserver1->wasNotified=false; | 
|---|
|  | 395 |  | 
|---|
|  | 396 | notificationObservable->operation2(); | 
|---|
|  | 397 | CPPUNIT_ASSERT(!notificationObserver1->wasNotified); | 
|---|
|  | 398 | CPPUNIT_ASSERT(notificationObserver2->wasNotified); | 
|---|
|  | 399 |  | 
|---|
|  | 400 | } | 
|---|
|  | 401 |  | 
|---|
| [4fb5a3] | 402 | void ObserverTest::doesReportTest(){ | 
|---|
|  | 403 | // Actual checks are in the Stub-methods for this | 
|---|
|  | 404 | blockObservable->changeMethod1(); | 
|---|
|  | 405 | blockObservable->changeMethod2(); | 
|---|
|  | 406 | blockObservable->noChangeMethod(); | 
|---|
|  | 407 | } | 
|---|
| [63c1f6] | 408 |  | 
|---|
| [bd58fb] | 409 | void ObserverTest::iteratorTest(){ | 
|---|
| [a7b761b] | 410 | int i = 0; | 
|---|
|  | 411 | // test the general iterator methods | 
|---|
| [8774c5] | 412 | for(ObservableSet::iterator iter=obsset->begin(); iter!=obsset->end();++iter){ | 
|---|
|  | 413 | CPPUNIT_ASSERT(i< obsset->num); | 
|---|
| [a7b761b] | 414 | i++; | 
|---|
|  | 415 | } | 
|---|
|  | 416 |  | 
|---|
|  | 417 | i=0; | 
|---|
| [8774c5] | 418 | for(ObservableSet::const_iterator iter=obsset->begin(); iter!=obsset->end();++iter){ | 
|---|
|  | 419 | CPPUNIT_ASSERT(i<obsset->num); | 
|---|
| [a7b761b] | 420 | i++; | 
|---|
|  | 421 | } | 
|---|
|  | 422 |  | 
|---|
| [8774c5] | 423 | obsset->signOn(observer1); | 
|---|
| [bd58fb] | 424 | { | 
|---|
| [a7b761b] | 425 | // we construct this out of the loop, so the iterator dies at the end of | 
|---|
|  | 426 | // the scope and not the end of the loop (allows more testing) | 
|---|
| [8774c5] | 427 | ObservableSet::iterator iter; | 
|---|
|  | 428 | for(iter=obsset->begin(); iter!=obsset->end(); ++iter){ | 
|---|
| [bd58fb] | 429 | (*iter)->changeMethod(); | 
|---|
|  | 430 | } | 
|---|
|  | 431 | // At this point no change should have been propagated | 
|---|
|  | 432 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates); | 
|---|
|  | 433 | } | 
|---|
|  | 434 | // After the Iterator has died the propagation should take place | 
|---|
|  | 435 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates); | 
|---|
| [a7b761b] | 436 |  | 
|---|
|  | 437 | // when using a const_iterator no changes should be propagated | 
|---|
| [8774c5] | 438 | for(ObservableSet::const_iterator iter = obsset->begin(); iter!=obsset->end();++iter); | 
|---|
| [a7b761b] | 439 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates); | 
|---|
| [8774c5] | 440 |  | 
|---|
|  | 441 | // we need to test the operator-> as well | 
|---|
|  | 442 | obsmap->signOn(observer2); | 
|---|
|  | 443 | { | 
|---|
|  | 444 | // we construct this out of the loop, so the iterator dies at the end of | 
|---|
|  | 445 | // the scope and not the end of the loop (allows more testing) | 
|---|
|  | 446 | ObservableMap::iterator iter; | 
|---|
|  | 447 | for(iter=obsmap->begin(); iter!=obsmap->end(); ++iter){ | 
|---|
|  | 448 | iter->second->changeMethod(); | 
|---|
|  | 449 | } | 
|---|
|  | 450 | // At this point no change should have been propagated | 
|---|
|  | 451 | CPPUNIT_ASSERT_EQUAL( 0, observer2->updates); | 
|---|
|  | 452 | } | 
|---|
|  | 453 | // After the Iterator has died the propagation should take place | 
|---|
|  | 454 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates); | 
|---|
|  | 455 |  | 
|---|
|  | 456 |  | 
|---|
|  | 457 | obsset->signOff(observer1); | 
|---|
|  | 458 | obsmap->signOff(observer2); | 
|---|
| [bd58fb] | 459 | } | 
|---|
|  | 460 |  | 
|---|
| [63c1f6] | 461 | void ObserverTest::CircleDetectionTest() { | 
|---|
| [a2c4f3] | 462 | std::cout << std::endl << "Warning: the next test involved methods that can produce infinite loops." << std::endl; | 
|---|
|  | 463 | std::cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << std::endl; | 
|---|
|  | 464 | std::cout << "Instead tests are run on these methods to see if termination is assured" << std::endl << std::endl; | 
|---|
|  | 465 | std::cout << "If this test does not complete in a few seconds, kill the test-suite and fix the Error in the circle detection mechanism" << std::endl; | 
|---|
| [63c1f6] | 466 |  | 
|---|
| [a2c4f3] | 467 | std::cout << std::endl << std::endl << "The following errors displayed by the observer framework can be ignored" << std::endl; | 
|---|
| [63c1f6] | 468 |  | 
|---|
|  | 469 | // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE | 
|---|
| [d5f216] | 470 | simpleObservable1->signOn(simpleObservable1); | 
|---|
| [7a176b] | 471 | #ifndef NDEBUG | 
|---|
| [4fb5a3] | 472 | CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure); | 
|---|
| [7a176b] | 473 | #else | 
|---|
|  | 474 | simpleObservable1->changeMethod(); | 
|---|
|  | 475 | #endif | 
|---|
| [d5f216] | 476 |  | 
|---|
|  | 477 | // more complex test | 
|---|
|  | 478 | simpleObservable1->signOff(simpleObservable1); | 
|---|
|  | 479 | simpleObservable1->signOn(simpleObservable2); | 
|---|
|  | 480 | simpleObservable2->signOn(simpleObservable1); | 
|---|
| [7a176b] | 481 | #ifndef NDEBUG | 
|---|
| [4fb5a3] | 482 | CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure); | 
|---|
| [7a176b] | 483 | #else | 
|---|
|  | 484 | simpleObservable1->changeMethod(); | 
|---|
|  | 485 | #endif | 
|---|
|  | 486 |  | 
|---|
|  | 487 |  | 
|---|
| [d5f216] | 488 | simpleObservable1->signOff(simpleObservable2); | 
|---|
|  | 489 | simpleObservable2->signOff(simpleObservable1); | 
|---|
| [63c1f6] | 490 | // when we reach this line, although we broke the DAG assumption the circle check works fine | 
|---|
|  | 491 | CPPUNIT_ASSERT(true); | 
|---|
|  | 492 | } | 
|---|