OpenFPM  5.2.0
Project that contain the implementation of distributed structures
multi_array_ref_openfpm_unit_test.cpp
1 #include "config.h"
2 #define BOOST_TEST_DYN_LINK
3 #include <boost/test/unit_test.hpp>
4 
5 #include "multi_array_ref_openfpm.hpp"
6 #include <boost/mpl/vector.hpp>
7 #include <iostream>
8 
9 BOOST_AUTO_TEST_SUITE( multi_array_ref_openfpm_suite )
10 
11 BOOST_AUTO_TEST_CASE( multi_array_ref_openfpm_use )
12 {
13  std::cout << "Test multi array start" << "\n";
14 
15  {
16 
17  float test_mem[10][3];
18  float * p_test = &test_mem[0][0];
19 
20  for (size_t i = 0 ; i < 10 ; i++)
21  {
22  for (size_t j = 0 ; j < 3 ; j++)
23  {
24  p_test[j*10+i] = i*10 + j; // <--------- Note I am inverting from the natural i*10+j
25  }
26  }
27 
28  openfpm::multi_array_ref_openfpm<float,2,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>>> ar((float *)test_mem,10);
29 
30  BOOST_REQUIRE_EQUAL(ar[0][0],0);
31  BOOST_REQUIRE_EQUAL(ar[0][1],1);
32  BOOST_REQUIRE_EQUAL(ar[1][0],10);
33  BOOST_REQUIRE_EQUAL(ar[2][2],22);
34  BOOST_REQUIRE_EQUAL(ar[1][2],12);
35  BOOST_REQUIRE_EQUAL(ar[9][2],92);
36 
37  }
38 
39  {
40 
41  float test_mem[10][3][7];
42  float * p_test = &test_mem[0][0][0];
43 
44  for (size_t i = 0 ; i < 10 ; i++)
45  {
46  for (size_t j = 0 ; j < 3 ; j++)
47  {
48  for (size_t k = 0 ; k < 7 ; k++)
49  {
50  p_test[(j*7+k)*10+i] = i*100 + j*10 + k; // <--------- Note I am inverting from the natural i*10+j
51  }
52  }
53  }
54 
55  openfpm::multi_array_ref_openfpm<float,3,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>>> ar2((float *)test_mem,10);
56 
57  BOOST_REQUIRE_EQUAL(ar2[0][0][0],0);
58  BOOST_REQUIRE_EQUAL(ar2[0][0][1],1);
59  BOOST_REQUIRE_EQUAL(ar2[0][0][2],2);
60  BOOST_REQUIRE_EQUAL(ar2[0][0][3],3);
61  BOOST_REQUIRE_EQUAL(ar2[0][0][4],4);
62  BOOST_REQUIRE_EQUAL(ar2[0][0][6],6);
63  BOOST_REQUIRE_EQUAL(ar2[0][1][0],10);
64  BOOST_REQUIRE_EQUAL(ar2[1][0][4],104);
65  BOOST_REQUIRE_EQUAL(ar2[2][2][6],226);
66  BOOST_REQUIRE_EQUAL(ar2[1][2][6],126);
67  BOOST_REQUIRE_EQUAL(ar2[9][2][3],923);
68  }
69 
70 
71  {
72 
73  float test_mem[10][3][7][2];
74  float * p_test = &test_mem[0][0][0][0];
75 
76  for (size_t i = 0 ; i < 10 ; i++)
77  {
78  for (size_t j = 0 ; j < 3 ; j++)
79  {
80  for (size_t k = 0 ; k < 7 ; k++)
81  {
82  for (size_t s = 0 ; s < 2 ; s++)
83  {
84  p_test[(j*7*2+k*2+s)*10+i] = i*1000 + j*100 + k*10 + s; // <--------- Note I am inverting from the natural
85  }
86  }
87  }
88  }
89 
90  openfpm::multi_array_ref_openfpm<float,4,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>,boost::mpl::int_<2>>> ar2((float *)test_mem,10);
91 
92  BOOST_REQUIRE_EQUAL(ar2[0][0][0][0],0);
93  BOOST_REQUIRE_EQUAL(ar2[0][0][0][1],1);
94  BOOST_REQUIRE_EQUAL(ar2[0][0][2][0],20);
95  BOOST_REQUIRE_EQUAL(ar2[0][0][2][1],21);
96  BOOST_REQUIRE_EQUAL(ar2[0][0][3][0],30);
97  BOOST_REQUIRE_EQUAL(ar2[0][0][4][0],40);
98  BOOST_REQUIRE_EQUAL(ar2[0][2][0][1],201);
99  BOOST_REQUIRE_EQUAL(ar2[0][1][0][0],100);
100  BOOST_REQUIRE_EQUAL(ar2[0][1][6][1],161);
101  BOOST_REQUIRE_EQUAL(ar2[2][2][6][1],2261);
102  BOOST_REQUIRE_EQUAL(ar2[1][1][5][1],1151);
103  BOOST_REQUIRE_EQUAL(ar2[9][2][4][1],9241);
104 
105  // write_test
106 
107  ar2[1][1][5][1] = 1111;
108  BOOST_REQUIRE_EQUAL(ar2[1][1][5][1],1111);
109 
110  }
111 
112  std::cout << "End multi array stop" << "\n";
113 }
114 
115 BOOST_AUTO_TEST_CASE( multi_array_ref_openfpm_copy )
116 {
117  std::cout << "Test multi array copy start" << "\n";
118 
119  {
120 
121  float test_mem[10][3][7][2];
122  float * p_test = &test_mem[0][0][0][0];
123 
124  for (size_t i = 0 ; i < 10 ; i++)
125  {
126  for (size_t j = 0 ; j < 3 ; j++)
127  {
128  for (size_t k = 0 ; k < 7 ; k++)
129  {
130  for (size_t s = 0 ; s < 2 ; s++)
131  {
132  p_test[(j*7*2+k*2+s)*10+i] = i*1000 + j*100 + k*10 + s; // <--------- Note I am inverting from the natural
133  }
134  }
135  }
136  }
137 
138  openfpm::multi_array_ref_openfpm<float,4,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>,boost::mpl::int_<2>>> ar2((float *)test_mem,10);
139 
140  float test_mem2[10][3][7][2];
141  openfpm::multi_array_ref_openfpm<float,4,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>,boost::mpl::int_<2>>> ar3((float *)test_mem2,10);
142 
143  // Copy and check
144 
145  ar3 = ar2;
146 
147  bool check = true;
148  for (size_t i = 0 ; i < 10 ; i++)
149  {
150  for (size_t j = 0 ; j < 3 ; j++)
151  {
152  for (size_t k = 0 ; k < 7 ; k++)
153  {
154  for (size_t s = 0 ; s < 2 ; s++)
155  {
156  check &= ar3[i][j][k][s] == ar2[i][j][k][s];
157  }
158  }
159  }
160  }
161 
162  BOOST_REQUIRE_EQUAL(check,true);
163 
164  }
165 
166  std::cout << "End multi array copy stop" << "\n";
167 }
168 
169 BOOST_AUTO_TEST_CASE( multi_array_ref_openfpm_bind_ref )
170 {
171  std::cout << "Test multi array bind_ref start" << "\n";
172 
173  {
174 
175  float test_mem[10][3][7][2];
176  float * p_test = &test_mem[0][0][0][0];
177 
178  for (size_t i = 0 ; i < 10 ; i++)
179  {
180  for (size_t j = 0 ; j < 3 ; j++)
181  {
182  for (size_t k = 0 ; k < 7 ; k++)
183  {
184  for (size_t s = 0 ; s < 2 ; s++)
185  {
186  p_test[(j*7*2+k*2+s)*10+i] = i*1000 + j*100 + k*10 + s; // <--------- Note I am inverting from the natural
187  }
188  }
189  }
190  }
191 
192  openfpm::multi_array_ref_openfpm<float,4,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>,boost::mpl::int_<2>>> ar2((float *)test_mem,10);
193 
194  openfpm::multi_array_ref_openfpm<float,4,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>,boost::mpl::int_<2>>> ar3((float *)NULL,10);
195  ar3.bind_ref(ar2);
196 
197  // Copy and check
198 
199  bool check = true;
200  for (size_t i = 0 ; i < 10 ; i++)
201  {
202  for (size_t j = 0 ; j < 3 ; j++)
203  {
204  for (size_t k = 0 ; k < 7 ; k++)
205  {
206  for (size_t s = 0 ; s < 2 ; s++)
207  {
208  check &= ar3[i][j][k][s] == ar2[i][j][k][s];
209  }
210  }
211  }
212  }
213 
214  BOOST_REQUIRE_EQUAL(check,true);
215 
216  }
217 
218  std::cout << "End multi array copy bind_ref" << "\n";
219 }
220 
221 BOOST_AUTO_TEST_CASE( multi_array_ref_openfpm_swap )
222 {
223  std::cout << "Test multi array swap start" << "\n";
224 
225  {
226 
227  float test_mem[10][3][7][2];
228  float * p_test = &test_mem[0][0][0][0];
229 
230  for (size_t i = 0 ; i < 10 ; i++)
231  {
232  for (size_t j = 0 ; j < 3 ; j++)
233  {
234  for (size_t k = 0 ; k < 7 ; k++)
235  {
236  for (size_t s = 0 ; s < 2 ; s++)
237  {
238  p_test[(j*7*2+k*2+s)*10+i] = i*1000 + j*100 + k*10 + s; // <--------- Note I am inverting from the natural
239  }
240  }
241  }
242  }
243 
244  openfpm::multi_array_ref_openfpm<float,4,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>,boost::mpl::int_<2>>> ar2((float *)test_mem,10);
245 
246  float test_mem2[10][3][7][2];
247  float * p_test2 = &test_mem2[0][0][0][0];
248 
249  for (size_t i = 0 ; i < 10 ; i++)
250  {
251  for (size_t j = 0 ; j < 3 ; j++)
252  {
253  for (size_t k = 0 ; k < 7 ; k++)
254  {
255  for (size_t s = 0 ; s < 2 ; s++)
256  {
257  p_test2[(j*7*2+k*2+s)*10+i] = i*1000 + j*100 + k*10 + s + 100000; // <--------- Note I am inverting from the natural
258  }
259  }
260  }
261  }
262 
263  openfpm::multi_array_ref_openfpm<float,4,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>,boost::mpl::int_<2>>> ar3((float *)test_mem2,10);
264  ar3.swap(ar2);
265 
266  // Copy and check
267 
268  bool check = true;
269  for (size_t i = 0 ; i < 10 ; i++)
270  {
271  for (size_t j = 0 ; j < 3 ; j++)
272  {
273  for (size_t k = 0 ; k < 7 ; k++)
274  {
275  for (size_t s = 0 ; s < 2 ; s++)
276  {
277  check &= ar3[i][j][k][s] == i*1000 + j*100 + k*10 + s;
278  check &= ar2[i][j][k][s] == i*1000 + j*100 + k*10 + s + 100000;
279  }
280  }
281  }
282  }
283 
284  BOOST_REQUIRE_EQUAL(check,true);
285 
286  }
287 
288  std::cout << "End multi array swap stop" << "\n";
289 }
290 
291 BOOST_AUTO_TEST_CASE( test_is_multi_array )
292 {
294 
295  BOOST_REQUIRE_EQUAL(test,false);
296 
297  test = openfpm::is_multi_array<openfpm::multi_array_ref_openfpm<float,4,boost::mpl::vector<boost::mpl::int_<-1>,boost::mpl::int_<3>,boost::mpl::int_<7>,boost::mpl::int_<2>>>>::value;
298 
299  BOOST_REQUIRE_EQUAL(test,true);
300 }
301 
302 BOOST_AUTO_TEST_SUITE_END()
303 
304