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  Dummy ()
61  : val_(1 + (rand() % 100000000))
62  { init(); }
63 
64  Dummy (int v)
65  : val_(v)
66  { init(); }
67 
68  virtual ~Dummy()
69  {
70  checksum() -= val_;
71  }
72 
73  virtual long
74 
75  acc (int i)
76  {
77  return val_+i;
78  }
79 
80  int
81  getVal() const
82  {
83  return val_;
84  }
85 
86  void
87  setVal (int newVal)
88  {
89  checksum() += newVal - val_;
90  val_ = newVal;
91  }
92 
93  friend void
94  swap (Dummy& dum1, Dummy& dum2)
95  {
96  std::swap(dum1.val_, dum2.val_);
97  }
98 
99  static long&
100  checksum()
101  {
102  return _local_checksum;
103  }
104 
105  static void
106  activateCtorFailure(bool indeed =true)
107  {
108  _throw_in_ctor = indeed;
109  }
110 
111 
112  private:
113  void
114  init()
115  {
116  checksum() += val_;
117  if (_throw_in_ctor)
118  throw val_;
119  }
120  };
121 
122 
123 
124 
130  struct Tracker
131  {
132  static lib::test::EventLog log;
133 
134  static constexpr int DEFUNCT = std::numeric_limits<int>::min();
135  static constexpr int DEAD = std::numeric_limits<int>::max();
136 
137  int val;
138 
139  ~Tracker()
140  {
141  log.call (this,"dtor", val);
142  val = DEAD;
143  }
144 
145  Tracker()
146  : val{rand() % 1000}
147  {
148  log.call (this,"ctor");
149  }
150 
151  Tracker(int v)
152  : val{v}
153  {
154  log.call (this,"ctor", v);
155  }
156 
157  Tracker(Tracker const& ol)
158  : val{ol.val}
159  {
160  log.call (this,"ctor-copy", ol);
161  }
162 
163  Tracker(Tracker && oo)
164  : val{oo.val}
165  {
166  log.call (this,"ctor-move", oo);
167  oo.val = DEFUNCT;
168  }
169 
170  Tracker&
171  operator= (Tracker const& ol)
172  {
173  if (util::isSameObject (*this, ol))
174  {
175  log.call (this, "self-assign-copy");
176  }
177  else
178  {
179  log.call (this, "assign-copy", ol);
180  val = ol.val;
181  }
182  return *this;
183  }
184 
185  Tracker&
186  operator= (Tracker && oo)
187  {
188  if (util::isSameObject (*this, oo))
189  {
190  log.call (this, "self-assign-move");
191  }
192  else
193  {
194  log.call (this, "assign-move", oo);
195  val = oo.val;
196  oo.val = DEFUNCT;
197  }
198  return *this;
199  }
200 
201  operator string() const
202  {
203  return util::_Fmt{"Track{%02d}"} % val;
204  }
205 
206  friend void
207  swap (Tracker& t1, Tracker& t2)
208  {
209  log.call ("static", "swap", t1, t2);
210  std::swap (t1.val, t2.val);
211  }
212  };
213 
214 
215 
216 }} // namespace lib::test
217 
static long _local_checksum
to verify ctor/dtor calls
Definition: testdummy.hpp:56
Support for verifying the occurrence of events from unit tests.
Definition: run.hpp:49
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
virtual long acc(int i)
Definition: testdummy.hpp:75
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:94
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:130
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