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
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/PreAllocHeapMemory.hpp"
7 #include "memory/ExtPreAlloc.hpp"
8 #include "memory/PtrMemory.hpp"
9 #include <cstring>
10 #include "Space/Shape/Point.hpp"
11 #include "util/object_util.hpp"
12 #include "vector_test_util.hpp"
13 
14 BOOST_AUTO_TEST_SUITE( vector_test )
15 
16 // Test vector iterator
17 
18 BOOST_AUTO_TEST_CASE (vector_iterator_test)
19 {
21 
22  openfpm::vector<Point_test<float>> v_ofp_test = allocate_openfpm(FIRST_PUSH);
23 
24  size_t count = 0;
25 
26  auto it = v_ofp_test.getIterator();
27 
28  while (it.isNext())
29  {
30  count++;
31 
32  ++it;
33  }
34 
35  BOOST_REQUIRE_EQUAL(count,v_ofp_test.size());
36 
37  count = 0;
38  auto it_f = v_ofp_test.getIteratorFrom( FIRST_PUSH / 2 );
39 
40  while (it_f.isNext())
41  {
42  count++;
43 
44  ++it_f;
45  }
46 
47  BOOST_REQUIRE_EQUAL(count, v_ofp_test.size() / 2 );
48 }
49 
50 // Test the openfpm vector
51 
52 BOOST_AUTO_TEST_CASE( vector_use)
53 {
54  std::cout << "Vector unit test start" << "\n";
55 
56  std::vector<Point_orig<float>> v_stl_test = allocate_stl();
57  openfpm::vector<Point_test<float>> v_ofp_test = allocate_openfpm(FIRST_PUSH);
58 
59  // try to duplicate the vector
60  openfpm::vector<Point_test<float>> dv_ofp_test = v_ofp_test.duplicate();
61 
62  // Check if the STL and openfpm match
63 
64  for (size_t i = 0; i < FIRST_PUSH; i++)
65  {
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]);
69 
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]);
79  }
80 
81  // Check if the duplicated vector match
82 
83  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
84  {
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]);
88 
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]);
98  }
99 
100  std::cout << "Vector unit test end" << "\n";
101 }
102 
103 // Pre alloc test
104 
105 struct pre_test
106 {
111 };
112 
113 BOOST_AUTO_TEST_CASE( vector_std_utility )
114 {
116 
117  // Create a vector with 13 element
119 
120  // add at the end some othe element
121  pb.add(0);
122  pb.add(1);
123  pb.add(2);
124 
125  // access the vector
126  for (size_t i = 0 ; i < 16 ; i++)
127  {
128  pb.get(i) = i+1;
129  }
130 
132 
133  pb.fill(0);
134 
135  // Check is zero
136  for (size_t i = 0 ; i < 16 ; i++)
137  {
138  BOOST_REQUIRE_EQUAL(pb.get(i),0ul);
139  }
140 
141 }
142 
143 size_t alloc[] = {235,345,0,520};
144 size_t n_alloc = sizeof(alloc)/sizeof(size_t);
145 
146 BOOST_AUTO_TEST_CASE ( vector_prealloc_ext )
147 {
148  // Memory for the ghost sending buffer
149  HeapMemory mem;
150 
151  // sequence of pre-allocation pattern
152  std::vector<size_t> pap;
153 
154  size_t total = 0;
155 
157 
158  // Calculate the total size required for the sending buffer
159  for (size_t i = 0 ; i < n_alloc ; i++)
160  {
161  size_t alloc_ele = vect.calculateMem(alloc[i],0);
162  pap.push_back(alloc_ele);
163  total += alloc_ele;
164  }
165 
166  // Create an object of preallocated memory
167  ExtPreAlloc<HeapMemory> * prAlloc = new ExtPreAlloc<HeapMemory>(pap,mem);
168 
169  typedef openfpm::vector<Point_test<float>,ExtPreAlloc<HeapMemory>> send_vector;
170 
171  // create a vector of send_vector (ExtPreAlloc warrant that all the created vector are contiguous)
173 
174  // resize
175  g_send.resize(n_alloc);
176 
177  // Number of allocation
178  for (size_t i = 0 ; i < n_alloc ; i++)
179  {
180  // set the preallocated memory to ensure contiguity
181  g_send.get(i).setMemory(*prAlloc);
182 
183  // resize the sending vector (No allocation is produced)
184  g_send.get(i).resize(alloc[i]);
185  }
186 
187  // Fill the send buffer with one
188  for (size_t i = 0 ; i < n_alloc ; i++)
189  {
190  auto it = g_send.get(i).getIterator();
191  auto & v = g_send.get(i);
192 
193  while(it.isNext())
194  {
195  auto kk = it.get();
196 
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;
201 
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;
205 
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;
215 
216  ++it;
217  }
218  }
219 
220  // check that HeapMemory contain ones in the right position
221  float * ptr = (float *) mem.getPointer();
222  size_t offset = 0;
223 
224  for (size_t i = 0 ; i < n_alloc ; i++)
225  {
226  for (size_t j = 0 ; j < alloc[i] ; j++)
227  BOOST_REQUIRE_EQUAL(ptr[j + offset/sizeof(float)],1.0f);
228 
229  offset += pap[i];
230  }
231 }
232 
233 
234 BOOST_AUTO_TEST_CASE( vector_prealloc )
235 {
237 
238  for (size_t i = 0 ; i < 3 ; i++)
239  {
240  // Create the size required to store the particles position and properties to communicate
242  size_t s1 = vect1.calculateMem(1024,0);
243  openfpm::vector<Point_test<float>> vect2;
244  size_t s2 = vect2.calculateMem(1024,0);
245 
246  // Preallocate the memory
247  size_t sz[2] = {s1,s2};
248  PreAllocHeapMemory<2> * mem = new PreAllocHeapMemory<2>(sz);
249 
250  // Set the memory allocator
251  pb.get(i).pos.setMemory(*mem);
252  pb.get(i).prp.setMemory(*mem);
253 
254  // set the size and allocate, using mem warrant that pos and prp is contiguous
255  pb.get(i).pos.resize(1024);
256  pb.get(i).prp.resize(1024);
257  }
258 }
259 
260 
261 BOOST_AUTO_TEST_CASE( object_test_creator )
262 {
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;
264 
265  BOOST_REQUIRE_EQUAL(tst , true);
266 }
267 
268 #define V_REM_PUSH 1024ul
269 
270 BOOST_AUTO_TEST_CASE(vector_remove )
271 {
272  typedef Point_test<float> p;
273 
275 
276  openfpm::vector<Point_test<float>> v1;
277 
278  for (size_t i = 0 ; i < V_REM_PUSH ; i++)
279  {
280  // Point
282  p.setx(i);
283 
284  v1.add(p);
285  }
286 
287  {
289  rem.add(0);
290  rem.add(1);
291  rem.add(2);
292  rem.add(3);
293 
294  v1.remove(rem);
295  }
296 
298 
299  BOOST_REQUIRE_EQUAL(v1.size(),1020ul);
300  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(0),4);
301 
302  {
304  rem.add(v1.size()-3);
305  rem.add(v1.size()-2);
306  rem.add(v1.size()-1);
307  rem.add(v1.size());
308 
309  v1.remove(rem);
310  }
311 
312  BOOST_REQUIRE_EQUAL(v1.size(),1016ul);
313  BOOST_REQUIRE_EQUAL(v1.template get<p::x>(v1.size()-1),1019);
314 
315  {
317  for (size_t i = 0 ; i < (V_REM_PUSH - 8) / 2 ; i++)
318  rem.add(i * 2);
319 
320  // remove all the even number
321  v1.remove(rem);
322  }
323 
324  BOOST_REQUIRE_EQUAL(v1.size(),508ul);
325 
326  // Check only odd
327  for (size_t i = 0 ; i < v1.size() ; i++)
328  {
329  BOOST_REQUIRE_EQUAL((size_t)v1.template get<p::x>(v1.size()-1) % 2, 1ul);
330  }
331 }
332 
333 BOOST_AUTO_TEST_CASE(vector_clear )
334 {
335  typedef Point_test<float> p;
336 
337  openfpm::vector<Point_test<float>> v1;
338 
339  for (size_t i = 0 ; i < V_REM_PUSH ; i++)
340  {
341  // Point
343  p.setx(i);
344 
345  v1.add(p);
346  }
347 
348  v1.clear();
349 
350  BOOST_REQUIRE_EQUAL(v1.size(),0ul);
351 
352  for (size_t i = 0 ; i < V_REM_PUSH ; i++)
353  {
354  // Point
356  p.setx(i);
357 
358  v1.add(p);
359  }
360 
361  BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH);
362 }
363 
364 BOOST_AUTO_TEST_CASE( vector_memory_repr )
365 {
366  // create a vector
367  openfpm::vector<Point_test<float>> v1;
368 
369  // Point
371  p.setx(1.0);
372  p.sety(2.0);
373  p.setz(3.0);
374  p.sets(4.0);
375 
376  // push objects
377 
378  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
379  {
380  // Modify p
381 
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;
385 
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;
395 
396  // add p
397 
398  v1.add(p);
399  }
400 
401  PtrMemory * ptr1 = new PtrMemory(v1.getPointer(),sizeof(Point_test<float>)*FIRST_PUSH);
402 
403  // create vector representation to a piece of memory already allocated
404 
405  openfpm::vector<Point_test<float>,PtrMemory,openfpm::grow_policy_identity> v2;
406 
407  v2.setMemory(*ptr1);
408 
409  v2.resize(FIRST_PUSH);
410 
411  // check
412 
413  // Check if the duplicated vector match
414 
415  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
416  {
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]);
420 
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]);
430  }
431 }
432 
433 BOOST_AUTO_TEST_CASE( vector_add_test_case )
434 {
435  // create two vector
436  openfpm::vector<Point_test<float>> v1;
437 
438  // Point
440  p.setx(1.0);
441  p.sety(2.0);
442  p.setz(3.0);
443  p.sets(4.0);
444 
445  // push objects
446 
447  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
448  {
449  // Modify p
450 
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;
454 
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;
464 
465  // add p
466 
467  v1.add(p);
468  }
469 
470  // Duplicate the vector
471  openfpm::vector<Point_test<float>> v2 = v1.duplicate();
472 
473  v1.template add_prp<Point_test<float>,HeapMemory,typename openfpm::grow_policy_double,P::x,P::y,P::z,P::s,P::v,P::t>(v2);
474 
475  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
476  {
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]);
480 
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]);
490  }
491 
492  // add homogeneous
493 
494  v1.template add(v2);
495 
496  for (size_t i = 0 ; i < FIRST_PUSH ; i++)
497  {
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]);
501 
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]);
511  }
512 
513 }
514 
516 
517 openfpm::vector<scalar<float>> & test_error_v()
518 {
520 
521  return v;
522 }
523 
524 BOOST_AUTO_TEST_CASE( vector_copy_and_compare )
525 {
526  {
528 
529  v1.add();
530  v1.last().add(1.0);
531  v1.last().add(10.0);
532  v1.last().add(11.0);
533  v1.last().add(21.0);
534  v1.last().add(13.0);
535  v1.add();
536  v1.last().add(11.0);
537  v1.last().add(41.0);
538  v1.last().add(61.0);
539  v1.last().add(91.0);
540  v1.add();
541  v1.last().add(133.0);
542  v1.last().add(221.0);
543 
545 
546  v2 = v1;
547 
548  bool ret = (v2 == v1);
549  BOOST_REQUIRE_EQUAL(ret,true);
550 
551  v1.get(2).get(1) = 222.0;
552 
553  ret = (v2 == v1);
554  BOOST_REQUIRE_EQUAL(ret,false);
555  }
556 
557  {
558  Box<3,float> bt({1.2,1.3,1.5},{6.0,7.0,8.0});
559 
561 
562  v1.add();
563  v1.last().add(bt);
564  v1.last().add(bt);
565  v1.last().add(bt);
566  v1.last().add(bt);
567  v1.last().add(bt);
568  v1.add();
569  v1.last().add(bt);
570  v1.last().add(bt);
571  v1.last().add(bt);
572  v1.last().add(bt);
573  v1.add();
574  v1.last().add(bt);
575  v1.last().add(bt);
576 
578 
579  v2 = v1;
580 
581  bool ret = (v2 == v1);
582  BOOST_REQUIRE_EQUAL(ret,true);
583 
584  v1.get(2).get(1).template get<Box<3,float>::p1>()[0] = 222.0;
585 
586  ret = (v2 == v1);
587  BOOST_REQUIRE_EQUAL(ret,false);
588  }
589 
590 }
591 
593 
594 BOOST_AUTO_TEST_CASE( vector_safety_check )
595 {
596 #if defined(SE_CLASS1) && defined (THROW_ON_ERROR)
597 
598  bool error = false;
599 
600  typedef Point_test<float> p;
601 
602  // Create a vector
603 
604  openfpm::vector<Point_test<float>> v(16);
605  openfpm::vector<Point_test<float>> v2(16);
606 
607  error = false;
608  try
609  {v.template get<p::x>(23);}
610  catch (size_t e)
611  {
612  error = true;
613  BOOST_REQUIRE_EQUAL(e,VECTOR_ERROR);
614  BOOST_REQUIRE_EQUAL(v.getLastError(),2001);
615  }
616  BOOST_REQUIRE_EQUAL(error,true);
617 
618  error = false;
620  try
621  {v.set(23,t);}
622  catch (size_t e)
623  {
624  error = true;
625  BOOST_REQUIRE_EQUAL(e,VECTOR_ERROR);
626  BOOST_REQUIRE_EQUAL(v.getLastError(),2001);
627  }
628  BOOST_REQUIRE_EQUAL(error,true);
629 
630  error = false;
631  try
632  {v.set(6,v2,23);}
633  catch (size_t e)
634  {
635  error = true;
636  BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
637  }
638  BOOST_REQUIRE_EQUAL(error,true);
639 
641 
642  error = false;
643  try
644  {v.template get<p::x>(-1);}
645  catch (size_t e)
646  {
647  error = true;
648  BOOST_REQUIRE_EQUAL(e,VECTOR_ERROR);
649  BOOST_REQUIRE_EQUAL(v.getLastError(),2001);
650  }
651  BOOST_REQUIRE_EQUAL(error,true);
652 
653  error = false;
655  try
656  {v.set(-1,t2);}
657  catch (size_t e)
658  {
659  error = true;
660  BOOST_REQUIRE_EQUAL(e,VECTOR_ERROR);
661  BOOST_REQUIRE_EQUAL(v.getLastError(),2001);
662  }
663  BOOST_REQUIRE_EQUAL(error,true);
664 
665  error = false;
666  try
667  {v.set(12,v2,-1);}
668  catch (size_t e)
669  {
670  error = true;
671  BOOST_REQUIRE_EQUAL(e,GRID_ERROR);
672  }
673  BOOST_REQUIRE_EQUAL(error,true);
674 
675  #if defined(SE_CLASS2) && defined (THROW_ON_ERROR)
676 
677  error = false;
678 
679  // Create a vector
680 
682  delete v3;
683 
684  // Try to access the class
685 
686  try
687  {v3->size();}
688  catch (size_t e)
689  {
690  error = true;
691  BOOST_REQUIRE_EQUAL(e,MEM_ERROR);
692  }
693  BOOST_REQUIRE_EQUAL(error,true);
694 
695  try
696  {
697  openfpm::vector<scalar<float>> vr = test_error_v();
698  }
699  catch (size_t e)
700  {
701  error = true;
702  BOOST_REQUIRE_EQUAL(e,MEM_ERROR);
703  }
704  BOOST_REQUIRE_EQUAL(error,true);
705 
706  #endif
707 
708 #endif
709 }
710 
711 BOOST_AUTO_TEST_CASE( vector_load_and_save_check )
712 {
714 
715  for (size_t i = 0; i < 5; i++)
716  {
717  v1.add();
718  for (size_t j = 0; j < 6; j++)
719  {
720  v1.get(i).add(j);
721  }
722  }
723 
724  v1.save("test_save");
725 
727 
728  v2.load("test_save");
729 
730  // check the v1 and v2 match
731 
732  BOOST_REQUIRE_EQUAL(v1.size(),v2.size());
733  for (size_t i = 0; i < v1.size(); i++)
734  {
735  BOOST_REQUIRE_EQUAL(v1.get(i).size(),v2.get(i).size());
736  for (size_t j = 0; j < 6; j++)
737  {
738  BOOST_REQUIRE_EQUAL(v1.get(i).get(j),v2.get(i).get(j));
739  }
740  }
741 }
742 
743 BOOST_AUTO_TEST_SUITE_END()
744 
745 #endif
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.
Definition: Box.hpp:56
Test structure used for several test.
Definition: Point_test.hpp:72
Implementation of 1-D std::vector like structure.
Definition: map_grid.hpp:94