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/ExtPreAlloc.hpp"
7 #include "memory/PtrMemory.hpp"
9 #include "Space/Shape/Point.hpp"
10 #include "util/object_util.hpp"
11 #include "vector_test_util.hpp"
13 BOOST_AUTO_TEST_SUITE( vector_test )
15 #define V_REM_PUSH 1024ul
17 template <
typename vector>
void test_iterator()
21 vector v_ofp_test = allocate_openfpm<vector>(FIRST_PUSH);
25 auto it = v_ofp_test.getIterator();
34 BOOST_REQUIRE_EQUAL(count,v_ofp_test.size());
37 auto it_f = v_ofp_test.getIteratorFrom( FIRST_PUSH / 2 );
46 BOOST_REQUIRE_EQUAL(count, v_ofp_test.size() / 2 );
49 template <
typename vector>
void test_vector_use()
51 std::vector<Point_orig<float>> v_stl_test = allocate_stl();
52 vector v_ofp_test = allocate_openfpm<vector>(FIRST_PUSH);
55 vector dv_ofp_test = v_ofp_test.duplicate();
59 for (
size_t i = 0; i < FIRST_PUSH; i++)
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]);
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]);
78 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
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]);
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]);
96 template <
typename vector>
void test_vector_remove()
104 for (
size_t i = 0 ; i < V_REM_PUSH ; i++)
125 BOOST_REQUIRE_EQUAL(v1.size(),1020ul);
126 BOOST_REQUIRE_EQUAL(v1.template get<p::x>(0),4);
130 rem.add(v1.size()-3);
131 rem.add(v1.size()-2);
132 rem.add(v1.size()-1);
138 BOOST_REQUIRE_EQUAL(v1.size(),1016ul);
139 BOOST_REQUIRE_EQUAL(v1.template get<p::x>(v1.size()-1),1019);
143 for (
size_t i = 0 ; i < (V_REM_PUSH - 8) / 2 ; i++)
150 BOOST_REQUIRE_EQUAL(v1.size(),508ul);
153 for (
size_t i = 0 ; i < v1.size() ; i++)
155 BOOST_REQUIRE_EQUAL((
size_t)v1.template get<p::x>(v1.size()-1) % 2, 1ul);
159 template <
typename vector>
void test_vector_insert()
165 for (
size_t i = 0 ; i < V_REM_PUSH ; i++)
174 BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH);
179 v1.template get<p::x>(0) = -9999.0;
183 v1.insert(V_REM_PUSH / 2);
184 v1.template get<p::x>(V_REM_PUSH / 2) = -9999.0;
188 v1.insert(v1.size()-1);
189 v1.template get<p::x>(v1.size()-1) = -9999.0;
191 BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH + 3);
193 BOOST_REQUIRE_EQUAL(v1.template get<p::x>(0), -9999.0);
194 BOOST_REQUIRE_EQUAL(v1.template get<p::x>(V_REM_PUSH / 2), -9999.0);
195 BOOST_REQUIRE_EQUAL(v1.template get<p::x>(v1.size()-1), -9999.0);
200 for (
size_t i = 0 ; i < v1.size() ; i++)
202 if (i == 0 || i == V_REM_PUSH / 2 || i == v1.size()-1)
205 BOOST_REQUIRE_EQUAL((
size_t)v1.template get<p::x>(i), c);
211 template <
typename vector>
void test_vector_clear()
215 for (
size_t i = 0 ; i < V_REM_PUSH ; i++)
226 BOOST_REQUIRE_EQUAL(v1.size(),0ul);
228 for (
size_t i = 0 ; i < V_REM_PUSH ; i++)
237 BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH);
240 template <
typename vector,
template <
typename>
class layout_base>
void test_vector_add_test_case()
254 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
262 p.
get<
P::t>()[0][0] = 10.0 + i;
263 p.
get<
P::t>()[0][1] = 13.0 + i;
265 p.
get<
P::t>()[1][0] = 19.0 + i;
266 p.
get<
P::t>()[1][1] = 23.0 + i;
270 p.
get<
P::t>()[2][2] = 11.0 + i;
278 vector v2 = v1.duplicate();
280 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);
282 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
284 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0], v1.template get<P::v>(i+v2.size())[0]);
285 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1], v1.template get<P::v>(i+v2.size())[1]);
286 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2], v1.template get<P::v>(i+v2.size())[2]);
288 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0], v1.template get<P::t>(i+v2.size())[0][0]);
289 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1], v1.template get<P::t>(i+v2.size())[0][1]);
290 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2], v1.template get<P::t>(i+v2.size())[0][2]);
291 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0], v1.template get<P::t>(i+v2.size())[1][0]);
292 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1], v1.template get<P::t>(i+v2.size())[1][1]);
293 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2], v1.template get<P::t>(i+v2.size())[1][2]);
294 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0], v1.template get<P::t>(i+v2.size())[2][0]);
295 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1], v1.template get<P::t>(i+v2.size())[2][1]);
296 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2], v1.template get<P::t>(i+v2.size())[2][2]);
303 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
305 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0], v1.template get<P::v>(i+2*v2.size())[0]);
306 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1], v1.template get<P::v>(i+2*v2.size())[1]);
307 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2], v1.template get<P::v>(i+2*v2.size())[2]);
309 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0], v1.template get<P::t>(i+2*v2.size())[0][0]);
310 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1], v1.template get<P::t>(i+2*v2.size())[0][1]);
311 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2], v1.template get<P::t>(i+2*v2.size())[0][2]);
312 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0], v1.template get<P::t>(i+2*v2.size())[1][0]);
313 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1], v1.template get<P::t>(i+2*v2.size())[1][1]);
314 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2], v1.template get<P::t>(i+2*v2.size())[1][2]);
315 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0], v1.template get<P::t>(i+2*v2.size())[2][0]);
316 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1], v1.template get<P::t>(i+2*v2.size())[2][1]);
317 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2], v1.template get<P::t>(i+2*v2.size())[2][2]);
321 template <
typename vector>
void test_vector_copy_and_compare()
338 v1.last().add(133.0);
339 v1.last().add(221.0);
345 bool ret = (v2 == v1);
346 BOOST_REQUIRE_EQUAL(ret,
true);
348 v1.get(2).get(1) = 222.0;
351 BOOST_REQUIRE_EQUAL(ret,
false);
378 bool ret = (v2 == v1);
379 BOOST_REQUIRE_EQUAL(ret,
true);
381 v1.get(2).get(1).template get<Box<3,float>::p1>()[0] = 222.0;
384 BOOST_REQUIRE_EQUAL(ret,
false);
388 template <
typename vector>
void test_vector_load_and_save_check()
392 for (
size_t i = 0; i < 5; i++)
395 for (
size_t j = 0; j < 6; j++)
401 v1.save(
"test_save");
403 v2.load(
"test_save");
407 BOOST_REQUIRE_EQUAL(v1.
size(),v2.
size());
408 for (
size_t i = 0; i < v1.
size(); i++)
410 BOOST_REQUIRE_EQUAL(v1.get(i).
size(),v2.get(i).
size());
411 for (
size_t j = 0; j < 6; j++)
413 BOOST_REQUIRE_EQUAL(v1.get(i).get(j),v2.get(i).get(j));
420 BOOST_AUTO_TEST_CASE (vector_iterator_test)
422 test_iterator< openfpm::vector<Point_test<float>> >();
428 BOOST_AUTO_TEST_CASE( vector_use)
430 std::cout <<
"Vector unit test start" <<
"\n";
432 test_vector_use<openfpm::vector<Point_test<float>>>();
435 std::cout <<
"Vector unit test end" <<
"\n";
439 size_t alloc[] = {235,345,0,520};
440 size_t n_alloc =
sizeof(alloc)/
sizeof(
size_t);
443 BOOST_AUTO_TEST_CASE(vector_remove )
445 test_vector_remove<openfpm::vector<Point_test<float>>>();
449 BOOST_AUTO_TEST_CASE(vector_insert )
451 test_vector_insert<openfpm::vector<Point_test<float>>>();
455 BOOST_AUTO_TEST_CASE(vector_clear )
457 test_vector_clear< openfpm::vector<Point_test<float>> >();
461 BOOST_AUTO_TEST_CASE( vector_add_test_case )
463 test_vector_add_test_case<openfpm::vector<Point_test<float>>,
memory_traits_lin>();
467 BOOST_AUTO_TEST_CASE( vector_copy_and_compare )
469 test_vector_copy_and_compare< openfpm::vector<Point_test<float>> >();
473 BOOST_AUTO_TEST_CASE( vector_load_and_save_check )
475 test_vector_load_and_save_check< openfpm::vector<Point_test<float>> >();
493 BOOST_AUTO_TEST_CASE( vector_safety_check )
495 #if defined(SE_CLASS1) && defined (THROW_ON_ERROR)
508 {v.template get<p::x>(23);}
509 catch (std::exception & e)
512 BOOST_REQUIRE_EQUAL(e.what(),
"Runtime vector error");
514 BOOST_REQUIRE_EQUAL(error,
true);
520 catch (std::exception & e)
523 BOOST_REQUIRE_EQUAL(e.what(),
"Runtime vector error");
525 BOOST_REQUIRE_EQUAL(error,
true);
530 catch (std::exception & e)
533 BOOST_REQUIRE_EQUAL(e.what(),
"Runtime grid error");
535 BOOST_REQUIRE_EQUAL(error,
true);
541 {v.template get<p::x>(-1);}
542 catch (std::exception & e)
545 BOOST_REQUIRE_EQUAL(e.what(),
"Runtime vector error");
547 BOOST_REQUIRE_EQUAL(error,
true);
553 catch (std::exception & e)
556 BOOST_REQUIRE_EQUAL(e.what(),
"Runtime vector error");
558 BOOST_REQUIRE_EQUAL(error,
true);
563 catch (std::exception & e)
566 BOOST_REQUIRE_EQUAL(e.what(),
"Runtime grid error");
568 BOOST_REQUIRE_EQUAL(error,
true);
570 #if defined(SE_CLASS2) && defined (THROW_ON_ERROR)
583 catch (std::exception & e)
586 BOOST_REQUIRE_EQUAL(e.what(),
"Runtime memory error");
588 BOOST_REQUIRE_EQUAL(error,
true);
594 catch (std::exception & e)
597 BOOST_REQUIRE_EQUAL(e.what(),
"Runtime memory error");
599 BOOST_REQUIRE_EQUAL(error,
true);
606 BOOST_AUTO_TEST_CASE( object_test_creator )
608 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;
610 BOOST_REQUIRE_EQUAL(tst ,
true);
613 BOOST_AUTO_TEST_CASE( vector_memory_repr )
627 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
631 p.
get<P::v>()[0] = 1.0 + i;
632 p.
get<P::v>()[1] = 2.0 + i;
633 p.
get<P::v>()[2] = 7.0 + i;
635 p.
get<
P::t>()[0][0] = 10.0 + i;
636 p.
get<
P::t>()[0][1] = 13.0 + i;
638 p.
get<
P::t>()[1][0] = 19.0 + i;
639 p.
get<
P::t>()[1][1] = 23.0 + i;
643 p.
get<
P::t>()[2][2] = 11.0 + i;
658 v2.resize(FIRST_PUSH);
664 for (
size_t i = 0 ; i < FIRST_PUSH ; i++)
666 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0],v2.template get<P::v>(i)[0]);
667 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1],v2.template get<P::v>(i)[1]);
668 BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2],v2.template get<P::v>(i)[2]);
670 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0],v2.template get<P::t>(i)[0][0]);
671 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1],v2.template get<P::t>(i)[0][1]);
672 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2],v2.template get<P::t>(i)[0][2]);
673 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0],v2.template get<P::t>(i)[1][0]);
674 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1],v2.template get<P::t>(i)[1][1]);
675 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2],v2.template get<P::t>(i)[1][2]);
676 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0],v2.template get<P::t>(i)[2][0]);
677 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1],v2.template get<P::t>(i)[2][1]);
678 BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2],v2.template get<P::t>(i)[2][2]);
682 BOOST_AUTO_TEST_CASE( vector_std_utility )
695 for (
size_t i = 0 ; i < 16 ; i++)
705 for (
size_t i = 0 ; i < 16 ; i++)
707 BOOST_REQUIRE_EQUAL(pb.get(i),0ul);
712 BOOST_AUTO_TEST_CASE ( vector_prealloc_ext )
718 std::vector<size_t> pap;
725 for (
size_t i = 0 ; i < n_alloc ; i++)
727 size_t alloc_ele = vect.calculateMem(alloc[i],0);
728 pap.push_back(alloc_ele);
741 g_send.resize(n_alloc);
744 for (
size_t i = 0 ; i < n_alloc ; i++)
747 g_send.get(i).setMemory(*prAlloc);
750 g_send.get(i).resize(alloc[i]);
754 for (
size_t i = 0 ; i < n_alloc ; i++)
756 auto it = g_send.get(i).getIterator();
757 auto & v = g_send.get(i);
763 v.template get<P::x>(kk) = 1.0f;
764 v.template get<P::y>(kk) = 1.0f;
765 v.template get<P::z>(kk) = 1.0f;
766 v.template get<P::s>(kk) = 1.0f;
768 v.template get<P::v>(kk)[0] = 1.0f;
769 v.template get<P::v>(kk)[1] = 1.0f;
770 v.template get<P::v>(kk)[2] = 1.0f;
772 v.template get<P::t>(kk)[0][0] = 1.0f;
773 v.template get<P::t>(kk)[0][1] = 1.0f;
774 v.template get<P::t>(kk)[0][2] = 1.0f;
775 v.template get<P::t>(kk)[1][0] = 1.0f;
776 v.template get<P::t>(kk)[1][1] = 1.0f;
777 v.template get<P::t>(kk)[1][2] = 1.0f;
778 v.template get<P::t>(kk)[2][0] = 1.0f;
779 v.template get<P::t>(kk)[2][1] = 1.0f;
780 v.template get<P::t>(kk)[2][2] = 1.0f;
794 for (
size_t i = 0 ; i < n_alloc ; i++)
796 for (
size_t j = 0 ; j < alloc[i] ; j++)
797 BOOST_REQUIRE_EQUAL(ptr[j + offset/
sizeof(
float)],1.0f);
808 BOOST_AUTO_TEST_SUITE_END()
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
static const unsigned int s
s property is at position 3 in the boost::fusion::vector
This class allocate, and destroy CPU memory.
Transform the boost::fusion::vector into memory specification (memory_traits)
virtual void * getPointer()
get a readable pointer with the data
Grow policy define how the vector should grow every time we exceed the size.
It model an expression expr1 * expr2.
void setx(T x_)
set the x property
static const unsigned int y
y property is at position 1 in the boost::fusion::vector
static const unsigned int x
x property is at position 0 in the boost::fusion::vector
boost::fusion::result_of::at< type, boost::mpl::int_< i > >::type get()
getter method for a general property i
static const unsigned int z
z property is at position 2 in the boost::fusion::vector
void sets(T s_)
set the s property
void sety(T y_)
set the y property
static const unsigned int t
t property is at position 5 in the boost::fusion::vector
Test structure used for several test.
static const unsigned int v
v property is at position 4 in the boost::fusion::vector
This class give memory from a preallocated memory, memory destruction is not performed.