source: src/unittests/ObserverTest.cpp@ a1510d

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
Last change on this file since a1510d was 5e5283, checked in by Frederik Heber <heber@…>, 15 years ago

FIX: Values have been used uninitialized.

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * ObserverTest.cpp
3 *
4 * Created on: Jan 19, 2010
5 * Author: crueger
6 */
7
8#include "ObserverTest.hpp"
9
10#include <cppunit/CompilerOutputter.h>
11#include <cppunit/extensions/TestFactoryRegistry.h>
12#include <cppunit/ui/text/TestRunner.h>
13
14#include "Patterns/Observer.hpp"
15
16#include <iostream>
17
18using namespace std;
19
20#ifdef HAVE_TESTRUNNER
21#include "UnitTestMain.hpp"
22#endif /*HAVE_TESTRUNNER*/
23
24// Registers the fixture into the 'registry'
25CPPUNIT_TEST_SUITE_REGISTRATION( ObserverTest );
26
27/******************* Test stubs ************************/
28
29class UpdateCountObserver : public Observer {
30public:
31 UpdateCountObserver() :
32 updates(0)
33 {};
34 void update(Observable *publisher){
35 updates++;
36 }
37 void subjectKilled(Observable *publisher) {
38 }
39 int updates;
40};
41
42class SimpleObservable : public Observable {
43public:
44 void changeMethod() {
45 OBSERVE;
46 int i = 0;
47 i++;
48 }
49};
50
51class CallObservable : public Observable {
52public:
53 void changeMethod1() {
54 OBSERVE;
55 int i = 0;
56 i++;
57 }
58
59 void changeMethod2() {
60 OBSERVE;
61 int i = 0;
62 i++;
63 changeMethod1();
64 }
65};
66
67class SuperObservable : public Observable {
68public:
69 SuperObservable(){
70 subObservable = new SimpleObservable();
71 subObservable->signOn(this);
72 }
73 ~SuperObservable(){
74 delete subObservable;
75 }
76 void changeMethod() {
77 OBSERVE;
78 int i = 0;
79 i++;
80 subObservable->changeMethod();
81 }
82 SimpleObservable *subObservable;
83};
84
85/******************* actuall tests ***************/
86
87void ObserverTest::setUp() {
88 simpleObservable1 = new SimpleObservable();
89 simpleObservable2 = new SimpleObservable();
90 callObservable = new CallObservable();
91 superObservable = new SuperObservable();
92
93 observer1 = new UpdateCountObserver();
94 observer2 = new UpdateCountObserver();
95 observer3 = new UpdateCountObserver();
96 observer4 = new UpdateCountObserver();
97}
98
99void ObserverTest::tearDown() {
100 delete simpleObservable1;
101 delete simpleObservable2;
102 delete callObservable;
103 delete superObservable;
104
105 delete observer1;
106 delete observer2;
107 delete observer3;
108 delete observer4;
109}
110
111void ObserverTest::doesUpdateTest()
112{
113 simpleObservable1->signOn(observer1);
114 simpleObservable1->signOn(observer2);
115 simpleObservable1->signOn(observer3);
116
117 simpleObservable2->signOn(observer2);
118 simpleObservable2->signOn(observer4);
119
120 simpleObservable1->changeMethod();
121 CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
122 CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
123 CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
124 CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
125
126 simpleObservable1->signOff(observer3);
127
128 simpleObservable1->changeMethod();
129 CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
130 CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
131 CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
132 CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
133
134 simpleObservable2->changeMethod();
135 CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
136 CPPUNIT_ASSERT_EQUAL( 3, observer2->updates );
137 CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
138 CPPUNIT_ASSERT_EQUAL( 1, observer4->updates );
139}
140
141
142void ObserverTest::doesBlockUpdateTest() {
143 callObservable->signOn(observer1);
144
145 callObservable->changeMethod1();
146 CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
147
148 callObservable->changeMethod2();
149 CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
150}
151
152void ObserverTest::doesSubObservableTest() {
153 superObservable->signOn(observer1);
154 superObservable->subObservable->signOn(observer2);
155
156 superObservable->subObservable->changeMethod();
157 CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
158 CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
159
160 superObservable->changeMethod();
161 CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
162 CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
163}
164
165
166void ObserverTest::CircleDetectionTest() {
167 cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
168 cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
169 cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
170 cout << "If this test does not complete in a few seconds, kill the test-suite and fix the Error in the circle detection mechanism" << endl;
171
172 cout << endl << endl << "The following errors displayed by the observer framework can be ignored" << endl;
173
174 // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
175 simpleObservable1->signOn(simpleObservable1);
176 simpleObservable1->changeMethod();
177
178 // more complex test
179 simpleObservable1->signOff(simpleObservable1);
180 simpleObservable1->signOn(simpleObservable2);
181 simpleObservable2->signOn(simpleObservable1);
182 simpleObservable1->changeMethod();
183 simpleObservable1->signOff(simpleObservable2);
184 simpleObservable2->signOff(simpleObservable1);
185 // when we reach this line, although we broke the DAG assumption the circle check works fine
186 CPPUNIT_ASSERT(true);
187}
Note: See TracBrowser for help on using the repository browser.