Lumiera  0.pre.03
»edit your freedom«
simple-allocator-test.cpp
Go to the documentation of this file.
1 /*
2  SimpleAllocator(Test) - check interface for simple custom allocations
3 
4  Copyright (C)
5  2011, 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/simple-allocator.hpp"
21 #include "lib/util.hpp"
22 
23 #include <string>
24 
25 
26 namespace lib {
27 namespace test{
28 
29  using util::isSameObject;
30  using std::string;
31 
32 
33 
34  namespace { // test data and helpers...
35 
36  long checksum_ = 0;
37 
41  template<uint siz>
42  class DummyObj
43  {
44  char crap_[siz];
45 
46  public:
47  DummyObj()
48  {
49  REQUIRE (siz);
50  for (uint i=0; i<siz; ++i)
51  checksum_ += (crap_[i] = rani(128));
52  }
53 
54  DummyObj (DummyObj const& o)
55  {
56  REQUIRE (siz);
57  for (uint i=0; i<siz; ++i)
58  checksum_ += (crap_[i] = o.crap_[i]);
59  }
60 
61  ~DummyObj()
62  {
63  for (uint i=0; i<siz; ++i)
64  checksum_ -= crap_[i];
65  }
66  };
67 
70  }
71 
72 
73 
74  /***********************************************************************************/
86  class SimpleAllocator_test : public Test
87  {
88 
89  virtual void
90  run (Arg)
91  {
92  CHECK (0 == checksum_);
93  seedRand();
94 
95  TestAllocator allocator;
96 
97  typedef string * PS;
98  typedef DummyObj<1> * PD1;
99  typedef DummyObj<23> * PD23;
100  CHECK (sizeof(DummyObj<1>) != sizeof(DummyObj<23>));
101 
102  PD1 pD11 = allocator.create<DummyObj<1>>();
103  PD1 pD12 = allocator.create<DummyObj<1>>();
104  PD23 pD21 = allocator.create<DummyObj<23>>();
105  PD23 pD22 = allocator.create<DummyObj<23>>();
106  PS pS11 = allocator.create<string> ("Lumiera");
107  PS pS12 = allocator.create<string> ("the paradox");
108 
109  CHECK (pD11);
110  CHECK (pD12);
111  CHECK (pD21);
112  CHECK (pD22);
113  CHECK (pS11);
114  CHECK (pS12);
115  CHECK (!isSameObject (*pD11, *pD12));
116  CHECK (!isSameObject (*pD11, *pD21));
117  CHECK (!isSameObject (*pD11, *pD22));
118  CHECK (!isSameObject (*pD11, *pS11));
119  CHECK (!isSameObject (*pD11, *pS12));
120  CHECK (!isSameObject (*pD12, *pD21));
121  CHECK (!isSameObject (*pD12, *pD22));
122  CHECK (!isSameObject (*pD12, *pS11));
123  CHECK (!isSameObject (*pD12, *pS12));
124  CHECK (!isSameObject (*pD21, *pD22));
125  CHECK (!isSameObject (*pD21, *pS11));
126  CHECK (!isSameObject (*pD21, *pS12));
127  CHECK (!isSameObject (*pD22, *pS11));
128  CHECK (!isSameObject (*pD22, *pS12));
129  CHECK (!isSameObject (*pS11, *pS12));
130 
131  CHECK (*pS11 == "Lumiera");
132  CHECK (*pS12 == "the paradox");
133 
134  PD23 pDxx = allocator.create<DummyObj<23>> (*pD21);
135  PS pSxx = allocator.create<string> (*pS12);
136 
137  CHECK (*pS12 == *pSxx);
138  CHECK (!isSameObject (*pS12, *pSxx));
139 
140  allocator.destroy (pD11);
141  allocator.destroy (pD12);
142  allocator.destroy (pD21);
143  allocator.destroy (pD22);
144  allocator.destroy (pS11);
145  allocator.destroy (pS12);
146  allocator.destroy (pDxx);
147  allocator.destroy (pSxx);
148 
149  CHECK (0 == allocator.numSlots<DummyObj<1>>());
150  CHECK (0 == allocator.numSlots<DummyObj<23>>());
151  CHECK (0 == allocator.numSlots<string>());
152  CHECK (0 == checksum_);
153  }
154  };
155 
156 
158  LAUNCHER (SimpleAllocator_test, "unit common");
159 
160 
161 }} // namespace lib::test
Definition: run.hpp:40
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
Implementation namespace for support and library code.
Frontend and marker interface for allocating small objects explicitly.
Yet-another ctor/dtor-tracking test dummy object....
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Frontend for explicit allocations, using a custom allocator.
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421