Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/unittests/ObserverTest.cpp

    r033a05 r7a176b  
    1111#include <cppunit/extensions/TestFactoryRegistry.h>
    1212#include <cppunit/ui/text/TestRunner.h>
     13#include <set>
    1314
    1415#include "Patterns/Observer.hpp"
     16#include "Patterns/ObservedIterator.hpp"
    1517#include "Helpers/Assert.hpp"
    1618
     
    3133public:
    3234  UpdateCountObserver() :
     35    Observer("UpdateCountObserver"),
    3336    updates(0)
    3437  {};
     
    4346class SimpleObservable : public Observable {
    4447public:
     48  SimpleObservable() :
     49    Observable("SimpleObservable")
     50  {}
     51
    4552  void changeMethod() {
    4653    OBSERVE;
     
    5259class CallObservable : public Observable {
    5360public:
     61  CallObservable() :
     62    Observable("CallObservable")
     63  {}
     64
    5465  void changeMethod1() {
    5566    OBSERVE;
     
    6879class BlockObservable : public Observable {
    6980public:
     81  BlockObservable() :
     82    Observable("BlockObservable")
     83  {}
     84
    7085  void changeMethod1(){
    7186    OBSERVE;
     
    102117class SuperObservable : public Observable {
    103118public:
    104   SuperObservable(){
     119  SuperObservable():
     120    Observable("SuperObservable")
     121  {
    105122    subObservable = new SimpleObservable();
    106123    subObservable->signOn(this);
     
    121138public:
    122139  NotificationObservable() :
    123       notification1(new Notification(this)),
    124       notification2(new Notification(this))
     140    Observable("NotificationObservable"),
     141    notification1(new Notification(this)),
     142    notification2(new Notification(this))
    125143  {}
    126144
     
    147165public:
    148166  NotificationObserver(Notification_ptr notification) :
     167    Observer("NotificationObserver"),
    149168    requestedNotification(notification),
    150169    wasNotified(false)
     
    162181  bool wasNotified;
    163182};
     183
     184class ObservableCollection : public Observable {
     185public:
     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
     218private:
     219  set theSet;
     220};
     221
    164222
    165223/******************* actuall tests ***************/
     
    173231  blockObservable = new BlockObservable();
    174232  notificationObservable = new NotificationObservable();
     233  collection = new ObservableCollection(5);
    175234
    176235  observer1 = new UpdateCountObserver();
     
    181240  notificationObserver1 = new NotificationObserver(notificationObservable->notification1);
    182241  notificationObserver2 = new NotificationObserver(notificationObservable->notification2);
    183 
    184242}
    185243
     
    191249  delete blockObservable;
    192250  delete notificationObservable;
     251  delete collection;
    193252
    194253  delete observer1;
     
    279338}
    280339
     340void 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
    281374void ObserverTest::CircleDetectionTest() {
    282375  cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
     
    289382  // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
    290383  simpleObservable1->signOn(simpleObservable1);
     384#ifndef NDEBUG
    291385  CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
     386#else
     387  simpleObservable1->changeMethod();
     388#endif
    292389
    293390  // more complex test
     
    295392  simpleObservable1->signOn(simpleObservable2);
    296393  simpleObservable2->signOn(simpleObservable1);
     394#ifndef NDEBUG
    297395  CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
     396#else
     397  simpleObservable1->changeMethod();
     398#endif
     399
     400
    298401  simpleObservable1->signOff(simpleObservable2);
    299402  simpleObservable2->signOff(simpleObservable1);
Note: See TracChangeset for help on using the changeset viewer.