OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
zmorton_unit_tests.cpp
1/*
2 * zmorton_unit_tests.hpp
3 *
4 * Created on: Aug 1, 2019
5 * Author: i-bird
6 */
7
8#ifndef ZMORTON_UNIT_TESTS_HPP_
9#define ZMORTON_UNIT_TESTS_HPP_
10
11#define BOOST_TEST_DYN_LINK
12#include <boost/test/unit_test.hpp>
13
14#include "util/zmorton.hpp"
15
16template<typename T>
17bool check(size_t res, grid_key_dx<2,T> & k)
18{
19 bool check = true;
20
21 check &= (k.get(0) & 0x1) == (res & 0x1);
22 check &= (k.get(0) & (0x1ul << 1)) == ((res & (0x1ul << 2)) >> 1);
23 check &= (k.get(0) & (0x1ul << 2)) == ((res & (0x1ul << 4)) >> 2);
24 check &= (k.get(0) & (0x1ul << 3)) == ((res & (0x1ul << 6)) >> 3);
25 check &= (k.get(0) & (0x1ul << 4)) == ((res & (0x1ul << 8)) >> 4);
26 check &= (k.get(0) & (0x1ul << 5)) == ((res & (0x1ul << 10)) >> 5);
27 check &= (k.get(0) & (0x1ul << 6)) == ((res & (0x1ul << 12)) >> 6);
28 check &= (k.get(0) & (0x1ul << 7)) == ((res & (0x1ul << 14)) >> 7);
29 check &= (k.get(0) & (0x1ul << 8)) == ((res & (0x1ul << 16)) >> 8);
30 check &= (k.get(0) & (0x1ul << 9)) == ((res & (0x1ul << 19)) >> 9);
31 check &= (k.get(0) & (0x1ul << 10)) == ((res & (0x1ul << 20)) >> 10);
32 check &= (k.get(0) & (0x1ul << 11)) == ((res & (0x1ul << 22)) >> 11);
33 check &= (k.get(0) & (0x1ul << 12)) == ((res & (0x1ul << 24)) >> 12);
34 check &= (k.get(0) & (0x1ul << 13)) == ((res & (0x1ul << 26)) >> 13);
35 check &= (k.get(0) & (0x1ul << 14)) == ((res & (0x1ul << 28)) >> 14);
36 check &= (k.get(0) & (0x1ul << 15)) == ((res & (0x1ul << 30)) >> 15);
37 check &= (k.get(0) & (0x1ul << 16)) == ((res & (0x1ul << 32)) >> 16);
38 check &= (k.get(0) & (0x1ul << 17)) == ((res & (0x1ul << 34)) >> 17);
39 check &= (k.get(0) & (0x1ul << 18)) == ((res & (0x1ul << 36)) >> 19);
40 check &= (k.get(0) & (0x1ul << 19)) == ((res & (0x1ul << 38)) >> 19);
41 check &= (k.get(0) & (0x1ul << 20)) == ((res & (0x1ul << 40)) >> 20);
42 check &= (k.get(0) & (0x1ul << 21)) == ((res & (0x1ul << 42)) >> 21);
43 check &= (k.get(0) & (0x1ul << 22)) == ((res & (0x1ul << 44)) >> 22);
44 check &= (k.get(0) & (0x1ul << 23)) == ((res & (0x1ul << 46)) >> 23);
45 check &= (k.get(0) & (0x1ul << 24)) == ((res & (0x1ul << 48)) >> 24);
46 check &= (k.get(0) & (0x1ul << 25)) == ((res & (0x1ul << 50)) >> 25);
47 check &= (k.get(0) & (0x1ul << 26)) == ((res & (0x1ul << 52)) >> 26);
48 check &= (k.get(0) & (0x1ul << 27)) == ((res & (0x1ul << 54)) >> 27);
49 check &= (k.get(0) & (0x1ul << 28)) == ((res & (0x1ul << 56)) >> 28);
50 check &= (k.get(0) & (0x1ul << 29)) == ((res & (0x1ul << 58)) >> 29);
51 check &= (k.get(0) & (0x1ul << 30)) == ((res & (0x1ul << 60)) >> 30);
52 check &= (k.get(0) & (0x1ul << 31)) == ((res & (0x1ul << 62)) >> 31);
53
54 res = res >> 1;
55
56 check &= (k.get(1) & 0x1) == (res & 0x1);
57 check &= (k.get(1) & (0x1ul << 1)) == ((res & (0x1ul << 2)) >> 1);
58 check &= (k.get(1) & (0x1ul << 2)) == ((res & (0x1ul << 4)) >> 2);
59 check &= (k.get(1) & (0x1ul << 3)) == ((res & (0x1ul << 6)) >> 3);
60 check &= (k.get(1) & (0x1ul << 4)) == ((res & (0x1ul << 8)) >> 4);
61 check &= (k.get(1) & (0x1ul << 5)) == ((res & (0x1ul << 10)) >> 5);
62 check &= (k.get(1) & (0x1ul << 6)) == ((res & (0x1ul << 12)) >> 6);
63 check &= (k.get(1) & (0x1ul << 7)) == ((res & (0x1ul << 14)) >> 7);
64 check &= (k.get(1) & (0x1ul << 8)) == ((res & (0x1ul << 16)) >> 8);
65 check &= (k.get(1) & (0x1ul << 9)) == ((res & (0x1ul << 19)) >> 9);
66 check &= (k.get(1) & (0x1ul << 10)) == ((res & (0x1ul << 20)) >> 10);
67 check &= (k.get(1) & (0x1ul << 11)) == ((res & (0x1ul << 22)) >> 11);
68 check &= (k.get(1) & (0x1ul << 12)) == ((res & (0x1ul << 24)) >> 12);
69 check &= (k.get(1) & (0x1ul << 13)) == ((res & (0x1ul << 26)) >> 13);
70 check &= (k.get(1) & (0x1ul << 14)) == ((res & (0x1ul << 28)) >> 14);
71 check &= (k.get(1) & (0x1ul << 15)) == ((res & (0x1ul << 30)) >> 15);
72 check &= (k.get(1) & (0x1ul << 16)) == ((res & (0x1ul << 32)) >> 16);
73 check &= (k.get(1) & (0x1ul << 17)) == ((res & (0x1ul << 34)) >> 17);
74 check &= (k.get(1) & (0x1ul << 18)) == ((res & (0x1ul << 36)) >> 19);
75 check &= (k.get(1) & (0x1ul << 19)) == ((res & (0x1ul << 38)) >> 19);
76 check &= (k.get(1) & (0x1ul << 20)) == ((res & (0x1ul << 40)) >> 20);
77 check &= (k.get(1) & (0x1ul << 21)) == ((res & (0x1ul << 42)) >> 21);
78 check &= (k.get(1) & (0x1ul << 22)) == ((res & (0x1ul << 44)) >> 22);
79 check &= (k.get(1) & (0x1ul << 23)) == ((res & (0x1ul << 46)) >> 23);
80 check &= (k.get(1) & (0x1ul << 24)) == ((res & (0x1ul << 48)) >> 24);
81 check &= (k.get(1) & (0x1ul << 25)) == ((res & (0x1ul << 50)) >> 25);
82 check &= (k.get(1) & (0x1ul << 26)) == ((res & (0x1ul << 52)) >> 26);
83 check &= (k.get(1) & (0x1ul << 27)) == ((res & (0x1ul << 54)) >> 27);
84 check &= (k.get(1) & (0x1ul << 28)) == ((res & (0x1ul << 56)) >> 28);
85 check &= (k.get(1) & (0x1ul << 29)) == ((res & (0x1ul << 58)) >> 29);
86 check &= (k.get(1) & (0x1ul << 30)) == ((res & (0x1ul << 60)) >> 30);
87 check &= (k.get(1) & (0x1ul << 31)) == ((res & (0x1ul << 62)) >> 31);
88
89 return check;
90}
91
92BOOST_AUTO_TEST_SUITE( zmorton_suite_test )
93
94BOOST_AUTO_TEST_CASE( zmorton_linearization_test )
95{
96 {
97 grid_key_dx<1> key(0);
98
99 BOOST_REQUIRE_EQUAL(lin_zid(key),0);
100
101 grid_key_dx<1> key2(100);
102
103 BOOST_REQUIRE_EQUAL(lin_zid(key2),100);
104 }
105
106 {
107 grid_key_dx<2> key({0,0});
108
109 BOOST_REQUIRE_EQUAL(lin_zid(key),0);
110
111 grid_key_dx<2> key2({2,2});
112
113 BOOST_REQUIRE_EQUAL(lin_zid(key2),12);
114
115 grid_key_dx<2> key3({3,2});
116
117 BOOST_REQUIRE_EQUAL(lin_zid(key3),13);
118
119 grid_key_dx<2> key4({0,3});
120
121 BOOST_REQUIRE_EQUAL(lin_zid(key4),10);
122
123 grid_key_dx<2> key5({3,0});
124
125 BOOST_REQUIRE_EQUAL(lin_zid(key5),5);
126
127 grid_key_dx<2> key6({165,347});
128
129 size_t res = lin_zid(key6);
130
131 BOOST_REQUIRE_EQUAL(check(res,key6),true);
132
133 grid_key_dx<2> key7({0xF,0XF});
134
135 res = lin_zid(key7);
136
137 BOOST_REQUIRE_EQUAL(res,0xFF);
138
139 grid_key_dx<2> key8({0xFF,0XFF});
140
141 res = lin_zid(key8);
142
143 BOOST_REQUIRE_EQUAL(res,0xFFFF);
144
145 grid_key_dx<2> key9({0xFFFF,0XFFFF});
146
147 res = lin_zid(key9);
148
149 BOOST_REQUIRE_EQUAL(res,0xFFFFFFFF);
150
151 grid_key_dx<2> key10({0xFFFFFFFF,0XFFFFFFFF});
152
153 res = lin_zid(key10);
154
155 BOOST_REQUIRE_EQUAL(res,0xFFFFFFFFFFFFFFFF);
156 }
157
158 {
159 grid_key_dx<3> key({0,0,0});
160
161 BOOST_REQUIRE_EQUAL(lin_zid(key),0);
162
163 grid_key_dx<3> key2({2,2,2});
164
165 BOOST_REQUIRE_EQUAL(lin_zid(key2),56);
166
167 grid_key_dx<3> key3({4,4,4});
168
169 BOOST_REQUIRE_EQUAL(lin_zid(key3),448);
170
171 grid_key_dx<3> key4({8,8,8});
172
173 BOOST_REQUIRE_EQUAL(lin_zid(key4),3584);
174
175 grid_key_dx<3> key5({16,16,16});
176
177 BOOST_REQUIRE_EQUAL(lin_zid(key5),(1 << 12) + (1 << 13) + (1 << 14));
178
179 grid_key_dx<3> key6({32,32,32});
180
181 BOOST_REQUIRE_EQUAL(lin_zid(key6),(1 << 15) + (1 << 16) + (1 << 17));
182
183 grid_key_dx<3> key7({64,64,64});
184
185 BOOST_REQUIRE_EQUAL(lin_zid(key7),(1 << 18) + (1 << 19) + (1 << 20));
186
187 grid_key_dx<3> key8({128,128,128});
188
189 BOOST_REQUIRE_EQUAL(lin_zid(key8),(1 << 21) + (1 << 22) + (1 << 23));
190
191 grid_key_dx<3> key9({256,256,256});
192
193 BOOST_REQUIRE_EQUAL(lin_zid(key9),(1 << 24) + (1 << 25) + (1 << 26));
194
195 grid_key_dx<3> key10({512,512,512});
196
197 BOOST_REQUIRE_EQUAL(lin_zid(key10),(1 << 27) + (1 << 28) + (1 << 29));
198
199 grid_key_dx<3> key11({1024,1024,1024});
200
201 BOOST_REQUIRE_EQUAL(lin_zid(key11),(1ul << 30) + (1ul << 31) + (1ul << 32));
202
203 grid_key_dx<3> key12({2048,2048,2048});
204
205 BOOST_REQUIRE_EQUAL(lin_zid(key12),(1ul << 33) + (1ul << 34) + (1ul << 35));
206
207 grid_key_dx<3> key13({4096,4096,4096});
208
209 BOOST_REQUIRE_EQUAL(lin_zid(key13),(1ul << 36) + (1ul << 37) + (1ul << 38));
210
211 grid_key_dx<3> key14({8192,8192,8192});
212
213 BOOST_REQUIRE_EQUAL(lin_zid(key14),(1ul << 39) + (1ul << 40) + (1ul << 41));
214
215 grid_key_dx<3> key15({16384,16384,16384});
216
217 BOOST_REQUIRE_EQUAL(lin_zid(key15),(1ul << 42) + (1ul << 43) + (1ul << 44));
218
219 grid_key_dx<3> key16({32768,32768,32768});
220
221 BOOST_REQUIRE_EQUAL(lin_zid(key16),(1ul << 45) + (1ul << 46) + (1ul << 47));
222
223 grid_key_dx<3> key17({65536,65536,65536});
224
225 BOOST_REQUIRE_EQUAL(lin_zid(key17),(1ul << 48) + (1ul << 49) + (1ul << 50));
226
227 grid_key_dx<3> key18({131072,131072,131072});
228
229 BOOST_REQUIRE_EQUAL(lin_zid(key18),(1ul << 51) + (1ul << 52) + (1ul << 53));
230
231 grid_key_dx<3> key19({262144,262144,262144});
232
233 BOOST_REQUIRE_EQUAL(lin_zid(key19),(1ul << 54) + (1ul << 55) + (1ul << 56));
234
235 grid_key_dx<3> key20({524288,524288,524288});
236
237 BOOST_REQUIRE_EQUAL(lin_zid(key20),(1ul << 57) + (1ul << 58) + (1ul << 59));
238
239 grid_key_dx<3> key21({1048576,1048576,1048576});
240
241 BOOST_REQUIRE_EQUAL(lin_zid(key21),(1ul << 60) + (1ul << 61) + (1ul << 62));
242 }
243}
244
245
246BOOST_AUTO_TEST_CASE( zmorton_invlinearization_test )
247{
248 {
249 grid_key_dx<1> key(0);
250 grid_key_dx<1> ikey;
251
252 size_t lin = lin_zid(key);
253 invlin_zid(lin,ikey);
254
255 BOOST_REQUIRE(key == ikey);
256
257 grid_key_dx<1> key2(100);
258 grid_key_dx<1> ikey2;
259
260 lin = lin_zid(key2);
261 invlin_zid(lin,ikey2);
262
263 BOOST_REQUIRE(key2 == ikey2);
264 }
265
266 {
267 grid_key_dx<2> key({0,0});
268 grid_key_dx<2> ikey;
269
270 size_t lin = lin_zid(key);
271 invlin_zid(lin,ikey);
272
273 BOOST_REQUIRE(key == ikey);
274
275 grid_key_dx<2> key2({2,2});
276 grid_key_dx<2> ikey2;
277
278 lin = lin_zid(key2);
279 invlin_zid(lin,ikey2);
280
281 BOOST_REQUIRE(key2 == ikey2);
282
283 grid_key_dx<2> key3({3,2});
284 grid_key_dx<2> ikey3;
285
286 lin = lin_zid(key3);
287 invlin_zid(lin,ikey3);
288
289 BOOST_REQUIRE(key3 == ikey3);
290
291 grid_key_dx<2> key4({0,3});
292 grid_key_dx<2> ikey4;
293
294 lin = lin_zid(key4);
295 invlin_zid(lin,ikey4);
296
297 BOOST_REQUIRE(key4 == ikey4);
298
299 grid_key_dx<2> key5({3,0});
300 grid_key_dx<2> ikey5({3,0});
301
302 lin = lin_zid(key5);
303 invlin_zid(lin,ikey5);
304
305 BOOST_REQUIRE(key5 == ikey5);
306
307 grid_key_dx<2> key6({165,347});
308 grid_key_dx<2> ikey6;
309
310 lin = lin_zid(key6);
311 invlin_zid(lin,ikey6);
312
313 BOOST_REQUIRE(key6 == ikey6);
314
315 grid_key_dx<2> key7({0xF,0XF});
316 grid_key_dx<2> ikey7;
317
318 lin = lin_zid(key7);
319 invlin_zid(lin,ikey7);
320
321 BOOST_REQUIRE(key7 == ikey7);
322
323 grid_key_dx<2> key8({0xFF,0XFF});
324 grid_key_dx<2> ikey8;
325
326 lin = lin_zid(key8);
327 invlin_zid(lin,ikey8);
328
329 BOOST_REQUIRE(key8 == ikey8);
330
331 grid_key_dx<2> key9({0xFFFF,0XFFFF});
332 grid_key_dx<2> ikey9;
333
334 lin = lin_zid(key9);
335 invlin_zid(lin,ikey9);
336
337 BOOST_REQUIRE(key9 == ikey9);
338
339 grid_key_dx<2> key10({0xFFFFFFFF,0XFFFFFFFF});
340 grid_key_dx<2> ikey10;
341
342 lin = lin_zid(key10);
343 invlin_zid(lin,ikey10);
344
345 BOOST_REQUIRE(key10 == ikey10);
346 }
347
348 {
349 grid_key_dx<3> key({0,0,0});
350 grid_key_dx<3> ikey;
351
352 size_t lin = lin_zid(key);
353 invlin_zid(lin,ikey);
354
355 BOOST_REQUIRE(key == ikey);
356
357 grid_key_dx<3> key2({2,2,2});
358 grid_key_dx<3> ikey2;
359
360 lin = lin_zid(key2);
361 invlin_zid(lin,ikey2);
362
363 BOOST_REQUIRE(key2 == ikey2);
364
365 grid_key_dx<3> key3({4,4,4});
366 grid_key_dx<3> ikey3;
367
368 lin = lin_zid(key3);
369 invlin_zid(lin,ikey3);
370
371 BOOST_REQUIRE(key3 != ikey);
372
373 grid_key_dx<3> key4({8,8,8});
374 grid_key_dx<3> ikey4;
375
376 lin = lin_zid(key4);
377 invlin_zid(lin,ikey4);
378
379 BOOST_REQUIRE(key4 == ikey4);
380
381 grid_key_dx<3> key5({16,16,16});
382 grid_key_dx<3> ikey5;
383
384 lin = lin_zid(key5);
385 invlin_zid(lin,ikey5);
386
387 BOOST_REQUIRE(key5 == ikey5);
388
389 grid_key_dx<3> key6({32,32,32});
390 grid_key_dx<3> ikey6;
391
392 lin = lin_zid(key6);
393 invlin_zid(lin,ikey6);
394
395 BOOST_REQUIRE(key6 == ikey6);
396
397 grid_key_dx<3> key7({64,64,64});
398 grid_key_dx<3> ikey7;
399
400 lin = lin_zid(key7);
401 invlin_zid(lin,ikey7);
402
403 BOOST_REQUIRE(key7 == ikey7);
404
405 grid_key_dx<3> key8({128,128,128});
406 grid_key_dx<3> ikey8;
407
408 lin = lin_zid(key8);
409 invlin_zid(lin,ikey8);
410
411 BOOST_REQUIRE(key8 == ikey8);
412
413 grid_key_dx<3> key9({256,256,256});
414 grid_key_dx<3> ikey9;
415
416 lin = lin_zid(key9);
417 invlin_zid(lin,ikey9);
418
419 BOOST_REQUIRE(key9 == ikey9);
420
421 grid_key_dx<3> key10({512,512,512});
422 grid_key_dx<3> ikey10;
423
424 lin = lin_zid(key10);
425 invlin_zid(lin,ikey10);
426
427 BOOST_REQUIRE(key10 == ikey10);
428
429 grid_key_dx<3> key11({1024,1024,1024});
430 grid_key_dx<3> ikey11;
431
432 lin = lin_zid(key11);
433 invlin_zid(lin,ikey11);
434
435 BOOST_REQUIRE(key11 == ikey11);
436
437 grid_key_dx<3> key12({2048,2048,2048});
438 grid_key_dx<3> ikey12;
439
440 lin = lin_zid(key12);
441 invlin_zid(lin,ikey12);
442
443 BOOST_REQUIRE(key12 == ikey12);
444
445 grid_key_dx<3> key13({4096,4096,4096});
446 grid_key_dx<3> ikey13;
447
448 lin = lin_zid(key13);
449 invlin_zid(lin,ikey13);
450
451 BOOST_REQUIRE(key13 == ikey13);
452
453 grid_key_dx<3> key14({8192,8192,8192});
454 grid_key_dx<3> ikey14;
455
456 lin = lin_zid(key14);
457 invlin_zid(lin,ikey14);
458
459 BOOST_REQUIRE(key14 == ikey14);
460
461 grid_key_dx<3> key15({16384,16384,16384});
462 grid_key_dx<3> ikey15;
463
464 lin = lin_zid(key15);
465 invlin_zid(lin,ikey15);
466
467 BOOST_REQUIRE(key15 == ikey15);
468
469 grid_key_dx<3> key16({32768,32768,32768});
470 grid_key_dx<3> ikey16;
471
472 lin = lin_zid(key16);
473 invlin_zid(lin,ikey16);
474
475 BOOST_REQUIRE(key16 == ikey16);
476
477 grid_key_dx<3> key17({65536,65536,65536});
478 grid_key_dx<3> ikey17;
479
480 lin = lin_zid(key17);
481 invlin_zid(lin,ikey17);
482
483 BOOST_REQUIRE(key17 == ikey17);
484
485 grid_key_dx<3> key18({131072,131072,131072});
486 grid_key_dx<3> ikey18;
487
488 lin = lin_zid(key18);
489 invlin_zid(lin,ikey18);
490
491 BOOST_REQUIRE(key18 == ikey18);
492
493 grid_key_dx<3> key19({262144,262144,262144});
494 grid_key_dx<3> ikey19;
495
496 lin = lin_zid(key19);
497 invlin_zid(lin,ikey19);
498
499 BOOST_REQUIRE(key19 == ikey19);
500
501 grid_key_dx<3> key20({524288,524288,524288});
502 grid_key_dx<3> ikey20;
503
504 lin = lin_zid(key20);
505 invlin_zid(lin,ikey20);
506
507 BOOST_REQUIRE(key20 == ikey20);
508
509 grid_key_dx<3> key21({1048576,1048576,1048576});
510 grid_key_dx<3> ikey21;
511
512 lin = lin_zid(key21);
513 invlin_zid(lin,ikey21);
514
515 BOOST_REQUIRE(key21 == ikey21);
516
517 bool match = true;
518
519 for (unsigned int i = 0 ; i < 100 ; i++)
520 {
521 for (unsigned int j = 0 ; j < 100 ; j++)
522 {
523 for (unsigned int k = 0 ; k < 100 ; k++)
524 {
525 grid_key_dx<3> key23({i,j,k});
526 grid_key_dx<3> ikey23;
527
528 lin = lin_zid(key23);
529 invlin_zid(lin,ikey23);
530
531 match &= key23 == ikey23;
532 }
533 }
534 }
535
536 BOOST_REQUIRE(match == true);
537 }
538}
539
540BOOST_AUTO_TEST_SUITE_END()
541
542
543#endif /* ZMORTON_UNIT_TESTS_HPP_ */
grid_key_dx is the key to access any element in the grid
Definition grid_key.hpp:19