OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
HyperCube_unit_test.hpp
1 #ifndef HYPERCUBE_UNIT_TEST_HPP
2 #define HYPERCUBE_UNIT_TEST_HPP
3 
4 #include "Space/Shape/HyperCube.hpp"
5 #include "util/util_debug.hpp"
6 
13 template<unsigned int dim> void check_lin(std::vector<comb<dim>> cmb)
14 {
15  // Check the linearization
16  for (size_t i = 0 ; i < cmb.size() ; i++)
17  {
18  BOOST_REQUIRE_EQUAL(HyperCube<dim>::LinId(cmb[i]),i);
19  }
20 }
21 
28 template<unsigned int dim> void check_lin_perm(std::vector<comb<dim>> cmb)
29 {
30  // Check the linearization
31  for (size_t i = 0 ; i < cmb.size() ; i++)
32  {
33  BOOST_REQUIRE_EQUAL(HyperCube<dim>::LinPerm(cmb[i]),i);
34  }
35 }
36 
43 template<unsigned int dim> bool isDinstict(std::vector<comb<dim>> combs)
44 {
45  // Check if the combination are dinstinct
46 
47  for (size_t i = 0 ; i < combs.size() ; i++)
48  {
49  for (size_t j = i+1 ; j < combs.size() ; j++)
50  {
51  if (combs[i] == combs[j])
52  return false;
53  }
54 
55  }
56 
57  return true;
58 }
59 
69 template<unsigned int dim> bool isSubdecomposition(std::vector<comb<dim>> combs, comb<dim> c)
70 {
71  for (size_t i = 0 ; i < combs.size() ; i++)
72  {
73  if (c.isSub(combs[i]) == false)
74  return false;
75  }
76 
77  return true;
78 }
79 
80 template<unsigned int dim> bool isValid(std::vector<comb<dim>> combs)
81 {
82  // Check if the combinations are valid
83 
84  for (size_t i = 0 ; i < combs.size() ; i++)
85  {
86  if (combs[i].isValid() == false)
87  return false;
88  }
89 
90  return true;
91 }
92 
93 BOOST_AUTO_TEST_SUITE( Hyper_cube )
94 
95 BOOST_AUTO_TEST_CASE( Hyper_cube_comb_use )
96 {
97  {
98  comb<3> c1({1,2,3});
99  comb<3> c2({1,1,1});
100 
101  comb<3> c_ret = c1 - c2;
102 
103  BOOST_REQUIRE_EQUAL(c_ret.c[0],2);
104  BOOST_REQUIRE_EQUAL(c_ret.c[1],1);
105  BOOST_REQUIRE_EQUAL(c_ret.c[2],0);
106  }
107 
108  {
109  comb<3> c1({1,2,3});
110  comb<3> c2;
111  c2 = c1 & 0x01;
112 
113  BOOST_REQUIRE_EQUAL(c2.c[0],1);
114  BOOST_REQUIRE_EQUAL(c2.c[1],0);
115  BOOST_REQUIRE_EQUAL(c2.c[2],1);
116  }
117 
118  {
119  comb<3> c1({1,2,3});
120  comb<3> c2({1,1,1});
121 
122  comb<3> c_ret = c1 + c2;
123 
124  BOOST_REQUIRE_EQUAL(c_ret.c[0],4);
125  BOOST_REQUIRE_EQUAL(c_ret.c[1],3);
126  BOOST_REQUIRE_EQUAL(c_ret.c[2],2);
127  }
128 
129  {
130  comb<3> c1({1,2,3});
131 
132  comb<3> c_ret = -c1;
133 
134  BOOST_REQUIRE_EQUAL(c_ret.c[0],-3);
135  BOOST_REQUIRE_EQUAL(c_ret.c[1],-2);
136  BOOST_REQUIRE_EQUAL(c_ret.c[2],-1);
137  }
138 
139  {
140  comb<3> c1({-1,1,0});
141 
142  comb<3> c_ret = c1.flip();
143 
144  BOOST_REQUIRE_EQUAL(c_ret.c[0],0);
145  BOOST_REQUIRE_EQUAL(c_ret.c[1],-1);
146  BOOST_REQUIRE_EQUAL(c_ret.c[2],-1);
147  }
148 }
149 
150 BOOST_AUTO_TEST_CASE( Hyper_cube_use)
151 {
152  std::cout << "Hyper-cube unit test start" << "\n";
153  // Test Hyper-Cube functionality
154 
155  size_t ele[8];
156 
158 
159  // Get the number of vertex (elements of dimension 0) of a line (dimension 1)
161  // Get the number of edge (elements of dimension 1) of a line (dimension 1)
163 
164  // Get combination for each dimensions
165  std::vector<comb<1>> v_c1_0 = HyperCube<1>::getCombinations_R(0);
166 
168 
169  // Check
170  BOOST_REQUIRE_EQUAL(ele[0],2ul);
171  BOOST_REQUIRE_EQUAL(ele[1],1ul);
172 
173  // Fill the expected vector
174 
175  comb<1> c[] = {{{1}},{{-1}}};
176 
177  // Check the linearization
178  check_lin(v_c1_0);
179 
180  std::vector<comb<1>> v1_st;
182  check_lin_perm(v1_st);
183 
184  boost_check_array(&c[0],&v_c1_0[0],2);
185 
187  // Number of vertex
189  // Number of edge
191  // Number of faces
193 
194  // Get combination for vertex (1,1) (-1,1) (-1,1) (-1,-1)
195  std::vector<comb<2>> v_c2_0 = HyperCube<2>::getCombinations_R(0);
196  // Get combination for edges (1,0) (-1,0) (0,1) (0,-1)
197  std::vector<comb<2>> v_c2_1 = HyperCube<2>::getCombinations_R(1);
198 
200 
201  // Check
202  BOOST_REQUIRE_EQUAL(ele[0],4ul);
203  BOOST_REQUIRE_EQUAL(ele[1],4ul);
204  BOOST_REQUIRE_EQUAL(ele[2],1ul);
205 
206  // Check combination
207 
208  comb<2> c2_0[] = {{1,1},{-1,1},{1,-1},{-1,-1}};
209  comb<2> c2_1[] = {{0,1},{0,-1},{1,0},{-1,0}};
210  check_lin(v_c2_0);
211  check_lin(v_c2_1);
212 
213  std::vector<comb<2>> v2_st;
215  check_lin_perm(v2_st);
216 
217  boost_check_array(&c2_0[0],&v_c2_0[0],4);
218  boost_check_array(&c2_1[0],&v_c2_1[0],4);
219 
221  // Number of vertex
223  // Number of edge
225  // Number of faces
227  // Number of Cubes
229 
230  // Get combination for vertex
231  std::vector<comb<3>> v_c3_0 = HyperCube<3>::getCombinations_R(0);
232  // Get combinations for edge
233  std::vector<comb<3>> v_c3_1 = HyperCube<3>::getCombinations_R(1);
234  // Get combinations for surfaces
235  std::vector<comb<3>> v_c3_2 = HyperCube<3>::getCombinations_R(2);
236 
238 
239  // Check
240  BOOST_REQUIRE_EQUAL(ele[0],8ul);
241  BOOST_REQUIRE_EQUAL(ele[1],12ul);
242  BOOST_REQUIRE_EQUAL(ele[2],6ul);
243  BOOST_REQUIRE_EQUAL(ele[3],1ul);
244 
245  // Check combination
246 
247  comb<3> c3_0[] = {{1,1,1},{-1,1,1},{1,-1,1},{-1,-1,1},{1,1,-1},{-1,1,-1},{1,-1,-1},{-1,-1,-1}};
248  comb<3> c3_1[] = {{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1},{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0}};
249  comb<3> c3_2[] = {{{0,0,1}},{{0,0,-1}},{{0,1,0}},{{0,-1,0}},{{1,0,0}},{{-1,0,0}}};
250  check_lin(v_c3_0);
251  check_lin(v_c3_1);
252  check_lin(v_c3_2);
253 
254  std::vector<comb<3>> v3_st;
256  check_lin_perm(v3_st);
257 
258  boost_check_array(&c3_0[0],&v_c3_0[0],8);
259  boost_check_array(&c3_1[0],&v_c3_1[0],12);
260  boost_check_array(&c3_2[0],&v_c3_2[0],6);
261 
262  // Tesseract
263  // Number of vertex
265  // Number of edge
267  // Number of faces
269  // Number of Cubes
271  // Number of Tessaract
273 
274  // Get combination for each dimensions
275  std::vector<comb<4>> v_c4_0 = HyperCube<4>::getCombinations_R(0);
276  std::vector<comb<4>> v_c4_1 = HyperCube<4>::getCombinations_R(1);
277  std::vector<comb<4>> v_c4_2 = HyperCube<4>::getCombinations_R(2);
278  std::vector<comb<4>> v_c4_3 = HyperCube<4>::getCombinations_R(3);
279  check_lin(v_c4_0);
280  check_lin(v_c4_1);
281  check_lin(v_c4_2);
282  check_lin(v_c4_3);
283 
284  std::vector<comb<4>> v4_st;
286  check_lin_perm(v1_st);
287 
288  // Check
289  BOOST_REQUIRE_EQUAL(ele[0],16ul);
290  BOOST_REQUIRE_EQUAL(ele[1],32ul);
291  BOOST_REQUIRE_EQUAL(ele[2],24ul);
292  BOOST_REQUIRE_EQUAL(ele[3],8ul);
293  BOOST_REQUIRE_EQUAL(ele[4],1ul);
294 
295  // Penteract
296  // Number of vertex
298  // Number of edge
300  // Number of faces
302  // Number of Cubes
304  // Number of Tessaract
306  // Number of Penteract
308 
309  // Get combination for each dimensions
310  std::vector<comb<5>> v_c5_0 = HyperCube<5>::getCombinations_R(0);
311  std::vector<comb<5>> v_c5_1 = HyperCube<5>::getCombinations_R(1);
312  std::vector<comb<5>> v_c5_2 = HyperCube<5>::getCombinations_R(2);
313  std::vector<comb<5>> v_c5_3 = HyperCube<5>::getCombinations_R(3);
314  std::vector<comb<5>> v_c5_4 = HyperCube<5>::getCombinations_R(4);
315  check_lin(v_c5_0);
316  check_lin(v_c5_1);
317  check_lin(v_c5_2);
318  check_lin(v_c5_3);
319  check_lin(v_c5_4);
320 
321  std::vector<comb<5>> v5_st;
323  check_lin_perm(v5_st);
324 
325  // Check
326  BOOST_REQUIRE_EQUAL(ele[0],32ul);
327  BOOST_REQUIRE_EQUAL(ele[1],80ul);
328  BOOST_REQUIRE_EQUAL(ele[2],80ul);
329  BOOST_REQUIRE_EQUAL(ele[3],40ul);
330  BOOST_REQUIRE_EQUAL(ele[4],10ul);
331  BOOST_REQUIRE_EQUAL(ele[5],1ul);
332 
333 
334  // Test SubHypercube 2D and 3D
335 
337  std::vector<comb<2>> sc2_0 = HyperCube<2>::getCombinations_R(0);
339 
340  for (size_t i = 0 ; i < sc2_0.size() ; i++)
341  {
342  // Expecting one element equal to c2[i]
344  std::vector<comb<2>> combs = SubHyperCube<2,0>::getCombinations_R(sc2_0[i],0);
346  BOOST_REQUIRE_EQUAL(combs.size(),1ul);
347  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
348  BOOST_REQUIRE_EQUAL(isValid(combs),true);
349  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc2_0[i]),true);
350  }
351 
353  std::vector<comb<2>> sc2_1 = HyperCube<2>::getCombinations_R(1);
355 
356  for (size_t i = 0 ; i < sc2_1.size() ; i++)
357  {
358  // Expecting two elements, valid, distinct, sub-decomposition of c2[i]
359 
361  std::vector<comb<2>> combs = SubHyperCube<2,1>::getCombinations_R(sc2_1[i],0);
363  BOOST_REQUIRE_EQUAL(combs.size(),2ul);
364  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
365  BOOST_REQUIRE_EQUAL(isValid(combs),true);
366  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc2_1[i]),true);
367 
368  // Expecting one element, valid, distinct, sub-decomposition of c2[i]
370  combs = SubHyperCube<2,1>::getCombinations_R(sc2_1[i],1);
372  BOOST_REQUIRE_EQUAL(combs.size(),1ul);
373  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
374  BOOST_REQUIRE_EQUAL(isValid(combs),true);
375  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc2_1[i]),true);
376  }
377 
379  std::vector<comb<2>> sc2_2 = HyperCube<2>::getCombinations_R(2);
381 
382  for (size_t i = 0 ; i < sc2_2.size() ; i++)
383  {
384  // Expecting two elements, valid, distinct, sub-decomposition of sc2_2[i]
386  std::vector<comb<2>> combs = SubHyperCube<2,2>::getCombinations_R(sc2_2[i],0);
388  BOOST_REQUIRE_EQUAL(combs.size(),4ul);
389  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
390  BOOST_REQUIRE_EQUAL(isValid(combs),true);
391  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc2_2[i]),true);
392 
393  // Expecting one element, valid, distinct, sub-decomposition of c2[i]
395  combs = SubHyperCube<2,2>::getCombinations_R(sc2_2[i],1);
397  BOOST_REQUIRE_EQUAL(combs.size(),4ul);
398  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
399  BOOST_REQUIRE_EQUAL(isValid(combs),true);
400  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc2_2[i]),true);
401 
402  // Expecting one element, valid, distinct, sub-decomposition of c2[i]
404  combs = SubHyperCube<2,2>::getCombinations_R(sc2_2[i],2);
406  BOOST_REQUIRE_EQUAL(combs.size(),1ul);
407  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
408  BOOST_REQUIRE_EQUAL(isValid(combs),true);
409  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc2_2[i]),true);
410  }
411 
413 
415  std::vector<comb<3>> sc3_0 = HyperCube<3>::getCombinations_R(0);
417 
418  for (size_t i = 0 ; i < sc3_0.size() ; i++)
419  {
420  // Expecting one element equal to sc3[i]
422  std::vector<comb<3>> combs = SubHyperCube<3,0>::getCombinations_R(sc3_0[i],0);
424  BOOST_REQUIRE_EQUAL(combs.size(),1ul);
425  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
426  BOOST_REQUIRE_EQUAL(isValid(combs),true);
427 // BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc3_0[i]),true);
428  }
429 
431  std::vector<comb<3>> sc3_1 = HyperCube<3>::getCombinations_R(1);
433 
434  for (size_t i = 0 ; i < sc3_1.size() ; i++)
435  {
436  // Expecting two elements, valid, distinct, sub-decomposition of sc3[i]
438  std::vector<comb<3>> combs = SubHyperCube<3,1>::getCombinations_R(sc3_1[i],0);
440  BOOST_REQUIRE_EQUAL(combs.size(),2ul);
441  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
442  BOOST_REQUIRE_EQUAL(isValid(combs),true);
443  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc3_1[i]),true);
444 
445  // Expecting one element, valid, distinct, sub-decomposition of c2[i]
447  combs = SubHyperCube<3,1>::getCombinations_R(sc3_1[i],1);
449  BOOST_REQUIRE_EQUAL(combs.size(),1ul);
450  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
451  BOOST_REQUIRE_EQUAL(isValid(combs),true);
452  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc3_1[i]),true);
453  }
454 
456  std::vector<comb<3>> sc3_2 = HyperCube<3>::getCombinations_R(2);
458 
459  for (size_t i = 0 ; i < sc3_2.size() ; i++)
460  {
461  // Expecting two elements, valid, distinct, sub-decomposition of sc3_2[i]
463  std::vector<comb<3>> combs = SubHyperCube<3,2>::getCombinations_R(sc3_2[i],0);
465  BOOST_REQUIRE_EQUAL(combs.size(),4ul);
466  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
467  BOOST_REQUIRE_EQUAL(isValid(combs),true);
468  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc3_2[i]),true);
469 
470  // Expecting one element, valid, distinct, sub-decomposition of c2[i]
472  combs = SubHyperCube<3,2>::getCombinations_R(sc3_2[i],1);
474  BOOST_REQUIRE_EQUAL(combs.size(),4ul);
475  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
476  BOOST_REQUIRE_EQUAL(isValid(combs),true);
477  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc3_2[i]),true);
478 
479  // Expecting one element, valid, distinct, sub-decomposition of c2[i]
481  combs = SubHyperCube<3,2>::getCombinations_R(sc3_2[i],2);
483  BOOST_REQUIRE_EQUAL(combs.size(),1ul);
484  BOOST_REQUIRE_EQUAL(isDinstict(combs),true);
485  BOOST_REQUIRE_EQUAL(isValid(combs),true);
486  BOOST_REQUIRE_EQUAL(isSubdecomposition(combs,sc3_2[i]),true);
487  }
488 
489  std::cout << "Hyper-cube unit test end" << "\n";
490 }
491 
492 BOOST_AUTO_TEST_SUITE_END()
493 
494 #endif
static size_t getNumberOfElements_R(size_t d)
Get the number of Elements of dimension d.
Definition: HyperCube.hpp:64
static std::vector< comb< dim > > getCombinations_R(size_t d)
Definition: HyperCube.hpp:97
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition: comb.hpp:34
static void BinPermutationsSt(std::vector< comb< dim >> &v)
Binary permutations.
Definition: HyperCube.hpp:222
bool isSub(comb< dim > cmb)
Check if the combination is a sub-element.
Definition: comb.hpp:66
comb< dim > flip()
flip the coefficent of the combination to be < 0
Definition: comb.hpp:148
static std::vector< comb< dim > > getCombinations_R(comb< dim > c, int d)
Definition: HyperCube.hpp:427
This class calculate elements of the hyper-cube.
Definition: HyperCube.hpp:54
char c[dim]
Array that store the combination.
Definition: comb.hpp:37