Lumiera  0.pre.03
»edit your freedom«
typed-allocation-manager-test.cpp
Go to the documentation of this file.
1 /*
2  TypedAllocationManager(Test) - check interface to pooled allocations
3 
4  Copyright (C) Lumiera.org
5  2009, 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/util.hpp"
31 
32 
33 #include <memory>
34 #include <cstdlib>
35 
36 
37 namespace lib {
38 namespace test{
39 
40 
41  using util::isSameObject;
42  using std::shared_ptr;
43  using std::rand;
44 
45 
46 
47  namespace { // test data and helpers...
48 
49  long checksum_ = 0;
50 
54  template<uint siz>
55  class DummyObj
56  {
57  char crap_[siz];
58 
59  public:
60  DummyObj()
61  {
62  REQUIRE (siz);
63  for (uint i=0; i<siz; ++i)
64  checksum_ += (crap_[i] = rand() % 128);
65  }
66 
67  ~DummyObj()
68  {
69  for (uint i=0; i<siz; ++i)
70  checksum_ -= crap_[i];
71  }
72  };
73  }
74 
75 
76 
77  /***********************************************************************************/
87  class TypedAllocationManager_test : public Test
88  {
89 
90  virtual void
91  run (Arg)
92  {
93  CHECK (0 == checksum_);
94 
95  TypedAllocationManager allocator;
96 
97  typedef shared_ptr<DummyObj<1>> PD1;
98  typedef shared_ptr<DummyObj<22>> PD22;
99  CHECK (sizeof(DummyObj<1>) != sizeof(DummyObj<22>));
100 
101  {
102  PD1 pD11 = allocator.create<DummyObj<1> >();
103  PD1 pD12 = allocator.create<DummyObj<1> >();
104  PD22 pD21 = allocator.create<DummyObj<22>>();
105  PD22 pD22 = allocator.create<DummyObj<22>>();
106  CHECK (pD11);
107  CHECK (pD12);
108  CHECK (pD21);
109  CHECK (pD22);
110  CHECK (1 == pD11.use_count());
111  CHECK (1 == pD12.use_count());
112  CHECK (1 == pD21.use_count());
113  CHECK (1 == pD22.use_count());
114  CHECK (!isSameObject (*pD11, *pD12));
115  CHECK (!isSameObject (*pD11, *pD21));
116  CHECK (!isSameObject (*pD11, *pD22));
117  CHECK (!isSameObject (*pD12, *pD21));
118  CHECK (!isSameObject (*pD12, *pD22));
119  CHECK (!isSameObject (*pD21, *pD22));
120 
121  PD22 pD2x = pD21;
122  CHECK (pD2x);
123  CHECK (2 == pD21.use_count());
124  CHECK (2 == pD2x.use_count());
125  CHECK (isSameObject (*pD21, *pD2x));
126 
127  CHECK (2 == allocator.numSlots<DummyObj<1> >());
128  CHECK (2 == allocator.numSlots<DummyObj<22>>());
129 
130  CHECK (0 == allocator.numSlots<long>()); // query just some unrelated type...
131  }
132 
133  CHECK (0 == allocator.numSlots<DummyObj<1> >());
134  CHECK (0 == allocator.numSlots<DummyObj<22>>());
135  CHECK (0 == checksum_);
136  }
137  };
138 
139 
141  LAUNCHER (TypedAllocationManager_test, "unit common");
142 
143 
144 }} // namespace lib::test
Abstract foundation for building custom allocation managers.
Definition: run.hpp:49
Implementation namespace for support and library code.
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Foundation for a custom allocation manager, tracking the created objects by smart-ptrs.
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:372