source: src/unittests/ActionSequenceTest.cpp@ a295d1

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

Added detailed documentation for the Action class

  • Property mode set to 100644
File size: 5.9 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#include "Actions/MakroAction.hpp"
16#include "Actions/ActionHistory.hpp"
17#include "Actions/ActionRegistry.hpp"
18
19#ifdef HAVE_TESTRUNNER
20#include "UnitTestMain.hpp"
21#endif /*HAVE_TESTRUNNER*/
22
23/********************************************** Test classes **************************************/
24
25// Registers the fixture into the 'registry'
26CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
27
28/* some neccessary stubs for tests */
29class canUndoActionStub : public Action
30{
31public:
32 canUndoActionStub(): Action("canUndoActionStub",false){}
33 virtual ~canUndoActionStub(){}
34
35 virtual Action::state_ptr performCall(){
36 return Action::success;
37 }
38 virtual Action::state_ptr performUndo(Action::state_ptr){
39 return Action::success;
40 }
41 virtual Action::state_ptr performRedo(Action::state_ptr){
42 return Action::success;
43 }
44 virtual bool canUndo(){
45 return true;
46 }
47 virtual bool shouldUndo(){
48 return true;
49 }
50};
51
52class cannotUndoActionStub : public Action
53{
54public:
55 cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
56 virtual ~cannotUndoActionStub(){}
57
58 virtual Action::state_ptr performCall(){
59 return Action::success;
60 }
61 virtual Action::state_ptr performUndo(Action::state_ptr){
62 return Action::success;
63 }
64 virtual Action::state_ptr performRedo(Action::state_ptr){
65 return Action::success;
66 }
67 virtual bool canUndo(){
68 return false;
69 }
70 virtual bool shouldUndo(){
71 return true;
72 }
73};
74
75class wasCalledActionStub : public Action
76{
77public:
78 wasCalledActionStub() :
79 Action("wasCalledActionStub",false),
80 called(false)
81 {}
82 virtual ~wasCalledActionStub(){}
83
84 virtual Action::state_ptr performCall(){
85 called = true;
86 return Action::success;
87 }
88 virtual Action::state_ptr performUndo(Action::state_ptr){
89 called = false;
90 return Action::success;
91 }
92 virtual Action::state_ptr performRedo(Action::state_ptr){
93 called = true;
94 return Action::success;
95 }
96 virtual bool canUndo(){
97 return true;
98 }
99 virtual bool shouldUndo(){
100 return true;
101 }
102 bool wasCalled(){
103 return called;
104 }
105private:
106 bool called;
107};
108
109void ActionSequenceTest::setUp(){
110 ActionHistory::init();
111 // create some necessary stubs used in this test
112 positive1 = new canUndoActionStub();
113 positive2 = new canUndoActionStub();
114 negative1 = new cannotUndoActionStub();
115 negative2 = new cannotUndoActionStub();
116
117 shouldCall1 = new wasCalledActionStub();
118 shouldCall2 = new wasCalledActionStub();
119 shouldNotCall1 = new wasCalledActionStub();
120 shouldNotCall2 = new wasCalledActionStub();
121
122}
123
124void ActionSequenceTest::tearDown(){
125 delete positive1;
126 delete positive2;
127 delete negative1;
128 delete negative2;
129
130 delete shouldCall1;
131 delete shouldCall2;
132 delete shouldNotCall1;
133 delete shouldNotCall2;
134
135 ActionHistory::purgeInstance();
136 ActionRegistry::purgeInstance();
137}
138
139void ActionSequenceTest::canUndoTest(){
140 // first section:
141 {
142 // test some combinations
143 {
144 ActionSequence *sequence = new ActionSequence();
145 sequence->addAction(positive1);
146 sequence->addAction(positive2);
147 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
148 delete sequence;
149 }
150 {
151 ActionSequence *sequence = new ActionSequence();
152 sequence->addAction(positive1);
153 sequence->addAction(negative2);
154 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
155 delete sequence;
156 }
157 {
158 ActionSequence *sequence = new ActionSequence();
159 sequence->addAction(negative1);
160 sequence->addAction(positive2);
161 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
162 delete sequence;
163 }
164 {
165 ActionSequence *sequence = new ActionSequence();
166 sequence->addAction(negative1);
167 sequence->addAction(negative2);
168 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
169 delete sequence;
170 }
171 }
172
173 // second section:
174 {
175 // empty sequence can be undone
176 ActionSequence *sequence = new ActionSequence();
177 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
178 // if only a positive action is contained it can be undone
179 sequence->addAction(positive1);
180 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
181 // the single negative action should block the process
182 sequence->addAction(negative1);
183 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
184 // after removing the negative action all is well again
185 sequence->removeLastAction();
186 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
187 delete sequence;
188 }
189}
190
191void ActionSequenceTest::doesCallTest(){
192 ActionSequence *sequence = new ActionSequence();
193 sequence->addAction(shouldCall1);
194 sequence->addAction(shouldCall2);
195 sequence->addAction(shouldNotCall1);
196 sequence->addAction(shouldNotCall2);
197 sequence->removeLastAction();
198 sequence->removeLastAction();
199
200 sequence->callAll();
201
202 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
203 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
204 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
205 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
206
207 delete sequence;
208}
209
210void ActionSequenceTest::doesUndoTest(){
211 ActionSequence *sequence = new ActionSequence();
212 wasCalledActionStub *wasCalled1 = new wasCalledActionStub();
213 wasCalledActionStub *wasCalled2 = new wasCalledActionStub();
214 sequence->addAction(wasCalled1);
215 sequence->addAction(wasCalled2);
216
217 MakroAction act("Test MakroAction",sequence,false);
218
219 act.call();
220
221 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
222 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
223
224 ActionHistory::getInstance().undoLast();
225
226 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
227 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
228
229}
230
231
Note: See TracBrowser for help on using the repository browser.