Lumiera  0.pre.03
»edit your freedom«
extent-family-test.cpp
Go to the documentation of this file.
1 /*
2  ExtentFamily(Test) - verify cyclic extents allocation scheme
3 
4  Copyright (C) Lumiera.org
5  2023, 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 
28 #include "lib/test/run.hpp"
30 #include "lib/iter-explorer.hpp"
31 #include "lib/util.hpp"
32 
33 #include <utility>
34 
35 using test::Test;
36 using util::isnil;
37 using util::isSameObject;
38 using lib::explore;
39 
40 
41 namespace vault{
42 namespace mem {
43 namespace test {
44 
45  using Extents = ExtentFamily<int, 10>;
46  using Extent = Extents::Extent;
47  using Iter = Extents::iterator;
48 
49 
50 
51 
52  /***************************************************************/
57  class ExtentFamily_test : public Test
58  {
59 
60  virtual void
61  run (Arg)
62  {
63  simpleUsage();
64  use_and_drop();
65  iteration();
66  reuseUnclean();
67  wrapAround();
68  }
69 
70 
73  void
75  {
76  Extents extents{5};
77  extents.openNew();
78  Extent& extent = *extents.begin();
79  CHECK (10 == extent.size());
80 
81  int num = rand() % 1000;
82  extent[2] = num;
83  extent[5] = num+5;
84  CHECK (num == extent[2]);
85  CHECK (num+5 == extent[5]);
86  }
87 
88 
89 
92  void
94  {
95  Extents extents{5};
96  CHECK ( 0 == watch(extents).first());
97  CHECK ( 0 == watch(extents).last());
98  CHECK ( 0 == watch(extents).active());
99  CHECK ( 5 == watch(extents).size());
100 
101  extents.openNew(3);
102  CHECK ( 0 == watch(extents).first());
103  CHECK ( 3 == watch(extents).last());
104  CHECK ( 3 == watch(extents).active());
105  CHECK ( 5 == watch(extents).size());
106 
107  extents.dropOld(2);
108  CHECK ( 2 == watch(extents).first());
109  CHECK ( 3 == watch(extents).last());
110  CHECK ( 1 == watch(extents).active());
111  CHECK ( 5 == watch(extents).size());
112  }
113 
114 
115 
119  void
121  {
122  Extents extents{5};
123  CHECK (isnil (extents));
124  Iter it = extents.begin();
125  CHECK (isnil (it)); // no extents provided yet
126 
127  extents.openNew(2); // allot two extents for active use
128  CHECK (it);
129  CHECK (0 == it.getIndex());
130  CHECK (isSameObject(*it, *extents.begin()));
131 
132  Extent& extent{*it};
133  CHECK (10 == extent.size());
134 
135  int num = rand() % 1000;
136  extent[2] = num;
137  CHECK (num == extent[2]);
138 
139  ++it;
140  CHECK (it);
141  CHECK (1 == it.getIndex());
142  Extent& nextEx{*it};
143  CHECK (not isSameObject(extent, nextEx));
144  CHECK (isSameObject(nextEx, *extents.last()));
145  nextEx[5] = extent[2] + 1;
146  CHECK (num == extent[2]);
147  CHECK (num+1 == nextEx[5]);
148 
149  ++it;
150  CHECK (it == extents.end());
151  CHECK (isnil (it)); // only two allocated
152  it.expandAlloc(); // but can expand allocation
153  CHECK (it);
154 
155  // iterate again to verify we get the same memory blocks
156  it = extents.begin();
157  CHECK (isSameObject(*it, extent));
158  CHECK ((*it)[2] == num);
159  ++it;
160  CHECK (isSameObject(*it, nextEx));
161  CHECK ((*it)[5] == num+1);
162  }
163 
164 
165 
169  void
171  {
172  struct Probe
173  {
174  short val;
175  Probe() : val(1 + rand() % 1000) { }
176  ~Probe() { val = 0; }
177  };
178 
179  using SpecialExtents = ExtentFamily<Probe, 1000>;
180 
181  SpecialExtents spex{3};
182  spex.openNew(2);
183  CHECK ( 0 == watch(spex).first());
184  CHECK ( 2 == watch(spex).last());
185 
186  // implant a new Probe object into each »slot« of the new extent
187  auto& extent = *spex.begin();
188  for (Probe& probe : extent)
189  new(&probe) Probe;
190 
191  auto calcChecksum = [](SpecialExtents::Extent& extent) -> size_t
192  {
193  size_t sum{0};
194  for (Probe& probe : extent)
195  sum += probe.val;
196  return sum;
197  };
198 
199  size_t checksum = calcChecksum (*spex.begin());
200 
201  // discard first extent, i.e. mark it as unused
202  // while the underlying memory block remains allocated
203  // and data within this block is not touched
204  spex.dropOld(1);
205  CHECK ( 1 == watch(spex).first());
206  CHECK ( 2 == watch(spex).last());
207 
208  // the »begin« (i.e. the first active extent is now another memory block
209  CHECK (not isSameObject (extent, *spex.begin()));
210  size_t checkSecond = calcChecksum (*spex.begin());
211  CHECK (checkSecond != checksum);
212 
213  // but the random data generated above still sits in the original (first) memory block
214  CHECK (checksum == calcChecksum (extent));
215 
216  // now let the actively allotted extents "wrap around"...
217  spex.dropOld(1);
218  CHECK ( 2 == watch(spex).first());
219  CHECK ( 2 == watch(spex).last());
220  spex.openNew(2);
221  CHECK ( 2 == watch(spex).first());
222  CHECK ( 1 == watch(spex).last());
223 
224  auto iter = spex.begin();
225  CHECK ( 2 == iter.getIndex());
226  ++iter;
227  CHECK ( 0 == iter.getIndex());
228  CHECK (isSameObject(*iter, extent));
229 
230  // and during all those allotting and dropping, data in the memory block was not touched,
231  // which also proves that constructors or destructors of the nominal "content" are not invoked
232  CHECK (checksum == calcChecksum (extent));
233  }
234 
235 
236 
245  void
247  {
248  // Helper to capture the storage addresses of all currently active Extents
249  auto snapshotAdr = [](Extents& extents)
250  {
251  auto takeAdr = [](auto& x){ return &*x; };
252  return explore(extents).transform(takeAdr).effuse();
253  };
254  auto verifyAdr = [](auto snapshot, auto it)
255  {
256  for (auto oldAddr : snapshot)
257  {
258  if (not isSameObject(*oldAddr, *it))
259  return false;
260  ++it;
261  }
262  return true;
263  };
264 
265 
266  Extents extents{5};
267  CHECK ( extents.empty());
268  CHECK ( 0 == watch(extents).first());
269  CHECK ( 0 == watch(extents).last());
270  CHECK ( 0 == watch(extents).active());
271  CHECK ( 5 == watch(extents).size());
272 
273  extents.openNew(4);
274  CHECK ( 0 == watch(extents).first());
275  CHECK ( 4 == watch(extents).last());
276  CHECK ( 4 == watch(extents).active());
277  CHECK ( 5 == watch(extents).size());
278 
279  auto snapshot = snapshotAdr(extents); // capture *addresses* of currently active Extents
280  CHECK (4 == snapshot.size());
281 
282  extents.openNew();
283  CHECK ( 0 == watch(extents).first());
284  CHECK ( 5 == watch(extents).last());
285  CHECK ( 5 == watch(extents).active());
286  CHECK (10 == watch(extents).size()); // Note: heuristics to over-allocate to some degree
287  CHECK (verifyAdr (snapshot, extents.begin()));
288 
289  extents.dropOld(3); // place the active window such as to start on last snapshotted Extent
290  CHECK ( 3 == watch(extents).first());
291  CHECK ( 5 == watch(extents).last());
292  CHECK ( 2 == watch(extents).active());
293  CHECK (10 == watch(extents).size());
294  CHECK (isSameObject (*extents.begin(), *snapshot.back()));
295 
296  extents.openNew(6); // now provoke a »wrapped« state of internal management of active Extents
297  CHECK ( 3 == watch(extents).first()); // ...Note: the position of the *first* active Extent...
298  CHECK ( 1 == watch(extents).last()); // ... is *behind* the position of the last active Extent
299  CHECK ( 8 == watch(extents).active()); // ... implying that the active strike wraps at allocation end
300  CHECK (10 == watch(extents).size());
301  snapshot = snapshotAdr (extents); // take a new snapshot; this also verifies proper iteration
302  CHECK (8 == snapshot.size());
303 
304  extents.openNew(2); // ask for more than can be accommodated without ambiguity
305  CHECK ( 8 == watch(extents).first()); // ...Note: new allocation was inserted, existing tail shifted
306  CHECK ( 3 == watch(extents).last()); // ... allowing for the requested two »slots« to be accommodated
307  CHECK (10 == watch(extents).active());
308  CHECK (15 == watch(extents).size());
309  CHECK (verifyAdr (snapshot, extents.begin())); // ... yet all existing Extent addresses have been rotated transparently
310 
311  extents.dropOld(10); // close out all active slots, wrapping the first-pos to approach last
312  CHECK ( 3 == watch(extents).first());
313  CHECK ( 3 == watch(extents).last());
314  CHECK ( 0 == watch(extents).active());
315  CHECK (15 == watch(extents).size());
316 
317  extents.openNew(12); // provoke a special boundary situation, where the end is *just wrapped*
318  CHECK ( 3 == watch(extents).first());
319  CHECK ( 0 == watch(extents).last());
320  CHECK (12 == watch(extents).active());
321  CHECK (15 == watch(extents).size());
322 
323  extents.dropOld(11); // and make this boundary situation even more nasty, just sitting on the rim
324  CHECK (14 == watch(extents).first());
325  CHECK ( 0 == watch(extents).last());
326  CHECK ( 1 == watch(extents).active());
327  CHECK (15 == watch(extents).size());
328 
329  CHECK (14 == extents.begin().getIndex());
330  snapshot = snapshotAdr (extents); // verify iteration end just after wrapping properly detected
331  CHECK (1 == snapshot.size());
332  CHECK (isSameObject (*extents.begin(), *snapshot.front()));
333 
334  extents.openNew(14); // and now provoke further expansion, adding new allocation right at start
335  CHECK (19 == watch(extents).first()); // ...Note: first must be relocated to sit again at the very rim
336  CHECK (14 == watch(extents).last()); // ... to allow last to sit at the index previously used by first
337  CHECK (15 == watch(extents).active());
338  CHECK (20 == watch(extents).size());
339 
340  CHECK (19 == extents.begin().getIndex()); // ... yet address of the first Extent remains the same, just held in another slot
341  CHECK (isSameObject (*extents.begin(), *snapshot.front()));
342  }
343  };
344 
345 
347  LAUNCHER (ExtentFamily_test, "unit memory");
348 
349 
350 
351 }}} // namespace vault::mem::test
logical structure of a memory Extent
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
void openNew(size_t cnt=1)
claim next cnt extents, possibly allocate.
Definition: run.hpp:49
Abstract Base Class for all testcases.
Definition: run.hpp:62
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Memory manager to provide a sequence of Extents for cyclic usage.
Memory management scheme for cyclically used memory extents.
Decorator-Adapter to make a »state core« iterable as Lumiera Forward Iterator.
Building tree expanding and backtracking evaluations within hierarchical scopes.
lib::IterableDecorator< Extent, IdxLink > iterator
allow transparent iteration of Extents, with the ability to expand storage
Vault-Layer implementation namespace root.
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:372