source: src/unittests/ObserverTest.cpp@ 63c1f6

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 63c1f6 was 63c1f6, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added generic observer pattern framework.
(cherry picked from commit 7bc7ce52eb7b4d606d90f49cbfa9da7a300c5d82)

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/unittests/Makefile.am

  • Property mode set to 100644
File size: 4.7 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// Registers the fixture into the 'registry'
21CPPUNIT_TEST_SUITE_REGISTRATION( ObserverTest );
22
23/******************* Test stubs ************************/
24
25class UpdateCountObserver : public Observer {
26public:
27 UpdateCountObserver() :
28 updates(0)
29 {};
30 void update(Observable *publisher){
31 updates++;
32 }
33 void subjectKilled(Observable *publisher) {
34 }
35 int updates;
36};
37
38class SimpleObservable : public Observable {
39public:
40 void changeMethod() {
41 START_OBSERVER;
42 int i;
43 i++;
44 FINISH_OBSERVER;
45 }
46};
47
48class CallObservable : public Observable {
49public:
50 void changeMethod1() {
51 START_OBSERVER;
52 int i;
53 i++;
54 FINISH_OBSERVER;
55 }
56
57 void changeMethod2() {
58 START_OBSERVER;
59 int i;
60 i++;
61 changeMethod1();
62 FINISH_OBSERVER;
63 }
64};
65
66class SuperObservable : public Observable {
67public:
68 SuperObservable(){
69 subObservable = new SimpleObservable();
70 subObservable->signOn(this);
71 }
72 ~SuperObservable(){
73 delete subObservable;
74 }
75 void changeMethod() {
76 START_OBSERVER;
77 int i;
78 i++;
79 subObservable->changeMethod();
80 FINISH_OBSERVER;
81 }
82 SimpleObservable *subObservable;
83};
84
85/******************* actuall tests ***************/
86
87void ObserverTest::setUp() {
88 simpleObservable = new SimpleObservable();
89 callObservable = new CallObservable();
90 superObservable = new SuperObservable();
91
92 observer1 = new UpdateCountObserver();
93 observer2 = new UpdateCountObserver();
94 observer3 = new UpdateCountObserver();
95}
96
97void ObserverTest::tearDown() {
98 delete simpleObservable;
99 delete callObservable;
100 delete superObservable;
101
102 delete observer1;
103 delete observer2;
104 delete observer3;
105}
106
107void ObserverTest::doesUpdateTest()
108{
109 simpleObservable->signOn(observer1);
110 simpleObservable->signOn(observer2);
111 simpleObservable->signOn(observer3);
112
113 simpleObservable->changeMethod();
114 CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
115 CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
116 CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
117
118 simpleObservable->signOff(observer3);
119
120 simpleObservable->changeMethod();
121 CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
122 CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
123 CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
124}
125
126
127void ObserverTest::doesBlockUpdateTest() {
128 callObservable->signOn(observer1);
129
130 callObservable->changeMethod1();
131 CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
132
133 callObservable->changeMethod2();
134 CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
135}
136
137void ObserverTest::doesSubObservableTest() {
138 superObservable->signOn(observer1);
139 superObservable->subObservable->signOn(observer2);
140
141 superObservable->subObservable->changeMethod();
142 CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
143 CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
144
145 superObservable->changeMethod();
146 CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
147 CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
148}
149
150
151void ObserverTest::CircleDetectionTest() {
152 cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
153 cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
154 cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
155 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;
156
157 cout << endl << endl << "The following error displayed by the observer framwork can be ignored" << endl;
158
159 // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
160 simpleObservable->signOn(simpleObservable);
161 simpleObservable->changeMethod();
162 // when we reach this line, although we broke the DAG assumption the circle check works fine
163 CPPUNIT_ASSERT(true);
164}
165
166/********************************************** Main routine **************************************/
167
168int main(int argc, char **argv)
169{
170 // Get the top level suite from the registry
171 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
172
173 // Adds the test to the list of test to run
174 CppUnit::TextUi::TestRunner runner;
175 runner.addTest( suite );
176
177 // Change the default outputter to a compiler error format outputter
178 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
179 std::cerr ) );
180 // Run the tests.
181 bool wasSucessful = runner.run();
182
183 // Return error code 1 if the one of test failed.
184 return wasSucessful ? 0 : 1;
185};
Note: See TracBrowser for help on using the repository browser.