Lumiera  0.pre.03
»edit your freedom«
scoped-ptrvect-test.cpp
Go to the documentation of this file.
1 /*
2  ScopedPtrVect(Test) - holding and owning a collection of noncopyable objects
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 
20 #include "lib/test/run.hpp"
21 #include "lib/test/test-helper.hpp"
22 #include "lib/util.hpp"
23 
24 #include "lib/scoped-ptrvect.hpp"
26 
27 
28 namespace lib {
29 namespace test{
30 
31  using ::Test;
32  using util::isnil;
33  using lumiera::error::LUMIERA_ERROR_ITER_EXHAUST;
34 
35 
36  typedef ScopedPtrVect<Dummy> VectD;
37 
38 
39  /****************************************************************/
45  class ScopedPtrVect_test : public Test
46  {
47 
48  virtual void
49  run (Arg)
50  {
51  simpleUsage();
52  iterating();
53  detaching();
54  }
55 
56 
57 
58  void
59  simpleUsage()
60  {
61  CHECK (0 == Dummy::checksum());
62  {
63  VectD holder;
64  CHECK (isnil (holder));
65  CHECK (0 == Dummy::checksum());
66 
67  Dummy* ptr = new Dummy();
68  Dummy& ref = holder.manage (ptr);
69  CHECK (!isnil (holder));
70  CHECK (0 != Dummy::checksum());
71  CHECK (&ref==ptr);
72 
73  holder.manage (new Dummy);
74  holder.manage (new Dummy);
75  CHECK (3 == holder.size());
76 
77  holder.clear();
78  CHECK (0 == Dummy::checksum());
79  CHECK (isnil (holder));
80 
81  holder.manage (new Dummy);
82  holder.manage (new Dummy);
83  holder.manage (new Dummy);
84  holder.manage (new Dummy);
85  holder.manage (new Dummy);
86  holder.manage (new Dummy);
87  holder.manage (new Dummy);
88  holder.manage (new Dummy);
89  holder.manage (new Dummy);
90  CHECK (9 == holder.size());
91  CHECK (0 < Dummy::checksum());
92  }
93  CHECK (0 == Dummy::checksum());
94  }
95 
96 
97  void
98  iterating()
99  {
100  CHECK (0 == Dummy::checksum());
101  {
102  VectD holder;
103  for (int i=0; i<16; ++i)
104  holder.manage(new Dummy(i));
105 
106  int check=0;
107  VectD::iterator ii = holder.begin();
108  while (ii)
109  {
110  CHECK (check == ii->getVal());
111  ++check;
112  ++ii;
113  }
114 
115 
116  // Test the const iterator
117  check = 0;
118  VectD::const_iterator cii = holder.begin();
119  while (cii)
120  {
121  CHECK (check == cii->getVal());
122  ++check;
123  ++cii;
124  }
125 
126 
127  // Verify correct behaviour of iteration end
128  CHECK (! (holder.end()));
129  CHECK (isnil (holder.end()));
130 
131  VERIFY_ERROR (ITER_EXHAUST, *holder.end() );
132  VERIFY_ERROR (ITER_EXHAUST, ++holder.end() );
133 
134  CHECK (ii == holder.end());
135  CHECK (cii == holder.end());
136  VERIFY_ERROR (ITER_EXHAUST, ++ii );
137  VERIFY_ERROR (ITER_EXHAUST, ++cii );
138 
139  }
140  CHECK (0 == Dummy::checksum());
141  }
142 
143 
144  void
145  detaching()
146  {
147  int id2, id3;
148  Dummy* extracted(0);
149  CHECK (0 == Dummy::checksum());
150  {
151  VectD holder;
152  CHECK (0 == Dummy::checksum());
153  CHECK (isnil (holder));
154 
155  holder.manage (new Dummy);
156  holder.manage (new Dummy);
157  holder.manage (new Dummy);
158  holder.manage (new Dummy);
159  holder.manage (new Dummy);
160  CHECK (5 == holder.size());
161  CHECK (0 < Dummy::checksum());
162 
163  id2 = holder[2].getVal();
164  id3 = holder[3].getVal();
165 
166  extracted = holder.detach(& holder[2]);
167  CHECK (id2 == extracted->getVal());
168  CHECK (id3 == holder[2].getVal());
169  CHECK (4 == holder.size());
170  }
171  CHECK (0 < Dummy::checksum()); // not all dummies are dead
172  CHECK (id2 == Dummy::checksum()); // #2 is alive!
173 
174  extracted->setVal(id2+id3);
175  CHECK (id2+id3 == Dummy::checksum());
176 
177  delete extracted;
178  CHECK (0 == Dummy::checksum());
179  }
180  };
181 
182 
183  LAUNCHER (ScopedPtrVect_test, "unit common");
184 
185 
186 }} // namespace lib::test
187 
wrapper for an existing Iterator type, automatically dereferencing the output of the former...
Definition: run.hpp:40
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Managing lifecycle for a collection of objects.
Implementation namespace for support and library code.
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
unittest helper code: test dummy objects to track instances.
A collection of frequently used helper functions to support unit testing.
T * detach(void *objAddress)
withdraw responsibility for a specific object.
A Dummy object for tests.
T & manage(T *obj)
take ownership of the given object, adding it at the end of the collection