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