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)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
22 
23 #include <functional>
24 
25 using std::function;
26 using std::bind;
27 
28 
29 namespace steam {
30 namespace control {
31 namespace test {
32 
33  namespace {
34 
35  int testVal=0;
36 
37  void
38  undo (short param, int memento)
39  {
40  testVal += param-memento;
41  }
42 
43  int
44  capture (short param)
45  {
46  return testVal+param;
47  }
48 
49 
53  const size_t ALIGNMENT = sizeof(size_t);
54  }
55 
56  using LERR_(MISSING_MEMENTO);
57 
58 
59 
60  /*************************************************************************************/
71  class MementoTie_test : public Test
72  {
73 
74  virtual void
75  run (Arg)
76  {
77  seedRand();
78  checkStateCapturingMechanism();
79  }
80 
81 
83  typedef void OpSIG(short);
85 
86 
87  void
88  checkStateCapturingMechanism ()
89  {
90  function<void(short,int)> undo_func = undo;
91  function< int(short)> cap_func = capture;
92 
93  MemHolder mementoHolder (undo_func,cap_func);
94 
95  CHECK (sizeof(MemHolder) <= sizeof(int) // storage for the memento
96  + 2 * sizeof(function<void()>) // storage for the 2 undecorated functors
97  + ALIGNMENT);
98 
99  function<OpSIG> bound_undo_func = mementoHolder.tieUndoFunc();
100  function<OpSIG> bound_cap_func = mementoHolder.tieCaptureFunc();
101 
102  VERIFY_ERROR (MISSING_MEMENTO, bound_undo_func(123) );
103  VERIFY_ERROR (MISSING_MEMENTO, mementoHolder.getState() );
104 
105  short rr (rani (100));
106  testVal = 0;
107  bound_cap_func(rr); // invoke state capturing
108 
109  CHECK (rr == mementoHolder.getState());
110 
111  testVal = 10; // meanwhile "somehow" mutate the state
112  bound_undo_func(0); // invoking the undo() feeds back the memento
113  CHECK (testVal == 10-rr);
114 
115  // this cycle can be repeated with different state values
116  rr = rani (100);
117  testVal = rr;
118  bound_cap_func(5); // capture new state
119  CHECK (5+rr == mementoHolder.getState());
120 
121  testVal = -20;
122  bound_undo_func(3*rr);
123  CHECK (testVal == -20 + 3*rr - (5+rr));
124  }
125  };
126 
127 
129  LAUNCHER (MementoTie_test, "unit controller");
130 
131 
132 }}} // namespace steam::control::test
void OpSIG(short)
assumed signature of the Command "Operation"
Definition: run.hpp:40
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
#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.
Simplistic 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:79