source: src/unittests/ActionSequenceTest.cpp@ e4afb4

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 v1.0.6
Last change on this file since e4afb4 was e4afb4, checked in by Frederik Heber <heber@…>, 14 years ago

Huge refactoring: Introduction of Traits to Actions.

This change is really big but the introduction of the Trait concept (at least
in its current light form) is so fundamental that lots of pieces had to be
changed in order to get everything working.

The main point why it was necessary to add these traits in the first place was
to comfortably allow for adding extension of Actions information-wise, i.e.
with stuff that is only important for the QtUI, such as icons, or tooltips, ...
This extra information should not be stored with Action itself, as it has
nothing to do with the workings of the Action. And neither should it get
stored with some blown-out-of-proportions MapOfActions class ...

The gist of the change is as follows:

  • OptionTrait contains the token, description, shortform and type of an option, such as ("position", "position in space, none, typeid(Vector)).
  • ActionTrait is the derived form for actions where additionally MenuPosition and MenuName are stored (and probably more to come for the GUI), also we have a set of OptionTrait instances, one for each option of the Action.
  • Action then contains this ActionTrait, specialized for each Action.
  • the preprocessor macros have been enhanced to gather all this information from the .def files.
  • MapOfActions is gone. Completely. Most of its use was to store this extra information and the ValueStorage part now is just in class ValueStorage.
  • ValueStorage is no more an interface to MapOfActions but as the name says a (type-safe) ValueStorage.

Listing the (remaining) changes in alphabetical order of the class:

  • Action
    • member value ::name dropped, ::getName() uses ActionTraits::getName()
    • new define NODEFAULT which is used in paramdefaults in .def files
    • all derived actions classes such as Process, Calculations, MakroAction,... have been adapated to use the ActionTrait concept as well.
  • ActionHistory
    • extraced RedoAction and UndoAction, shifted implementation into their own object files and they use .def files as well (i.e. streamlined with method used for other actions)
  • MenuDescription
    • contain information on Menus such as name, ...
    • new unit test checks for consistency
  • molecule
    • const member functions: Copy(), Output() and OutputBonds()
  • OptionRegistry
    • new registry class for options only
    • we want the same type throughout the code for each token, e.g. "position"
    • the registry containts checks for consistency
  • OptionTrait
    • default values are specified in paramdefaults, none are given by NODEFAULT
    • introduced default for translate-atoms, point-correlation, pair-correlation
  • Registry pattern
    • new unit test, but only sceleton code so far
  • ...Query, also ...Pipe
    • atoms, molecule and elements are now all const
    • also ValueStorage's signatures all have const therein
  • ValueStorage
    • set/queryCurrentValue from MapOfActions
    • at times VectorValue has been in .def files where Vector was in the signature. This is cleared. Such stuff is only present for e.g. BoxVector being queried as a Vector. But this is a feature and intended.
  • World
    • most of the (un)selection functions now work on const atoms and molecules
    • in one case we need a const_cast to remove this, but this is intentional, as the vector of selected atoms stores non-const pointers and this is ok.

There is only one test which had to be changed slightly because a specific
option token as "position" must now have the same type everywhere, e.g. always
Vector.

  • TESTFIX: Simple_configuration/2: --position -> --domain-position (and associated to BoxVector)
  • Property mode set to 100644
File size: 7.4 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(const ActionTraits &_trait):
47 Action(_trait,false){}
48 virtual ~canUndoActionStub(){}
49
50 virtual void getParametersfromValueStorage(){
51 }
52
53 virtual Dialog* fillDialog(Dialog *dialog){
54 ASSERT(dialog,"No Dialog given when filling action dialog");
55 return dialog;
56 }
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 true;
69 }
70 virtual bool shouldUndo(){
71 return true;
72 }
73};
74
75class cannotUndoActionStub : public Action
76{
77public:
78 cannotUndoActionStub(const ActionTraits &_trait) :
79 Action(_trait,false){}
80 virtual ~cannotUndoActionStub(){}
81
82 virtual void getParametersfromValueStorage(){
83 }
84
85 virtual Dialog* fillDialog(Dialog *dialog){
86 ASSERT(dialog,"No Dialog given when filling action dialog");
87 return dialog;
88 }
89
90 virtual Action::state_ptr performCall(){
91 return Action::success;
92 }
93 virtual Action::state_ptr performUndo(Action::state_ptr){
94 return Action::success;
95 }
96 virtual Action::state_ptr performRedo(Action::state_ptr){
97 return Action::success;
98 }
99 virtual bool canUndo(){
100 return false;
101 }
102 virtual bool shouldUndo(){
103 return true;
104 }
105};
106
107class wasCalledActionStub : public Action
108{
109public:
110 wasCalledActionStub(const ActionTraits &_trait) :
111 Action(_trait,false),
112 called(false)
113 {}
114 virtual ~wasCalledActionStub(){}
115
116 virtual void getParametersfromValueStorage(){
117 }
118
119 virtual Dialog* fillDialog(Dialog *dialog){
120 return dialog;
121 }
122 virtual Action::state_ptr performCall(){
123 called = true;
124 return Action::success;
125 }
126 virtual Action::state_ptr performUndo(Action::state_ptr){
127 called = false;
128 return Action::success;
129 }
130 virtual Action::state_ptr performRedo(Action::state_ptr){
131 called = true;
132 return Action::success;
133 }
134 virtual bool canUndo(){
135 return true;
136 }
137 virtual bool shouldUndo(){
138 return true;
139 }
140 bool wasCalled(){
141 return called;
142 }
143private:
144 bool called;
145};
146
147void ActionSequenceTest::setUp(){
148 static bool hasDescriptor = false;
149 ActionHistory::init();
150 // TODO: find a way to really reset the factory to a clean state in tear-down
151 if(!hasDescriptor){
152 UIFactory::registerFactory(new DummyUIFactory::description());
153 hasDescriptor = true;
154 }
155 UIFactory::makeUserInterface("Dummy");
156 // create some necessary stubs used in this test
157 ActionTraits canUndoTrait("canUndoActionStub");
158 ActionTraits cannotUndoTrait("cannotUndoActionStub");
159 positive1 = new canUndoActionStub(canUndoTrait);
160 positive2 = new canUndoActionStub(canUndoTrait);
161 negative1 = new cannotUndoActionStub(cannotUndoTrait);
162 negative2 = new cannotUndoActionStub(cannotUndoTrait);
163
164 ActionTraits wasCalledTrait("wasCalledActionStub");
165 shouldCall1 = new wasCalledActionStub(wasCalledTrait);
166 shouldCall2 = new wasCalledActionStub(wasCalledTrait);
167 shouldNotCall1 = new wasCalledActionStub(wasCalledTrait);
168 shouldNotCall2 = new wasCalledActionStub(wasCalledTrait);
169
170}
171
172void ActionSequenceTest::tearDown(){
173 delete positive1;
174 delete positive2;
175 delete negative1;
176 delete negative2;
177
178 delete shouldCall1;
179 delete shouldCall2;
180 delete shouldNotCall1;
181 delete shouldNotCall2;
182
183 ActionHistory::purgeInstance();
184 ActionRegistry::purgeInstance();
185 UIFactory::purgeInstance();
186}
187
188void ActionSequenceTest::canUndoTest(){
189 // first section:
190 {
191 // test some combinations
192 {
193 ActionSequence *sequence = new ActionSequence();
194 sequence->addAction(positive1);
195 sequence->addAction(positive2);
196 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
197 delete sequence;
198 }
199 {
200 ActionSequence *sequence = new ActionSequence();
201 sequence->addAction(positive1);
202 sequence->addAction(negative2);
203 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
204 delete sequence;
205 }
206 {
207 ActionSequence *sequence = new ActionSequence();
208 sequence->addAction(negative1);
209 sequence->addAction(positive2);
210 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
211 delete sequence;
212 }
213 {
214 ActionSequence *sequence = new ActionSequence();
215 sequence->addAction(negative1);
216 sequence->addAction(negative2);
217 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
218 delete sequence;
219 }
220 }
221
222 // second section:
223 {
224 // empty sequence can be undone
225 ActionSequence *sequence = new ActionSequence();
226 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
227 // if only a positive action is contained it can be undone
228 sequence->addAction(positive1);
229 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
230 // the single negative action should block the process
231 sequence->addAction(negative1);
232 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
233 // after removing the negative action all is well again
234 sequence->removeLastAction();
235 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
236 delete sequence;
237 }
238}
239
240void ActionSequenceTest::doesCallTest(){
241 ActionSequence *sequence = new ActionSequence();
242 sequence->addAction(shouldCall1);
243 sequence->addAction(shouldCall2);
244 sequence->addAction(shouldNotCall1);
245 sequence->addAction(shouldNotCall2);
246 sequence->removeLastAction();
247 sequence->removeLastAction();
248
249 sequence->callAll();
250
251 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
252 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
253 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
254 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
255
256 delete sequence;
257}
258
259void ActionSequenceTest::doesUndoTest(){
260 ActionSequence *sequence = new ActionSequence();
261 ActionTraits wasCalledTrait("wasCalledActionStub");
262 wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
263 wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
264 sequence->addAction(wasCalled1);
265 sequence->addAction(wasCalled2);
266
267 ActionTraits MakroTrait("Test MakroAction");
268 MakroAction act(MakroTrait,sequence,false);
269
270 act.call();
271
272 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
273 CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
274
275 ActionHistory::getInstance().undoLast();
276
277 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
278 CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
279
280}
281
282
Note: See TracBrowser for help on using the repository browser.