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