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 | { |
19 | ////////////////////////////////// |
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 | |
100 | //! [Create push and multiple remove] |
101 | |
102 | vector v1; |
103 | |
104 | for (size_t i = 0 ; i < V_REM_PUSH ; i++) |
105 | { |
106 | // Point |
107 | Point_test<float> p; |
108 | p.setx(i); |
109 | |
110 | v1.add(p); |
111 | } |
112 | |
113 | { |
114 | openfpm::vector<size_t> rem; |
115 | rem.add(0); |
116 | rem.add(1); |
117 | rem.add(2); |
118 | rem.add(3); |
119 | |
120 | v1.remove(rem); |
121 | } |
122 | |
123 | //! [Create push and multiple remove] |
124 | |
125 | BOOST_REQUIRE_EQUAL(v1.size(),1020ul); |
126 | BOOST_REQUIRE_EQUAL(v1.template get<p::x>(0),4); |
127 | |
128 | { |
129 | openfpm::vector<size_t> rem; |
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 | { |
142 | openfpm::vector<size_t> rem; |
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 | template <typename vector> void test_vector_insert() |
160 | { |
161 | typedef Point_test<float> p; |
162 | |
163 | vector v1; |
164 | |
165 | for (size_t i = 0 ; i < V_REM_PUSH ; i++) |
166 | { |
167 | // Point |
168 | Point_test<float> p; |
169 | p.setx(i); |
170 | |
171 | v1.add(p); |
172 | } |
173 | |
174 | BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH); |
175 | |
176 | // Add one at the first element |
177 | |
178 | v1.insert(0); |
179 | v1.template get<p::x>(0) = -9999.0; |
180 | |
181 | // Add one in the middle |
182 | |
183 | v1.insert(V_REM_PUSH / 2); |
184 | v1.template get<p::x>(V_REM_PUSH / 2) = -9999.0; |
185 | |
186 | // Add one at the end |
187 | |
188 | v1.insert(v1.size()-1); |
189 | v1.template get<p::x>(v1.size()-1) = -9999.0; |
190 | |
191 | BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH + 3); |
192 | |
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); |
196 | |
197 | size_t c = 0; |
198 | |
199 | // Check only odd |
200 | for (size_t i = 0 ; i < v1.size() ; i++) |
201 | { |
202 | if (i == 0 || i == V_REM_PUSH / 2 || i == v1.size()-1) |
203 | continue; |
204 | |
205 | BOOST_REQUIRE_EQUAL((size_t)v1.template get<p::x>(i), c); |
206 | |
207 | c++; |
208 | } |
209 | } |
210 | |
211 | template <typename vector> void test_vector_clear() |
212 | { |
213 | vector v1; |
214 | |
215 | for (size_t i = 0 ; i < V_REM_PUSH ; i++) |
216 | { |
217 | // Point |
218 | Point_test<float> p; |
219 | p.setx(i); |
220 | |
221 | v1.add(p); |
222 | } |
223 | |
224 | v1.clear(); |
225 | |
226 | BOOST_REQUIRE_EQUAL(v1.size(),0ul); |
227 | |
228 | for (size_t i = 0 ; i < V_REM_PUSH ; i++) |
229 | { |
230 | // Point |
231 | Point_test<float> p; |
232 | p.setx(i); |
233 | |
234 | v1.add(p); |
235 | } |
236 | |
237 | BOOST_REQUIRE_EQUAL(v1.size(),V_REM_PUSH); |
238 | } |
239 | |
240 | template <typename vector, template <typename> class layout_base> void test_vector_add_test_case() |
241 | { |
242 | // create two vector |
243 | vector v1; |
244 | |
245 | // Point |
246 | Point_test<float> p; |
247 | p.setx(1.0); |
248 | p.sety(2.0); |
249 | p.setz(3.0); |
250 | p.sets(4.0); |
251 | |
252 | // push objects |
253 | |
254 | for (size_t i = 0 ; i < FIRST_PUSH ; i++) |
255 | { |
256 | // Modify p |
257 | |
258 | p.get<P::v>()[0] = 1.0 + i; |
259 | p.get<P::v>()[1] = 2.0 + i; |
260 | p.get<P::v>()[2] = 7.0 + i; |
261 | |
262 | p.get<P::t>()[0][0] = 10.0 + i; |
263 | p.get<P::t>()[0][1] = 13.0 + i; |
264 | p.get<P::t>()[0][2] = 8.0 + i; |
265 | p.get<P::t>()[1][0] = 19.0 + i; |
266 | p.get<P::t>()[1][1] = 23.0 + i; |
267 | p.get<P::t>()[1][2] = 5.0 + i; |
268 | p.get<P::t>()[2][0] = 4.0 + i; |
269 | p.get<P::t>()[2][1] = 3.0 + i; |
270 | p.get<P::t>()[2][2] = 11.0 + i; |
271 | |
272 | // add p |
273 | |
274 | v1.add(p); |
275 | } |
276 | |
277 | // Duplicate the vector |
278 | vector v2 = v1.duplicate(); |
279 | |
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); |
281 | |
282 | for (size_t i = 0 ; i < FIRST_PUSH ; i++) |
283 | { |
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]); |
287 | |
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]); |
297 | } |
298 | |
299 | // add homogeneous |
300 | |
301 | v1.template add(v2); |
302 | |
303 | for (size_t i = 0 ; i < FIRST_PUSH ; i++) |
304 | { |
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]); |
308 | |
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]); |
318 | } |
319 | } |
320 | |
321 | template <typename vector> void test_vector_copy_and_compare() |
322 | { |
323 | { |
324 | openfpm::vector<openfpm::vector<float>> v1; |
325 | |
326 | v1.add(); |
327 | v1.last().add(1.0); |
328 | v1.last().add(10.0); |
329 | v1.last().add(11.0); |
330 | v1.last().add(21.0); |
331 | v1.last().add(13.0); |
332 | v1.add(); |
333 | v1.last().add(11.0); |
334 | v1.last().add(41.0); |
335 | v1.last().add(61.0); |
336 | v1.last().add(91.0); |
337 | v1.add(); |
338 | v1.last().add(133.0); |
339 | v1.last().add(221.0); |
340 | |
341 | openfpm::vector<openfpm::vector<float>> v2; |
342 | |
343 | v2 = v1; |
344 | |
345 | bool ret = (v2 == v1); |
346 | BOOST_REQUIRE_EQUAL(ret,true); |
347 | |
348 | v1.get(2).get(1) = 222.0; |
349 | |
350 | ret = (v2 == v1); |
351 | BOOST_REQUIRE_EQUAL(ret,false); |
352 | } |
353 | |
354 | { |
355 | Box<3,float> bt({1.2,1.3,1.5},{6.0,7.0,8.0}); |
356 | |
357 | openfpm::vector<openfpm::vector<Box<3,float>>> v1; |
358 | |
359 | v1.add(); |
360 | v1.last().add(bt); |
361 | v1.last().add(bt); |
362 | v1.last().add(bt); |
363 | v1.last().add(bt); |
364 | v1.last().add(bt); |
365 | v1.add(); |
366 | v1.last().add(bt); |
367 | v1.last().add(bt); |
368 | v1.last().add(bt); |
369 | v1.last().add(bt); |
370 | v1.add(); |
371 | v1.last().add(bt); |
372 | v1.last().add(bt); |
373 | |
374 | openfpm::vector<openfpm::vector<Box<3,float>>> v2; |
375 | |
376 | v2 = v1; |
377 | |
378 | bool ret = (v2 == v1); |
379 | BOOST_REQUIRE_EQUAL(ret,true); |
380 | |
381 | v1.get(2).get(1).template get<Box<3,float>::p1>()[0] = 222.0; |
382 | |
383 | ret = (v2 == v1); |
384 | BOOST_REQUIRE_EQUAL(ret,false); |
385 | } |
386 | } |
387 | |
388 | template <typename vector> void test_vector_load_and_save_check() |
389 | { |
390 | openfpm::vector<openfpm::vector<float>> v1; |
391 | |
392 | for (size_t i = 0; i < 5; i++) |
393 | { |
394 | v1.add(); |
395 | for (size_t j = 0; j < 6; j++) |
396 | { |
397 | v1.get(i).add(j); |
398 | } |
399 | } |
400 | |
401 | v1.save("test_save" ); |
402 | openfpm::vector<openfpm::vector<float>> v2; |
403 | v2.load("test_save" ); |
404 | |
405 | // check the v1 and v2 match |
406 | |
407 | BOOST_REQUIRE_EQUAL(v1.size(),v2.size()); |
408 | for (size_t i = 0; i < v1.size(); i++) |
409 | { |
410 | BOOST_REQUIRE_EQUAL(v1.get(i).size(),v2.get(i).size()); |
411 | for (size_t j = 0; j < 6; j++) |
412 | { |
413 | BOOST_REQUIRE_EQUAL(v1.get(i).get(j),v2.get(i).get(j)); |
414 | } |
415 | } |
416 | } |
417 | |
418 | // Test vector iterator |
419 | |
420 | BOOST_AUTO_TEST_CASE (vector_iterator_test) |
421 | { |
422 | test_iterator< openfpm::vector<Point_test<float>> >(); |
423 | test_iterator< openfpm::vector<Point_test<float>,HeapMemory,memory_traits_inte> >(); |
424 | } |
425 | |
426 | // Test the openfpm vector |
427 | |
428 | BOOST_AUTO_TEST_CASE( vector_use) |
429 | { |
430 | std::cout << "Vector unit test start" << "\n" ; |
431 | |
432 | test_vector_use<openfpm::vector<Point_test<float>>>(); |
433 | test_vector_use< openfpm::vector<Point_test<float>,HeapMemory,memory_traits_inte> >(); |
434 | |
435 | std::cout << "Vector unit test end" << "\n" ; |
436 | } |
437 | |
438 | |
439 | size_t alloc[] = {235,345,0,520}; |
440 | size_t n_alloc = sizeof(alloc)/sizeof(size_t); |
441 | |
442 | |
443 | BOOST_AUTO_TEST_CASE(vector_remove ) |
444 | { |
445 | test_vector_remove<openfpm::vector<Point_test<float>>>(); |
446 | test_vector_remove< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >(); |
447 | } |
448 | |
449 | BOOST_AUTO_TEST_CASE(vector_insert ) |
450 | { |
451 | test_vector_insert<openfpm::vector<Point_test<float>>>(); |
452 | test_vector_insert< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte > >(); |
453 | } |
454 | |
455 | BOOST_AUTO_TEST_CASE(vector_clear ) |
456 | { |
457 | test_vector_clear< openfpm::vector<Point_test<float>> >(); |
458 | test_vector_clear< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >(); |
459 | } |
460 | |
461 | BOOST_AUTO_TEST_CASE( vector_add_test_case ) |
462 | { |
463 | test_vector_add_test_case<openfpm::vector<Point_test<float>>,memory_traits_lin>(); |
464 | test_vector_add_test_case<openfpm::vector<Point_test<float>,HeapMemory,memory_traits_inte>, memory_traits_inte >(); |
465 | } |
466 | |
467 | BOOST_AUTO_TEST_CASE( vector_copy_and_compare ) |
468 | { |
469 | test_vector_copy_and_compare< openfpm::vector<Point_test<float>> >(); |
470 | test_vector_copy_and_compare< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >(); |
471 | } |
472 | |
473 | BOOST_AUTO_TEST_CASE( vector_load_and_save_check ) |
474 | { |
475 | test_vector_load_and_save_check< openfpm::vector<Point_test<float>> >(); |
476 | test_vector_load_and_save_check< openfpm::vector<Point_test<float>,HeapMemory, memory_traits_inte> >(); |
477 | } |
478 | |
479 | ////////// Test function /////////// |
480 | ///////////////////////////////////// |
481 | |
482 | BOOST_AUTO_TEST_CASE( vector_safety_check ) |
483 | { |
484 | #if defined(SE_CLASS1) && defined (THROW_ON_ERROR) |
485 | |
486 | bool error = false; |
487 | |
488 | typedef Point_test<float> p; |
489 | |
490 | // Create a vector |
491 | |
492 | openfpm::vector<Point_test<float>> v(16); |
493 | openfpm::vector<Point_test<float>> v2(16); |
494 | |
495 | error = false; |
496 | try |
497 | {v.template get<p::x>(23);} |
498 | catch (std::exception & e) |
499 | { |
500 | error = true; |
501 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime vector error" ); |
502 | } |
503 | BOOST_REQUIRE_EQUAL(error,true); |
504 | |
505 | error = false; |
506 | Point_test<float> t; |
507 | try |
508 | {v.set(23,t);} |
509 | catch (std::exception & e) |
510 | { |
511 | error = true; |
512 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime vector error" ); |
513 | } |
514 | BOOST_REQUIRE_EQUAL(error,true); |
515 | |
516 | error = false; |
517 | try |
518 | {v.set(6,v2,23);} |
519 | catch (std::exception & e) |
520 | { |
521 | error = true; |
522 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
523 | } |
524 | BOOST_REQUIRE_EQUAL(error,true); |
525 | |
526 | //// Negative key |
527 | |
528 | error = false; |
529 | try |
530 | {v.template get<p::x>(-1);} |
531 | catch (std::exception & e) |
532 | { |
533 | error = true; |
534 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime vector error" ); |
535 | } |
536 | BOOST_REQUIRE_EQUAL(error,true); |
537 | |
538 | error = false; |
539 | Point_test<float> t2; |
540 | try |
541 | {v.set(-1,t2);} |
542 | catch (std::exception & e) |
543 | { |
544 | error = true; |
545 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime vector error" ); |
546 | } |
547 | BOOST_REQUIRE_EQUAL(error,true); |
548 | |
549 | error = false; |
550 | try |
551 | {v.set(12,v2,-1);} |
552 | catch (std::exception & e) |
553 | { |
554 | error = true; |
555 | BOOST_REQUIRE_EQUAL(e.what(),"Runtime grid error" ); |
556 | } |
557 | BOOST_REQUIRE_EQUAL(error,true); |
558 | |
559 | #endif |
560 | } |
561 | |
562 | BOOST_AUTO_TEST_CASE( object_test_creator ) |
563 | { |
564 | 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; |
565 | |
566 | BOOST_REQUIRE_EQUAL(tst , true); |
567 | } |
568 | |
569 | BOOST_AUTO_TEST_CASE( vector_memory_repr ) |
570 | { |
571 | // create a vector |
572 | openfpm::vector<Point_test<float>> v1; |
573 | |
574 | // Point |
575 | Point_test<float> p; |
576 | p.setx(1.0); |
577 | p.sety(2.0); |
578 | p.setz(3.0); |
579 | p.sets(4.0); |
580 | |
581 | // push objects |
582 | |
583 | for (size_t i = 0 ; i < FIRST_PUSH ; i++) |
584 | { |
585 | // Modify p |
586 | |
587 | p.get<P::v>()[0] = 1.0 + i; |
588 | p.get<P::v>()[1] = 2.0 + i; |
589 | p.get<P::v>()[2] = 7.0 + i; |
590 | |
591 | p.get<P::t>()[0][0] = 10.0 + i; |
592 | p.get<P::t>()[0][1] = 13.0 + i; |
593 | p.get<P::t>()[0][2] = 8.0 + i; |
594 | p.get<P::t>()[1][0] = 19.0 + i; |
595 | p.get<P::t>()[1][1] = 23.0 + i; |
596 | p.get<P::t>()[1][2] = 5.0 + i; |
597 | p.get<P::t>()[2][0] = 4.0 + i; |
598 | p.get<P::t>()[2][1] = 3.0 + i; |
599 | p.get<P::t>()[2][2] = 11.0 + i; |
600 | |
601 | // add p |
602 | |
603 | v1.add(p); |
604 | } |
605 | |
606 | PtrMemory * ptr1 = new PtrMemory(v1.getPointer(),sizeof(Point_test<float>)*FIRST_PUSH); |
607 | |
608 | // create vector representation to a piece of memory already allocated |
609 | |
610 | openfpm::vector<Point_test<float>,PtrMemory, memory_traits_lin,openfpm::grow_policy_identity> v2; |
611 | |
612 | v2.setMemory(*ptr1); |
613 | |
614 | v2.resize(FIRST_PUSH); |
615 | |
616 | // check |
617 | |
618 | // Check if the duplicated vector match |
619 | |
620 | for (size_t i = 0 ; i < FIRST_PUSH ; i++) |
621 | { |
622 | BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[0],v2.template get<P::v>(i)[0]); |
623 | BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[1],v2.template get<P::v>(i)[1]); |
624 | BOOST_REQUIRE_EQUAL(v1.template get<P::v>(i)[2],v2.template get<P::v>(i)[2]); |
625 | |
626 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][0],v2.template get<P::t>(i)[0][0]); |
627 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][1],v2.template get<P::t>(i)[0][1]); |
628 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[0][2],v2.template get<P::t>(i)[0][2]); |
629 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][0],v2.template get<P::t>(i)[1][0]); |
630 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][1],v2.template get<P::t>(i)[1][1]); |
631 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[1][2],v2.template get<P::t>(i)[1][2]); |
632 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][0],v2.template get<P::t>(i)[2][0]); |
633 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][1],v2.template get<P::t>(i)[2][1]); |
634 | BOOST_REQUIRE_EQUAL(v1.template get<P::t>(i)[2][2],v2.template get<P::t>(i)[2][2]); |
635 | } |
636 | } |
637 | |
638 | BOOST_AUTO_TEST_CASE( vector_std_utility ) |
639 | { |
640 | //! [Create add and access stl] |
641 | |
642 | // Create a vector with 13 element |
643 | openfpm::vector<size_t> pb(13); |
644 | |
645 | // add at the end some othe element |
646 | pb.add(0); |
647 | pb.add(1); |
648 | pb.add(2); |
649 | |
650 | // access the vector |
651 | for (size_t i = 0 ; i < 16 ; i++) |
652 | { |
653 | pb.get(i) = i+1; |
654 | } |
655 | |
656 | //! [Create add and access stl] |
657 | |
658 | pb.fill(0); |
659 | |
660 | // Check is zero |
661 | for (size_t i = 0 ; i < 16 ; i++) |
662 | { |
663 | BOOST_REQUIRE_EQUAL(pb.get(i),0ul); |
664 | } |
665 | |
666 | } |
667 | |
668 | BOOST_AUTO_TEST_CASE ( vector_prealloc_ext ) |
669 | { |
670 | // Memory for the ghost sending buffer |
671 | HeapMemory mem; |
672 | |
673 | // sequence of pre-allocation pattern |
674 | std::vector<size_t> pap; |
675 | |
676 | size_t total = 0; |
677 | |
678 | openfpm::vector<Point_test<float>> vect; |
679 | |
680 | // Calculate the total size required for the sending buffer |
681 | for (size_t i = 0 ; i < n_alloc ; i++) |
682 | { |
683 | size_t alloc_ele = vect.calculateMem(alloc[i],0); |
684 | pap.push_back(alloc_ele); |
685 | total += alloc_ele; |
686 | } |
687 | |
688 | // Create an object of preallocated memory |
689 | ExtPreAlloc<HeapMemory> * prAlloc = new ExtPreAlloc<HeapMemory>(total,mem); |
690 | |
691 | typedef openfpm::vector<Point_test<float>,ExtPreAlloc<HeapMemory>> send_vector; |
692 | |
693 | // create a vector of send_vector (ExtPreAlloc warrant that all the created vector are contiguous) |
694 | openfpm::vector<send_vector> g_send; |
695 | |
696 | // resize |
697 | g_send.resize(n_alloc); |
698 | |
699 | // Number of allocation |
700 | for (size_t i = 0 ; i < n_alloc ; i++) |
701 | { |
702 | // set the preallocated memory to ensure contiguity |
703 | g_send.get(i).setMemory(*prAlloc); |
704 | |
705 | // resize the sending vector (No allocation is produced) |
706 | g_send.get(i).resize(alloc[i]); |
707 | } |
708 | |
709 | // Fill the send buffer with one |
710 | for (size_t i = 0 ; i < n_alloc ; i++) |
711 | { |
712 | auto it = g_send.get(i).getIterator(); |
713 | auto & v = g_send.get(i); |
714 | |
715 | while(it.isNext()) |
716 | { |
717 | auto kk = it.get(); |
718 | |
719 | v.template get<P::x>(kk) = 1.0f; |
720 | v.template get<P::y>(kk) = 1.0f; |
721 | v.template get<P::z>(kk) = 1.0f; |
722 | v.template get<P::s>(kk) = 1.0f; |
723 | |
724 | v.template get<P::v>(kk)[0] = 1.0f; |
725 | v.template get<P::v>(kk)[1] = 1.0f; |
726 | v.template get<P::v>(kk)[2] = 1.0f; |
727 | |
728 | v.template get<P::t>(kk)[0][0] = 1.0f; |
729 | v.template get<P::t>(kk)[0][1] = 1.0f; |
730 | v.template get<P::t>(kk)[0][2] = 1.0f; |
731 | v.template get<P::t>(kk)[1][0] = 1.0f; |
732 | v.template get<P::t>(kk)[1][1] = 1.0f; |
733 | v.template get<P::t>(kk)[1][2] = 1.0f; |
734 | v.template get<P::t>(kk)[2][0] = 1.0f; |
735 | v.template get<P::t>(kk)[2][1] = 1.0f; |
736 | v.template get<P::t>(kk)[2][2] = 1.0f; |
737 | |
738 | ++it; |
739 | } |
740 | } |
741 | |
742 | // In SE_CLASS3 the memory layout is different disable this check |
743 | |
744 | #ifndef SE_CLASS3 |
745 | |
746 | // check that HeapMemory contain ones in the right position |
747 | float * ptr = (float *) mem.getPointer(); |
748 | size_t offset = 0; |
749 | |
750 | for (size_t i = 0 ; i < n_alloc ; i++) |
751 | { |
752 | for (size_t j = 0 ; j < alloc[i] ; j++) |
753 | BOOST_REQUIRE_EQUAL(ptr[j + offset/sizeof(float)],1.0f); |
754 | |
755 | offset += pap[i]; |
756 | } |
757 | |
758 | #endif |
759 | |
760 | /* coverity[leaked_storage] */ |
761 | } |
762 | |
763 | #ifdef CUDA_GPU |
764 | |
765 | BOOST_AUTO_TEST_CASE ( test_gpu_iterator ) |
766 | { |
767 | openfpm::vector<Point<3,float>> pl; |
768 | |
769 | pl.resize(10); |
770 | |
771 | { |
772 | auto ite = pl.getGPUIterator(); |
773 | |
774 | BOOST_REQUIRE_EQUAL(ite.wthr.x,1ul); |
775 | BOOST_REQUIRE_EQUAL(ite.wthr.x,1ul); |
776 | BOOST_REQUIRE_EQUAL(ite.wthr.x,1ul); |
777 | |
778 | BOOST_REQUIRE_EQUAL(ite.thr.x,10ul); |
779 | BOOST_REQUIRE_EQUAL(ite.thr.y,1ul); |
780 | BOOST_REQUIRE_EQUAL(ite.thr.z,1ul); |
781 | } |
782 | |
783 | pl.resize(33); |
784 | |
785 | { |
786 | auto ite = pl.getGPUIterator(32); |
787 | |
788 | BOOST_REQUIRE_EQUAL(ite.wthr.x,2ul); |
789 | BOOST_REQUIRE_EQUAL(ite.wthr.y,1ul); |
790 | BOOST_REQUIRE_EQUAL(ite.wthr.z,1ul); |
791 | |
792 | BOOST_REQUIRE_EQUAL(ite.thr.x,32ul); |
793 | BOOST_REQUIRE_EQUAL(ite.thr.y,1ul); |
794 | BOOST_REQUIRE_EQUAL(ite.thr.z,1ul); |
795 | } |
796 | } |
797 | |
798 | #endif |
799 | |
800 | BOOST_AUTO_TEST_CASE( vector_cuda_to_kernel_recursive ) |
801 | { |
802 | typedef openfpm::vector_gpu<aggregate<int,long int>> test1_type; |
803 | typedef openfpm::vector_gpu<aggregate<int,openfpm::vector_gpu<aggregate<long int>>>> test2_type; |
804 | typedef openfpm::vector_gpu<aggregate<int,openfpm::vector_gpu<aggregate<Box<2,float>>>>> test3_type; |
805 | typedef openfpm::vector<Box<3,float>,CudaMemory,memory_traits_inte> test4_type; |
806 | typedef openfpm::vector_gpu<aggregate<int,openfpm::vector_gpu<Box<2,float>>>> test5_type; |
807 | |
808 | typedef typename toKernel_transform<memory_traits_inte,test1_type>::type tker1; |
809 | typedef typename toKernel_transform<memory_traits_inte,test2_type>::type tker2; |
810 | typedef typename toKernel_transform<memory_traits_inte,test3_type>::type tker3; |
811 | typedef typename toKernel_transform<memory_traits_inte,test4_type>::type tker4; |
812 | typedef typename toKernel_transform<memory_traits_inte,test5_type>::type tker5; |
813 | |
814 | bool test = std::is_same<tker1,openfpm::vector_gpu_ker<aggregate<int, long>, memory_traits_inte>>::value; |
815 | |
816 | BOOST_REQUIRE_EQUAL(test,true); |
817 | |
818 | test = std::is_same<tker2,openfpm::vector_gpu_ker<aggregate<int, openfpm::vector_gpu_ker<aggregate<long>, memory_traits_inte> >, memory_traits_inte>>::value; |
819 | |
820 | BOOST_REQUIRE_EQUAL(test,true); |
821 | |
822 | 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; |
823 | |
824 | BOOST_REQUIRE_EQUAL(test,true); |
825 | |
826 | test = std::is_same<tker4,openfpm::vector_gpu_ker<Box<3,float>,memory_traits_inte>>::value; |
827 | |
828 | BOOST_REQUIRE_EQUAL(test,true); |
829 | |
830 | 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; |
831 | |
832 | BOOST_REQUIRE_EQUAL(test,true); |
833 | } |
834 | |
835 | BOOST_AUTO_TEST_SUITE_END() |
836 | |
837 | #endif |
838 | |