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)
5  2009, 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 
14 
35 #include "lib/test/run.hpp"
36 #include "lib/typed-counter.hpp"
38 #include "lib/random.hpp"
39 #include "lib/util.hpp"
40 
41 #include <cstdlib>
42 #include <utility>
43 #include <array>
44 
45 
46 namespace lib {
47 namespace test{
48 
49  using util::isnil;
50  using lib::rani;
51 
52 
53  namespace { // test parametrisation...
54  const uint MAX_INDEX = 10;
55  const uint NUM_THREADS = 100;
56  const uint NUM_ITERATIONS = 10000;
57  }
58 
59 
60 
61  /***********************************************************************************/
68  class TypedCounter_test : public Test
69  {
70 
71  void
72  run (Arg)
73  {
74  simpleUsageTest();
75  tortureTest();
76  }
77 
78 
79  void
80  simpleUsageTest()
81  {
82  TypedCounter myCounter;
83  CHECK (isnil (myCounter));
84  CHECK (0==myCounter.size());
85 
86  CHECK (0 == myCounter.get<short>());
87  CHECK (0 < myCounter.size());
88  // probably greater than 1;
89  // other parts of the application allocate type-IDs as well
90 
91  // now allocate a counter for a type not seen yet
92  struct X { };
93  struct U { };
94 
95  CHECK (0 == myCounter.get<X>());
96  size_t sX = myCounter.size();
97 
98  CHECK (0 == myCounter.get<U>());
99  CHECK (sX + 1 == myCounter.size());
100  CHECK (0 == myCounter.get<X>());
101  CHECK (sX + 1 == myCounter.size());
102 
103  CHECK (-1 == myCounter.dec<X>());
104  CHECK (-2 == myCounter.dec<X>());
105  CHECK (+1 == myCounter.inc<U>());
106 
107  CHECK (-2 == myCounter.get<X>());
108  CHECK (+1 == myCounter.get<U>());
109 
110  // each new type has gotten a new "slot" (i.e. a distinct type-ID)
111  IxID typeID_short = TypedContext<TypedCounter>::ID<short>::get();
112  IxID typeID_X = TypedContext<TypedCounter>::ID<X>::get();
113  IxID typeID_U = TypedContext<TypedCounter>::ID<U>::get();
114 
115  CHECK (0 < typeID_short);
116  CHECK (0 < typeID_X);
117  CHECK (0 < typeID_U);
118  CHECK (typeID_short < typeID_X);
119  CHECK (typeID_X < typeID_U);
120  // type-IDs are allocated in the order of first usage
121 
122  CHECK (sX + 1 == myCounter.size());
123  }
124 
125 
126 
128  template<size_t i>
129  struct Dummy { };
130 
131  template<size_t i>
132  static void
133  increment (TypedCounter& counter)
134  {
135  counter.inc<Dummy<i>>();
136  }
137 
143  template<size_t...I>
144  static auto
145  buildOperatorsTable(std::index_sequence<I...>)
146  {
147  using Operator = void(*)(TypedCounter&);
148  return std::array<Operator, MAX_INDEX>{increment<I>...};
149  }
150 
151  template<size_t...I>
152  static size_t
153  sumAllCounters(TypedCounter& counter, std::index_sequence<I...>)
154  {
155  return (counter.get<Dummy<I>>() + ... );
156  }
157 
158 
159 
168  void
170  {
171  seedRand();
172 
173  using IDX = std::make_index_sequence<MAX_INDEX>;
174  auto operators = buildOperatorsTable(IDX{});
175 
176  TypedCounter testCounter;
177 
178  auto testSubject = [&
179  ,gen = makeRandGen()]
180  (size_t) mutable -> size_t
181  {
182  uint i = gen.i(MAX_INDEX);
183  operators[i](testCounter);
184  return 1;
185  };
186 
187  threadBenchmark<NUM_THREADS> (testSubject, NUM_ITERATIONS);
188 
189  size_t expectedIncrements = NUM_THREADS * NUM_ITERATIONS;
190  CHECK (sumAllCounters(testCounter, IDX{}) == expectedIncrements);
191  }
192  };
193 
194 
196  LAUNCHER (TypedCounter_test, "unit common");
197 
198 
199 }} // 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:40
const uint NUM_THREADS
number of threads to run in parallel
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
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
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Generating (pseudo) random numbers with controlled seed.
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