]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/polygon/test/voronoi_ctypes_test.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / polygon / test / voronoi_ctypes_test.cpp
1 // Boost.Polygon library voronoi_ctypes_test.cpp file
2
3 // Copyright Andrii Sydorchuk 2010-2012.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 // See http://www.boost.org for updates, documentation, and revision history.
9
10 #include <boost/core/lightweight_test.hpp>
11 #include <boost/polygon/detail/voronoi_ctypes.hpp>
12 #include <boost/random/mersenne_twister.hpp>
13 #include <vector>
14 #include <ctime>
15
16 using namespace boost::polygon::detail;
17
18 type_converter_fpt to_fpt;
19
20 void ulp_comparison_test1()
21 {
22 ulp_comparison<double> ulp_cmp;
23 uint64 a = 22;
24 uint64 b = 27;
25 fpt64 da, db;
26 std::memcpy(&da, &a, sizeof(uint64));
27 std::memcpy(&db, &b, sizeof(uint64));
28 BOOST_TEST_EQ(ulp_cmp(da, db, 1), ulp_cmp.LESS);
29 BOOST_TEST_EQ(ulp_cmp(db, da, 1), ulp_cmp.MORE);
30 BOOST_TEST_EQ(ulp_cmp(da, db, 4), ulp_cmp.LESS);
31 BOOST_TEST_EQ(ulp_cmp(da, db, 5), ulp_cmp.EQUAL);
32 BOOST_TEST_EQ(ulp_cmp(da, db, 6), ulp_cmp.EQUAL);
33 }
34
35 void ulp_comparison_test2()
36 {
37 ulp_comparison<fpt64> ulp_cmp;
38 uint64 a = 0ULL;
39 uint64 b = 0x8000000000000002ULL;
40 fpt64 da, db;
41 std::memcpy(&da, &a, sizeof(uint64));
42 std::memcpy(&db, &b, sizeof(uint64));
43 BOOST_TEST_EQ(ulp_cmp(da, db, 1), ulp_cmp.MORE);
44 BOOST_TEST_EQ(ulp_cmp(db, da, 1), ulp_cmp.LESS);
45 BOOST_TEST_EQ(ulp_cmp(da, db, 2), ulp_cmp.EQUAL);
46 BOOST_TEST_EQ(ulp_cmp(da, db, 3), ulp_cmp.EQUAL);
47 }
48
49 void extended_exponent_fpt_test1()
50 {
51 boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
52 fpt64 b = 0.0;
53 efpt64 eeb(b);
54 for (int i = 0; i < 1000; ++i) {
55 fpt64 a = to_fpt(static_cast<int64>(gen()));
56 efpt64 eea(a);
57 efpt64 neg = -eea;
58 efpt64 sum = eea + eeb;
59 efpt64 dif = eea - eeb;
60 efpt64 mul = eea * eeb;
61 BOOST_TEST_EQ(to_fpt(neg), -a);
62 BOOST_TEST_EQ(to_fpt(sum), a + b);
63 BOOST_TEST_EQ(to_fpt(dif), a - b);
64 BOOST_TEST_EQ(to_fpt(mul), a * b);
65 }
66 }
67
68 void extended_exponent_fpt_test2()
69 {
70 boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
71 fpt64 a = 0.0;
72 efpt64 eea(a);
73 for (int i = 0; i < 1000; ++i) {
74 fpt64 b = to_fpt(static_cast<int64>(gen()));
75 if (b == 0.0)
76 continue;
77 efpt64 eeb(b);
78 efpt64 neg = -eea;
79 efpt64 sum = eea + eeb;
80 efpt64 dif = eea - eeb;
81 efpt64 mul = eea * eeb;
82 efpt64 div = eea / eeb;
83 BOOST_TEST_EQ(to_fpt(neg), -a);
84 BOOST_TEST_EQ(to_fpt(sum), a + b);
85 BOOST_TEST_EQ(to_fpt(dif), a - b);
86 BOOST_TEST_EQ(to_fpt(mul), a * b);
87 BOOST_TEST_EQ(to_fpt(div), a / b);
88 }
89 }
90
91 void extended_exponent_fpt_test3()
92 {
93 boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
94 for (int i = 0; i < 1000; ++i) {
95 fpt64 a = to_fpt(static_cast<int64>(gen()));
96 fpt64 b = to_fpt(static_cast<int64>(gen()));
97 if (b == 0.0)
98 continue;
99 efpt64 eea(a);
100 efpt64 eeb(b);
101 efpt64 neg = -eea;
102 efpt64 sum = eea + eeb;
103 efpt64 dif = eea - eeb;
104 efpt64 mul = eea * eeb;
105 efpt64 div = eea / eeb;
106 BOOST_TEST_EQ(to_fpt(neg), -a);
107 BOOST_TEST_EQ(to_fpt(sum), a + b);
108 BOOST_TEST_EQ(to_fpt(dif), a - b);
109 BOOST_TEST_EQ(to_fpt(mul), a * b);
110 BOOST_TEST_EQ(to_fpt(div), a / b);
111 }
112 }
113
114 void extended_exponent_fpt_test4()
115 {
116 for (int exp = 0; exp < 64; ++exp)
117 for (int i = 1; i < 100; ++i) {
118 fpt64 a = i;
119 fpt64 b = to_fpt(1LL << exp);
120 efpt64 eea(a);
121 efpt64 eeb(b);
122 efpt64 neg = -eea;
123 efpt64 sum = eea + eeb;
124 efpt64 dif = eea - eeb;
125 efpt64 mul = eea * eeb;
126 efpt64 div = eea / eeb;
127 BOOST_TEST_EQ(to_fpt(neg), -a);
128 BOOST_TEST_EQ(to_fpt(sum), a + b);
129 BOOST_TEST_EQ(to_fpt(dif), a - b);
130 BOOST_TEST_EQ(to_fpt(mul), a * b);
131 BOOST_TEST_EQ(to_fpt(div), a / b);
132 }
133 }
134
135 void extended_exponent_fpt_test5()
136 {
137 for (int i = 0; i < 100; ++i) {
138 efpt64 a(to_fpt(i * i));
139 efpt64 b = a.sqrt();
140 BOOST_TEST_EQ(to_fpt(b), to_fpt(i));
141 }
142 }
143
144 void extended_exponent_fpt_test6()
145 {
146 for (int i = -10; i <= 10; ++i) {
147 efpt64 a(to_fpt(i));
148 BOOST_TEST_EQ(is_pos(a), i > 0);
149 BOOST_TEST_EQ(is_neg(a), i < 0);
150 BOOST_TEST_EQ(is_zero(a), !i);
151 }
152 }
153
154 void extended_int_test1()
155 {
156 typedef extended_int<1> eint32;
157 eint32 e1(0), e2(32), e3(-32);
158 BOOST_TEST_EQ(e1.count(), 0);
159 BOOST_TEST_EQ(e1.size(), 0U);
160 BOOST_TEST_EQ(e2.count(), 1);
161 BOOST_TEST_EQ(e2.chunks()[0], 32U);
162 BOOST_TEST_EQ(e2.size(), 1U);
163 BOOST_TEST_EQ(e3.count(), -1);
164 BOOST_TEST_EQ(e3.chunks()[0], 32U);
165 BOOST_TEST_EQ(e3.size(), 1U);
166 }
167
168 void extended_int_test2()
169 {
170 typedef extended_int<2> eint64;
171 int64 val64 = 0x7fffffffffffffffLL;
172 eint64 e1(0), e2(32), e3(-32), e4(val64), e5(-val64);
173 BOOST_TEST_EQ(e1.count(), 0);
174 BOOST_TEST_EQ(e2.count(), 1);
175 BOOST_TEST_EQ(e2.chunks()[0], 32U);
176 BOOST_TEST_EQ(e3.count(), -1);
177 BOOST_TEST_EQ(e3.chunks()[0], 32U);
178 BOOST_TEST_EQ(e4.count(), 2);
179 BOOST_TEST_EQ(e4.chunks()[0], 0xffffffff);
180 BOOST_TEST_EQ(e4.chunks()[1], val64 >> 32);
181 BOOST_TEST_EQ(e5.count(), -2);
182 BOOST_TEST_EQ(e5.chunks()[0], 0xffffffff);
183 BOOST_TEST_EQ(e5.chunks()[1], val64 >> 32);
184 }
185
186 void extended_int_test3()
187 {
188 typedef extended_int<2> eint64;
189 std::vector<uint32> chunks;
190 chunks.push_back(1);
191 chunks.push_back(2);
192 eint64 e1(chunks, true), e2(chunks, false);
193 BOOST_TEST_EQ(e1.count(), 2);
194 BOOST_TEST_EQ(e1.chunks()[0], 2U);
195 BOOST_TEST_EQ(e1.chunks()[1], 1U);
196 BOOST_TEST_EQ(e2.count(), -2);
197 BOOST_TEST_EQ(e2.chunks()[0], 2U);
198 BOOST_TEST_EQ(e2.chunks()[1], 1U);
199 }
200
201 void extended_int_test4()
202 {
203 typedef extended_int<2> eint64;
204 std::vector<uint32> chunks;
205 chunks.push_back(1);
206 chunks.push_back(2);
207 eint64 e1(chunks, true), e2(chunks, false);
208 BOOST_TEST_EQ(e1 == e2, false);
209 BOOST_TEST_EQ(e1 == -e2, true);
210 BOOST_TEST_EQ(e1 != e2, true);
211 BOOST_TEST_EQ(e1 != -e2, false);
212 BOOST_TEST_EQ(e1 < e2, false);
213 BOOST_TEST_EQ(e1 < -e2, false);
214 BOOST_TEST_EQ(e1 <= e2, false);
215 BOOST_TEST_EQ(e1 <= -e2, true);
216 BOOST_TEST_EQ(e1 > e2, true);
217 BOOST_TEST_EQ(e1 > -e2, false);
218 BOOST_TEST_EQ(e1 >= e2, true);
219 BOOST_TEST_EQ(e1 >= -e2, true);
220 }
221
222 void extended_int_test5()
223 {
224 typedef extended_int<2> eint64;
225 boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
226 for (int i = 0; i < 1000; ++i) {
227 int64 i1 = static_cast<int64>(gen());
228 int64 i2 = static_cast<int64>(gen());
229 eint64 e1(i1), e2(i2);
230 BOOST_TEST_EQ(e1 == e2, i1 == i2);
231 BOOST_TEST_EQ(e1 != e2, i1 != i2);
232 BOOST_TEST_EQ(e1 > e2, i1 > i2);
233 BOOST_TEST_EQ(e1 >= e2, i1 >= i2);
234 BOOST_TEST_EQ(e1 < e2, i1 < i2);
235 BOOST_TEST_EQ(e1 <= e2, i1 <= i2);
236 }
237 }
238
239 void extended_int_test6()
240 {
241 typedef extended_int<1> eint32;
242 eint32 e1(32);
243 eint32 e2 = -e1;
244 BOOST_TEST_EQ(e2.count(), -1);
245 BOOST_TEST_EQ(e2.size(), 1U);
246 BOOST_TEST_EQ(e2.chunks()[0], 32U);
247 }
248
249 void extended_int_test7()
250 {
251 typedef extended_int<2> eint64;
252 boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
253 for (int i = 0; i < 1000; ++i) {
254 int64 i1 = static_cast<int64>(gen()) >> 2;
255 int64 i2 = static_cast<int64>(gen()) >> 2;
256 eint64 e1(i1), e2(i2), e3(i1 + i2), e4(i1 - i2);
257 BOOST_TEST(e1 + e2 == e3);
258 BOOST_TEST(e1 - e2 == e4);
259 }
260 }
261
262 void extended_int_test8()
263 {
264 typedef extended_int<2> eint64;
265 boost::mt19937 gen(static_cast<uint32>(time(NULL)));
266 for (int i = 0; i < 1000; ++i) {
267 int64 i1 = static_cast<int32>(gen());
268 int64 i2 = static_cast<int32>(gen());
269 eint64 e1(i1), e2(i2), e3(i1 * i2);
270 BOOST_TEST(e1 * e2 == e3);
271 }
272 }
273
274 void extended_int_test9()
275 {
276 typedef extended_int<1> eint32;
277 for (int i = -10; i <= 10; ++i) {
278 for (int j = -10; j <= 10; ++j) {
279 eint32 e1(i), e2(j), e3(i+j), e4(i-j), e5(i*j);
280 BOOST_TEST(e1 + e2 == e3);
281 BOOST_TEST(e1 - e2 == e4);
282 BOOST_TEST(e1 * e2 == e5);
283 }
284 }
285 }
286
287 void extended_int_test10()
288 {
289 typedef extended_int<2> eint64;
290 boost::mt19937_64 gen(static_cast<uint32>(time(NULL)));
291 for (int i = 0; i < 100; ++i) {
292 int64 i1 = static_cast<int64>(gen()) >> 20;
293 int64 i2 = i1 >> 32;
294 eint64 e1(i1), e2(i2);
295 BOOST_TEST(to_fpt(e1) == static_cast<fpt64>(i1));
296 BOOST_TEST(to_fpt(e2) == static_cast<fpt64>(i2));
297 }
298 }
299
300 void extened_int_test11()
301 {
302 typedef extended_int<64> eint2048;
303 eint2048 two(2), value(1);
304 for (int i = 0; i < 1024; ++i)
305 value = value * two;
306 BOOST_TEST_EQ(value.count(), 33);
307 for (std::size_t i = 1; i < value.size(); ++i)
308 BOOST_TEST_EQ(value.chunks()[i-1], 0U);
309 BOOST_TEST_EQ(value.chunks()[32], 1U);
310 }
311
312 int main()
313 {
314 ulp_comparison_test1();
315 ulp_comparison_test2();
316 extended_exponent_fpt_test1();
317 extended_exponent_fpt_test2();
318 extended_exponent_fpt_test3();
319 extended_exponent_fpt_test4();
320 extended_exponent_fpt_test5();
321 extended_exponent_fpt_test6();
322 extended_int_test1();
323 extended_int_test2();
324 extended_int_test3();
325 extended_int_test4();
326 extended_int_test5();
327 extended_int_test6();
328 extended_int_test7();
329 extended_int_test8();
330 extended_int_test9();
331 extended_int_test10();
332 extened_int_test11();
333 return boost::report_errors();
334 }