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