Lumiera  0.pre.03
»edit your freedom«
del-stash-test.cpp
Go to the documentation of this file.
1 /*
2  DelStash(Test) - verify a facility to memorise and trigger deleter functions
3 
4  Copyright (C)
5  2010, 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/del-stash.hpp"
21 
22 
23 
24 namespace lib {
25 namespace test{
26 
27  namespace { // probe victims
28 
29  ulong MAX_MASS = 200; // number of victims to kill at once
30 
31  ulong checksum = 0;
32 
33 
34  template<uint siz>
35  class Probe
36  {
37  uint mySiz_;
38  char myCrap_[siz];
39 
40  public:
41  Probe()
42  : mySiz_(siz)
43  {
44  REQUIRE (siz);
45  for (uint i=0; i<siz; ++i)
46  {
47  char c (rani(256));
48  checksum += c;
49  myCrap_[i] = c;
50  }
51  }
52 
53  ~Probe()
54  {
55  CHECK (siz == mySiz_, "using wrong type information for de-allocation");
56  for (uint i=0; i<siz; ++i)
57  checksum -= myCrap_[i];
58  }
59  };
60 
61 
62  template<uint x>
63  inline Probe<x>*
64  makeViktim ()
65  {
66  return new Probe<x>();
67  }
68 
69  template<uint x>
70  inline void
71  feedViktim (DelStash& killer)
72  {
73  killer.manage (new Probe<x>());
74  }
75 
76 
77  }//(End) test data
78 
79 
80 
81 
82  /************************************************************************/
92  class DelStash_test : public Test
93  {
94 
95  virtual void
96  run (Arg)
97  {
98  seedRand();
99  checksum = 0;
100 
101  checkSingleKill();
102  checkCustomKill();
103  checkMassKill();
104  checkAutoKill();
105  }
106 
107 
108  void
109  checkSingleKill ()
110  {
111  DelStash killer;
112  CHECK (0 == killer.size());
113 
114  killer.manage<short> (NULL);
115  CHECK (0 == killer.size());
116 
117  Probe<5> *p = makeViktim<5>();
118  Probe<7> &r = *makeViktim<7>();
119  void *v = makeViktim<9>();
120  CHECK (0 < checksum);
121 
122  killer.manage (p);
123  killer.manage (r);
124  killer.manage (static_cast<Probe<9>*> (v));
125 
126  CHECK (3 == killer.size());
127 
128  killer.kill (r);
129  CHECK (2 == killer.size());
130 
131  killer.kill (p);
132  CHECK (1 == killer.size());
133 
134  killer.kill (p);
135  CHECK (1 == killer.size()); // spurious kill requests ignored
136 
137  killer.kill (v);
138  CHECK (0 == killer.size());
139  CHECK (0 == checksum);
140  }
141 
142 
143  void
144  feedViktims (DelStash& killer)
145  {
146  for (uint i=1; i <= MAX_MASS; ++i)
147  switch (i% 5) {
148  case 0: feedViktim<12> (killer); break;
149  case 1: feedViktim<23> (killer); break;
150  case 2: feedViktim<34> (killer); break;
151  case 3: feedViktim<45> (killer); break;
152  case 4: feedViktim<56> (killer); break;
153  }
154  }
155 
156 
157  void
158  checkMassKill ()
159  {
160  DelStash killer;
161  CHECK (0 == killer.size());
162 
163  CHECK (0 == checksum);
164  CHECK (0 == killer.size());
165 
166  feedViktims (killer);
167  CHECK (MAX_MASS == killer.size());
168 
169  killer.killAll();
170  CHECK (0 == killer.size());
171  CHECK (0 == checksum);
172  }
173 
174 
175  void
176  checkAutoKill()
177  {
178  {
179  DelStash killer;
180  CHECK (0 == killer.size());
181  CHECK (0 == checksum);
182 
183  feedViktims (killer);
184  Probe<444> * individuum = makeViktim<444>();
185  killer.manage (individuum);
186  feedViktims (killer);
187  killer.manage (makeViktim<5555>());
188  feedViktims (killer);
189 
190  CHECK (3*MAX_MASS + 2 == killer.size());
191 
192  killer.kill(individuum);
193  CHECK (3*MAX_MASS + 1 == killer.size());
194 
195  CHECK (0 < checksum);
196  }// killer going out of scope...
197 
198  CHECK (0 == checksum);
199  }
200 
201 
205  void
207  {
208  DelStash killer;
209  CHECK (0 == killer.size());
210 
216  class Special
217  : Probe<555>
218  {
219  char secret_;
220 
221  public:
222  Special()
223  : Probe<555>()
224  , secret_('a' + rani('z'-'a' +1))
225  {
226  checksum += secret_;
227  }
228 
229  static void
230  selfKill (void *it)
231  {
232  Special *self = static_cast<Special*> (it);
233  checksum -= self->secret_;
234  delete self;
235  }
236  };
237 
238 
239  void * type_erased = new Special();
240  CHECK (0 < checksum);
241 
242  killer.manage (type_erased, &Special::selfKill);
243  CHECK (1 == killer.size());
244 
245  killer.kill(type_erased);
246  CHECK (0 == killer.size());
247  CHECK (0 == checksum);
248  }
249  };
250 
251 
253  LAUNCHER (DelStash_test, "unit common");
254 
255 
256 }} // namespace lib::test
Definition: run.hpp:40
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
Collecting and finally triggering deleter functions.
Implementation namespace for support and library code.
Simplistic test class runner.
Manage a collection of deleter functions.
Definition: del-stash.hpp:56