source: src/unittests/ActionSequenceTest.cpp@ 5f8660a

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 5f8660a was bcf653, checked in by Frederik Heber <heber@…>, 15 years ago

Added copyright note to each .cpp file and an extensive one to builder.cpp.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * ActionSequenzTest.cpp
10 *
11 * Created on: Dec 17, 2009
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include <cppunit/CompilerOutputter.h>
21#include <cppunit/extensions/TestFactoryRegistry.h>
22#include <cppunit/ui/text/TestRunner.h>
23
24#include "unittests/ActionSequenceTest.hpp"
25#include "Actions/Action.hpp"
26#include "Actions/ActionSequence.hpp"
27#include "Actions/MakroAction.hpp"
28#include "Actions/ActionHistory.hpp"
29#include "Actions/ActionRegistry.hpp"
30
31#include "DummyUI.hpp"
32
33#ifdef HAVE_TESTRUNNER
34#include "UnitTestMain.hpp"
35#endif /*HAVE_TESTRUNNER*/
36
37/********************************************** Test classes **************************************/
38
39// Registers the fixture into the 'registry'
40CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
41
42/* some neccessary stubs for tests */
43class canUndoActionStub : public Action
44{
45public:
46 canUndoActionStub(): Action("canUndoActionStub",false){}
47 virtual ~canUndoActionStub(){}
48
49 virtual Dialog* fillDialog(Dialog *dialog){
50 ASSERT(dialog,"No Dialog given when filling action dialog");
51 return dialog;
52 }
53
54 virtual Action::state_ptr performCall(){
55 return Action::success;
56 }
57 virtual Action::state_ptr performUndo(Action::state_ptr){
58 return Action::success;
59 }
60 virtual Action::state_ptr performRedo(Action::state_ptr){
61 return Action::success;
62 }
63 virtual bool canUndo(){
64 return true;
65 }
66 virtual bool shouldUndo(){
67 return true;
68 }
69};
70
71class cannotUndoActionStub : public Action
72{
73public:
74 cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
75 virtual ~cannotUndoActionStub(){}
76
77 virtual Dialog* fillDialog(Dialog *dialog){
78 ASSERT(dialog,"No Dialog given when filling action dialog");
79 return dialog;
80 }
81
82 virtual Action::state_ptr performCall(){
83 return Action::success;
84 }
85 virtual Action::state_ptr performUndo(Action::state_ptr){
86 return Action::success;
87 }
88 virtual Action::state_ptr performRedo(Action::state_ptr){
89 return Action::success;
90 }
91 virtual bool canUndo(){
92 return false;
93 }
94 virtual bool shouldUndo(){
95 return true;
96 }
97};
98
99class wasCalledActionStub : public Action
100{
101public:
102 wasCalledActionStub() :
103 Action("wasCalledActionStub",false),
104 called(false)
105 {}
106 virtual ~wasCalledActionStub(){}
107
108 virtual Dialog* fillDialog(Dialog *dialog){
109 return dialog;
110 }
111 virtual Action::state_ptr performCall(){
112 called = true;
113 return Action::success;
114 }
115 virtual Action::state_ptr performUndo(Action::state_ptr){
116 called = false;
117 return Action::success;
118 }
119 virtual Action::state_ptr performRedo(Action::state_ptr){
120 called = true;
121 return Action::success;
122 }
123 virtual bool canUndo(){
124 return true;
125 }
126 virtual bool shouldUndo(){
127 return true;
128 }
129 bool wasCalled(){
130 return called;
131 }
132private:
133 bool called;
134};
135
136void ActionSequenceTest::setUp(){
137 static bool hasDescriptor = false;
138 ActionHistory::init();
139 // TODO: find a way to really reset the factory to a clean state in tear-down
140 if(!hasDescriptor){
141 UIFactory::registerFactory(new DummyUIFactory::description());
142 hasDescriptor = true;
143 }
144 UIFactory::makeUserInterface("Dummy");
145 // create some necessary stubs used in this test
146 positive1 = new canUndoActionStub();
147 positive2 = new canUndoActionStub();
148 negative1 = new cannotUndoActionStub();
149 negative2 = new cannotUndoActionStub();
150
151 shouldCall1 = new wasCalledActionStub();
152 shouldCall2 = new wasCalledActionStub();
153 shouldNotCall1 = new wasCalledActionStub();
154 shouldNotCall2 = new wasCalledActionStub();
155
156}
157
158void ActionSequenceTest::tearDown(){
159 delete positive1;
160 delete positive2;
161 delete negative1;
162 delete negative2;
163
164 delete shouldCall1;
165 delete shouldCall2;
166 delete shouldNotCall1;
167 delete shouldNotCall2;
168
169 ActionHistory::purgeInstance();
170 ActionRegistry::purgeInstance();
171 UIFactory::purgeInstance();
172}
173
174void ActionSequenceTest::canUndoTest(){
175 // first section:
176 {
177 // test some combinations
178 {
179 ActionSequence *sequence = new ActionSequence();
180 sequence->addAction(positive1);
181 sequence->addAction(positive2);
182 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
183 delete sequence;
184 }
185 {
186 ActionSequence *sequence = new ActionSequence();
187 sequence->addAction(positive1);
188 sequence->addAction(negative2);
189 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
190 delete sequence;
191 }
192 {
193 ActionSequence *sequence = new ActionSequence();
194 sequence->addAction(negative1);
195 sequence->addAction(positive2);
196 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
197 delete sequence;
198 }
199 {
200 ActionSequence *sequence = new ActionSequence();
201 sequence->addAction(negative1);
202 sequence->addAction(negative2);
203 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
204 delete sequence;
205 }
206 }
207
208 // second section:
209 {
210 // empty sequence can be undone
211 ActionSequence *sequence = new ActionSequence();
212 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
213 // if only a positive action is contained it can be undone
214 sequence->addAction(positive1);
215 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
216 // the single negative action should block the process
217 sequence->addAction(negative1);
218 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
219 // after removing the negative action all is well again
220 sequence->removeLastAction();
221 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
222 delete sequence;
223 }
224}
225
226void ActionSequenceTest::doesCallTest(){
227 ActionSequence *sequence = new ActionSequence();
228 sequence->addAction(shouldCall1);
229 sequence->addAction(shouldCall2);
230 sequence->addAction(shouldNotCall1);
231 sequence->addAction(shouldNotCall2);
232 sequence->removeLastAction();
233 sequence->removeLastAction();
234
235 sequence->callAll();
236
237 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
238 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
239 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
240 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
241
242 delete sequence;
243}
244
245void ActionSequenceTest::doesUndoTest(){
246 ActionSequence *sequence = new ActionSequence();
247 wasCalledActionStub *wasCalled1 = new wasCalledActionStub();
248 wasCalledActionStub *wasCalled2 = new wasCalledActionStub();
249 sequence->addAction(wasCalled1);
250 sequence->addAction(wasCalled2);
251
252 MakroAction act("Test MakroAction",sequence,false);
253
254 act.call();
255
256 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
257 CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
258
259 ActionHistory::getInstance().undoLast();
260
261 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
262 CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
263
264}
265
266
Note: See TracBrowser for help on using the repository browser.