Lumiera  0.pre.03
»edit your freedom«
memento-tie-test.cpp
Go to the documentation of this file.
1 /*
2  MementoTie(Test) - check the mechanism for capturing and providing undo-state
3 
4  Copyright (C) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
31 
32 #include <functional>
33 #include <cstdlib>
34 
35 using std::function;
36 using std::bind;
37 using std::rand;
38 
39 
40 namespace steam {
41 namespace control {
42 namespace test {
43 
44  namespace {
45 
46  int testVal=0;
47 
48  void
49  undo (short param, int memento)
50  {
51  testVal += param-memento;
52  }
53 
54  int
55  capture (short param)
56  {
57  return testVal+param;
58  }
59 
60 
64  const size_t ALIGNMENT = sizeof(size_t);
65  }
66 
67  using LERR_(MISSING_MEMENTO);
68 
69 
70 
71  /*************************************************************************************/
82  class MementoTie_test : public Test
83  {
84 
85  virtual void
86  run (Arg)
87  {
88  checkStateCapturingMechanism();
89  }
90 
91 
93  typedef void OpSIG(short);
95 
96 
97  void
98  checkStateCapturingMechanism ()
99  {
100  function<void(short,int)> undo_func = undo;
101  function< int(short)> cap_func = capture;
102 
103  MemHolder mementoHolder (undo_func,cap_func);
104 
105  CHECK (sizeof(MemHolder) <= sizeof(int) // storage for the memento
106  + 2 * sizeof(function<void()>) // storage for the 2 undecorated functors
107  + ALIGNMENT);
108 
109  function<OpSIG> bound_undo_func = mementoHolder.tieUndoFunc();
110  function<OpSIG> bound_cap_func = mementoHolder.tieCaptureFunc();
111 
112  VERIFY_ERROR (MISSING_MEMENTO, bound_undo_func(123) );
113  VERIFY_ERROR (MISSING_MEMENTO, mementoHolder.getState() );
114 
115  short rr (rand() %100);
116  testVal = 0;
117  bound_cap_func(rr); // invoke state capturing
118 
119  CHECK (rr == mementoHolder.getState());
120 
121  testVal = 10; // meanwhile "somehow" mutate the state
122  bound_undo_func(0); // invoking the undo() feeds back the memento
123  CHECK (testVal == 10-rr);
124 
125  // this cycle can be repeated with different state values
126  rr = (rand() %100);
127  testVal = rr;
128  bound_cap_func(5); // capture new state
129  CHECK (5+rr == mementoHolder.getState());
130 
131  testVal = -20;
132  bound_undo_func(3*rr);
133  CHECK (testVal == -20 + 3*rr - (5+rr));
134  }
135  };
136 
137 
139  LAUNCHER (MementoTie_test, "unit controller");
140 
141 
142 }}} // namespace steam::control::test
void OpSIG(short)
assumed signature of the Command "Operation"
Definition: run.hpp:49
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Steam-Layer implementation namespace root.
A special binding used by Steam-Layer commands for capturing UNDO state information.
Simple test class runner.
const size_t ALIGNMENT
maximum additional storage maybe wasted due to alignment of the memento value within MementoTie ...
A collection of frequently used helper functions to support unit testing.
Binding together state capturing and execution of the undo operation.
Definition: memento-tie.hpp:88