Changeset 257c77 for src/unittests/ObserverTest.cpp
- Timestamp:
- Jun 17, 2010, 2:55:56 PM (16 years ago)
- Branches:
- Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, Candidate_v1.7.0, Candidate_v1.7.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
- Children:
- b47bfc
- Parents:
- 992fd7 (diff), 5f5a7b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
src/unittests/ObserverTest.cpp (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/unittests/ObserverTest.cpp
r992fd7 r257c77 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; … … 277 336 blockObservable->changeMethod2(); 278 337 blockObservable->noChangeMethod(); 338 } 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); 279 372 } 280 373
Note:
See TracChangeset
for help on using the changeset viewer.
