Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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
30using ::Test;
31using util::isnil;
32using util::_Fmt;
33
34
35namespace util {
36namespace test {
37
38
39
40 namespace{ // Test data and operations
41
43 const uint NUMBER_LIMIT = 1 << 30;
44
45 typedef std::vector<int> VecI;
46
47 VecI
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 and 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
112
113 verifyIntegerTypes<int>();
114 verifyIntegerTypes<long>();
115 verifyIntegerTypes<short>();
116 verifyIntegerTypes<int64_t>();
117 verifyIntegerTypes<llong>();
118
119 if (not isnil (arg))
121 }
122
123
124 void
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
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
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
233 for (I ii =testdata.begin(); ii!=testdata.end(); )
234 {
235 integerDiv (*ii++, *ii++);
236 }
237 DISPLAY_TIMINGS (Integer_div)
238
240 for (I ii =testdata.begin(); ii!=testdata.end(); )
241 {
242 floor (double(*ii++) / *ii++);
243 }
244 DISPLAY_TIMINGS (double_floor)
245
247 for (I ii =testdata.begin(); ii!=testdata.end(); )
248 {
249 floordiv (*ii++, *ii++);
250 }
251 DISPLAY_TIMINGS (floordiv_int)
252
254 for (I ii =testdata.begin(); ii!=testdata.end(); )
255 {
256 floordiv (long(*ii++), long(*ii++));
257 }
258 DISPLAY_TIMINGS (floordiv_long)
259
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
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
A front-end for using printf-style formatting.
#define DISPLAY_TIMINGS(ID)
#define START_TIMINGS
Automatically use custom string conversion in C++ stream output.
Front-end for printf-style string template interpolation.
unsigned int uint
Definition integral.hpp:29
Test runner and basic definitions for tests.
long integerDiv(long num, long den)
the built-in integer division operator, packaged as inline function for timing comparison
long floordiv_alternate(long num, long den)
an alternate formulation, which turned out to perform slightly worse
std::vector< uint > VecI
I floordiv(I num, I den)
floor function for integer arithmetics.
bool isnil(lib::time::Duration const &dur)
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Utilities for quantisation (grid alignment) and comparisons.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...