Ignore:
Timestamp:
Jul 14, 2010, 10:27:19 AM (14 years ago)
Author:
Tillmann Crueger <crueger@…>
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, 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:
51be2a
Parents:
b99bf3
Message:

Added operator-> for Observed Iterators

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/unittests/ObserverTest.cpp

    rb99bf3 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;
     
    360400  int i = 0;
    361401  // test the general iterator methods
    362   for(ObservableCollection::iterator iter=collection->begin(); iter!=collection->end();++iter){
    363     CPPUNIT_ASSERT(i< collection->num);
     402  for(ObservableSet::iterator iter=obsset->begin(); iter!=obsset->end();++iter){
     403    CPPUNIT_ASSERT(i< obsset->num);
    364404    i++;
    365405  }
    366406
    367407  i=0;
    368   for(ObservableCollection::const_iterator iter=collection->begin(); iter!=collection->end();++iter){
    369     CPPUNIT_ASSERT(i<collection->num);
    370     i++;
    371   }
    372 
    373   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);
    374414  {
    375415    // we construct this out of the loop, so the iterator dies at the end of
    376416    // the scope and not the end of the loop (allows more testing)
    377     ObservableCollection::iterator iter;
    378     for(iter=collection->begin(); iter!=collection->end(); ++iter){
     417    ObservableSet::iterator iter;
     418    for(iter=obsset->begin(); iter!=obsset->end(); ++iter){
    379419      (*iter)->changeMethod();
    380420    }
     
    386426
    387427  // when using a const_iterator no changes should be propagated
    388   for(ObservableCollection::const_iterator iter = collection->begin(); iter!=collection->end();++iter);
     428  for(ObservableSet::const_iterator iter = obsset->begin(); iter!=obsset->end();++iter);
    389429  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates);
    390   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);
    391449}
    392450
Note: See TracChangeset for help on using the changeset viewer.