Lumiera  0.pre.03
»edit your freedom«
testdummy.hpp
Go to the documentation of this file.
1 /*
2  TESTDUMMY.hpp - yet another test dummy for tracking ctor/dtor calls
3 
4  Copyright (C) Lumiera.org
5  2008, 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 
23 
32 #include "lib/nocopy.hpp"
33 #include "lib/test/event-log.hpp"
34 #include "lib/format-string.hpp"
35 #include "lib/util.hpp"
36 
37 #include <algorithm>
38 #include <limits>
39 
40 
41 namespace lib {
42 namespace test{
43 
44 
50  class Dummy
52  {
53  int val_;
54 
56  static long _local_checksum;
57  static bool _throw_in_ctor;
58 
59  public:
60  virtual ~Dummy()
61  {
62  checksum() -= val_;
63  }
64 
65  Dummy ()
66  : val_(1 + (rand() % 100000000))
67  { init(); }
68 
69  Dummy (int v)
70  : val_(v)
71  { init(); }
72 
73  Dummy (Dummy && oDummy)
74  : Dummy(0)
75  {
76  swap (*this, oDummy);
77  }
78 
79  Dummy&
80  operator= (Dummy && oDummy)
81  {
82  if (&oDummy != this)
83  swap (*this, oDummy);
84  return *this;
85  }
86 
87 
89  virtual long
90  acc (int i)
91  {
92  return val_+i;
93  }
94 
95  int
96  getVal() const
97  {
98  return val_;
99  }
100 
101  void
102  setVal (int newVal)
103  {
104  checksum() += newVal - val_;
105  val_ = newVal;
106  }
107 
108  friend void
109  swap (Dummy& dum1, Dummy& dum2)
110  {
111  std::swap (dum1.val_, dum2.val_);
112  }
113 
114  static long&
115  checksum()
116  {
117  return _local_checksum;
118  }
119 
120  static void
121  activateCtorFailure(bool indeed =true)
122  {
123  _throw_in_ctor = indeed;
124  }
125 
126 
127  private:
128  void
129  init()
130  {
131  checksum() += val_;
132  if (_throw_in_ctor)
133  throw val_;
134  }
135  };
136 
137 
138 
139 
145  struct Tracker
146  {
147  static lib::test::EventLog log;
148 
149  static constexpr int DEFUNCT = std::numeric_limits<int>::min();
150  static constexpr int DEAD = std::numeric_limits<int>::max();
151 
152  int val;
153 
154  ~Tracker()
155  {
156  log.call (this,"dtor", val);
157  val = DEAD;
158  }
159 
160  Tracker()
161  : val{rand() % 1000}
162  {
163  log.call (this,"ctor");
164  }
165 
166  Tracker(int v)
167  : val{v}
168  {
169  log.call (this,"ctor", v);
170  }
171 
172  Tracker(Tracker const& ol)
173  : val{ol.val}
174  {
175  log.call (this,"ctor-copy", ol);
176  }
177 
178  Tracker(Tracker && oo)
179  : val{oo.val}
180  {
181  log.call (this,"ctor-move", oo);
182  oo.val = DEFUNCT;
183  }
184 
185  Tracker&
186  operator= (Tracker const& ol)
187  {
188  if (util::isSameObject (*this, ol))
189  {
190  log.call (this, "self-assign-copy");
191  }
192  else
193  {
194  log.call (this, "assign-copy", ol);
195  val = ol.val;
196  }
197  return *this;
198  }
199 
200  Tracker&
201  operator= (Tracker && oo)
202  {
203  if (util::isSameObject (*this, oo))
204  {
205  log.call (this, "self-assign-move");
206  }
207  else
208  {
209  log.call (this, "assign-move", oo);
210  val = oo.val;
211  oo.val = DEFUNCT;
212  }
213  return *this;
214  }
215 
216  operator string() const
217  {
218  return util::_Fmt{"Track{%02d}"} % val;
219  }
220 
221  friend void
222  swap (Tracker& t1, Tracker& t2)
223  {
224  log.call ("static", "swap", t1, t2);
225  std::swap (t1.val, t2.val);
226  }
227  };
228 
229 
230 
231 }} // namespace lib::test
232 
static long _local_checksum
to verify ctor/dtor calls
Definition: testdummy.hpp:56
Types marked with this mix-in may be moved and move-assigned.
Definition: nocopy.hpp:72
Support for verifying the occurrence of events from unit tests.
Definition: run.hpp:49
virtual long acc(int i)
a dummy API operation
Definition: testdummy.hpp:90
Helper to log and verify the occurrence of events.
Definition: event-log.hpp:284
Front-end for printf-style string template interpolation.
A front-end for using printf-style formatting.
friend void swap(Dummy &dum1, Dummy &dum2)
Definition: testdummy.hpp:109
Implementation namespace for support and library code.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
A tracking Dummy object for tests.
Definition: testdummy.hpp:145
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
EventLog & call(string target, string function)
Log occurrence of a function call with no arguments.
Definition: event-log.cpp:699
A Dummy object for tests.
Definition: testdummy.hpp:50