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
Packer_nested_tests.hpp
1 #ifndef SRC_PACKER_NESTED_TESTS_HPP_
2 #define SRC_PACKER_NESTED_TESTS_HPP_
3 
4 #include "Pack_selector.hpp"
5 #include "Packer.hpp"
6 #include "Unpacker.hpp"
7 #include "Grid/grid_util_test.hpp"
8 #include <iostream>
9 
10 //Testing packing and unpacking for different vectors
11 
12 BOOST_AUTO_TEST_SUITE( packer_unpacker )
13 
14 BOOST_AUTO_TEST_CASE ( vector_ptst_packer_unpacker )
15 {
16  std::cout << "Vector pack/unpack test start" << "\n";
17 
19  for (size_t i = 0; i < 5; i++) {
21  for (size_t j = 0; j < 6; j++) {
22  v4.add(allocate_openfpm(7));
23  }
24  v.add(v4);
25  }
26 
27  typedef Point_test<float> pt;
28 
29  //Pack request vector
30  std::vector<size_t> pap_prp;
31 
32  //Pack requesting
33 
34  Packer<decltype(v),HeapMemory>::packRequest<pt::x, pt::v>(v,pap_prp);
35  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],(sizeof(float) + sizeof(float[3])) * 7);
36 
37 
38 
39  //Just to see the elements of pack request vector
40 #ifdef DEBUG
41  for (size_t i = 0; i < pap_prp.size(); i++)
42  std::cout << pap_prp[i] << std::endl;
43 #endif
44 
45  // Calculate how much preallocated memory we need to pack all the objects
46  size_t req = ExtPreAlloc<HeapMemory>::calculateMem(pap_prp);
47 
48  // allocate the memory
49  HeapMemory pmem;
50  pmem.allocate(req);
51  ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(pap_prp,pmem));
52  mem.incRef();
53 
54  //Packing
55 
56  Pack_stat sts;
57 
58  Packer<decltype(v),HeapMemory>::pack<pt::x,pt::v>(mem,v,sts);
59 
60  //Unpacking
61 
62  Unpack_stat ps;
63 
65 
66  Unpacker<decltype(v_unp),HeapMemory>::unpack<pt::x,pt::v>(mem,v_unp,ps);
67 
68  //Check equality vector v_unp
69  for (size_t k = 0; k < v_unp.size(); k++)
70  {
71  for (size_t i = 0; i < v_unp.get(k).size(); i++)
72  {
73  auto it = v_unp.get(k).get(i).getIterator();
74 
75  while (it.isNext())
76  {
77  float f1 = v_unp.get(k).get(i).template get<pt::x>(it.get());
78  float f2 = v.get(k).get(i).template get<pt::x>(it.get());
79 
80  BOOST_REQUIRE_EQUAL(f1,f2);
81 
82  for (size_t j = 0 ; j < 3 ; j++)
83  {
84  f1 = v_unp.get(k).get(i).template get<pt::v>(it.get())[j];
85  f2 = v.get(k).get(i).template get<pt::v>(it.get())[j];
86 
87  BOOST_REQUIRE_EQUAL(f1,f2);
88  }
89  ++it;
90  }
91  }
92  }
93 
94 }
95 
96 BOOST_AUTO_TEST_CASE ( vector_std_packer_unpacker )
97 {
99  for (size_t i = 0; i < 5; i++) {
101  for (size_t j = 0; j < 6; j++) {
103  for (size_t k = 0; k < 7; k++) {
104  v7.add(1);
105  }
106  v6.add(v7);
107  }
108  v2.add(v6);
109  }
110 
111  //Pack request vector
112  std::vector<size_t> pap_prp;
113 
114  //Pack requesting
115 
117  BOOST_REQUIRE_EQUAL(pap_prp[pap_prp.size()-1],sizeof(float) * 7);
118 
119  //Just to see the elements of pack request vector
120 #ifdef DEBUG
121  for (size_t i = 0; i < pap_prp.size(); i++)
122  std::cout << pap_prp[i] << std::endl;
123 #endif
124 
125  // Calculate how much preallocated memory we need to pack all the objects
126  size_t req = ExtPreAlloc<HeapMemory>::calculateMem(pap_prp);
127 
128  // allocate the memory
129  HeapMemory pmem;
130  pmem.allocate(req);
131  ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(pap_prp,pmem));
132  mem.incRef();
133 
134  //Packing
135 
136  Pack_stat sts;
137 
139 
140  //Unpacking
141 
142  Unpack_stat ps;
143 
145 
147 
148  //Check equality vector v2_unp
149  for (size_t k = 0; k < v2_unp.size(); k++)
150  {
151  for (size_t i = 0; i < v2_unp.get(k).size(); i++)
152  {
153  for (size_t j = 0; j < v2_unp.get(k).get(i).size(); j++)
154  {
155  float f1 = v2_unp.get(k).get(i).get(j);
156  float f2 = v2.get(k).get(i).get(j);
157 
158  BOOST_REQUIRE_EQUAL(f1,f2);
159  }
160  }
161  }
162 
163  std::cout << "Vector pack/unpack test stop" << "\n";
164 
165 }
166 BOOST_AUTO_TEST_SUITE_END()
167 
168 #endif /* SRC_PACKER_NESTED_TESTS_HPP_ */
Unpacker class.
Definition: Unpacker.hpp:28
Packing class.
Definition: Packer.hpp:46
Unpacking status object.
Definition: Pack_stat.hpp:15
Test structure used for several test.
Definition: Point_test.hpp:72
Implementation of 1-D std::vector like structure.
Definition: map_grid.hpp:94
Packing status object.
Definition: Pack_stat.hpp:49