OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
vector_unit_tests.hpp
1 #ifndef VECTOR_UNIT_TESTS_HPP
2 #define VECTOR_UNIT_TESTS_HPP
3 
4 #include "map_vector.hpp"
5 #include "Point_test.hpp"
6 #include "memory/ExtPreAlloc.hpp"
7 #include "memory/PtrMemory.hpp"
8 #include <cstring>
9 #include "Space/Shape/Point.hpp"
10 #include "util/object_util.hpp"
11 #include "vector_test_util.hpp"
12 
13 BOOST_AUTO_TEST_SUITE( vector_test )
14 
15 #define V_REM_PUSH 1024ul
16 
17 template <typename vector> void test_iterator()
18 {
20 
21  vector v_ofp_test = allocate_openfpm<vector>(FIRST_PUSH);
22 
23  size_t count = 0;
24 
25  auto it = v_ofp_test.getIterator();
26 
27  while (it.isNext())
28  {
29  count++;
30 
31  ++it;
32  }
33 
34  BOOST_REQUIRE_EQUAL(count,v_ofp_test.size());
35 
36  count = 0;
37  auto it_f = v_ofp_test.getIteratorFrom( FIRST_PUSH / 2 );
38 
39  while (it_f.isNext())
40  {
41  count++;
42 
43  ++it_f;
44  }
45 
46  BOOST_REQUIRE_EQUAL(count, v_ofp_test.size() / 2 );
47 }
48 
49 template <typename vector> void test_vector_use()
50 {
51  std::vector<Point_orig<float>> v_stl_test = allocate_stl();
52  vector v_ofp_test = allocate_openfpm<vector>(FIRST_PUSH);
53 
54  // try to duplicate the vector
55  vector dv_ofp_test = v_ofp_test.duplicate();
56 
57  // Check if the STL and openfpm match
58 
59  for (size_t i = 0; i < FIRST_PUSH; i++)
60  {
61  BOOST_REQUIRE_EQUAL(v_stl_test[i].v[0],v_ofp_test.template get<P::v>(i)[0]);
62  BOOST_REQUIRE_EQUAL(v_stl_test[i].v[1],v_ofp_test.template get<P::v>(i)[1]);
63  BOOST_REQUIRE_EQUAL(v_stl_test[i].v[2],v_ofp_test.template get<P::v>(i)[2]);
64 
65  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[0][0],v_ofp_test.template get<P::t>(i)[0][0]);
66  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[0][1],v_ofp_test.template get<P::t>(i)[0][1]);
67  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[0][2],v_ofp_test.template get<P::t>(i)[0][2]);
68  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[1][0],v_ofp_test.template get<P::t>(i)[1][0]);
69  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[1][1],v_ofp_test.template get<P::t>(i)[1][1]);
70  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[1][2],v_ofp_test.template get<P::t>(i)[1][2]);
71  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[2][0],v_ofp_test.template get<P::t>(i)[2][0]);
72  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[2][1],v_ofp_test.template get<P::t>(i)[2][1]);
73  BOOST_REQUIRE_EQUAL(v_stl_test[i].t[2][2],v_ofp_test.template get<P::t>(i)[2][2]);
74  }
75 
76  // Check if the duplicated vector match
77 
78  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
79  {
80  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::v>(i)[0],v_ofp_test.template get<P::v>(i)[0]);
81  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::v>(i)[1],v_ofp_test.template get<P::v>(i)[1]);
82  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::v>(i)[2],v_ofp_test.template get<P::v>(i)[2]);
83 
84  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[0][0],v_ofp_test.template get<P::t>(i)[0][0]);
85  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[0][1],v_ofp_test.template get<P::t>(i)[0][1]);
86  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[0][2],v_ofp_test.template get<P::t>(i)[0][2]);
87  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[1][0],v_ofp_test.template get<P::t>(i)[1][0]);
88  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[1][1],v_ofp_test.template get<P::t>(i)[1][1]);
89  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[1][2],v_ofp_test.template get<P::t>(i)[1][2]);
90  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[2][0],v_ofp_test.template get<P::t>(i)[2][0]);
91  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[2][1],v_ofp_test.template get<P::t>(i)[2][1]);
92  BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[2][2],v_ofp_test.template get<P::t>(i)[2][2]);
93  }
94 }
95 
96 template <typename vector> void test_vector_remove()
97 {
98  typedef Point_test<float> p;
99 
101 
102  vector v1;
103 
104  for (size_t i = 0 ; i < V_REM_PUSH ; i++)
105  {
106  // Point
108  p.setx(i);
109 
110  v1.add(p);
111  }
112 
113  {
115  rem.add(0);
116  rem.add(1);
117  rem.add(2);
118  rem.add(3);
119 
120  v1.remove(rem);
121  }
122 
124 
125  BOOST_REQUIRE_EQUAL(v1.size(),1020ul);
126  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(0),4);
127 
128  {
130  rem.add(v1.size()-3);
131  rem.add(v1.size()-2);
132  rem.add(v1.size()-1);
133  rem.add(v1.size());
134 
135  v1.remove(rem);
136  }
137 
138  BOOST_REQUIRE_EQUAL(v1.size(),1016ul);
139  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(v1.size()-1),1019);
140 
141  {
143  for (size_t i = 0 ; i < (V_REM_PUSH - 8) / 2 ; i++)
144  rem.add(i * 2);
145 
146  // remove all the even number
147  v1.remove(rem);
148  }
149 
150  BOOST_REQUIRE_EQUAL(v1.size(),508ul);
151 
152  // Check only odd
153  for (size_t i = 0 ; i < v1.size() ; i++)
154  {
155  BOOST_REQUIRE_EQUAL((size_t)v1.template get<p::x>(v1.size()-1) % 2, 1ul);
156  }
157 }
158 
159 
160 template <typename vector> void test_vector_remove_aggregate()
161 {
162  typedef Point_test<float> p;
163 
165 
166  vector v1;
167 
168  for (size_t i = 0 ; i < V_REM_PUSH ; i++)
169  {
170  // Point
172  p.setx(i);
173 
174  v1.add(p);
175  }
176 
177  {
179  rem.add();
180  rem.last().get<0>() = 0;
181  rem.add();
182  rem.last().get<0>() = 1;
183  rem.add();
184  rem.last().get<0>() = 2;
185  rem.add();
186  rem.last().get<0>() = 3;
187 
188  v1.remove(rem);
189  }
190 
192 
193  BOOST_REQUIRE_EQUAL(v1.size(),1020ul);
194  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(0),4);
195 
196  {
198  rem.add();
199  rem.last().get<0>() = v1.size()-3;
200  rem.add();
201  rem.last().get<0>() = v1.size()-2;
202  rem.add();
203  rem.last().get<0>() = v1.size()-1;
204  rem.add();
205  rem.last().get<0>() = v1.size();
206 
207  v1.remove(rem);
208  }
209 
210  BOOST_REQUIRE_EQUAL(v1.size(),1016ul);
211  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(v1.size()-1),1019);
212 
213  {
215  for (size_t i = 0 ; i < (V_REM_PUSH - 8) / 2 ; i++)
216  {
217  rem.add();
218  rem.last().get<0>() = i * 2;
219  }
220  // remove all the even number
221  v1.remove(rem);
222  }
223 
224  BOOST_REQUIRE_EQUAL(v1.size(),508ul);
225 
226  // Check only odd
227  for (size_t i = 0 ; i < v1.size() ; i++)
228  {
229  BOOST_REQUIRE_EQUAL((size_t)v1.template get<p::x>(v1.size()-1) % 2, 1ul);
230  }
231 }
232 
233 template <typename vector> void test_vector_insert()
234 {
235  typedef Point_test<float> p;
236 
237  vector v1;
238 
239  for (size_t i = 0 ; i < V_REM_PUSH ; i++)
240  {
241  // Point
243  p.setx(i);
244 
245  v1.add(p);
246  }
247 
248  BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH);
249 
250  // Add one at the first element
251 
252  v1.insert(0);
253  v1.template get<p::x>(0) = -9999.0;
254 
255  // Add one in the middle
256 
257  v1.insert(V_REM_PUSH / 2);
258  v1.template get<p::x>(V_REM_PUSH / 2) = -9999.0;
259 
260  // Add one at the end
261 
262  v1.insert(v1.size()-1);
263  v1.template get<p::x>(v1.size()-1) = -9999.0;
264 
265  BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH + 3);
266 
267  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(0), -9999.0);
268  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(V_REM_PUSH / 2), -9999.0);
269  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(v1.size()-1), -9999.0);
270 
271  size_t c = 0;
272 
273  // Check only odd
274  for (size_t i = 0 ; i < v1.size() ; i++)
275  {
276  if (i == 0 || i == V_REM_PUSH / 2 || i == v1.size()-1)
277  continue;
278 
279  BOOST_REQUIRE_EQUAL((size_t)v1.template get<p::x>(i), c);
280 
281  c++;
282  }
283 }
284 
285 template <typename vector> void test_vector_clear()
286 {
287  vector v1;
288 
289  for (size_t i = 0 ; i < V_REM_PUSH ; i++)
290  {
291  // Point
293  p.setx(i);
294 
295  v1.add(p);
296  }
297 
298  v1.clear();
299 
300  BOOST_REQUIRE_EQUAL(v1.size(),0ul);
301 
302  for (size_t i = 0 ; i < V_REM_PUSH ; i++)
303  {
304  // Point
306  p.setx(i);
307 
308  v1.add(p);
309  }
310 
311  BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH);
312 }
313 
314 template <typename vector, template <typename> class layout_base> void test_vector_add_test_case()
315 {
316  // create two vector
317  vector v1;
318 
319  // Point
321  p.setx(1.0);
322  p.sety(2.0);
323  p.setz(3.0);
324  p.sets(4.0);
325 
326  // push objects
327 
328  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
329  {
330  // Modify p
331 
332  p.get<P::v>()[0] = 1.0 + i;
333  p.get<P::v>()[1] = 2.0 + i;
334  p.get<P::v>()[2] = 7.0 + i;
335 
336  p.get<P::t>()[0][0] = 10.0 + i;
337  p.get<P::t>()[0][1] = 13.0 + i;
338  p.get<P::t>()[0][2] = 8.0 + i;
339  p.get<P::t>()[1][0] = 19.0 + i;
340  p.get<P::t>()[1][1] = 23.0 + i;
341  p.get<P::t>()[1][2] = 5.0 + i;
342  p.get<P::t>()[2][0] = 4.0 + i;
343  p.get<P::t>()[2][1] = 3.0 + i;
344  p.get<P::t>()[2][2] = 11.0 + i;
345 
346  // add p
347 
348  v1.add(p);
349  }
350 
351  // Duplicate the vector
352  vector v2 = v1.duplicate();
353 
354  v1.template add_prp<Point_test<float>,HeapMemory,typename openfpm::grow_policy_double,OPENFPM_NATIVE,layout_base,P::x,P::y,P::z,P::s,P::v,P::t>(v2);
355 
356  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
357  {
358  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0], v1.template get<P::v>(i+v2.size())[0]);
359  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1], v1.template get<P::v>(i+v2.size())[1]);
360  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2], v1.template get<P::v>(i+v2.size())[2]);
361 
362  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0], v1.template get<P::t>(i+v2.size())[0][0]);
363  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1], v1.template get<P::t>(i+v2.size())[0][1]);
364  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2], v1.template get<P::t>(i+v2.size())[0][2]);
365  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0], v1.template get<P::t>(i+v2.size())[1][0]);
366  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1], v1.template get<P::t>(i+v2.size())[1][1]);
367  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2], v1.template get<P::t>(i+v2.size())[1][2]);
368  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0], v1.template get<P::t>(i+v2.size())[2][0]);
369  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1], v1.template get<P::t>(i+v2.size())[2][1]);
370  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2], v1.template get<P::t>(i+v2.size())[2][2]);
371  }
372 
373  // add homogeneous
374 
375  v1.template add(v2);
376 
377  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
378  {
379  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0], v1.template get<P::v>(i+2*v2.size())[0]);
380  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1], v1.template get<P::v>(i+2*v2.size())[1]);
381  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2], v1.template get<P::v>(i+2*v2.size())[2]);
382 
383  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0], v1.template get<P::t>(i+2*v2.size())[0][0]);
384  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1], v1.template get<P::t>(i+2*v2.size())[0][1]);
385  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2], v1.template get<P::t>(i+2*v2.size())[0][2]);
386  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0], v1.template get<P::t>(i+2*v2.size())[1][0]);
387  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1], v1.template get<P::t>(i+2*v2.size())[1][1]);
388  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2], v1.template get<P::t>(i+2*v2.size())[1][2]);
389  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0], v1.template get<P::t>(i+2*v2.size())[2][0]);
390  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1], v1.template get<P::t>(i+2*v2.size())[2][1]);
391  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2], v1.template get<P::t>(i+2*v2.size())[2][2]);
392  }
393 }
394 
395 template <typename vector> void test_vector_copy_and_compare()
396 {
397  {
399 
400  v1.add();
401  v1.last().add(1.0);
402  v1.last().add(10.0);
403  v1.last().add(11.0);
404  v1.last().add(21.0);
405  v1.last().add(13.0);
406  v1.add();
407  v1.last().add(11.0);
408  v1.last().add(41.0);
409  v1.last().add(61.0);
410  v1.last().add(91.0);
411  v1.add();
412  v1.last().add(133.0);
413  v1.last().add(221.0);
414 
416 
417  v2 = v1;
418 
419  bool ret = (v2 == v1);
420  BOOST_REQUIRE_EQUAL(ret,true);
421 
422  v1.get(2).get(1) = 222.0;
423 
424  ret = (v2 == v1);
425  BOOST_REQUIRE_EQUAL(ret,false);
426  }
427 
428  {
429  Box<3,float> bt({1.2,1.3,1.5},{6.0,7.0,8.0});
430 
432 
433  v1.add();
434  v1.last().add(bt);
435  v1.last().add(bt);
436  v1.last().add(bt);
437  v1.last().add(bt);
438  v1.last().add(bt);
439  v1.add();
440  v1.last().add(bt);
441  v1.last().add(bt);
442  v1.last().add(bt);
443  v1.last().add(bt);
444  v1.add();
445  v1.last().add(bt);
446  v1.last().add(bt);
447 
449 
450  v2 = v1;
451 
452  bool ret = (v2 == v1);
453  BOOST_REQUIRE_EQUAL(ret,true);
454 
455  v1.get(2).get(1).template get<Box<3,float>::p1>()[0] = 222.0;
456 
457  ret = (v2 == v1);
458  BOOST_REQUIRE_EQUAL(ret,false);
459  }
460 }
461 
462 template <typename vector> void test_vector_load_and_save_check()
463 {
465 
466  for (size_t i = 0; i < 5; i++)
467  {
468  v1.add();
469  for (size_t j = 0; j < 6; j++)
470  {
471  v1.get(i).add(j);
472  }
473  }
474 
475  v1.save("test_save");
477  v2.load("test_save");
478 
479  // check the v1 and v2 match
480 
481  BOOST_REQUIRE_EQUAL(v1.size(),v2.size());
482  for (size_t i = 0; i < v1.size(); i++)
483  {
484  BOOST_REQUIRE_EQUAL(v1.get(i).size(),v2.get(i).size());
485  for (size_t j = 0; j < 6; j++)
486  {
487  BOOST_REQUIRE_EQUAL(v1.get(i).get(j),v2.get(i).get(j));
488  }
489  }
490 }
491 
492 // Test vector iterator
493 
494 BOOST_AUTO_TEST_CASE (vector_iterator_test)
495 {
496  test_iterator< openfpm::vector<Point_test<float>> >();
497  test_iterator< openfpm::vector<Point_test<float>,HeapMemory,memory_traits_inte> >();
498 }
499 
500 // Test the openfpm vector
501 
502 BOOST_AUTO_TEST_CASE( vector_use)
503 {
504  std::cout << "Vector unit test start" << "\n";
505 
506  test_vector_use<openfpm::vector<Point_test<float>>>();
507  test_vector_use< openfpm::vector<Point_test<float>,HeapMemory,memory_traits_inte> >();
508 
509  std::cout << "Vector unit test end" << "\n";
510 }
511 
512 
513 size_t alloc[] = {235,345,0,520};
514 size_t n_alloc = sizeof(alloc)/sizeof(size_t);
515 
516 
517 BOOST_AUTO_TEST_CASE(vector_remove )
518 {
519  test_vector_remove<openfpm::vector<Point_test<float>>>();
520  test_vector_remove< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >();
521 }
522 
523 BOOST_AUTO_TEST_CASE(vector_remove_aggregate )
524 {
525  test_vector_remove_aggregate<openfpm::vector<Point_test<float>>>();
526  test_vector_remove_aggregate< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >();
527 }
528 
529 BOOST_AUTO_TEST_CASE(vector_insert )
530 {
531  test_vector_insert<openfpm::vector<Point_test<float>>>();
532  test_vector_insert< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte > >();
533 }
534 
535 BOOST_AUTO_TEST_CASE(vector_clear )
536 {
537  test_vector_clear< openfpm::vector<Point_test<float>> >();
538  test_vector_clear< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >();
539 }
540 
541 BOOST_AUTO_TEST_CASE( vector_add_test_case )
542 {
543  test_vector_add_test_case<openfpm::vector<Point_test<float>>,memory_traits_lin>();
544  test_vector_add_test_case<openfpm::vector<Point_test<float>,HeapMemory,memory_traits_inte>, memory_traits_inte >();
545 }
546 
547 BOOST_AUTO_TEST_CASE( vector_copy_and_compare )
548 {
549  test_vector_copy_and_compare< openfpm::vector<Point_test<float>> >();
550  test_vector_copy_and_compare< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >();
551 }
552 
553 BOOST_AUTO_TEST_CASE( vector_load_and_save_check )
554 {
555  test_vector_load_and_save_check< openfpm::vector<Point_test<float>> >();
556  test_vector_load_and_save_check< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >();
557 }
558 
561 
562 BOOST_AUTO_TEST_CASE( vector_safety_check )
563 {
564 #if defined(SE_CLASS1) && defined (THROW_ON_ERROR)
565 
566  bool error = false;
567 
568  typedef Point_test<float> p;
569 
570  // Create a vector
571 
574 
575  error = false;
576  try
577  {v.template get<p::x>(23);}
578  catch (std::exception & e)
579  {
580  error = true;
581  BOOST_REQUIRE_EQUAL(e.what(),"Runtime vector error");
582  }
583  BOOST_REQUIRE_EQUAL(error,true);
584 
585  error = false;
587  try
588  {v.set(23,t);}
589  catch (std::exception & e)
590  {
591  error = true;
592  BOOST_REQUIRE_EQUAL(e.what(),"Runtime vector error");
593  }
594  BOOST_REQUIRE_EQUAL(error,true);
595 
596  error = false;
597  try
598  {v.set(6,v2,23);}
599  catch (std::exception & e)
600  {
601  error = true;
602  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
603  }
604  BOOST_REQUIRE_EQUAL(error,true);
605 
607 
608  error = false;
609  try
610  {v.template get<p::x>(-1);}
611  catch (std::exception & e)
612  {
613  error = true;
614  BOOST_REQUIRE_EQUAL(e.what(),"Runtime vector error");
615  }
616  BOOST_REQUIRE_EQUAL(error,true);
617 
618  error = false;
620  try
621  {v.set(-1,t2);}
622  catch (std::exception & e)
623  {
624  error = true;
625  BOOST_REQUIRE_EQUAL(e.what(),"Runtime vector error");
626  }
627  BOOST_REQUIRE_EQUAL(error,true);
628 
629  error = false;
630  try
631  {v.set(12,v2,-1);}
632  catch (std::exception & e)
633  {
634  error = true;
635  BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error");
636  }
637  BOOST_REQUIRE_EQUAL(error,true);
638 
639 #endif
640 }
641 
642 BOOST_AUTO_TEST_CASE( object_test_creator )
643 {
644  bool tst = std::is_same< typename object_creator<Point_test<float>::type,0,1,5>::type, typename boost::fusion::vector3<float,float,float[3][3]> >::value;
645 
646  BOOST_REQUIRE_EQUAL(tst , true);
647 }
648 
649 BOOST_AUTO_TEST_CASE( vector_memory_repr )
650 {
651  // create a vector
653 
654  // Point
656  p.setx(1.0);
657  p.sety(2.0);
658  p.setz(3.0);
659  p.sets(4.0);
660 
661  // push objects
662 
663  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
664  {
665  // Modify p
666 
667  p.get<P::v>()[0] = 1.0 + i;
668  p.get<P::v>()[1] = 2.0 + i;
669  p.get<P::v>()[2] = 7.0 + i;
670 
671  p.get<P::t>()[0][0] = 10.0 + i;
672  p.get<P::t>()[0][1] = 13.0 + i;
673  p.get<P::t>()[0][2] = 8.0 + i;
674  p.get<P::t>()[1][0] = 19.0 + i;
675  p.get<P::t>()[1][1] = 23.0 + i;
676  p.get<P::t>()[1][2] = 5.0 + i;
677  p.get<P::t>()[2][0] = 4.0 + i;
678  p.get<P::t>()[2][1] = 3.0 + i;
679  p.get<P::t>()[2][2] = 11.0 + i;
680 
681  // add p
682 
683  v1.add(p);
684  }
685 
686  PtrMemory * ptr1 = new PtrMemory(v1.getPointer(),sizeof(Point_test<float>)*FIRST_PUSH);
687 
688  // create vector representation to a piece of memory already allocated
689 
691 
692  v2.setMemory(*ptr1);
693 
694  v2.resize(FIRST_PUSH);
695 
696  // check
697 
698  // Check if the duplicated vector match
699 
700  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
701  {
702  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0],v2.template get<P::v>(i)[0]);
703  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1],v2.template get<P::v>(i)[1]);
704  BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2],v2.template get<P::v>(i)[2]);
705 
706  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0],v2.template get<P::t>(i)[0][0]);
707  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1],v2.template get<P::t>(i)[0][1]);
708  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2],v2.template get<P::t>(i)[0][2]);
709  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0],v2.template get<P::t>(i)[1][0]);
710  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1],v2.template get<P::t>(i)[1][1]);
711  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2],v2.template get<P::t>(i)[1][2]);
712  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0],v2.template get<P::t>(i)[2][0]);
713  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1],v2.template get<P::t>(i)[2][1]);
714  BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2],v2.template get<P::t>(i)[2][2]);
715  }
716 }
717 
718 BOOST_AUTO_TEST_CASE( vector_std_utility )
719 {
721 
722  // Create a vector with 13 element
724 
725  // add at the end some othe element
726  pb.add(0);
727  pb.add(1);
728  pb.add(2);
729 
730  // access the vector
731  for (size_t i = 0 ; i < 16 ; i++)
732  {
733  pb.get(i) = i+1;
734  }
735 
737 
738  pb.fill(0);
739 
740  // Check is zero
741  for (size_t i = 0 ; i < 16 ; i++)
742  {
743  BOOST_REQUIRE_EQUAL(pb.get(i),0ul);
744  }
745 
746 }
747 
748 BOOST_AUTO_TEST_CASE ( vector_prealloc_ext )
749 {
750  // Memory for the ghost sending buffer
751  HeapMemory mem;
752 
753  // sequence of pre-allocation pattern
754  std::vector<size_t> pap;
755 
756  size_t total = 0;
757 
759 
760  // Calculate the total size required for the sending buffer
761  for (size_t i = 0 ; i < n_alloc ; i++)
762  {
763  size_t alloc_ele = vect.calculateMem(alloc[i],0);
764  pap.push_back(alloc_ele);
765  total += alloc_ele;
766  }
767 
768  // Create an object of preallocated memory
769  ExtPreAlloc<HeapMemory> * prAlloc = new ExtPreAlloc<HeapMemory>(total,mem);
770 
772 
773  // create a vector of send_vector (ExtPreAlloc warrant that all the created vector are contiguous)
775 
776  // resize
777  g_send.resize(n_alloc);
778 
779  // Number of allocation
780  for (size_t i = 0 ; i < n_alloc ; i++)
781  {
782  // set the preallocated memory to ensure contiguity
783  g_send.get(i).setMemory(*prAlloc);
784 
785  // resize the sending vector (No allocation is produced)
786  g_send.get(i).resize(alloc[i]);
787  }
788 
789  // Fill the send buffer with one
790  for (size_t i = 0 ; i < n_alloc ; i++)
791  {
792  auto it = g_send.get(i).getIterator();
793  auto & v = g_send.get(i);
794 
795  while(it.isNext())
796  {
797  auto kk = it.get();
798 
799  v.template get<P::x>(kk) = 1.0f;
800  v.template get<P::y>(kk) = 1.0f;
801  v.template get<P::z>(kk) = 1.0f;
802  v.template get<P::s>(kk) = 1.0f;
803 
804  v.template get<P::v>(kk)[0] = 1.0f;
805  v.template get<P::v>(kk)[1] = 1.0f;
806  v.template get<P::v>(kk)[2] = 1.0f;
807 
808  v.template get<P::t>(kk)[0][0] = 1.0f;
809  v.template get<P::t>(kk)[0][1] = 1.0f;
810  v.template get<P::t>(kk)[0][2] = 1.0f;
811  v.template get<P::t>(kk)[1][0] = 1.0f;
812  v.template get<P::t>(kk)[1][1] = 1.0f;
813  v.template get<P::t>(kk)[1][2] = 1.0f;
814  v.template get<P::t>(kk)[2][0] = 1.0f;
815  v.template get<P::t>(kk)[2][1] = 1.0f;
816  v.template get<P::t>(kk)[2][2] = 1.0f;
817 
818  ++it;
819  }
820  }
821 
822  // In SE_CLASS3 the memory layout is different disable this check
823 
824 #ifndef SE_CLASS3
825 
826  // check that HeapMemory contain ones in the right position
827  float * ptr = (float *) mem.getPointer();
828  size_t offset = 0;
829 
830  for (size_t i = 0 ; i < n_alloc ; i++)
831  {
832  for (size_t j = 0 ; j < alloc[i] ; j++)
833  BOOST_REQUIRE_EQUAL(ptr[j + offset/sizeof(float)],1.0f);
834 
835  offset += pap[i];
836  }
837 
838 #endif
839 
840  /* coverity[leaked_storage] */
841 }
842 
843 #ifdef CUDA_GPU
844 
845 BOOST_AUTO_TEST_CASE ( test_gpu_iterator )
846 {
848 
849  pl.resize(10);
850 
851  {
852  auto ite = pl.getGPUIterator();
853 
854  BOOST_REQUIRE_EQUAL(ite.wthr.x,1ul);
855  BOOST_REQUIRE_EQUAL(ite.wthr.x,1ul);
856  BOOST_REQUIRE_EQUAL(ite.wthr.x,1ul);
857 
858  BOOST_REQUIRE_EQUAL(ite.thr.x,10ul);
859  BOOST_REQUIRE_EQUAL(ite.thr.y,1ul);
860  BOOST_REQUIRE_EQUAL(ite.thr.z,1ul);
861  }
862 
863  pl.resize(33);
864 
865  {
866  auto ite = pl.getGPUIterator(32);
867 
868  BOOST_REQUIRE_EQUAL(ite.wthr.x,2ul);
869  BOOST_REQUIRE_EQUAL(ite.wthr.y,1ul);
870  BOOST_REQUIRE_EQUAL(ite.wthr.z,1ul);
871 
872  BOOST_REQUIRE_EQUAL(ite.thr.x,32ul);
873  BOOST_REQUIRE_EQUAL(ite.thr.y,1ul);
874  BOOST_REQUIRE_EQUAL(ite.thr.z,1ul);
875  }
876 }
877 
878 #endif
879 
880 BOOST_AUTO_TEST_CASE( vector_cuda_to_kernel_recursive )
881 {
887 
893 
894  bool test = std::is_same<tker1,openfpm::vector_gpu_ker<aggregate<int, long>, memory_traits_inte>>::value;
895 
896  BOOST_REQUIRE_EQUAL(test,true);
897 
898  test = std::is_same<tker2,openfpm::vector_gpu_ker<aggregate<int, openfpm::vector_gpu_ker<aggregate<long>, memory_traits_inte> >, memory_traits_inte>>::value;
899 
900  BOOST_REQUIRE_EQUAL(test,true);
901 
902  test = std::is_same<tker3,openfpm::vector_gpu_ker<aggregate<int, openfpm::vector_gpu_ker<aggregate<Box<2,float>>, memory_traits_inte> >, memory_traits_inte>>::value;
903 
904  BOOST_REQUIRE_EQUAL(test,true);
905 
906  test = std::is_same<tker4,openfpm::vector_gpu_ker<Box<3,float>,memory_traits_inte>>::value;
907 
908  BOOST_REQUIRE_EQUAL(test,true);
909 
910  test = std::is_same<tker5,openfpm::vector_gpu_ker<aggregate<int, openfpm::vector_gpu_ker<Box<2,float>, memory_traits_inte> >, memory_traits_inte>>::value;
911 
912  BOOST_REQUIRE_EQUAL(test,true);
913 }
914 
915 BOOST_AUTO_TEST_SUITE_END()
916 
917 #endif
Transform the boost::fusion::vector into memory specification (memory_traits)
Grow policy define how the vector should grow every time we exceed the size.
void setz(T z_)
set the z property
Definition: Point_test.hpp:190
static const unsigned int s
s property is at position 3 in the boost::fusion::vector
Definition: Point_test.hpp:143
size_t size()
Stub size.
Definition: map_vector.hpp:211
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:83
virtual void * getPointer()
get a readable pointer with the data
Definition: HeapMemory.cpp:228
Grow policy define how the vector should grow every time we exceed the size.
It model an expression expr1 * expr2.
Definition: mul.hpp:119
void setx(T x_)
set the x property
Definition: Point_test.hpp:176
static const unsigned int y
y property is at position 1 in the boost::fusion::vector
Definition: Point_test.hpp:137
static const unsigned int x
x property is at position 0 in the boost::fusion::vector
Definition: Point_test.hpp:134
static const unsigned int z
z property is at position 2 in the boost::fusion::vector
Definition: Point_test.hpp:140
auto get() -> decltype(boost::fusion::at_c< i >(data))
getter method for a general property i
Definition: Point_test.hpp:219
void sets(T s_)
set the s property
Definition: Point_test.hpp:197
void sety(T y_)
set the y property
Definition: Point_test.hpp:183
static const unsigned int t
t property is at position 5 in the boost::fusion::vector
Definition: Point_test.hpp:149
Test structure used for several test.
Definition: Point_test.hpp:105
static const unsigned int v
v property is at position 4 in the boost::fusion::vector
Definition: Point_test.hpp:146
This class give memory from a preallocated memory, memory destruction is not performed.
Definition: PtrMemory.hpp:39