Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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"
22#include "lib/util.hpp"
23
26
27
28namespace lib {
29namespace test{
30
31 using ::Test;
32 using util::isnil;
33 using lumiera::error::LUMIERA_ERROR_ITER_EXHAUST;
34
35
37
38
39 /****************************************************************/
45 class ScopedPtrVect_test : public Test
46 {
47
48 virtual void
49 run (Arg)
50 {
52 iterating();
53 detaching();
54 moving();
55 }
56
57
58
59 void
61 {
62 CHECK (0 == Dummy::checksum());
63 {
64 VectD holder;
65 CHECK (isnil (holder));
66 CHECK (0 == Dummy::checksum());
67
68 Dummy* ptr = new Dummy();
69 Dummy& ref = holder.manage (ptr);
70 CHECK (!isnil (holder));
71 CHECK (0 != Dummy::checksum());
72 CHECK (&ref==ptr);
73
74 holder.manage (new Dummy);
75 holder.manage (new Dummy);
76 CHECK (3 == holder.size());
77
78 holder.clear();
79 CHECK (0 == Dummy::checksum());
80 CHECK (isnil (holder));
81
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 holder.manage (new Dummy);
91 CHECK (9 == holder.size());
92 CHECK (0 < Dummy::checksum());
93 }
94 CHECK (0 == Dummy::checksum());
95 }
96
97
98 void
100 {
101 CHECK (0 == Dummy::checksum());
102 {
103 VectD holder;
104 for (int i=0; i<16; ++i)
105 holder.manage(new Dummy(i));
106
107 int check=0;
108 VectD::iterator ii = holder.begin();
109 while (ii)
110 {
111 CHECK (check == ii->getVal());
112 ++check;
113 ++ii;
114 }
115
116
117 // Test the const iterator
118 check = 0;
119 VectD::const_iterator cii = holder.begin();
120 while (cii)
121 {
122 CHECK (check == cii->getVal());
123 ++check;
124 ++cii;
125 }
126
127
128 // Verify correct behaviour of iteration end
129 CHECK (not holder.end());
130 CHECK (isnil (holder.end()));
131
132 VERIFY_ERROR (ITER_EXHAUST, *holder.end() );
133 VERIFY_ERROR (ITER_EXHAUST, ++holder.end() );
134
135 CHECK (ii == holder.end());
136 CHECK (cii == holder.end());
137 VERIFY_ERROR (ITER_EXHAUST, ++ii );
138 VERIFY_ERROR (ITER_EXHAUST, ++cii );
139
140 }
141 CHECK (0 == Dummy::checksum());
142 }
143
144
145 void
147 {
148 int id2, id3;
149 Dummy* extracted{nullptr};
150 CHECK (0 == Dummy::checksum());
151 {
152 VectD holder;
153 CHECK (0 == Dummy::checksum());
154 CHECK (isnil (holder));
155
156 holder.manage (new Dummy);
157 holder.manage (new Dummy);
158 holder.manage (new Dummy);
159 holder.manage (new Dummy);
160 holder.manage (new Dummy);
161 CHECK (5 == holder.size());
162 CHECK (0 < Dummy::checksum());
163
164 id2 = holder[2].getVal();
165 id3 = holder[3].getVal();
166
167 extracted = holder.detach(& holder[2]);
168 CHECK (id2 == extracted->getVal());
169 CHECK (id3 == holder[2].getVal());
170 CHECK (4 == holder.size());
171 }
172 CHECK (0 < Dummy::checksum()); // not all dummies are dead
173 CHECK (id2 == Dummy::checksum()); // #2 is alive!
174
175 extracted->setVal(id2+id3);
176 CHECK (id2+id3 == Dummy::checksum());
177
178 delete extracted;
179 CHECK (0 == Dummy::checksum());
180 }
181
182
183 void
185 { {
186 VectD org;
187 VectD left;
188 CHECK (0 == Dummy::checksum());
189
190 org.manage (new Dummy);
191 org.manage (new Dummy);
192 org.manage (new Dummy);
193
194 CHECK (not isnil (org));
195 CHECK ( isnil (left));
196 auto sum = Dummy::checksum();
197 CHECK (sum > 0);
198 int id0 = org[0].getVal(),
199 id1 = org[1].getVal(),
200 id2 = org[2].getVal();
201
202 // create by move
203 VectD right{std::move (org)};
204 CHECK ( isnil (org));
205 CHECK ( isnil (left));
206 CHECK (not isnil (right));
207 CHECK (sum == Dummy::checksum());
208
209 // move-assignment
210 left = std::move (right);
211 CHECK ( isnil (org));
212 CHECK (not isnil (left));
213 CHECK ( isnil (right));
214 CHECK (sum == Dummy::checksum());
215 CHECK (id0 == left[0].getVal());
216 CHECK (id1 == left[1].getVal());
217 CHECK (id2 == left[2].getVal());
218
219 }
220 CHECK (0 == Dummy::checksum());
221 }
222 };
223
224
225 LAUNCHER (ScopedPtrVect_test, "unit common");
226
227
228}} // namespace lib::test
229
Simple vector based collection of pointers, managing lifecycle of the pointed-to objects.
A Dummy object for tests.
static long & checksum()
ScopedPtrVect< Dummy > VectD
Implementation namespace for support and library code.
Test runner and basic definitions for tests.
bool isnil(lib::time::Duration const &dur)
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Managing lifecycle for a collection of objects.
A collection of frequently used helper functions to support unit testing.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
unittest helper code: test dummy objects to track instances.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...