source: src/unittests/ActionSequenceTest.cpp@ fc1b24

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

Added a central registry that allows access to actions by name.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * ActionSequenzTest.cpp
3 *
4 * Created on: Dec 17, 2009
5 * Author: crueger
6 */
7
8#include <cppunit/CompilerOutputter.h>
9#include <cppunit/extensions/TestFactoryRegistry.h>
10#include <cppunit/ui/text/TestRunner.h>
11
12#include "unittests/ActionSequenceTest.hpp"
13#include "Actions/Action.hpp"
14#include "Actions/ActionSequence.hpp"
15
16// Registers the fixture into the 'registry'
17CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
18
19void setUp();
20void tearDown();
21
22void canUndoTest();
23
24/* some neccessary stubs for tests */
25class canUndoActionStub : public Action
26{
27public:
28 canUndoActionStub(): Action("canUndoActionStub",false){}
29 virtual ~canUndoActionStub(){}
30
31 virtual void call(){}
32 virtual void undo(){}
33 virtual bool canUndo(){
34 return true;
35 }
36};
37
38class cannotUndoActionStub : public Action
39{
40public:
41 cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
42 virtual ~cannotUndoActionStub(){}
43
44 virtual void call(){}
45 virtual void undo(){}
46 virtual bool canUndo(){
47 return false;
48 }
49};
50
51class wasCalledActionStub : public Action
52{
53public:
54 wasCalledActionStub() :
55 Action("wasCalledActionStub",false),
56 called(false)
57 {}
58 virtual ~wasCalledActionStub(){}
59
60 virtual void call(){
61 called = true;
62 }
63 virtual void undo(){
64 called = false;
65 }
66 virtual bool canUndo(){
67 return true;
68 }
69 bool wasCalled(){
70 return called;
71 }
72private:
73 bool called;
74};
75
76void ActionSequenceTest::setUp(){
77 // create some necessary stubs used in this test
78 positive1 = new canUndoActionStub();
79 positive2 = new canUndoActionStub();
80 negative1 = new cannotUndoActionStub();
81 negative2 = new cannotUndoActionStub();
82
83 shouldCall1 = new wasCalledActionStub();
84 shouldCall2 = new wasCalledActionStub();
85 shouldNotCall1 = new wasCalledActionStub();
86 shouldNotCall2 = new wasCalledActionStub();
87
88}
89
90void ActionSequenceTest::tearDown(){
91 delete positive1;
92 delete positive2;
93 delete negative1;
94 delete negative2;
95
96 delete shouldCall1;
97 delete shouldCall2;
98 delete shouldNotCall1;
99 delete shouldNotCall2;
100
101}
102
103void ActionSequenceTest::canUndoTest(){
104 // first section:
105 {
106 // test some combinations
107 {
108 ActionSequence *sequence = new ActionSequence();
109 sequence->addAction(positive1);
110 sequence->addAction(positive2);
111 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
112 delete sequence;
113 }
114 {
115 ActionSequence *sequence = new ActionSequence();
116 sequence->addAction(positive1);
117 sequence->addAction(negative2);
118 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
119 delete sequence;
120 }
121 {
122 ActionSequence *sequence = new ActionSequence();
123 sequence->addAction(negative1);
124 sequence->addAction(positive2);
125 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
126 delete sequence;
127 }
128 {
129 ActionSequence *sequence = new ActionSequence();
130 sequence->addAction(negative1);
131 sequence->addAction(negative2);
132 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
133 delete sequence;
134 }
135 }
136
137 // second section:
138 {
139 // empty sequence can be undone
140 ActionSequence *sequence = new ActionSequence();
141 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
142 // if only a positive action is contained it can be undone
143 sequence->addAction(positive1);
144 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
145 // the single negative action should block the process
146 sequence->addAction(negative1);
147 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
148 // after removing the negative action all is well again
149 sequence->removeLastAction();
150 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
151 delete sequence;
152 }
153}
154
155void ActionSequenceTest::doesCallTest(){
156 ActionSequence *sequence = new ActionSequence();
157 sequence->addAction(shouldCall1);
158 sequence->addAction(shouldCall2);
159 sequence->addAction(shouldNotCall1);
160 sequence->addAction(shouldNotCall2);
161 sequence->removeLastAction();
162 sequence->removeLastAction();
163
164 sequence->callAll();
165
166 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
167 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
168 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
169 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
170
171}
172
173void ActionSequenceTest::doesUndoTest(){
174 ActionSequence *sequence = new ActionSequence();
175 sequence->addAction(shouldNotCall1);
176 sequence->addAction(shouldNotCall2);
177 sequence->addAction(shouldCall1);
178 sequence->addAction(shouldCall2);
179
180 sequence->callAll();
181
182 sequence->removeLastAction();
183 sequence->removeLastAction();
184
185 sequence->undoAll();
186
187 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
188 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
189 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
190 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
191
192}
193
194
195/********************************************** Main routine **************************************/
196
197int main(int argc, char **argv)
198{
199 // Get the top level suite from the registry
200 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
201
202 // Adds the test to the list of test to run
203 CppUnit::TextUi::TestRunner runner;
204 runner.addTest( suite );
205
206 // Change the default outputter to a compiler error format outputter
207 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
208 std::cerr ) );
209 // Run the tests.
210 bool wasSucessful = runner.run();
211
212 // Return error code 1 if the one of test failed.
213 return wasSucessful ? 0 : 1;
214};
Note: See TracBrowser for help on using the repository browser.