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
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  Box<3,float> b1({1.0,2.4,5.3},{6.0,7.9,9.9});
249  Box<3,float> b2 = b1;
250 
251  bool ret = (b1 == b2);
252 
253  BOOST_REQUIRE_EQUAL(ret,true);
254 
255  std::cout << "Box unit test stop" << "\n";
256 }
257 
258 BOOST_AUTO_TEST_SUITE_END()
259 
260 
261 
262 #endif /* BOX_UNIT_TESTS_HPP_ */
void magnify_fix_P1(T mg)
Magnify the box by a factor keeping fix the point P1.
Definition: Box.hpp:762
T getLow(int i) const
get the i-coordinate of the low bound interval of the box
Definition: Box.hpp:484
bool isValid() const
Check if the Box is a valid box P2 >= P1.
Definition: Box.hpp:929
void magnify(T mg)
Magnify the box.
Definition: Box.hpp:744
T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:495
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:20
void enclose(Box< dim, T > &en)
Refine the box to enclose the given box and itself.
Definition: Box.hpp:789
void enlarge(const Box< dim, T > &gh)
Enlarge the box with ghost margin.
Definition: Box.hpp:691
This class represent an N-dimensional box.
Definition: Box.hpp:56
void enlarge_fix_P1(Box< dim, S > &gh)
Enlarge the box with ghost margin keeping fix the point P1.
Definition: Box.hpp:724
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:809