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) Lumiera.org
5  2011, 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"
29 #include "lib/simple-allocator.hpp"
30 #include "lib/util.hpp"
31 
32 #include <cstdlib>
33 #include <string>
34 
35 
36 namespace lib {
37 namespace test{
38 
39 
40  using util::isSameObject;
41  using std::string;
42  using std::rand;
43 
44 
45 
46  namespace { // test data and helpers...
47 
48  long checksum_ = 0;
49 
53  template<uint siz>
54  class DummyObj
55  {
56  char crap_[siz];
57 
58  public:
59  DummyObj()
60  {
61  REQUIRE (siz);
62  for (uint i=0; i<siz; ++i)
63  checksum_ += (crap_[i] = rand() % 128);
64  }
65 
66  DummyObj (DummyObj const& o)
67  {
68  REQUIRE (siz);
69  for (uint i=0; i<siz; ++i)
70  checksum_ += (crap_[i] = o.crap_[i]);
71  }
72 
73  ~DummyObj()
74  {
75  for (uint i=0; i<siz; ++i)
76  checksum_ -= crap_[i];
77  }
78  };
79 
82  }
83 
84 
85 
86  /***********************************************************************************/
98  class SimpleAllocator_test : public Test
99  {
100 
101  virtual void
102  run (Arg)
103  {
104  CHECK (0 == checksum_);
105 
106  TestAllocator allocator;
107 
108  typedef string * PS;
109  typedef DummyObj<1> * PD1;
110  typedef DummyObj<23> * PD23;
111  CHECK (sizeof(DummyObj<1>) != sizeof(DummyObj<23>));
112 
113  PD1 pD11 = allocator.create<DummyObj<1>>();
114  PD1 pD12 = allocator.create<DummyObj<1>>();
115  PD23 pD21 = allocator.create<DummyObj<23>>();
116  PD23 pD22 = allocator.create<DummyObj<23>>();
117  PS pS11 = allocator.create<string> ("Lumiera");
118  PS pS12 = allocator.create<string> ("the paradox");
119 
120  CHECK (pD11);
121  CHECK (pD12);
122  CHECK (pD21);
123  CHECK (pD22);
124  CHECK (pS11);
125  CHECK (pS12);
126  CHECK (!isSameObject (*pD11, *pD12));
127  CHECK (!isSameObject (*pD11, *pD21));
128  CHECK (!isSameObject (*pD11, *pD22));
129  CHECK (!isSameObject (*pD11, *pS11));
130  CHECK (!isSameObject (*pD11, *pS12));
131  CHECK (!isSameObject (*pD12, *pD21));
132  CHECK (!isSameObject (*pD12, *pD22));
133  CHECK (!isSameObject (*pD12, *pS11));
134  CHECK (!isSameObject (*pD12, *pS12));
135  CHECK (!isSameObject (*pD21, *pD22));
136  CHECK (!isSameObject (*pD21, *pS11));
137  CHECK (!isSameObject (*pD21, *pS12));
138  CHECK (!isSameObject (*pD22, *pS11));
139  CHECK (!isSameObject (*pD22, *pS12));
140  CHECK (!isSameObject (*pS11, *pS12));
141 
142  CHECK (*pS11 == "Lumiera");
143  CHECK (*pS12 == "the paradox");
144 
145  PD23 pDxx = allocator.create<DummyObj<23>> (*pD21);
146  PS pSxx = allocator.create<string> (*pS12);
147 
148  CHECK (*pS12 == *pSxx);
149  CHECK (!isSameObject (*pS12, *pSxx));
150 
151  allocator.destroy (pD11);
152  allocator.destroy (pD12);
153  allocator.destroy (pD21);
154  allocator.destroy (pD22);
155  allocator.destroy (pS11);
156  allocator.destroy (pS12);
157  allocator.destroy (pDxx);
158  allocator.destroy (pSxx);
159 
160  CHECK (0 == allocator.numSlots<DummyObj<1>>());
161  CHECK (0 == allocator.numSlots<DummyObj<23>>());
162  CHECK (0 == allocator.numSlots<string>());
163  CHECK (0 == checksum_);
164  }
165  };
166 
167 
169  LAUNCHER (SimpleAllocator_test, "unit common");
170 
171 
172 }} // namespace lib::test
Definition: run.hpp:49
Implementation namespace for support and library code.
Frontend and marker interface for allocating small objects explicitly.
Yet-another ctor/dtor-tracking test dummy object....
Simple 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, bypassing any custom comparison operators.
Definition: util.hpp:372