source: src/Actions/unittests/ActionSequenceUnitTest.cpp@ 46b181

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 46b181 was 46b181, checked in by Frederik Heber <heber@…>, 11 years ago

Added Action::outputAsCLI() and ActionQueue::outputAsCLI().

  • prints sequence of Actions along with their options.
  • Property mode set to 100644
File size: 8.1 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[bcf653]22 */
23
[147339]24/*
[f7c0c4]25 * ActionSequenceUnitTest.cpp
[147339]26 *
27 * Created on: Dec 17, 2009
28 * Author: crueger
29 */
30
[bf3817]31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
[147339]36#include <cppunit/CompilerOutputter.h>
37#include <cppunit/extensions/TestFactoryRegistry.h>
38#include <cppunit/ui/text/TestRunner.h>
39
[f7c0c4]40#include "ActionSequenceUnitTest.hpp"
[147339]41#include "Actions/Action.hpp"
[628577]42#include "Actions/ActionQueue.hpp"
[147339]43#include "Actions/ActionSequence.hpp"
[2efa90]44#include "Actions/MakroAction.hpp"
[147339]45
[fdcd1b]46#include "stubs/DummyUI.hpp"
[112f90]47
[ce7fdc]48using namespace MoleCuilder;
49
[9b6b2f]50#ifdef HAVE_TESTRUNNER
51#include "UnitTestMain.hpp"
52#endif /*HAVE_TESTRUNNER*/
53
54/********************************************** Test classes **************************************/
55
[147339]56// Registers the fixture into the 'registry'
57CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
58
59/* some neccessary stubs for tests */
60class canUndoActionStub : public Action
61{
62public:
[3139b2]63 canUndoActionStub(const ActionTrait &_trait):
[126867]64 Action(_trait){}
[147339]65 virtual ~canUndoActionStub(){}
66
[047878]67 virtual Dialog* fillDialog(Dialog *dialog){
68 ASSERT(dialog,"No Dialog given when filling action dialog");
69 return dialog;
[80951de]70 }
71
[b5b01e]72 virtual ActionState::ptr performCall(){
[67e2b3]73 return Action::success;
74 }
[b5b01e]75 virtual ActionState::ptr performUndo(ActionState::ptr){
[67e2b3]76 return Action::success;
77 }
[b5b01e]78 virtual ActionState::ptr performRedo(ActionState::ptr){
[67e2b3]79 return Action::success;
80 }
[147339]81 virtual bool canUndo(){
82 return true;
83 }
[67e2b3]84 virtual bool shouldUndo(){
85 return true;
86 }
[46b181]87 void outputAsCLI(std::ostream &ost) const
88 {}
[147339]89};
90
91class cannotUndoActionStub : public Action
92{
93public:
[3139b2]94 cannotUndoActionStub(const ActionTrait &_trait) :
[126867]95 Action(_trait){}
[147339]96 virtual ~cannotUndoActionStub(){}
97
[047878]98 virtual Dialog* fillDialog(Dialog *dialog){
99 ASSERT(dialog,"No Dialog given when filling action dialog");
100 return dialog;
[80951de]101 }
102
[b5b01e]103 virtual ActionState::ptr performCall(){
[67e2b3]104 return Action::success;
105 }
[b5b01e]106 virtual ActionState::ptr performUndo(ActionState::ptr){
[67e2b3]107 return Action::success;
108 }
[b5b01e]109 virtual ActionState::ptr performRedo(ActionState::ptr){
[67e2b3]110 return Action::success;
111 }
[147339]112 virtual bool canUndo(){
113 return false;
114 }
[67e2b3]115 virtual bool shouldUndo(){
116 return true;
117 }
[46b181]118 void outputAsCLI(std::ostream &ost) const
119 {}
[147339]120};
121
[0229f9]122class wasCalledActionStub : public Action
123{
124public:
[3139b2]125 wasCalledActionStub(const ActionTrait &_trait) :
[126867]126 Action(_trait),
[0229f9]127 called(false)
128 {}
129 virtual ~wasCalledActionStub(){}
[147339]130
[047878]131 virtual Dialog* fillDialog(Dialog *dialog){
132 return dialog;
[80951de]133 }
[b5b01e]134 virtual ActionState::ptr performCall(){
[0229f9]135 called = true;
[67e2b3]136 return Action::success;
[0229f9]137 }
[b5b01e]138 virtual ActionState::ptr performUndo(ActionState::ptr){
[0229f9]139 called = false;
[67e2b3]140 return Action::success;
141 }
[b5b01e]142 virtual ActionState::ptr performRedo(ActionState::ptr){
[67e2b3]143 called = true;
144 return Action::success;
[0229f9]145 }
146 virtual bool canUndo(){
147 return true;
148 }
[67e2b3]149 virtual bool shouldUndo(){
150 return true;
151 }
[46b181]152 void outputAsCLI(std::ostream &ost) const
153 {}
[0229f9]154 bool wasCalled(){
155 return called;
156 }
157private:
158 bool called;
159};
[147339]160
[0229f9]161void ActionSequenceTest::setUp(){
[694d84]162 hasDescriptor = false;
[6367dd]163 // setup ActionHistory
164 ActionQueue::getInstance();
[112f90]165 // TODO: find a way to really reset the factory to a clean state in tear-down
166 if(!hasDescriptor){
167 UIFactory::registerFactory(new DummyUIFactory::description());
168 hasDescriptor = true;
169 }
170 UIFactory::makeUserInterface("Dummy");
[147339]171 // create some necessary stubs used in this test
[3139b2]172 ActionTrait canUndoTrait("canUndoActionStub");
173 ActionTrait cannotUndoTrait("cannotUndoActionStub");
[e4afb4]174 positive1 = new canUndoActionStub(canUndoTrait);
175 positive2 = new canUndoActionStub(canUndoTrait);
176 negative1 = new cannotUndoActionStub(cannotUndoTrait);
177 negative2 = new cannotUndoActionStub(cannotUndoTrait);
178
[3139b2]179 ActionTrait wasCalledTrait("wasCalledActionStub");
[e4afb4]180 shouldCall1 = new wasCalledActionStub(wasCalledTrait);
181 shouldCall2 = new wasCalledActionStub(wasCalledTrait);
182 shouldNotCall1 = new wasCalledActionStub(wasCalledTrait);
183 shouldNotCall2 = new wasCalledActionStub(wasCalledTrait);
[0229f9]184
185}
186
187void ActionSequenceTest::tearDown(){
188 delete positive1;
189 delete positive2;
190 delete negative1;
191 delete negative2;
[147339]192
[0229f9]193 delete shouldCall1;
194 delete shouldCall2;
195 delete shouldNotCall1;
196 delete shouldNotCall2;
197
[628577]198 ActionQueue::purgeInstance();
[694d84]199 {
200 UIFactory::purgeInstance();
201 hasDescriptor = false;
202 }
[0229f9]203}
204
205void ActionSequenceTest::canUndoTest(){
[147339]206 // first section:
207 {
208 // test some combinations
209 {
210 ActionSequence *sequence = new ActionSequence();
211 sequence->addAction(positive1);
212 sequence->addAction(positive2);
213 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
214 delete sequence;
215 }
216 {
217 ActionSequence *sequence = new ActionSequence();
218 sequence->addAction(positive1);
219 sequence->addAction(negative2);
220 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
221 delete sequence;
222 }
223 {
224 ActionSequence *sequence = new ActionSequence();
225 sequence->addAction(negative1);
226 sequence->addAction(positive2);
227 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
228 delete sequence;
229 }
230 {
231 ActionSequence *sequence = new ActionSequence();
232 sequence->addAction(negative1);
233 sequence->addAction(negative2);
234 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
235 delete sequence;
236 }
237 }
238
239 // second section:
240 {
241 // empty sequence can be undone
242 ActionSequence *sequence = new ActionSequence();
243 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
244 // if only a positive action is contained it can be undone
245 sequence->addAction(positive1);
246 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
247 // the single negative action should block the process
248 sequence->addAction(negative1);
249 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
250 // after removing the negative action all is well again
251 sequence->removeLastAction();
252 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
253 delete sequence;
254 }
[0229f9]255}
[147339]256
[0229f9]257void ActionSequenceTest::doesCallTest(){
258 ActionSequence *sequence = new ActionSequence();
259 sequence->addAction(shouldCall1);
260 sequence->addAction(shouldCall2);
261 sequence->addAction(shouldNotCall1);
262 sequence->addAction(shouldNotCall2);
263 sequence->removeLastAction();
264 sequence->removeLastAction();
265
266 sequence->callAll();
267
268 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
269 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
270 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
271 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
272
[f59d81]273 delete sequence;
[0229f9]274}
275
276void ActionSequenceTest::doesUndoTest(){
277 ActionSequence *sequence = new ActionSequence();
[3139b2]278 ActionTrait wasCalledTrait("wasCalledActionStub");
[e4afb4]279 wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
280 wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
[2efa90]281 sequence->addAction(wasCalled1);
282 sequence->addAction(wasCalled2);
[0229f9]283
[3139b2]284 ActionTrait MakroTrait("Test MakroAction");
[126867]285 MakroAction act(MakroTrait,*sequence);
[0229f9]286
[2efa90]287 act.call();
[0229f9]288
[2efa90]289 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
[ec149d]290 CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
[0229f9]291
[6367dd]292 ActionQueue::getInstance().undoLast();
[2efa90]293
294 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
[ec149d]295 CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
[147339]296
[22b786]297 delete sequence;
[147339]298}
299
300
Note: See TracBrowser for help on using the repository browser.