source: src/unittests/ActionSequenceTest.cpp@ 0229f9

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

Added more tests for Actionsequence

  • Test if every included object is called and every removed object is left alone
  • Test if every included object is undone and every removed object is left alone
  • Property mode set to 100644
File size: 5.3 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(){}
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(){}
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 called(false)
56 {}
57 virtual ~wasCalledActionStub(){}
58
59 virtual void call(){
60 called = true;
61 }
62 virtual void undo(){
63 called = false;
64 }
65 virtual bool canUndo(){
66 return true;
67 }
68 bool wasCalled(){
69 return called;
70 }
71private:
72 bool called;
73};
74
75void ActionSequenceTest::setUp(){
76 // create some necessary stubs used in this test
77 positive1 = new canUndoActionStub();
78 positive2 = new canUndoActionStub();
79 negative1 = new cannotUndoActionStub();
80 negative2 = new cannotUndoActionStub();
81
82 shouldCall1 = new wasCalledActionStub();
83 shouldCall2 = new wasCalledActionStub();
84 shouldNotCall1 = new wasCalledActionStub();
85 shouldNotCall2 = new wasCalledActionStub();
86
87}
88
89void ActionSequenceTest::tearDown(){
90 delete positive1;
91 delete positive2;
92 delete negative1;
93 delete negative2;
94
95 delete shouldCall1;
96 delete shouldCall2;
97 delete shouldNotCall1;
98 delete shouldNotCall2;
99
100}
101
102void ActionSequenceTest::canUndoTest(){
103 // first section:
104 {
105 // test some combinations
106 {
107 ActionSequence *sequence = new ActionSequence();
108 sequence->addAction(positive1);
109 sequence->addAction(positive2);
110 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
111 delete sequence;
112 }
113 {
114 ActionSequence *sequence = new ActionSequence();
115 sequence->addAction(positive1);
116 sequence->addAction(negative2);
117 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
118 delete sequence;
119 }
120 {
121 ActionSequence *sequence = new ActionSequence();
122 sequence->addAction(negative1);
123 sequence->addAction(positive2);
124 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
125 delete sequence;
126 }
127 {
128 ActionSequence *sequence = new ActionSequence();
129 sequence->addAction(negative1);
130 sequence->addAction(negative2);
131 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
132 delete sequence;
133 }
134 }
135
136 // second section:
137 {
138 // empty sequence can be undone
139 ActionSequence *sequence = new ActionSequence();
140 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
141 // if only a positive action is contained it can be undone
142 sequence->addAction(positive1);
143 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
144 // the single negative action should block the process
145 sequence->addAction(negative1);
146 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
147 // after removing the negative action all is well again
148 sequence->removeLastAction();
149 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
150 delete sequence;
151 }
152}
153
154void ActionSequenceTest::doesCallTest(){
155 ActionSequence *sequence = new ActionSequence();
156 sequence->addAction(shouldCall1);
157 sequence->addAction(shouldCall2);
158 sequence->addAction(shouldNotCall1);
159 sequence->addAction(shouldNotCall2);
160 sequence->removeLastAction();
161 sequence->removeLastAction();
162
163 sequence->callAll();
164
165 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
166 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
167 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
168 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
169
170}
171
172void ActionSequenceTest::doesUndoTest(){
173 ActionSequence *sequence = new ActionSequence();
174 sequence->addAction(shouldNotCall1);
175 sequence->addAction(shouldNotCall2);
176 sequence->addAction(shouldCall1);
177 sequence->addAction(shouldCall2);
178
179 sequence->callAll();
180
181 sequence->removeLastAction();
182 sequence->removeLastAction();
183
184 sequence->undoAll();
185
186 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
187 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
188 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
189 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
190
191}
192
193
194/********************************************** Main routine **************************************/
195
196int main(int argc, char **argv)
197{
198 // Get the top level suite from the registry
199 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
200
201 // Adds the test to the list of test to run
202 CppUnit::TextUi::TestRunner runner;
203 runner.addTest( suite );
204
205 // Change the default outputter to a compiler error format outputter
206 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
207 std::cerr ) );
208 // Run the tests.
209 bool wasSucessful = runner.run();
210
211 // Return error code 1 if the one of test failed.
212 return wasSucessful ? 0 : 1;
213};
Note: See TracBrowser for help on using the repository browser.