OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
Box_unit_tests.hpp
1 /*
2  * Box_unit_tests.hpp
3  *
4  * Created on: May 8, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef BOX_UNIT_TESTS_HPP_
9 #define BOX_UNIT_TESTS_HPP_
10 
11 BOOST_AUTO_TEST_SUITE( box_test )
12 
13 BOOST_AUTO_TEST_CASE( box_use)
14 {
15  std::cout << "Box unit test start" << "\n";
16 
18 
19  float spacing[2] = {0.1,0.1};
20 
21  Box<2,float> sp({1.0,1.0},{2.0,3.0});
22  sp.expand(spacing);
23 
24  BOOST_REQUIRE_CLOSE(sp.getLow(0),1.0,0.0001);
25  BOOST_REQUIRE_CLOSE(sp.getLow(1),1.0,0.0001);
26 
27  BOOST_REQUIRE_CLOSE(sp.getHigh(0),2.1,0.0001);
28  BOOST_REQUIRE_CLOSE(sp.getHigh(1),3.1,0.0001);
29 
31 
32  {
34 
35  Box<3,float> box1({0.1,0.2,0.3},{1.0,1.1,1.3});
36  Box<3,float> box2({0.5,0.6,0.7},{2.0,2.1,2.2});
37 
38  box1.enclose(box2);
39 
40  BOOST_REQUIRE_EQUAL(box1.getLow(0),0.1f);
41  BOOST_REQUIRE_EQUAL(box1.getLow(1),0.2f);
42  BOOST_REQUIRE_EQUAL(box1.getLow(2),0.3f);
43 
44  BOOST_REQUIRE_EQUAL(box1.getHigh(0),2.0f);
45  BOOST_REQUIRE_EQUAL(box1.getHigh(1),2.1f);
46  BOOST_REQUIRE_EQUAL(box1.getHigh(2),2.2f);
47 
49  }
50 
51  // Create the smallest boxes between several boxes or
52  // Create a box that is contained into more boxes centering them on p1
53 
54  {
55 
57  Box<3,float> box1({0.0,0.0,0.0},{1.0,1.1,1.3});
58  Box<3,float> box2({0.5,2.0,0.5},{2.0,2.1,2.2});
59  Box<3,float> box3({1.5,1.5,4.2},{5.0,5.1,5.2});
60 
61  box1.contained(box2);
62  box1.contained(box3);
63 
64  BOOST_REQUIRE_CLOSE(box1.getHigh(0),1.0f,0.0001);
65  BOOST_REQUIRE_CLOSE(box1.getHigh(1),0.1f,0.0001);
66  BOOST_REQUIRE_CLOSE(box1.getHigh(2),1.0f,0.0001);
67 
69  }
70 
71  {
73 
74  Box<3,float> box1({0.1,0.2,0.3},{1.0,1.1,1.3});
75  Box<3,float> box2({-0.5,-0.6,-0.7},{0.5,0.6,0.7});
76 
77  box1.enlarge(box2);
78 
79  BOOST_REQUIRE_CLOSE(box1.getLow(0),-0.4,0.0001);
80  BOOST_REQUIRE_CLOSE(box1.getLow(1),-0.4,0.0001);
81  BOOST_REQUIRE_CLOSE(box1.getLow(2),-0.4,0.0001);
82 
83  BOOST_REQUIRE_CLOSE(box1.getHigh(0),1.5,0.0001);
84  BOOST_REQUIRE_CLOSE(box1.getHigh(1),1.7,0.0001);
85  BOOST_REQUIRE_CLOSE(box1.getHigh(2),2.0,0.0001);
86 
88  }
89 
90  {
92 
93  Box<3,float> box1({0.1,0.2,0.3},{1.0,1.1,1.3});
94  Box<3,float> box2({-0.5,-0.6,-0.7},{0.5,0.6,0.7});
95 
96  box1.enlarge_fix_P1(box2);
97 
98  BOOST_REQUIRE_CLOSE(box1.getLow(0),0.1,0.0001);
99  BOOST_REQUIRE_CLOSE(box1.getLow(1),0.2,0.0001);
100  BOOST_REQUIRE_CLOSE(box1.getLow(2),0.3,0.0001);
101 
102  BOOST_REQUIRE_CLOSE(box1.getHigh(0),2.0,0.0001);
103  BOOST_REQUIRE_CLOSE(box1.getHigh(1),2.3,0.0001);
104  BOOST_REQUIRE_CLOSE(box1.getHigh(2),2.7,0.0001);
105 
107  }
108 
109  {
111 
112  Box<3,float> box1({0.1,0.2,0.3},{1.0,1.1,1.3});
113 
114  box1.magnify(1.001);
115 
116  BOOST_REQUIRE_CLOSE(box1.getLow(0),0.1001,0.001);
117  BOOST_REQUIRE_CLOSE(box1.getLow(1),0.2002,0.001);
118  BOOST_REQUIRE_CLOSE(box1.getLow(2),0.3003,0.001);
119 
120  BOOST_REQUIRE_CLOSE(box1.getHigh(0),1.001,0.00001);
121  BOOST_REQUIRE_CLOSE(box1.getHigh(1),1.1011,0.00001);
122  BOOST_REQUIRE_CLOSE(box1.getHigh(2),1.3013,0.00001);
123 
124  // Check that the dimensions of the box are magnified of 0.1%
125  BOOST_REQUIRE_CLOSE(box1.getHigh(0) - box1.getLow(0), 1.001 * 0.9 ,0.001);
126  BOOST_REQUIRE_CLOSE(box1.getHigh(1) - box1.getLow(1), 1.001 * 0.9, 0.001);
127  BOOST_REQUIRE_CLOSE(box1.getHigh(2) - box1.getLow(2), 1.001 * 1.0, 0.001);
128 
130  }
131 
132  {
134 
135  Box<3,float> box1({0.1,0.2,0.3},{1.0,1.1,1.3});
136 
137  box1.magnify_fix_P1(1.001);
138 
139  BOOST_REQUIRE_CLOSE(box1.getLow(0),0.1,0.0001);
140  BOOST_REQUIRE_CLOSE(box1.getLow(1),0.2,0.0001);
141  BOOST_REQUIRE_CLOSE(box1.getLow(2),0.3,0.0001);
142 
143  BOOST_REQUIRE_CLOSE(box1.getHigh(0),1.0009,0.00001);
144  BOOST_REQUIRE_CLOSE(box1.getHigh(1),1.1009,0.00001);
145  BOOST_REQUIRE_CLOSE(box1.getHigh(2),1.301,0.00001);
146 
147  // Check that the dimensions of the box are magnified of 0.1%
148  BOOST_REQUIRE_CLOSE(box1.getHigh(0) - box1.getLow(0), 1.001 * 0.9 ,0.00001);
149  BOOST_REQUIRE_CLOSE(box1.getHigh(1) - box1.getLow(1), 1.001 * 0.9, 0.00001);
150  BOOST_REQUIRE_CLOSE(box1.getHigh(2) - box1.getLow(2), 1.001 * 1.0, 0.00001);
151 
153  }
154 
155  {
157 
158  Box<3,float> box1({0.1,0.5,0.6},{1.0,1.2,1.4});
159  Point<3,float> pnt({0.1,0.2,0.3});
160 
161  box1 -= pnt;
162 
163  BOOST_REQUIRE_CLOSE(box1.getLow(0),0.0,0.0001);
164  BOOST_REQUIRE_CLOSE(box1.getLow(1),0.3,0.0001);
165  BOOST_REQUIRE_CLOSE(box1.getLow(2),0.3,0.0001);
166 
167  BOOST_REQUIRE_CLOSE(box1.getHigh(0),0.9,0.0001);
168  BOOST_REQUIRE_CLOSE(box1.getHigh(1),1.0,0.0001);
169  BOOST_REQUIRE_CLOSE(box1.getHigh(2),1.1,0.0001);
170 
172 
173  box1 -= box1.getP2();
174 
175  BOOST_REQUIRE_CLOSE(box1.getLow(0),-0.9,0.0001);
176  BOOST_REQUIRE_CLOSE(box1.getLow(1),-0.7,0.0001);
177  BOOST_REQUIRE_CLOSE(box1.getLow(2),-0.8,0.0001);
178 
179  BOOST_REQUIRE_CLOSE(box1.getHigh(0),0.0,0.0001);
180  BOOST_REQUIRE_CLOSE(box1.getHigh(1),0.0,0.0001);
181  BOOST_REQUIRE_CLOSE(box1.getHigh(2),0.0,0.0001);
182 
183  box1 -= box1.getP1();
184 
185  BOOST_REQUIRE_CLOSE(box1.getLow(0),0.0,0.0001);
186  BOOST_REQUIRE_CLOSE(box1.getLow(1),0.0,0.0001);
187  BOOST_REQUIRE_CLOSE(box1.getLow(2),0.0,0.0001);
188 
189  BOOST_REQUIRE_CLOSE(box1.getHigh(0),0.9,0.0001);
190  BOOST_REQUIRE_CLOSE(box1.getHigh(1),0.7,0.0001);
191  BOOST_REQUIRE_CLOSE(box1.getHigh(2),0.8,0.0001);
192  }
193 
194  {
195  Box<2,size_t> invalid1({5,7},{3,9});
196  Box<2,size_t> invalid2({5,11},{9,9});
197  Box<2,size_t> invalid3({12,11},{9,9});
198 
199  Box<2,size_t> valid1({1,5},{6,9});
200  Box<2,size_t> valid2({1,1},{1,1});
201 
202  bool val = invalid1.isValid();
203  BOOST_REQUIRE_EQUAL(val,false);
204  val = invalid2.isValid();
205  BOOST_REQUIRE_EQUAL(invalid2.isValid(),false);
206  val = invalid3.isValid();
207  BOOST_REQUIRE_EQUAL(invalid3.isValid(),false);
208  val = valid1.isValid();
209  BOOST_REQUIRE_EQUAL(valid1.isValid(),true);
210  val = valid2.isValid();
211  BOOST_REQUIRE_EQUAL(valid2.isValid(),true);
212 
213  }
214 
215  {
217 
218  Box<2,float> box({0.5,0.6},{1.4,1.3});
219  Point<2,float> p({3.0,4.0});
220  Box<2,float> result;
221 
222  result = box * p;
223 
224  BOOST_REQUIRE_CLOSE(result.getHigh(0),4.2,0.0001);
225  BOOST_REQUIRE_CLOSE(result.getHigh(1),5.2,0.0001);
226 
227  BOOST_REQUIRE_CLOSE(result.getLow(0),1.5,0.0001);
228  BOOST_REQUIRE_CLOSE(result.getLow(1),2.4,0.0001);
229 
231  }
232 
233  {
235 
236  Box<2,float> box({0.0,0.0},{1.0,1.0});
237  Point<2,float> p1({0.0,0.0});
238  Point<2,float> p2({0.5,0.5});
239  Point<2,float> p3({1.0,0.5});
240 
241  BOOST_REQUIRE_EQUAL(box.isInsideNP(p1),true);
242  BOOST_REQUIRE_EQUAL(box.isInsideNP(p2),true);
243  BOOST_REQUIRE_EQUAL(box.isInsideNP(p3),false);
244 
246  }
247 
248  {
250 
251  Box<2,float> box({0.0,0.0},{1.0,1.0});
252  Point<2,float> p1({0.0,0.0});
253  Point<2,float> p2({0.5,0.5});
254  Point<2,float> p3({1.0,0.5});
255 
256  BOOST_REQUIRE_EQUAL(box.isInside(p1),true);
257  BOOST_REQUIRE_EQUAL(box.isInside(p2),true);
258  BOOST_REQUIRE_EQUAL(box.isInside(p3),true);
259 
261  }
262 
263  {
265 
266  Box<2,float> box({0.0,0.0},{1.0,1.0});
267  Point<2,float> p1({0.0,0.0});
268  Point<2,float> p2({0.5,0.5});
269  Point<2,float> p3({1.0,0.5});
270 
271  BOOST_REQUIRE_EQUAL(box.isInsideNB(p1),false);
272  BOOST_REQUIRE_EQUAL(box.isInsideNB(p2),true);
273  BOOST_REQUIRE_EQUAL(box.isInsideNB(p3),false);
274 
276  }
277 
278  Box<3,float> b1({1.0,2.4,5.3},{6.0,7.9,9.9});
279  Box<3,float> b2 = b1;
280 
281  bool ret = (b1 == b2);
282 
283  BOOST_REQUIRE_EQUAL(ret,true);
284 
285  b1.invalidate();
286 
287  BOOST_REQUIRE_EQUAL(b1.isValid(),false);
288 
289  std::cout << "Box unit test stop" << "\n";
290 }
291 
292 BOOST_AUTO_TEST_CASE( box_min_distance_test )
293 {
294  // 2D
295 
296  Box<2,float> b1({0.0,0.0},{1.0,1.0});
297 
298  // Tounching boxes
299  Box<2,float> b2({-1.0,-1.0},{0.0,0.0});
300  Box<2,float> b3({-1.0, 0.0},{0.0,1.0});
301  Box<2,float> b4({-1.0, 1.0},{0.0,2.0});
302  Box<2,float> b5({ 0.0,-1.0},{1.0,0.0});
303  Box<2,float> b6({ 0.0, 0.0},{1.0,1.0});
304  Box<2,float> b7({ 0.0, 1.0},{1.0,2.0});
305  Box<2,float> b8({ 1.0,-1.0},{2.0,0.0});
306  Box<2,float> b9({ 1.0, 0.0},{2.0,1.0});
307  Box<2,float> b10({1.0, 1.0},{2.0,2.0});
308 
309  BOOST_REQUIRE_EQUAL(b1.min_distance(b2),0.0);
310  BOOST_REQUIRE_EQUAL(b1.min_distance(b3),0.0);
311  BOOST_REQUIRE_EQUAL(b1.min_distance(b4),0.0);
312  BOOST_REQUIRE_EQUAL(b1.min_distance(b5),0.0);
313  BOOST_REQUIRE_EQUAL(b1.min_distance(b6),0.0);
314  BOOST_REQUIRE_EQUAL(b1.min_distance(b7),0.0);
315  BOOST_REQUIRE_EQUAL(b1.min_distance(b8),0.0);
316  BOOST_REQUIRE_EQUAL(b1.min_distance(b9),0.0);
317  BOOST_REQUIRE_EQUAL(b1.min_distance(b10),0.0);
318 
319  // shift 0.1 on X
320 
321  Point<2,float> p({-0.1,0.0});
322 
323  b2 += p;
324  b3 += p;
325  b4 += p;
326  b5 += p;
327  b6 += p;
328  b7 += p;
329  b8 += p;
330  b9 += p;
331  b10 += p;
332 
333  BOOST_REQUIRE_CLOSE(b1.min_distance(b2),0.1,0.01);
334  BOOST_REQUIRE_CLOSE(b1.min_distance(b3),0.1,0.01);
335  BOOST_REQUIRE_CLOSE(b1.min_distance(b4),0.1,0.01);
336  BOOST_REQUIRE_CLOSE(b1.min_distance(b5),0.1,0.01);
337  BOOST_REQUIRE_CLOSE(b1.min_distance(b6),0.1,0.01);
338  BOOST_REQUIRE_CLOSE(b1.min_distance(b7),0.1,0.01);
339  BOOST_REQUIRE_CLOSE(b1.min_distance(b8),0.1,0.01);
340  BOOST_REQUIRE_CLOSE(b1.min_distance(b9),0.1,0.01);
341  BOOST_REQUIRE_CLOSE(b1.min_distance(b10),0.1,0.01);
342 
343  // shift out -0.1 on Y
344 
345  Point<2,float> p2({0.0,-0.1});
346 
347  // Tounching boxes
348  b2 += p2;
349  b3 += p2;
350  b4 += p2;
351  b5 += p2;
352  b6 += p2;
353  b7 += p2;
354  b8 += p2;
355  b9 += p2;
356  b10 += p2;
357 
358  BOOST_REQUIRE_CLOSE(b1.min_distance(b2),sqrt(0.01 + 0.01),0.01);
359  BOOST_REQUIRE_CLOSE(b1.min_distance(b3),sqrt(0.01 + 0.01),0.01);
360  BOOST_REQUIRE_CLOSE(b1.min_distance(b4),sqrt(0.01 + 0.01),0.01);
361  BOOST_REQUIRE_CLOSE(b1.min_distance(b5),sqrt(0.01 + 0.01),0.01);
362  BOOST_REQUIRE_CLOSE(b1.min_distance(b6),sqrt(0.01 + 0.01),0.01);
363  BOOST_REQUIRE_CLOSE(b1.min_distance(b7),sqrt(0.01 + 0.01),0.01);
364  BOOST_REQUIRE_CLOSE(b1.min_distance(b8),sqrt(0.01 + 0.01),0.01);
365  BOOST_REQUIRE_CLOSE(b1.min_distance(b9),sqrt(0.01 + 0.01),0.01);
366  BOOST_REQUIRE_CLOSE(b1.min_distance(b10),sqrt(0.01 + 0.01),0.01);
367 }
368 
369 BOOST_AUTO_TEST_CASE( box_is_inside_with_border )
370 {
371  // 2D
372 
373  size_t bc_nn[] = {NON_PERIODIC,NON_PERIODIC};
374  size_t bc_pn[] = {PERIODIC,NON_PERIODIC};
375  size_t bc_np[] = {NON_PERIODIC,PERIODIC};
376  size_t bc_pp[] = {PERIODIC,PERIODIC};
377 
378  Box<2,float> border({0.0,0.0},{1.0,1.0});
379 
380  // non-tounching box
381  Box<2,float> b2({0.1,0.1},{0.2,0.2});
382  Box<2,float> b3({0.1,0.1},{1.0,1.0});
383 
384  Point<2,float> p1({0.15,0.15});
385  Point<2,float> p2({0.25,0.25});
386  Point<2,float> p3({0.15,0.2});
387 
388  Point<2,float> p4({0.1,0.25});
389  Point<2,float> p5({0.15,1.0});
390  Point<2,float> p6({0.1,1.0});
391 
393 
394  bool result = b2.isInsideNP_with_border(p1,border,bc_nn);
395  BOOST_REQUIRE_EQUAL(result,true);
396 
397  result = b2.isInsideNP_with_border(p2,border,bc_nn);
398  BOOST_REQUIRE_EQUAL(result,false);
399 
400  result = b2.isInsideNP_with_border(p3,border,bc_nn);
401  BOOST_REQUIRE_EQUAL(result,false);
402 
403  result = b3.isInsideNP_with_border(p1,border,bc_nn);
404 
405  BOOST_REQUIRE_EQUAL(result,true);
406 
407  result = b3.isInsideNP_with_border(p4,border,bc_nn);
408  BOOST_REQUIRE_EQUAL(result,true);
409 
410  result = b3.isInsideNP_with_border(p5,border,bc_nn);
411  BOOST_REQUIRE_EQUAL(result,true);
412 
413  result = b3.isInsideNP_with_border(p6,border,bc_nn);
414  BOOST_REQUIRE_EQUAL(result,true);
415 
417 
418  result = b2.isInsideNP_with_border(p1,border,bc_pn);
419  BOOST_REQUIRE_EQUAL(result,true);
420 
421  result = b2.isInsideNP_with_border(p2,border,bc_pn);
422  BOOST_REQUIRE_EQUAL(result,false);
423 
424  result = b2.isInsideNP_with_border(p3,border,bc_pn);
425  BOOST_REQUIRE_EQUAL(result,false);
426 
427  result = b3.isInsideNP_with_border(p1,border,bc_pn);
428 
429  BOOST_REQUIRE_EQUAL(result,true);
430 
431  result = b3.isInsideNP_with_border(p4,border,bc_pn);
432  BOOST_REQUIRE_EQUAL(result,true);
433 
434  result = b3.isInsideNP_with_border(p5,border,bc_pn);
435  BOOST_REQUIRE_EQUAL(result,true);
436 
437  result = b3.isInsideNP_with_border(p6,border,bc_pn);
438  BOOST_REQUIRE_EQUAL(result,true);
439 
441 
442  result = b2.isInsideNP_with_border(p1,border,bc_np);
443  BOOST_REQUIRE_EQUAL(result,true);
444 
445  result = b2.isInsideNP_with_border(p2,border,bc_np);
446  BOOST_REQUIRE_EQUAL(result,false);
447 
448  result = b2.isInsideNP_with_border(p3,border,bc_np);
449  BOOST_REQUIRE_EQUAL(result,false);
450 
451  result = b3.isInsideNP_with_border(p1,border,bc_np);
452 
453  BOOST_REQUIRE_EQUAL(result,true);
454 
455  result = b3.isInsideNP_with_border(p4,border,bc_np);
456  BOOST_REQUIRE_EQUAL(result,true);
457 
458  result = b3.isInsideNP_with_border(p5,border,bc_np);
459  BOOST_REQUIRE_EQUAL(result,false);
460 
461  result = b3.isInsideNP_with_border(p6,border,bc_np);
462  BOOST_REQUIRE_EQUAL(result,false);
463 
465 
466  result = b2.isInsideNP_with_border(p1,border,bc_pp);
467  BOOST_REQUIRE_EQUAL(result,true);
468 
469  result = b2.isInsideNP_with_border(p2,border,bc_pp);
470  BOOST_REQUIRE_EQUAL(result,false);
471 
472  result = b2.isInsideNP_with_border(p3,border,bc_pp);
473  BOOST_REQUIRE_EQUAL(result,false);
474 
475  result = b3.isInsideNP_with_border(p1,border,bc_pp);
476 
477  BOOST_REQUIRE_EQUAL(result,true);
478 
479  result = b3.isInsideNP_with_border(p4,border,bc_pp);
480  BOOST_REQUIRE_EQUAL(result,true);
481 
482  result = b3.isInsideNP_with_border(p5,border,bc_pp);
483  BOOST_REQUIRE_EQUAL(result,false);
484 
485  result = b3.isInsideNP_with_border(p6,border,bc_pp);
486  BOOST_REQUIRE_EQUAL(result,false);
487 }
488 
489 BOOST_AUTO_TEST_SUITE_END()
490 
491 
492 
493 #endif /* BOX_UNIT_TESTS_HPP_ */
__device__ __host__ bool isInsideNP_with_border(const Point< dim, T > &p, const Box< dim, T > &border, const bc_type(&bc)[dim]) const
Check if the point is inside the region excluding the positive part.
Definition: Box.hpp:1067
void magnify_fix_P1(T mg)
Magnify the box by a factor keeping fix the point P1.
Definition: Box.hpp:907
__device__ __host__ T getLow(int i) const
get the i-coordinate of the low bound interval of the box
Definition: Box.hpp:556
void magnify(T mg)
Magnify the box.
Definition: Box.hpp:889
Point< dim, T > getP2() const
Get the point p2.
Definition: Box.hpp:722
void expand(T(&exp)[dim])
expand the box by a vector
Definition: Box.hpp:793
void enclose(const Box< dim, T > &en)
Refine the box to enclose the given box and itself.
Definition: Box.hpp:935
void enlarge(const Box< dim, T > &gh)
Enlarge the box with ghost margin.
Definition: Box.hpp:823
This class represent an N-dimensional box.
Definition: Box.hpp:60
bool isValid() const
Check if the Box is a valid box P2 >= P1.
Definition: Box.hpp:1180
void enlarge_fix_P1(Box< dim, S > &gh)
Enlarge the box with ghost margin keeping fix the point P1.
Definition: Box.hpp:856
void contained(const Box< dim, T > &en, const bool reset_p1=true)
Refine the box to be contained in the given box and itself.
Definition: Box.hpp:955
__device__ __host__ T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:567
Point< dim, T > getP1() const
Get the point p1.
Definition: Box.hpp:708