Lumiera  0.pre.03
»edit your freedom«
util-floordiv-test.cpp
Go to the documentation of this file.
1 /*
2  UtilFloordiv(Test) - verify integer rounding function
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/util-quant.hpp"
21 #include "lib/util.hpp"
22 
23 #include "lib/format-cout.hpp"
24 #include "lib/format-string.hpp"
25 
26 #include <cmath>
27 #include <time.h>
28 #include <vector>
29 
30 using ::Test;
31 using util::isnil;
32 using util::_Fmt;
33 
34 
35 namespace util {
36 namespace test {
37 
38 
39 
40  namespace{ // Test data and operations
41 
42  const uint NUM_ELMS_PERFORMANCE_TEST = 50000000;
43  const uint NUMBER_LIMIT = 1 << 30;
44 
45  typedef std::vector<int> VecI;
46 
47  VecI
48  buildTestNumberz (uint cnt)
49  {
50  VecI data;
51  for (uint i=0; i<cnt; ++i)
52  {
53  int someNumber = -int(NUMBER_LIMIT)+rani(2*NUMBER_LIMIT);
54  if (!someNumber) someNumber -= 1 + rani(NUMBER_LIMIT);
55 
56  data.push_back (someNumber);
57  }
58  return data;
59  }
60 
61 
65  inline long
66  integerDiv (long num, long den)
67  {
68  return num / den;
69  }
70 
71 
75  inline long
76  floordiv_alternate (long num, long den)
77  {
78  ldiv_t res = ldiv(num,den);
79  return (0 >= res.quot && res.rem)? res.quot-1
80  : res.quot;
81  }
82 
83  } // (End) test data and operations
84 
85 
86 
87  /******************************************************************/
103  class UtilFloordiv_test : public Test
104  {
105 
106  virtual void
107  run (Arg arg)
108  {
109  seedRand();
110 
111  verifyBehaviour ();
112 
113  verifyIntegerTypes<int>();
114  verifyIntegerTypes<long>();
115  verifyIntegerTypes<short>();
116  verifyIntegerTypes<int64_t>();
117  verifyIntegerTypes<llong>();
118 
119  if (!isnil (arg))
121  }
122 
123 
124  void
125  verifyBehaviour ()
126  {
127  CHECK ( 3 == floordiv ( 12,4));
128  CHECK ( 2 == floordiv ( 11,4));
129  CHECK ( 2 == floordiv ( 10,4));
130  CHECK ( 2 == floordiv ( 9,4));
131  CHECK ( 2 == floordiv ( 8,4));
132  CHECK ( 1 == floordiv ( 7,4));
133  CHECK ( 1 == floordiv ( 6,4));
134  CHECK ( 1 == floordiv ( 5,4));
135  CHECK ( 1 == floordiv ( 4,4));
136  CHECK ( 0 == floordiv ( 3,4));
137  CHECK ( 0 == floordiv ( 2,4));
138  CHECK ( 0 == floordiv ( 1,4));
139  CHECK ( 0 == floordiv ( 0,4));
140  CHECK (-1 == floordiv (- 1,4));
141  CHECK (-1 == floordiv (- 2,4));
142  CHECK (-1 == floordiv (- 3,4));
143  CHECK (-1 == floordiv (- 4,4));
144  CHECK (-2 == floordiv (- 5,4));
145  CHECK (-2 == floordiv (- 6,4));
146  CHECK (-2 == floordiv (- 7,4));
147  CHECK (-2 == floordiv (- 8,4));
148  CHECK (-3 == floordiv (- 9,4));
149  CHECK (-3 == floordiv (-10,4));
150  CHECK (-3 == floordiv (-11,4));
151  CHECK (-3 == floordiv (-12,4));
152  }
153 
154 
155  template<typename I>
156  void
157  verifyIntegerTypes ()
158  {
159  I n,d,expectedRes;
160 
161  for (int i=-12; i <= 12; ++i)
162  {
163  n = i;
164  d = 4;
165  expectedRes = floordiv (i,4);
166  CHECK (floordiv(n,d) == expectedRes);
167  }
168  }
169 
170 
171 
207  void
209  {
210  VecI testdata = buildTestNumberz (2*NUM_ELMS_PERFORMANCE_TEST);
211  typedef VecI::const_iterator I;
212 
213  clock_t start(0), stop(0);
214  _Fmt resultDisplay{"timings(%s)%|30T.|%5.3fsec\n"};
215 
216 #define START_TIMINGS start=clock();
217 #define DISPLAY_TIMINGS(ID) \
218  stop = clock(); \
219  cout << resultDisplay % STRINGIFY (ID) % (double(stop-start)/CLOCKS_PER_SEC) ;
220 
221  START_TIMINGS
222  for (I ii =testdata.begin(); ii!=testdata.end(); )
223  {
224  int num = *ii;
225  ++ii;
226  int den = *ii;
227  ++ii;
228  CHECK (floor(double(num)/den) == floordiv(num,den));
229  }
230  DISPLAY_TIMINGS (Verification)
231 
232  START_TIMINGS
233  for (I ii =testdata.begin(); ii!=testdata.end(); )
234  {
235  integerDiv (*ii++, *ii++);
236  }
237  DISPLAY_TIMINGS (Integer_div)
238 
239  START_TIMINGS
240  for (I ii =testdata.begin(); ii!=testdata.end(); )
241  {
242  floor (double(*ii++) / *ii++);
243  }
244  DISPLAY_TIMINGS (double_floor)
245 
246  START_TIMINGS
247  for (I ii =testdata.begin(); ii!=testdata.end(); )
248  {
249  floordiv (*ii++, *ii++);
250  }
251  DISPLAY_TIMINGS (floordiv_int)
252 
253  START_TIMINGS
254  for (I ii =testdata.begin(); ii!=testdata.end(); )
255  {
256  floordiv (long(*ii++), long(*ii++));
257  }
258  DISPLAY_TIMINGS (floordiv_long)
259 
260  START_TIMINGS
261  for (I ii =testdata.begin(); ii!=testdata.end(); )
262  {
263  floordiv (int64_t(*ii++), int64_t(*ii++));
264  }
265  DISPLAY_TIMINGS (floordiv_int64_t)
266 
267  START_TIMINGS
268  for (I ii =testdata.begin(); ii!=testdata.end(); )
269  {
270  floordiv_alternate (*ii++, *ii++);
271  }
272  DISPLAY_TIMINGS (floordiv_long_alt)
273  }
274  };
275 
276 
277 
278 
279  LAUNCHER (UtilFloordiv_test, "unit common");
280 
281 
282 }} // namespace util::test
I floordiv(I num, I den)
floor function for integer arithmetics.
Definition: util-quant.hpp:97
Automatically use custom string conversion in C++ stream output.
Common functions for handling of time values.
Definition: run.hpp:40
Front-end for printf-style string template interpolation.
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
A front-end for using printf-style formatting.
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
long integerDiv(long num, long den)
the built-in integer division operator, packaged as inline function for timing comparison ...
Utilities for quantisation (grid alignment) and comparisons.
long floordiv_alternate(long num, long den)
an alternate formulation, which turned out to perform slightly worse