Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/unittests/ObserverTest.cpp

    rcd5047 r8774c5  
    182182};
    183183
    184 class ObservableCollection : public Observable {
     184class ObservableSet : public Observable {
    185185public:
    186186  typedef std::set<SimpleObservable*> set;
     
    188188  typedef set::const_iterator const_iterator;
    189189
    190   ObservableCollection(int _num) :
     190  ObservableSet(int _num) :
    191191    Observable("ObservableCollection"),
    192192    num(_num)
     
    199199  }
    200200
    201   ~ObservableCollection(){
     201  ~ObservableSet(){
    202202    set::iterator iter;
    203203    for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
    204204      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
     222class ObservableMap : public Observable {
     223public:
     224  typedef std::map<int,SimpleObservable*> set;
     225  typedef ObservedIterator<set> iterator;
     226  typedef set::const_iterator const_iterator;
     227
     228  ObservableMap(int _num) :
     229    Observable("ObservableCollection"),
     230    num(_num)
     231  {
     232    for(int i=0; i<num; ++i){
     233      SimpleObservable *content = new SimpleObservable();
     234      content->signOn(this);
     235      theSet.insert(make_pair(i,content));
     236    }
     237  }
     238
     239  ~ObservableMap(){
     240    set::iterator iter;
     241    for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
     242      delete iter->second;
    205243    }
    206244  }
     
    231269  blockObservable = new BlockObservable();
    232270  notificationObservable = new NotificationObservable();
    233   collection = new ObservableCollection(5);
     271  obsset = new ObservableSet(5);
     272  obsmap = new ObservableMap(5);
    234273
    235274  observer1 = new UpdateCountObserver();
     
    249288  delete blockObservable;
    250289  delete notificationObservable;
    251   delete collection;
     290  delete obsset;
     291  delete obsmap;
    252292
    253293  delete observer1;
     
    268308  simpleObservable2->signOn(observer4);
    269309
     310  CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
     311  CPPUNIT_ASSERT_EQUAL( 0, observer2->updates );
     312  CPPUNIT_ASSERT_EQUAL( 0, observer3->updates );
     313  CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
     314
     315
    270316  simpleObservable1->changeMethod();
    271317  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
     
    292338void ObserverTest::doesBlockUpdateTest() {
    293339  callObservable->signOn(observer1);
     340  CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
    294341
    295342  callObservable->changeMethod1();
     
    311358  CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
    312359  CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
     360}
     361
     362void ObserverTest::outsideLockTest(){
     363  callObservable->signOn(observer1);
     364  CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
     365
     366  {
     367    LOCK_OBSERVABLE(*callObservable);
     368    CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
     369  }
     370  // lock is gone now, observer should have notified
     371  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
    313372}
    314373
     
    341400  int i = 0;
    342401  // test the general iterator methods
    343   for(ObservableCollection::iterator iter=collection->begin(); iter!=collection->end();++iter){
    344     CPPUNIT_ASSERT(i< collection->num);
     402  for(ObservableSet::iterator iter=obsset->begin(); iter!=obsset->end();++iter){
     403    CPPUNIT_ASSERT(i< obsset->num);
    345404    i++;
    346405  }
    347406
    348407  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);
     408  for(ObservableSet::const_iterator iter=obsset->begin(); iter!=obsset->end();++iter){
     409    CPPUNIT_ASSERT(i<obsset->num);
     410    i++;
     411  }
     412
     413  obsset->signOn(observer1);
    355414  {
    356415    // we construct this out of the loop, so the iterator dies at the end of
    357416    // 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){
     417    ObservableSet::iterator iter;
     418    for(iter=obsset->begin(); iter!=obsset->end(); ++iter){
    360419      (*iter)->changeMethod();
    361420    }
     
    367426
    368427  // when using a const_iterator no changes should be propagated
    369   for(ObservableCollection::const_iterator iter = collection->begin(); iter!=collection->end();++iter);
     428  for(ObservableSet::const_iterator iter = obsset->begin(); iter!=obsset->end();++iter);
    370429  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates);
    371   collection->signOff(observer1);
     430
     431  // we need to test the operator-> as well
     432  obsmap->signOn(observer2);
     433  {
     434    // we construct this out of the loop, so the iterator dies at the end of
     435    // the scope and not the end of the loop (allows more testing)
     436    ObservableMap::iterator iter;
     437    for(iter=obsmap->begin(); iter!=obsmap->end(); ++iter){
     438      iter->second->changeMethod();
     439    }
     440    // At this point no change should have been propagated
     441    CPPUNIT_ASSERT_EQUAL( 0, observer2->updates);
     442  }
     443  // After the Iterator has died the propagation should take place
     444  CPPUNIT_ASSERT_EQUAL( 1, observer2->updates);
     445
     446
     447  obsset->signOff(observer1);
     448  obsmap->signOff(observer2);
    372449}
    373450
     
    382459  // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
    383460  simpleObservable1->signOn(simpleObservable1);
     461#ifndef NDEBUG
    384462  CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
     463#else
     464  simpleObservable1->changeMethod();
     465#endif
    385466
    386467  // more complex test
     
    388469  simpleObservable1->signOn(simpleObservable2);
    389470  simpleObservable2->signOn(simpleObservable1);
     471#ifndef NDEBUG
    390472  CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
     473#else
     474  simpleObservable1->changeMethod();
     475#endif
     476
     477
    391478  simpleObservable1->signOff(simpleObservable2);
    392479  simpleObservable2->signOff(simpleObservable1);
Note: See TracChangeset for help on using the changeset viewer.