Lumiera  0.pre.03
»edit your freedom«
typed-counter-test.cpp
Go to the documentation of this file.
1 /*
2  TypedCounter(Test) - managing a set of type based contexts
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 
23 
44 #include "lib/test/run.hpp"
45 #include "lib/typed-counter.hpp"
47 #include "lib/util.hpp"
48 
49 #include <cstdlib>
50 #include <utility>
51 #include <array>
52 
53 
54 namespace lib {
55 namespace test{
56 
57  using util::isnil;
58  using std::rand;
59 
60 
61  namespace { // test parametrisation...
62  const uint MAX_INDEX = 10;
63  const uint NUM_THREADS = 100;
64  const uint NUM_ITERATIONS = 10000;
65  }
66 
67 
68 
69  /***********************************************************************************/
76  class TypedCounter_test : public Test
77  {
78 
79  void
80  run (Arg)
81  {
82  simpleUsageTest();
83  tortureTest();
84  }
85 
86 
87  void
88  simpleUsageTest()
89  {
90  TypedCounter myCounter;
91  CHECK (isnil (myCounter));
92  CHECK (0==myCounter.size());
93 
94  CHECK (0 == myCounter.get<short>());
95  CHECK (0 < myCounter.size());
96  // probably greater than 1;
97  // other parts of the application allocate type-IDs as well
98 
99  // now allocate a counter for a type not seen yet
100  struct X { };
101  struct U { };
102 
103  CHECK (0 == myCounter.get<X>());
104  size_t sX = myCounter.size();
105 
106  CHECK (0 == myCounter.get<U>());
107  CHECK (sX + 1 == myCounter.size());
108  CHECK (0 == myCounter.get<X>());
109  CHECK (sX + 1 == myCounter.size());
110 
111  CHECK (-1 == myCounter.dec<X>());
112  CHECK (-2 == myCounter.dec<X>());
113  CHECK (+1 == myCounter.inc<U>());
114 
115  CHECK (-2 == myCounter.get<X>());
116  CHECK (+1 == myCounter.get<U>());
117 
118  // each new type has gotten a new "slot" (i.e. a distinct type-ID)
119  IxID typeID_short = TypedContext<TypedCounter>::ID<short>::get();
120  IxID typeID_X = TypedContext<TypedCounter>::ID<X>::get();
121  IxID typeID_U = TypedContext<TypedCounter>::ID<U>::get();
122 
123  CHECK (0 < typeID_short);
124  CHECK (0 < typeID_X);
125  CHECK (0 < typeID_U);
126  CHECK (typeID_short < typeID_X);
127  CHECK (typeID_X < typeID_U);
128  // type-IDs are allocated in the order of first usage
129 
130  CHECK (sX + 1 == myCounter.size());
131  }
132 
133 
134 
136  template<size_t i>
137  struct Dummy { };
138 
139  template<size_t i>
140  static void
141  increment (TypedCounter& counter)
142  {
143  counter.inc<Dummy<i>>();
144  }
145 
151  template<size_t...I>
152  static auto
153  buildOperatorsTable(std::index_sequence<I...>)
154  {
155  using Operator = void(*)(TypedCounter&);
156  return std::array<Operator, MAX_INDEX>{increment<I>...};
157  }
158 
159  template<size_t...I>
160  static size_t
161  sumAllCounters(TypedCounter& counter, std::index_sequence<I...>)
162  {
163  return (counter.get<Dummy<I>>() + ... );
164  }
165 
166 
167 
176  void
178  {
179  std::srand (::time (NULL));
180 
181  using IDX = std::make_index_sequence<MAX_INDEX>;
182  auto operators = buildOperatorsTable(IDX{});
183 
184  TypedCounter testCounter;
185 
186  auto testSubject = [&](size_t) -> size_t
187  {
188  uint i = rand() % MAX_INDEX;
189  operators[i](testCounter);
190  return 1;
191  };
192 
193  threadBenchmark<NUM_THREADS> (testSubject, NUM_ITERATIONS);
194 
195  size_t expectedIncrements = NUM_THREADS * NUM_ITERATIONS;
196  CHECK (sumAllCounters(testCounter, IDX{}) == expectedIncrements);
197  }
198  };
199 
200 
202  LAUNCHER (TypedCounter_test, "unit common");
203 
204 
205 }} // namespace lib::test
Creating series of type-based contexts.
Utility providing a set of counters, each tied to a specific type.
const uint NUM_ITERATIONS
number of repeated random accesses per Thread
Definition: run.hpp:49
const uint NUM_THREADS
number of threads to run in parallel
Functions to perform (multithreaded) timing measurement on a given functor.
Implementation namespace for support and library code.
const uint MAX_INDEX
number of distinct types / counters
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Provide type-IDs for a specific context.
static auto buildOperatorsTable(std::index_sequence< I... >)
Helper for tortureTest(): Build a table of functors, where the i-th entry invokes the function increm...
parametrised marker type to designate a counter to be incremented