- Timestamp:
- Jan 16, 2011, 6:34:28 PM (15 years ago)
- Children:
- fe056c
- Parents:
- 192c04
- Location:
- src
- Files:
-
- 2 added
- 3 edited
-
Patterns/unittests/Makefile.am (modified) (1 diff)
-
Patterns/unittests/ObserverUnitTest.cpp (modified) (2 diffs)
-
Patterns/unittests/stubs/ObserverStub.cpp (added)
-
Patterns/unittests/stubs/ObserverStub.hpp (added)
-
unittests/Makefile.am (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Patterns/unittests/Makefile.am
r192c04 r56d62f 101 101 ObserverUnitTest_SOURCES = $(top_srcdir)/src/unittests/UnitTestMain.cpp \ 102 102 ObserverUnitTest.cpp \ 103 ObserverUnitTest.hpp 103 ObserverUnitTest.hpp \ 104 stubs/ObserverStub.cpp \ 105 stubs/ObserverStub.hpp 104 106 nodist_ObserverUnitTest_SOURCES = \ 105 107 ../Observer.hpp \ -
src/Patterns/unittests/ObserverUnitTest.cpp
r192c04 r56d62f 23 23 #include <set> 24 24 25 #include "Observer.hpp"26 #include "ObservedIterator.hpp"27 25 #include "Assert.hpp" 28 26 29 27 #include <iostream> 28 29 #include "stubs/ObserverStub.hpp" 30 30 31 31 #include "ObserverUnitTest.hpp" … … 39 39 40 40 /******************* Test stubs ************************/ 41 42 class UpdateCountObserver : public Observer {43 public:44 UpdateCountObserver() :45 Observer("UpdateCountObserver"),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:58 SimpleObservable() :59 Observable("SimpleObservable")60 {}61 62 void changeMethod() {63 OBSERVE;64 int i = 0;65 i++;66 }67 };68 69 class CallObservable : public Observable {70 public:71 CallObservable() :72 Observable("CallObservable")73 {}74 75 void changeMethod1() {76 OBSERVE;77 int i = 0;78 i++;79 }80 81 void changeMethod2() {82 OBSERVE;83 int i = 0;84 i++;85 changeMethod1();86 }87 };88 89 class BlockObservable : public Observable {90 public:91 BlockObservable() :92 Observable("BlockObservable")93 {}94 95 void changeMethod1(){96 OBSERVE;97 // test if we report correctly as blocked98 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 found110 CPPUNIT_ASSERT(isBlocked());111 }112 113 void internalMethod2(){114 OBSERVE;115 // Both this method and the caller do block116 // Does the reporting still work as expected?117 CPPUNIT_ASSERT(isBlocked());118 }119 120 void noChangeMethod(){121 // No Block introduced here122 // reported correctely?123 CPPUNIT_ASSERT(!isBlocked());124 }125 };126 127 class SuperObservable : public Observable {128 public:129 SuperObservable():130 Observable("SuperObservable")131 {132 subObservable = new SimpleObservable();133 subObservable->signOn(this);134 }135 ~SuperObservable(){136 delete subObservable;137 }138 void changeMethod() {139 OBSERVE;140 int i = 0;141 i++;142 subObservable->changeMethod();143 }144 SimpleObservable *subObservable;145 };146 147 class NotificationObservable : public Observable {148 public:149 NotificationObservable() :150 Observable("NotificationObservable"),151 notification1(new Notification(this)),152 notification2(new Notification(this))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) :177 Observer("NotificationObserver"),178 requestedNotification(notification),179 wasNotified(false)180 {}181 182 void update(Observable*){}183 void subjectKilled(Observable*){}184 void recieveNotification(Observable *publisher, Notification_ptr notification){185 ASSERT(requestedNotification==notification,"Notification received that was not requested");186 wasNotified = true;187 }188 189 Notification_ptr requestedNotification;190 191 bool wasNotified;192 };193 194 class ObservableSet : public Observable {195 public:196 typedef std::set<SimpleObservable*> set;197 typedef ObservedIterator<set> iterator;198 typedef set::const_iterator const_iterator;199 200 ObservableSet(int _num) :201 Observable("ObservableCollection"),202 num(_num)203 {204 for(int i=0; i<num; ++i){205 SimpleObservable *content = new SimpleObservable();206 content->signOn(this);207 theSet.insert(content);208 }209 }210 211 ~ObservableSet(){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 226 const int num;227 228 private:229 set theSet;230 };231 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);245 theSet.insert(std::make_pair(i,content));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 41 270 42 -
src/unittests/Makefile.am
r192c04 r56d62f 39 39 $(top_srcdir)/src/Patterns/unittests/stubs/ManipulableCloneStub.cpp \ 40 40 $(top_srcdir)/src/Patterns/unittests/stubs/ManipulablePrototypeFactoryStub.cpp \ 41 $(top_srcdir)/src/Patterns/unittests/stubs/ObserverStub.cpp \ 41 42 $(top_srcdir)/src/Patterns/unittests/stubs/PrototypeFactoryStub.cpp \ 42 43 InfoUnitTest.cpp \ … … 66 67 $(top_srcdir)/src/Patterns/unittests/stubs/ManipulablePrototypeFactoryStub.def \ 67 68 $(top_srcdir)/src/Patterns/unittests/stubs/ManipulablePrototypeFactoryStub.undef \ 69 $(top_srcdir)/src/Patterns/unittests/stubs/ObserverStub.hpp \ 68 70 $(top_srcdir)/src/Patterns/unittests/stubs/PrototypeFactoryStub.def \ 69 71 $(top_srcdir)/src/Patterns/unittests/stubs/PrototypeFactoryStub.undef \
Note:
See TracChangeset
for help on using the changeset viewer.
