1 #ifndef GRID_UNIT_TEST_HPP
2 #define GRID_UNIT_TEST_HPP
4 #include "map_grid.hpp"
5 #include "Point_test.hpp"
6 #include "Space/Shape/HyperCube.hpp"
8 #include "grid_util_test.hpp"
10 #ifdef TEST_COVERAGE_MODE
16 template<
unsigned int dim,
typename g>
void test_layout_gridNd(g & c3,
size_t sz);
17 template<
unsigned int dim,
typename g>
void test_layout_gridObjNd(g & c3,
size_t sz);
18 template<
typename g>
void test_layout_grid3d(g & c3,
size_t sz);
26 template<
unsigned int dim>
void test_all_grid(
size_t sz)
28 std::vector<size_t> szz;
31 for (
size_t i = 0 ; i < dim ; i++)
37 c3.template setMemory();
38 test_layout_gridNd<dim>(c3,sz);}
41 c3.template setMemory();
42 test_layout_gridObjNd<dim>(c3,sz);}
45 c3.template setMemory();
46 test_layout_gridNd<dim>(c3,sz);}
49 c3.template setMemory();
50 test_layout_gridObjNd<dim>(c3,sz);}
55 c3.template setMemory();
56 test_layout_gridObjNd<dim>(c3,sz);}
59 c3.template setMemory();
60 test_layout_gridNd<dim>(c3,sz);}
63 c3.template setMemory();
64 test_layout_gridNd<dim>(c3,sz);}
67 c3.template setMemory();
68 test_layout_gridObjNd<dim>(c3,sz);}
73 template<
typename g>
void test_layout_grid3d(g & c3,
size_t sz)
77 std::cout <<
"3D Array with grid_key (without redundant dimension): " <<
"\n";
88 for (
size_t i = 0 ; i < sz ; i++)
90 for (
size_t j = 0 ; j < sz ; j++)
92 for (
size_t k = 0 ; k < sz ; k++)
97 c3.template get<P::x>(kk) = 1.1f;
98 c3.template get<P::y>(kk) = 1.2f;
99 c3.template get<P::z>(kk) = 1.3f;
100 c3.template get<P::s>(kk) = 1.0f;
102 c3.template get<P::v>(kk)[0] = 1.0f;
103 c3.template get<P::v>(kk)[1] = 2.0f;
104 c3.template get<P::v>(kk)[2] = 3.0f;
106 c3.template get<P::t>(kk)[0][0] = 1.0f;
107 c3.template get<P::t>(kk)[0][1] = 2.0f;
108 c3.template get<P::t>(kk)[0][2] = 3.0f;
109 c3.template get<P::t>(kk)[1][0] = 4.0f;
110 c3.template get<P::t>(kk)[1][1] = 5.0f;
111 c3.template get<P::t>(kk)[1][2] = 6.0f;
112 c3.template get<P::t>(kk)[2][0] = 7.0f;
113 c3.template get<P::t>(kk)[2][1] = 8.0f;
114 c3.template get<P::t>(kk)[2][2] = 9.0f;
125 std::cout <<
"End : " << sz*sz*sz*16*4 <<
" Byte " <<
" Bandwidth: " << sz*sz*sz*16*4/1024/1024/t.
getwct() <<
" MB/s ";
132 for (
size_t i = 0 ; i < sz ; i++)
134 for (
size_t j = 0 ; j < sz ; j++)
136 for (
size_t k = 0 ; k < sz ; k++)
140 c3.template get<P::x>(kk) = i;
141 c3.template get<P::y>(kk) = j;
142 c3.template get<P::z>(kk) = k;
143 c3.template get<P::s>(kk) = i+j+k;
145 c3.template get<P::v>(kk)[0] = i;
146 c3.template get<P::v>(kk)[1] = j;
147 c3.template get<P::v>(kk)[2] = k;
149 c3.template get<P::t>(kk)[0][0] = i+i;
150 c3.template get<P::t>(kk)[0][1] = i+j;
151 c3.template get<P::t>(kk)[0][2] = i+k;
152 c3.template get<P::t>(kk)[1][0] = j+i;
153 c3.template get<P::t>(kk)[1][1] = j+j;
154 c3.template get<P::t>(kk)[1][2] = j+k;
155 c3.template get<P::t>(kk)[2][0] = k+i;
156 c3.template get<P::t>(kk)[2][1] = k+j;
157 c3.template get<P::t>(kk)[2][2] = k+k;
162 for (
size_t i = 0 ; i < sz ; i++)
164 for (
size_t j = 0 ; j < sz ; j++)
166 for (
size_t k = 0 ; k < sz ; k++)
170 if (c3.template get<P::x>(kk) != i) passed =
false;
171 if (c3.template get<P::y>(kk) != j) passed =
false;
172 if (c3.template get<P::z>(kk) != k) passed =
false;
173 if (c3.template get<P::s>(kk) != i+j+k) passed =
false;
175 if (c3.template get<P::v>(kk)[0] != i) passed =
false;
176 if (c3.template get<P::v>(kk)[1] != j) passed =
false;
177 if (c3.template get<P::v>(kk)[2] != k) passed =
false;
179 if (c3.template get<P::t>(kk)[0][0] != i+i) passed =
false;
180 if (c3.template get<P::t>(kk)[0][1] != i+j) passed =
false;
181 if (c3.template get<P::t>(kk)[0][2] != i+k) passed =
false;
182 if (c3.template get<P::t>(kk)[1][0] != j+i) passed =
false;
183 if (c3.template get<P::t>(kk)[1][1] != j+j) passed =
false;
184 if (c3.template get<P::t>(kk)[1][2] != j+k) passed =
false;
185 if (c3.template get<P::t>(kk)[2][0] != k+i) passed =
false;
186 if (c3.template get<P::t>(kk)[2][1] != k+j) passed =
false;
187 if (c3.template get<P::t>(kk)[2][2] != k+k) passed =
false;
192 BOOST_REQUIRE_EQUAL(passed,
true);
195 template<
unsigned int dim,
typename g>
void test_layout_gridObjNd(g & c3,
size_t sz)
198 std::cout << dim <<
"D Array with grid_key (without redundant dimension): " <<
"\n";
215 auto v = c3.get_o(kk);
219 v.template get<P::x>() = 1.1f;
220 v.template get<P::y>() = 1.2f;
221 v.template get<P::z>() = 1.3f;
222 v.template get<P::s>() = 1.0f;
224 v.template get<P::v>()[0] = 1.0f;
225 v.template get<P::v>()[1] = 2.0f;
226 v.template get<P::v>()[2] = 3.0f;
228 v.template get<P::t>()[0][0] = 1.0f;
229 v.template get<P::t>()[0][1] = 2.0f;
230 v.template get<P::t>()[0][2] = 3.0f;
231 v.template get<P::t>()[1][0] = 4.0f;
232 v.template get<P::t>()[1][1] = 5.0f;
233 v.template get<P::t>()[1][2] = 6.0f;
234 v.template get<P::t>()[2][0] = 7.0f;
235 v.template get<P::t>()[2][1] = 8.0f;
236 v.template get<P::t>()[2][2] = 9.0f;
256 std::cout <<
"End : " << pow(sz,dim)*16*4/1024/1024 <<
" MB " <<
" Bandwidth: " << pow(sz,dim)*16*4/1024/1024/t.getect() <<
" MB/s " <<
"\n";
266 key_it = c3.getIterator();
268 while (key_it.isNext())
272 if (c3.template get<P::x>(kk) != c3.getGrid().LinId(kk)) passed =
false;
273 if (c3.template get<P::y>(kk) != c3.getGrid().LinId(kk)+1) passed =
false;
274 if (c3.template get<P::z>(kk) != c3.getGrid().LinId(kk)+2) passed =
false;
275 if (c3.template get<P::s>(kk) != c3.getGrid().LinId(kk)+3) passed =
false;
277 if (c3.template get<P::v>(kk)[0] != c3.getGrid().LinId(kk)+123) passed =
false;
278 if (c3.template get<P::v>(kk)[1] != c3.getGrid().LinId(kk)+124) passed =
false;
279 if (c3.template get<P::v>(kk)[2] != c3.getGrid().LinId(kk)+125) passed =
false;
281 if (c3.template get<P::t>(kk)[0][0] != c3.getGrid().LinId(kk)+567) passed =
false;
282 if (c3.template get<P::t>(kk)[0][1] != c3.getGrid().LinId(kk)+568) passed =
false;
283 if (c3.template get<P::t>(kk)[0][2] != c3.getGrid().LinId(kk)+569) passed =
false;
284 if (c3.template get<P::t>(kk)[1][0] != c3.getGrid().LinId(kk)+570) passed =
false;
285 if (c3.template get<P::t>(kk)[1][1] != c3.getGrid().LinId(kk)+571) passed =
false;
286 if (c3.template get<P::t>(kk)[1][2] != c3.getGrid().LinId(kk)+572) passed =
false;
287 if (c3.template get<P::t>(kk)[2][0] != c3.getGrid().LinId(kk)+573) passed =
false;
288 if (c3.template get<P::t>(kk)[2][1] != c3.getGrid().LinId(kk)+574) passed =
false;
289 if (c3.template get<P::t>(kk)[2][2] != c3.getGrid().LinId(kk)+575) passed =
false;
294 BOOST_REQUIRE_EQUAL(passed,
true);
297 template<
unsigned int dim,
typename g>
void test_layout_gridNd(g & c3,
size_t sz)
300 std::cout << dim <<
"D Array with grid_key (without redundant dimension): " <<
"\n";
315 c3.template get<P::x>(kk) = 1.1f;
316 c3.template get<P::y>(kk) = 1.2f;
317 c3.template get<P::z>(kk) = 1.3f;
318 c3.template get<P::s>(kk) = 1.0f;
320 c3.template get<P::v>(kk)[0] = 1.0f;
321 c3.template get<P::v>(kk)[1] = 2.0f;
322 c3.template get<P::v>(kk)[2] = 3.0f;
324 c3.template get<P::t>(kk)[0][0] = 1.0f;
325 c3.template get<P::t>(kk)[0][1] = 2.0f;
326 c3.template get<P::t>(kk)[0][2] = 3.0f;
327 c3.template get<P::t>(kk)[1][0] = 4.0f;
328 c3.template get<P::t>(kk)[1][1] = 5.0f;
329 c3.template get<P::t>(kk)[1][2] = 6.0f;
330 c3.template get<P::t>(kk)[2][0] = 7.0f;
331 c3.template get<P::t>(kk)[2][1] = 8.0f;
332 c3.template get<P::t>(kk)[2][2] = 9.0f;
342 std::cout <<
"End : " << pow(sz,dim)*16*4/1024/1024 <<
" MB " <<
" Bandwidth: " << pow(sz,dim)*16*4/1024/1024/t.
getwct() <<
" MB/s " <<
"\n";
349 key_it = c3.getIterator();
355 c3.template get<P::x>(kk) = c3.getGrid().LinId(kk);
356 c3.template get<P::y>(kk) = c3.getGrid().LinId(kk)+1;
357 c3.template get<P::z>(kk) = c3.getGrid().LinId(kk)+2;
358 c3.template get<P::s>(kk) = c3.getGrid().LinId(kk)+3;
360 c3.template get<P::v>(kk)[0] = c3.getGrid().LinId(kk)+123;
361 c3.template get<P::v>(kk)[1] = c3.getGrid().LinId(kk)+124;
362 c3.template get<P::v>(kk)[2] = c3.getGrid().LinId(kk)+125;
364 c3.template get<P::t>(kk)[0][0] = c3.getGrid().LinId(kk)+567;
365 c3.template get<P::t>(kk)[0][1] = c3.getGrid().LinId(kk)+568;
366 c3.template get<P::t>(kk)[0][2] = c3.getGrid().LinId(kk)+569;
367 c3.template get<P::t>(kk)[1][0] = c3.getGrid().LinId(kk)+570;
368 c3.template get<P::t>(kk)[1][1] = c3.getGrid().LinId(kk)+571;
369 c3.template get<P::t>(kk)[1][2] = c3.getGrid().LinId(kk)+572;
370 c3.template get<P::t>(kk)[2][0] = c3.getGrid().LinId(kk)+573;
371 c3.template get<P::t>(kk)[2][1] = c3.getGrid().LinId(kk)+574;
372 c3.template get<P::t>(kk)[2][2] = c3.getGrid().LinId(kk)+575;
378 key_it = c3.getIterator();
384 if (c3.template get<P::x>(kk) != c3.getGrid().LinId(kk)) passed =
false;
385 if (c3.template get<P::y>(kk) != c3.getGrid().LinId(kk)+1) passed =
false;
386 if (c3.template get<P::z>(kk) != c3.getGrid().LinId(kk)+2) passed =
false;
387 if (c3.template get<P::s>(kk) != c3.getGrid().LinId(kk)+3) passed =
false;
389 if (c3.template get<P::v>(kk)[0] != c3.getGrid().LinId(kk)+123) passed =
false;
390 if (c3.template get<P::v>(kk)[1] != c3.getGrid().LinId(kk)+124) passed =
false;
391 if (c3.template get<P::v>(kk)[2] != c3.getGrid().LinId(kk)+125) passed =
false;
393 if (c3.template get<P::t>(kk)[0][0] != c3.getGrid().LinId(kk)+567) passed =
false;
394 if (c3.template get<P::t>(kk)[0][1] != c3.getGrid().LinId(kk)+568) passed =
false;
395 if (c3.template get<P::t>(kk)[0][2] != c3.getGrid().LinId(kk)+569) passed =
false;
396 if (c3.template get<P::t>(kk)[1][0] != c3.getGrid().LinId(kk)+570) passed =
false;
397 if (c3.template get<P::t>(kk)[1][1] != c3.getGrid().LinId(kk)+571) passed =
false;
398 if (c3.template get<P::t>(kk)[1][2] != c3.getGrid().LinId(kk)+572) passed =
false;
399 if (c3.template get<P::t>(kk)[2][0] != c3.getGrid().LinId(kk)+573) passed =
false;
400 if (c3.template get<P::t>(kk)[2][1] != c3.getGrid().LinId(kk)+574) passed =
false;
401 if (c3.template get<P::t>(kk)[2][2] != c3.getGrid().LinId(kk)+575) passed =
false;
406 BOOST_REQUIRE_EQUAL(passed,
true);
435 key_it = c3.getIterator();
441 c3.template get<P::x>(kk) = 0.0;
446 for(
size_t i = 0 ; i <= dim ; i++)
453 for (
size_t j = 0 ; j < combs.size() ; j++)
462 for (
size_t k = 0 ; k < dim ; k++)
466 if (combs[j].c[k] == -1)
471 else if (combs[j].c[k] == 1)
473 start.
set_d(k,c3.getGrid().size(k)-1);
474 stop.
set_d(k,c3.getGrid().size(k)-1);
479 stop.
set_d(k,c3.getGrid().size(k)-2);
483 auto key_it = c3.getSubIterator(start,stop);
489 BOOST_REQUIRE_EQUAL(c3.template get<P::x>(kk),0.0);
491 c3.template get<P::x>(kk) = 1.0;
500 key_it = c3.getIterator();
506 BOOST_REQUIRE_EQUAL(c3.template get<P::x>(kk),1.0);
512 BOOST_AUTO_TEST_SUITE( grid_test )
514 BOOST_AUTO_TEST_CASE( grid_iterator_test_use)
521 size_t div[3] = {16,16,16};
530 while (g_it.isNext())
542 BOOST_REQUIRE_EQUAL(count, (
size_t)16*16*16);
552 size_t div[3] = {16,16,16};
564 while (g_it.isNext())
576 BOOST_REQUIRE_EQUAL(count, (
size_t)14*14*14);
583 bool val = g_it.get() == start;
585 BOOST_REQUIRE_EQUAL(val,
true);
589 BOOST_AUTO_TEST_CASE( grid_sub_iterator_test )
596 size_t div[3] = {16,16,16};
606 auto g_it = g.getIterator(start,stop);
609 while (g_it.isNext())
614 g.template get<p::x>(key) = 1.0;
621 BOOST_REQUIRE_EQUAL(count, (
size_t)14*14*14);
630 size_t sz[] = {16,16,16};
639 BOOST_AUTO_TEST_CASE( grid_safety_check )
641 #if defined(SE_CLASS1) && defined (THROW_ON_ERROR)
647 size_t sz[] = {16,16,16};
661 {g.template get<p::x>(keyOut);}
665 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
666 BOOST_REQUIRE_EQUAL(g.getLastError(),1001);
668 BOOST_REQUIRE_EQUAL(error,
true);
673 {g.template get<p::x>(keyOut);}
677 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
678 BOOST_REQUIRE_EQUAL(g.getLastError(),1002);
680 BOOST_REQUIRE_EQUAL(error,
true);
689 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
690 BOOST_REQUIRE_EQUAL(g.getLastError(),1002);
692 BOOST_REQUIRE_EQUAL(error,
true);
696 {g.set(keyGood,g2,keyOut);}
700 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
701 BOOST_REQUIRE_EQUAL(g.getLastError(),1004);
703 BOOST_REQUIRE_EQUAL(error,
true);
709 {g.template get<p::x>(keyNeg);}
713 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
714 BOOST_REQUIRE_EQUAL(g.getLastError(),1003);
716 BOOST_REQUIRE_EQUAL(error,
true);
725 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
726 BOOST_REQUIRE_EQUAL(g.getLastError(),1003);
728 BOOST_REQUIRE_EQUAL(error,
true);
732 {g.set(keyGood,g2,keyNeg);}
736 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
737 BOOST_REQUIRE_EQUAL(g.getLastError(),1005);
739 BOOST_REQUIRE_EQUAL(error,
true);
741 #if defined(SE_CLASS2) && defined (THROW_ON_ERROR)
757 BOOST_REQUIRE_EQUAL(e,MEM_ERROR);
759 BOOST_REQUIRE_EQUAL(error,
true);
767 BOOST_REQUIRE_EQUAL(e,MEM_ERROR);
769 BOOST_REQUIRE_EQUAL(error,
true);
776 BOOST_AUTO_TEST_CASE( grid_use)
781 std::cout <<
"Grid unit test start" <<
"\n";
783 std::vector<size_t> sz;
784 sz.push_back(GS_SIZE);
785 sz.push_back(GS_SIZE);
786 sz.push_back(GS_SIZE);
793 #ifndef TEST_COVERAGE_MODE
797 test_all_grid<5>(18);
798 test_all_grid<4>(37);
799 test_all_grid<3>(126);
800 test_all_grid<2>(1414);
801 test_all_grid<1>(2000000);
816 test_all_grid<2>(16);
817 test_all_grid<1>(256);
822 for (
int i = 0 ; i <= GS_SIZE ; i++)
836 test_layout_grid3d(c3,i);
841 test_layout_grid3d(c3,i);}
850 test_layout_grid3d(c3,i);}
856 test_layout_grid3d(c3,i);}
860 std::cout <<
"Grid unit test end" <<
"\n";
863 BOOST_AUTO_TEST_CASE( grid_iterator_sp_test )
865 size_t sz[3] = {16,16,16};
873 auto info = c3.getGrid();
886 BOOST_REQUIRE_EQUAL(count,2185ul);
895 BOOST_AUTO_TEST_CASE( C_array_test )
900 std::cout <<
"Grid size known at runtime" <<
"\n";
901 std::cout <<
"1D Array with index calculation: " <<
"\n";
906 int gs_sq = GS_SIZE*GS_SIZE;
914 for (
int i = 0 ; i < GS_SIZE ; i++)
916 for (
int j = 0 ; j < GS_SIZE ; j++)
918 for (
int k = 0 ; k < GS_SIZE ; k++)
920 pA[i*gs_sq+j*gs+k].x = 1.1f;
921 pA[i*gs_sq+j*gs+k].y = 1.2f;
922 pA[i*gs_sq+j*gs+k].z = 1.3f;
923 pA[i*gs_sq+j*gs+k].s = 1.0f;
925 pA[i*gs_sq+j*gs+k].v[0] = 1.0f;
926 pA[i*gs_sq+j*gs+k].v[1] = 2.0f;
927 pA[i*gs_sq+j*gs+k].v[2] = 3.0f;
929 pA[i*gs_sq+j*gs+k].t[0][0] = 1.0f;
930 pA[i*gs_sq+j*gs+k].t[0][1] = 2.0f;
931 pA[i*gs_sq+j*gs+k].t[0][2] = 3.0f;
932 pA[i*gs_sq+j*gs+k].t[1][0] = 4.0f;
933 pA[i*gs_sq+j*gs+k].t[1][1] = 5.0f;
934 pA[i*gs_sq+j*gs+k].t[1][2] = 6.0f;
935 pA[i*gs_sq+j*gs+k].t[2][0] = 7.0f;
936 pA[i*gs_sq+j*gs+k].t[2][1] = 8.0f;
937 pA[i*gs_sq+j*gs+k].t[2][2] = 9.0f;
947 std::cout <<
"End : " << GS_SIZE*GS_SIZE*GS_SIZE*16*4/1024/1024 <<
" MB " <<
" Bandwidth: " << GS_SIZE*GS_SIZE*GS_SIZE*16*4/1024/1024/t.
getcputime() <<
" MB/s \n";
951 BOOST_AUTO_TEST_CASE(grid_operator_equal)
955 size_t sz[] = {16,16};
962 auto it = g1.getIterator();
968 g1.template get<b::p1>(key)[0] = key.
get(0);
969 g1.template get<b::p2>(key)[1] = key.
get(1);
979 bool ret = (g2 == g1);
981 BOOST_REQUIRE_EQUAL(ret,
true);
995 auto it1 = g1.getIterator();
999 auto key = it1.
get();
1001 g1.template get<b::p1>(key)[0] = key.
get(0);
1002 g1.template get<b::p2>(key)[1] = key.
get(1);
1007 auto it2 = g2.getIterator();
1009 while (it2.isNext())
1011 auto key = it2.
get();
1013 g2.template get<b::p1>(key)[0] = key.
get(0);
1014 g2.template get<b::p2>(key)[1] = key.
get(1);
1022 BOOST_AUTO_TEST_CASE(grid_operator_swap)
1024 size_t sz1[] = {16,16};
1025 size_t sz2[] = {5,5};
1038 fill_2_grid_data(g1_old,g2_old);
1039 fill_2_grid_data(g1,g2);
1047 bool ret = (g2 == g1_old);
1048 BOOST_REQUIRE_EQUAL(ret,
true);
1049 ret = (g1 == g2_old);
1050 BOOST_REQUIRE_EQUAL(ret,
true);
1053 BOOST_AUTO_TEST_SUITE_END()
size_t size() const
return the size of the grid
This is an N-dimensional grid or an N-dimensional array with memory_traits_inte layout.
static std::vector< comb< dim > > getCombinations_R(size_t d)
grid_key_dx is the key to access any element in the grid
double getwct()
Return the elapsed real time.
mem_id get(size_t i) const
Get the i index.
double getcputime()
Return the cpu time.
bool isNext()
Check if there is the next element.
void start()
Start the timer.
This class represent an N-dimensional box.
const grid_key_dx< dim > & get()
Get the actual key.
This class is a trick to indicate the compiler a specific specialization pattern. ...
void set(a v, T...t)
set the grid key from a list of numbers
Definition of a class Point in plain C++ and boost::vector for testing purpose.
class that store the information of the grid like number of point on each direction and define the in...
void set_d(size_t i, mem_id id)
Set the i index.
Test structure used for several test.
Class for cpu time benchmarking.
void stop()
Stop the timer.