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
util_test.hpp
1 /*
2  * util_test.hpp
3  *
4  * Created on: May 31, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef UTIL_TEST_HPP_
9 #define UTIL_TEST_HPP_
10 
11 #include "config.h"
12 #include "object_util.hpp"
13 #include "Point_test.hpp"
14 #include "util/ct_array.hpp"
15 #include "Vector/map_vector.hpp"
16 #include "common.hpp"
17 #include "check_no_pointers.hpp"
18 #include "Grid/util.hpp"
19 #include "data_type/scalar.hpp"
20 #include "util/convert.hpp"
21 #include <iostream>
22 #include "mul_array_extents.hpp"
23 
25 
27 {
28  float data;
29 
30  static constexpr bool stag_mask[] = {true,false,true};
31 };
32 
34 {
35  float data;
36 };
37 
39 
41 
43 {
44  struct attributes
45  {
46  static const std::string name[2];
47  };
48 };
49 
50 const std::string test_has_attributes::attributes::name[]={"attributes1","attributes2"};
51 
53 {
54 };
55 
57 
58 BOOST_AUTO_TEST_SUITE( util_test )
59 
60 BOOST_AUTO_TEST_CASE( object_prop_copy )
61 {
62  {
64  typedef Point_test<float> p;
65  typedef Point_test<float>::type vboost;
66  typedef object_creator<Point_test<float>::type,0,1,4>::type vboost_red;
67 
68  object<vboost> src;
70 
71  // fill the source properties x,y,v = 0,1,4 with data
72 
73  boost::fusion::at_c<p::x>(src.data) = 1.0;
74  boost::fusion::at_c<p::y>(src.data) = 2.0;
75 
76  for (size_t i = 0 ; i < 3 ; i++)
77  boost::fusion::at_c<p::v>(src.data)[i] = i + 5.0;
78 
79  // copy from src to dst
80  object_si_d<object<vboost>,object<vboost_red>,OBJ_NORMAL,0,1,4>(src,dst);
81 
82  // Check the result
83  BOOST_REQUIRE_EQUAL(boost::fusion::at_c<0>(dst.data),1.0);
84  BOOST_REQUIRE_EQUAL(boost::fusion::at_c<1>(dst.data),2.0);
85 
86  for (size_t i = 0 ; i < 3 ; i++)
87  BOOST_REQUIRE_EQUAL(boost::fusion::at_c<2>(dst.data)[i],i + 5.0);
88 
90 
92 
93  typedef encapc<1,Point_test<float>,openfpm::vector<Point_test<float>>::memory_conf> encap_src;
94  typedef encapc<1,object<vboost_red>,openfpm::vector<object<vboost_red>>::memory_conf> encap_dst;
95 
96  openfpm::vector<p> v_point;
98 
99  v_point.resize(2);
100  v_point_red.resize(2);
101 
102  v_point.template get<p::x>(0) = 1.0;
103  v_point.template get<p::y>(0) = 2.0;
104 
105  for (size_t i = 0 ; i < 3 ; i++)
106  v_point.template get<p::v>(0)[i] = i + 5.0;
107 
108  auto src_e = v_point.get(0);
109  auto dst_e = v_point_red.get(0);
110 
112 
113  BOOST_REQUIRE_EQUAL(v_point_red.get(0).template get<0>(),1.0);
114  BOOST_REQUIRE_EQUAL(v_point_red.get(0).template get<1>(),2.0);
115 
116  for (size_t i = 0 ; i < 3 ; i++)
117  BOOST_REQUIRE_EQUAL(v_point_red.get(0).template get<2>()[i],i + 5.0);
118 
120  }
121 
122  {
123  // Object write test
124 
126  typedef Point_test<float> p;
127  typedef Point_test<float>::type vboost;
128  typedef object_creator<Point_test<float>::type,0,1,4>::type vboost_red;
129 
130  object<vboost> dst;
131  object<vboost_red> src;
132 
133  // fill the source properties 0,1,2 with data
134 
135  boost::fusion::at_c<0>(src.data) = 1.0;
136  boost::fusion::at_c<1>(src.data) = 2.0;
137 
138  for (size_t i = 0 ; i < 3 ; i++)
139  boost::fusion::at_c<2>(src.data)[i] = i + 5.0;
140 
141  // copy from src to dst
142  object_s_di<object<vboost_red>,object<vboost>,OBJ_NORMAL,0,1,4>(src,dst);
143 
144  // Check the result
145  BOOST_REQUIRE_EQUAL(boost::fusion::at_c<p::x>(dst.data),1.0);
146  BOOST_REQUIRE_EQUAL(boost::fusion::at_c<p::y>(dst.data),2.0);
147 
148  for (size_t i = 0 ; i < 3 ; i++)
149  BOOST_REQUIRE_EQUAL(boost::fusion::at_c<p::v>(dst.data)[i],i + 5.0);
150 
152 
154 
155  typedef encapc<1,Point_test<float>,openfpm::vector<Point_test<float>>::memory_conf> encap_dst;
156  typedef encapc<1,object<vboost_red>,openfpm::vector<object<vboost_red>>::memory_conf> encap_src;
157 
158  openfpm::vector<p> v_point;
160 
161  v_point.resize(2);
162  v_point_red.resize(2);
163 
164  v_point_red.template get<0>(0) = 11.0;
165  v_point_red.template get<1>(0) = 12.0;
166 
167  for (size_t i = 0 ; i < 3 ; i++)
168  v_point_red.template get<2>(0)[i] = i + 15.0;
169 
170  auto dst_e = v_point.get(0);
171  auto src_e = v_point_red.get(0);
172 
174 
175  BOOST_REQUIRE_EQUAL(v_point.get(0).template get<p::x>(),11.0);
176  BOOST_REQUIRE_EQUAL(v_point.get(0).template get<p::y>(),12.0);
177 
178  for (size_t i = 0 ; i < 3 ; i++)
179  BOOST_REQUIRE_EQUAL(v_point.get(0).template get<p::v>()[i],i + 15.0);
180 
182  }
183 
184  {
186  struct no_method_pointer
187  {
188  float a;
189  int b;
190  };
191 
192  struct no_pointer
193  {
194  double c;
195  int d;
196 
197  static bool noPointers() {return true;}
198  };
199 
200  struct with_pointer
201  {
202  double * c;
203  int d;
204 
205  static bool noPointers() {return false;}
206  };
207 
208  typedef boost::fusion::vector<int,float,double,no_method_pointer,no_pointer,with_pointer,no_method_pointer> v;
209 
210  int val = boost::mpl::size< noPointers_sequence<v,0,2>::type >::value;
211  BOOST_REQUIRE_EQUAL(val,0);
212 
213  val = boost::mpl::size< noPointers_sequence<v,0,2,4,5>::type >::value;
214  BOOST_REQUIRE_EQUAL(val,0);
215 
216  val = boost::mpl::size< noPointers_sequence<v,0,1,2,3,4,5,6>::type >::value;
217  BOOST_REQUIRE_EQUAL(val,2);
218 
219  typedef boost::fusion::vector<int *,float &,double *,no_method_pointer *,no_pointer &,with_pointer *,no_method_pointer &> vp;
220 
221  val = boost::mpl::size< noPointers_sequence<vp,0,1,2,3,4,5,6>::type >::value;
222  BOOST_REQUIRE_EQUAL(val,2);
223 
225  }
226 
227  // Check the the object respect the noPointers construction
228  {
229 
230  struct no_method_pointer
231  {
232  float a;
233  int b;
234  };
235 
236  struct no_pointer
237  {
238  double c;
239  int d;
240 
241  static bool noPointers() {return true;}
242  };
243 
244  struct with_pointer
245  {
246  double * c;
247  int d;
248 
249  static bool noPointers() {return false;}
250  };
251 
252  typedef boost::fusion::vector<int,float,double> v;
253  int val = object<v>::noPointers();
254  BOOST_REQUIRE_EQUAL(val,PNP::NO_POINTERS);
255 
256  typedef boost::fusion::vector<int,float,double,no_pointer> va;
257  val = object<va>::noPointers();
258  BOOST_REQUIRE_EQUAL(val,PNP::NO_POINTERS);
259 
260  typedef boost::fusion::vector<int,float,double,no_pointer,with_pointer> vb;
261  val = object<vb>::noPointers();
262  BOOST_REQUIRE_EQUAL(val,PNP::POINTERS);
263 
264  typedef boost::fusion::vector<int,float,double,no_pointer,double *> vc;
265  val = object<vc>::noPointers();
266  BOOST_REQUIRE_EQUAL(val,PNP::POINTERS);
267 
268  typedef boost::fusion::vector<int,float,double,no_pointer,double &> vd;
269  val = object<vd>::noPointers();
270  BOOST_REQUIRE_EQUAL(val,PNP::POINTERS);
271 
272  typedef boost::fusion::vector<int,float,double,no_pointer,double[3]> ve;
273  val = object<ve>::noPointers();
274  BOOST_REQUIRE_EQUAL(val,PNP::NO_POINTERS);
275 
276  typedef boost::fusion::vector<int,float,double,no_pointer,no_pointer *> vf;
277  val = object<vf>::noPointers();
278  BOOST_REQUIRE_EQUAL(val,PNP::POINTERS);
279 
280  typedef boost::fusion::vector<int,float,double,no_pointer,no_pointer &> vg;
281  val = object<vg>::noPointers();
282  BOOST_REQUIRE_EQUAL(val,PNP::POINTERS);
283 
284  typedef boost::fusion::vector<int,float,double,no_pointer,no_pointer[3]> vh;
285  val = object<vh>::noPointers();
286  BOOST_REQUIRE_EQUAL(val,PNP::NO_POINTERS);
287  }
288 }
289 
291 
292 template<size_t index, size_t N> struct MetaFunc {
293  enum { value = index + N };
294 };
295 
297 
298 BOOST_AUTO_TEST_CASE( generate_array )
299 {
300  {
302  const size_t count = 5;
303  typedef typename ::generate_array<size_t,count, MetaFunc>::result ct_test;
304 
305  // ct_test::data is equivalent to const size_t [5] = {5,6,7,8,9}
306 
307  for (size_t i = 0 ; i < count; ++i)
308  {
309  const size_t ct_val = ct_test::data[i];
310  BOOST_REQUIRE_EQUAL(ct_val,count+i);
311  }
313  }
314 
315  // check constexpr compile time array as template parameters
316 
317 #ifndef COVERTY_SCAN
318 
319  {
321  const size_t count = 5;
322  typedef typename ::generate_array_constexpr<size_t,count, MetaFunc>::result ct_test_ce;
323 
324  // ct_test_ce::data is equivalent to constexpr size_t [5] = {5,6,7,8,9}
325 
327  BOOST_REQUIRE_EQUAL(ct_calc,11ul);
329  }
330 
331 #endif
332 }
333 
334 BOOST_AUTO_TEST_CASE( check_templates_util_function )
335 {
336  {
337  {
339 
340  struct test_no_ptr
341  {
342  static bool noPointers() {return true;}
343  };
344 
345  struct test_ptr
346  {
347  static bool noPointers() {return false;}
348  };
349 
350  struct test_unknown
351  {
352  };
353 
354  BOOST_REQUIRE_EQUAL(has_noPointers<test_no_ptr>::type::value,true);
355  BOOST_REQUIRE_EQUAL(has_noPointers<test_ptr>::type::value,true);
356  BOOST_REQUIRE_EQUAL(has_noPointers<test_unknown>::type::value,false);
357 
359 
360  }
361 
362  {
364 
365  int val = is_openfpm_native<Point_test<float>>::value;
366  BOOST_REQUIRE_EQUAL(val, true);
368  BOOST_REQUIRE_EQUAL(val, false);
369 
371  }
372 
373  {
375 
376  struct test_typedef_same_data
377  {
378  typedef boost::fusion::vector<float,double,float[3]> type;
379 
380  type data;
381  };
382 
383  struct test_typedef_not_same_data
384  {
385  typedef boost::fusion::vector<float,double,float[3]> type;
386 
387  boost::fusion::vector<float,double> data;
388  };
389 
391  BOOST_REQUIRE_EQUAL(val, true);
393  BOOST_REQUIRE_EQUAL(val, false);
394 
396  }
397 
398  {
400 
401  struct test_has_data
402  {
403  float data;
404  };
405 
406  struct test_no_has_data
407  {
408  };
409 
411  BOOST_REQUIRE_EQUAL(val, true);
413  BOOST_REQUIRE_EQUAL(val, false);
414 
416  }
417 
418  {
420 
422  BOOST_REQUIRE_EQUAL(val, true);
424  BOOST_REQUIRE_EQUAL(val, false);
425 
427  }
428 
429  {
431 
432  struct test_has_typedef
433  {
434  typedef float type;
435  };
436 
437  struct test_no_has_data
438  {
439  };
440 
442  BOOST_REQUIRE_EQUAL(val, true);
444  BOOST_REQUIRE_EQUAL(val, false);
445 
447  }
448 
449  {
451 
453  BOOST_REQUIRE_EQUAL(val, true);
455  BOOST_REQUIRE_EQUAL(val, false);
456 
458  }
459 
461  struct test_no_ptr
462  {
463  static bool noPointers() {return PNP::NO_POINTERS;}
464  };
465 
466  struct test_ptr
467  {
468  static bool noPointers() {return PNP::POINTERS;}
469  };
470 
471  struct test_unknown
472  {
473  };
474 
476  BOOST_REQUIRE_EQUAL(val,PNP::NO_POINTERS);
478  BOOST_REQUIRE_EQUAL(val,PNP::POINTERS);
480  BOOST_REQUIRE_EQUAL(val,PNP::UNKNOWN);
481 
483 
484  {
486 
487  struct stub_object
488  {
489  float a;
490  double b;
491  };
492 
493  bool val = is_grid< grid_cpu<2,scalar<float> > >::value;
494  BOOST_REQUIRE_EQUAL( val ,true);
496  BOOST_REQUIRE_EQUAL( val , true);
498  BOOST_REQUIRE_EQUAL( val ,true);
499 
500  val = is_grid< grid_gpu<2,scalar<float> > >::value;
501  BOOST_REQUIRE_EQUAL(val ,true);
503  BOOST_REQUIRE_EQUAL(val, true);
505  BOOST_REQUIRE_EQUAL(val,true);
506 
508  BOOST_REQUIRE_EQUAL( val, false);
509  val = is_grid< stub_object > ::value;
510  BOOST_REQUIRE_EQUAL( val, false);
511 
513  }
514 
515  {
517 
518  struct stub_object
519  {
520  float a;
521  double b;
522  };
523 
524  bool val = is_vector< openfpm::vector<scalar<float> > >::value;
525  BOOST_REQUIRE_EQUAL( val ,true);
527  BOOST_REQUIRE_EQUAL( val , true);
529  BOOST_REQUIRE_EQUAL( val ,true);
530 
532  BOOST_REQUIRE_EQUAL(val ,true);
534  BOOST_REQUIRE_EQUAL(val, true);
536  BOOST_REQUIRE_EQUAL(val,true);
537 
539  BOOST_REQUIRE_EQUAL( val, false);
540  val = is_vector< stub_object > ::value;
541  BOOST_REQUIRE_EQUAL( val, false);
542 
544  }
545 
546  {
548 
549  struct stub_object
550  {
551  float a;
552  double b;
553  };
554 
556  BOOST_REQUIRE_EQUAL( val ,true);
558  BOOST_REQUIRE_EQUAL( val , true);
560  BOOST_REQUIRE_EQUAL( val ,true);
561 
563  BOOST_REQUIRE_EQUAL( val ,true);
565  BOOST_REQUIRE_EQUAL( val , true);
567  BOOST_REQUIRE_EQUAL( val ,true);
568 
570  BOOST_REQUIRE_EQUAL( val, false);
571  val = is_encap< stub_object > ::value;
572  BOOST_REQUIRE_EQUAL( val, false);
573 
575  }
576 
577  {
579 
580  size_t mul = array_extents<float>::mul();
581  BOOST_REQUIRE_EQUAL(mul,1ul);
583  BOOST_REQUIRE_EQUAL(mul,3ul);
585  BOOST_REQUIRE_EQUAL(mul,3ul*2ul);
587  BOOST_REQUIRE_EQUAL(mul,3ul*2ul*5ul);
588 
590  }
591  }
592 }
593 
594 BOOST_AUTO_TEST_CASE( check_convert_function )
595 {
596  {
598 
599  comb<2> c;
600  c.c[0] = 1;
601  c.c[1] = -1;
602 
604 
605  BOOST_REQUIRE_EQUAL(p.get(0),1ul);
606  BOOST_REQUIRE_EQUAL(p.get(1),(size_t)-1);
607 
609  }
610 
611  {
613 
614  comb<3> c;
615  c.c[0] = 1;
616  c.c[1] = -1;
617  c.c[2] = 0;
618 
620 
621  BOOST_REQUIRE_EQUAL(p.get(0),1);
622  BOOST_REQUIRE_EQUAL(p.get(1),-1);
623  BOOST_REQUIRE_EQUAL(p.get(2),0);
624 
626  }
627 }
628 
629 BOOST_AUTO_TEST_SUITE_END()
630 
631 #endif /* UTIL_TEST_HPP_ */
It copy the properties from one object to another.
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:71
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition: comb.hpp:34
Main class to generate compile-time array.
Definition: ct_array.hpp:113
check if T::type and T.data has the same type
Definition: common.hpp:131
static Point< dim, St > convert(const comb< dim > &c)
Return the combination converted to point.
Definition: convert.hpp:25
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:20
This is a container to create a general object.
This class check if the type T has pointers inside.
is_openfpm_native check if a type is an openfpm native structure type
Definition: common.hpp:247
T get(int i) const
Get coordinate.
Definition: Point.hpp:42
Definition: util.hpp:14
this structure encapsulate an object of the grid
Definition: Encap.hpp:209
[Check has_posMask struct definition]
Definition: util_test.hpp:26
[Metafunction definition]
Definition: util_test.hpp:292
Struct that give functionalities on array extensions.
[Check has_posMask struct definition]
Definition: util_test.hpp:42
It create a boost::fusion vector with the selected properties.
Test structure used for several test.
Definition: Point_test.hpp:72
Implementation of 1-D std::vector like structure.
Definition: map_grid.hpp:94
char c[dim]
Array that store the combination.
Definition: comb.hpp:37
It copy the properties from one object to another.