1 #ifndef VECTOR_UNIT_TESTS_HPP
2 #define VECTOR_UNIT_TESTS_HPP
4 #include "map_vector.hpp"
5 #include "Point_test.hpp"
6 #include "memory/PreAllocHeapMemory.hpp"
7 #include "memory/ExtPreAlloc.hpp"
8 #include "memory/PtrMemory.hpp"
10 #include "Space/Shape/Point.hpp"
11 #include "util/object_util.hpp"
12 #include "vector_test_util.hpp"
14 BOOST_AUTO_TEST_SUITE( vector_test )
18 BOOST_AUTO_TEST_CASE (vector_iterator_test)
26 auto it = v_ofp_test.getIterator();
35 BOOST_REQUIRE_EQUAL(count,v_ofp_test.size());
38 auto it_f = v_ofp_test.getIteratorFrom( FIRST_PUSH / 2 );
47 BOOST_REQUIRE_EQUAL(count, v_ofp_test.size() / 2 );
52 BOOST_AUTO_TEST_CASE( vector_use)
54 std::cout <<
"Vector unit test start" <<
"\n";
56 std::vector<Point_orig<float>> v_stl_test = allocate_stl();
64 for (
size_t i = 0; i < FIRST_PUSH; i++)
66 BOOST_REQUIRE_EQUAL(v_stl_test[i].v[0],v_ofp_test.template get<P::v>(i)[0]);
67 BOOST_REQUIRE_EQUAL(v_stl_test[i].v[1],v_ofp_test.template get<P::v>(i)[1]);
68 BOOST_REQUIRE_EQUAL(v_stl_test[i].v[2],v_ofp_test.template get<P::v>(i)[2]);
70 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[0][0],v_ofp_test.template get<P::t>(i)[0][0]);
71 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[0][1],v_ofp_test.template get<P::t>(i)[0][1]);
72 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[0][2],v_ofp_test.template get<P::t>(i)[0][2]);
73 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[1][0],v_ofp_test.template get<P::t>(i)[1][0]);
74 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[1][1],v_ofp_test.template get<P::t>(i)[1][1]);
75 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[1][2],v_ofp_test.template get<P::t>(i)[1][2]);
76 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[2][0],v_ofp_test.template get<P::t>(i)[2][0]);
77 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[2][1],v_ofp_test.template get<P::t>(i)[2][1]);
78 BOOST_REQUIRE_EQUAL(v_stl_test[i].t[2][2],v_ofp_test.template get<P::t>(i)[2][2]);
83 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
85 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::v>(i)[0],v_ofp_test.template get<P::v>(i)[0]);
86 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::v>(i)[1],v_ofp_test.template get<P::v>(i)[1]);
87 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::v>(i)[2],v_ofp_test.template get<P::v>(i)[2]);
89 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[0][0],v_ofp_test.template get<P::t>(i)[0][0]);
90 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[0][1],v_ofp_test.template get<P::t>(i)[0][1]);
91 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[0][2],v_ofp_test.template get<P::t>(i)[0][2]);
92 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[1][0],v_ofp_test.template get<P::t>(i)[1][0]);
93 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[1][1],v_ofp_test.template get<P::t>(i)[1][1]);
94 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[1][2],v_ofp_test.template get<P::t>(i)[1][2]);
95 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[2][0],v_ofp_test.template get<P::t>(i)[2][0]);
96 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[2][1],v_ofp_test.template get<P::t>(i)[2][1]);
97 BOOST_REQUIRE_EQUAL(dv_ofp_test.template get<P::t>(i)[2][2],v_ofp_test.template get<P::t>(i)[2][2]);
100 std::cout <<
"Vector unit test end" <<
"\n";
113 BOOST_AUTO_TEST_CASE( vector_std_utility )
126 for (
size_t i = 0 ; i < 16 ; i++)
136 for (
size_t i = 0 ; i < 16 ; i++)
138 BOOST_REQUIRE_EQUAL(pb.get(i),0ul);
143 size_t alloc[] = {235,345,0,520};
144 size_t n_alloc =
sizeof(alloc)/
sizeof(
size_t);
146 BOOST_AUTO_TEST_CASE ( vector_prealloc_ext )
152 std::vector<size_t> pap;
159 for (
size_t i = 0 ; i < n_alloc ; i++)
161 size_t alloc_ele = vect.calculateMem(alloc[i],0);
162 pap.push_back(alloc_ele);
167 ExtPreAlloc<HeapMemory> * prAlloc =
new ExtPreAlloc<HeapMemory>(pap,mem);
175 g_send.resize(n_alloc);
178 for (
size_t i = 0 ; i < n_alloc ; i++)
181 g_send.get(i).setMemory(*prAlloc);
184 g_send.get(i).resize(alloc[i]);
188 for (
size_t i = 0 ; i < n_alloc ; i++)
190 auto it = g_send.get(i).getIterator();
191 auto & v = g_send.get(i);
197 v.template get<P::x>(kk) = 1.0f;
198 v.template get<P::y>(kk) = 1.0f;
199 v.template get<P::z>(kk) = 1.0f;
200 v.template get<P::s>(kk) = 1.0f;
202 v.template get<P::v>(kk)[0] = 1.0f;
203 v.template get<P::v>(kk)[1] = 1.0f;
204 v.template get<P::v>(kk)[2] = 1.0f;
206 v.template get<P::t>(kk)[0][0] = 1.0f;
207 v.template get<P::t>(kk)[0][1] = 1.0f;
208 v.template get<P::t>(kk)[0][2] = 1.0f;
209 v.template get<P::t>(kk)[1][0] = 1.0f;
210 v.template get<P::t>(kk)[1][1] = 1.0f;
211 v.template get<P::t>(kk)[1][2] = 1.0f;
212 v.template get<P::t>(kk)[2][0] = 1.0f;
213 v.template get<P::t>(kk)[2][1] = 1.0f;
214 v.template get<P::t>(kk)[2][2] = 1.0f;
221 float * ptr = (
float *) mem.getPointer();
224 for (
size_t i = 0 ; i < n_alloc ; i++)
226 for (
size_t j = 0 ; j < alloc[i] ; j++)
227 BOOST_REQUIRE_EQUAL(ptr[j + offset/
sizeof(
float)],1.0f);
234 BOOST_AUTO_TEST_CASE( vector_prealloc )
238 for (
size_t i = 0 ; i < 3 ; i++)
242 size_t s1 = vect1.calculateMem(1024,0);
243 openfpm::vector<Point_test<float>> vect2;
244 size_t s2 = vect2.calculateMem(1024,0);
247 size_t sz[2] = {s1,s2};
248 PreAllocHeapMemory<2> * mem =
new PreAllocHeapMemory<2>(sz);
251 pb.get(i).pos.setMemory(*mem);
252 pb.get(i).prp.setMemory(*mem);
255 pb.get(i).pos.resize(1024);
256 pb.get(i).prp.resize(1024);
261 BOOST_AUTO_TEST_CASE( object_test_creator )
263 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;
265 BOOST_REQUIRE_EQUAL(tst ,
true);
268 #define V_REM_PUSH 1024ul
270 BOOST_AUTO_TEST_CASE(vector_remove )
276 openfpm::vector<Point_test<float>> v1;
278 for (
size_t i = 0 ; i < V_REM_PUSH ; i++)
299 BOOST_REQUIRE_EQUAL(v1.size(),1020ul);
300 BOOST_REQUIRE_EQUAL(v1.template get<p::x>(0),4);
304 rem.add(v1.size()-3);
305 rem.add(v1.size()-2);
306 rem.add(v1.size()-1);
312 BOOST_REQUIRE_EQUAL(v1.size(),1016ul);
313 BOOST_REQUIRE_EQUAL(v1.template get<p::x>(v1.size()-1),1019);
317 for (
size_t i = 0 ; i < (V_REM_PUSH - 8) / 2 ; i++)
324 BOOST_REQUIRE_EQUAL(v1.size(),508ul);
327 for (
size_t i = 0 ; i < v1.size() ; i++)
329 BOOST_REQUIRE_EQUAL((
size_t)v1.template get<p::x>(v1.size()-1) % 2, 1ul);
333 BOOST_AUTO_TEST_CASE(vector_clear )
337 openfpm::vector<Point_test<float>> v1;
339 for (
size_t i = 0 ; i < V_REM_PUSH ; i++)
350 BOOST_REQUIRE_EQUAL(v1.size(),0ul);
352 for (
size_t i = 0 ; i < V_REM_PUSH ; i++)
361 BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH);
364 BOOST_AUTO_TEST_CASE( vector_memory_repr )
367 openfpm::vector<Point_test<float>> v1;
378 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
382 p.get<P::v>()[0] = 1.0 + i;
383 p.get<P::v>()[1] = 2.0 + i;
384 p.get<P::v>()[2] = 7.0 + i;
386 p.get<P::t>()[0][0] = 10.0 + i;
387 p.get<P::t>()[0][1] = 13.0 + i;
388 p.get<P::t>()[0][2] = 8.0 + i;
389 p.get<P::t>()[1][0] = 19.0 + i;
390 p.get<P::t>()[1][1] = 23.0 + i;
391 p.get<P::t>()[1][2] = 5.0 + i;
392 p.get<P::t>()[2][0] = 4.0 + i;
393 p.get<P::t>()[2][1] = 3.0 + i;
394 p.get<P::t>()[2][2] = 11.0 + i;
401 PtrMemory * ptr1 =
new PtrMemory(v1.getPointer(),
sizeof(
Point_test<float>)*FIRST_PUSH);
409 v2.resize(FIRST_PUSH);
415 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
417 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0],v2.template get<P::v>(i)[0]);
418 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1],v2.template get<P::v>(i)[1]);
419 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2],v2.template get<P::v>(i)[2]);
421 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0],v2.template get<P::t>(i)[0][0]);
422 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1],v2.template get<P::t>(i)[0][1]);
423 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2],v2.template get<P::t>(i)[0][2]);
424 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0],v2.template get<P::t>(i)[1][0]);
425 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1],v2.template get<P::t>(i)[1][1]);
426 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2],v2.template get<P::t>(i)[1][2]);
427 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0],v2.template get<P::t>(i)[2][0]);
428 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1],v2.template get<P::t>(i)[2][1]);
429 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2],v2.template get<P::t>(i)[2][2]);
433 BOOST_AUTO_TEST_CASE( vector_add_test_case )
436 openfpm::vector<Point_test<float>> v1;
447 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
451 p.get<P::v>()[0] = 1.0 + i;
452 p.get<P::v>()[1] = 2.0 + i;
453 p.get<P::v>()[2] = 7.0 + i;
455 p.get<P::t>()[0][0] = 10.0 + i;
456 p.get<P::t>()[0][1] = 13.0 + i;
457 p.get<P::t>()[0][2] = 8.0 + i;
458 p.get<P::t>()[1][0] = 19.0 + i;
459 p.get<P::t>()[1][1] = 23.0 + i;
460 p.get<P::t>()[1][2] = 5.0 + i;
461 p.get<P::t>()[2][0] = 4.0 + i;
462 p.get<P::t>()[2][1] = 3.0 + i;
463 p.get<P::t>()[2][2] = 11.0 + i;
471 openfpm::vector<Point_test<float>> v2 = v1.duplicate();
475 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
477 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0], v1.template get<P::v>(i+v2.size())[0]);
478 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1], v1.template get<P::v>(i+v2.size())[1]);
479 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2], v1.template get<P::v>(i+v2.size())[2]);
481 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0], v1.template get<P::t>(i+v2.size())[0][0]);
482 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1], v1.template get<P::t>(i+v2.size())[0][1]);
483 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2], v1.template get<P::t>(i+v2.size())[0][2]);
484 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0], v1.template get<P::t>(i+v2.size())[1][0]);
485 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1], v1.template get<P::t>(i+v2.size())[1][1]);
486 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2], v1.template get<P::t>(i+v2.size())[1][2]);
487 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0], v1.template get<P::t>(i+v2.size())[2][0]);
488 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1], v1.template get<P::t>(i+v2.size())[2][1]);
489 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2], v1.template get<P::t>(i+v2.size())[2][2]);
496 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
498 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0], v1.template get<P::v>(i+2*v2.size())[0]);
499 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1], v1.template get<P::v>(i+2*v2.size())[1]);
500 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2], v1.template get<P::v>(i+2*v2.size())[2]);
502 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0], v1.template get<P::t>(i+2*v2.size())[0][0]);
503 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1], v1.template get<P::t>(i+2*v2.size())[0][1]);
504 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2], v1.template get<P::t>(i+2*v2.size())[0][2]);
505 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0], v1.template get<P::t>(i+2*v2.size())[1][0]);
506 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1], v1.template get<P::t>(i+2*v2.size())[1][1]);
507 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2], v1.template get<P::t>(i+2*v2.size())[1][2]);
508 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0], v1.template get<P::t>(i+2*v2.size())[2][0]);
509 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1], v1.template get<P::t>(i+2*v2.size())[2][1]);
510 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2], v1.template get<P::t>(i+2*v2.size())[2][2]);
524 BOOST_AUTO_TEST_CASE( vector_copy_and_compare )
541 v1.last().add(133.0);
542 v1.last().add(221.0);
548 bool ret = (v2 == v1);
549 BOOST_REQUIRE_EQUAL(ret,
true);
551 v1.get(2).get(1) = 222.0;
554 BOOST_REQUIRE_EQUAL(ret,
false);
581 bool ret = (v2 == v1);
582 BOOST_REQUIRE_EQUAL(ret,
true);
584 v1.get(2).get(1).template get<Box<3,float>::p1>()[0] = 222.0;
587 BOOST_REQUIRE_EQUAL(ret,
false);
594 BOOST_AUTO_TEST_CASE( vector_safety_check )
596 #if defined(SE_CLASS1) && defined (THROW_ON_ERROR)
604 openfpm::vector<Point_test<float>> v(16);
605 openfpm::vector<Point_test<float>> v2(16);
609 {v.template get<p::x>(23);}
613 BOOST_REQUIRE_EQUAL(e,VECTOR_ERROR);
614 BOOST_REQUIRE_EQUAL(v.getLastError(),2001);
616 BOOST_REQUIRE_EQUAL(error,
true);
625 BOOST_REQUIRE_EQUAL(e,VECTOR_ERROR);
626 BOOST_REQUIRE_EQUAL(v.getLastError(),2001);
628 BOOST_REQUIRE_EQUAL(error,
true);
636 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
638 BOOST_REQUIRE_EQUAL(error,
true);
644 {v.template get<p::x>(-1);}
648 BOOST_REQUIRE_EQUAL(e,VECTOR_ERROR);
649 BOOST_REQUIRE_EQUAL(v.getLastError(),2001);
651 BOOST_REQUIRE_EQUAL(error,
true);
660 BOOST_REQUIRE_EQUAL(e,VECTOR_ERROR);
661 BOOST_REQUIRE_EQUAL(v.getLastError(),2001);
663 BOOST_REQUIRE_EQUAL(error,
true);
671 BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
673 BOOST_REQUIRE_EQUAL(error,
true);
675 #if defined(SE_CLASS2) && defined (THROW_ON_ERROR)
691 BOOST_REQUIRE_EQUAL(e,MEM_ERROR);
693 BOOST_REQUIRE_EQUAL(error,
true);
702 BOOST_REQUIRE_EQUAL(e,MEM_ERROR);
704 BOOST_REQUIRE_EQUAL(error,
true);
711 BOOST_AUTO_TEST_CASE( vector_load_and_save_check )
715 for (
size_t i = 0; i < 5; i++)
718 for (
size_t j = 0; j < 6; j++)
724 v1.save(
"test_save");
728 v2.load(
"test_save");
732 BOOST_REQUIRE_EQUAL(v1.size(),v2.size());
733 for (
size_t i = 0; i < v1.size(); i++)
735 BOOST_REQUIRE_EQUAL(v1.get(i).size(),v2.get(i).size());
736 for (
size_t j = 0; j < 6; j++)
738 BOOST_REQUIRE_EQUAL(v1.get(i).get(j),v2.get(i).get(j));
743 BOOST_AUTO_TEST_SUITE_END()
Grow policy define how the vector should grow every time we exceed the size.
openfpm::vector< Point< 2, float >, PreAllocHeapMemory< 2 >, openfpm::grow_policy_identity > pos
position vector
openfpm::vector< Point_test< float >, PreAllocHeapMemory< 2 >, openfpm::grow_policy_identity > prp
properties vector
Grow policy define how the vector should grow every time we exceed the size.
This class represent an N-dimensional box.
Test structure used for several test.
Implementation of 1-D std::vector like structure.