1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2014, Oracle and/or its affiliates.
5
6 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
10
11 #ifndef BOOST_TEST_MODULE
12 #define BOOST_TEST_MODULE test_distance_default_result
13 #endif
14
15 #include <cstddef>
16 #include <iostream>
17
18 #include <boost/test/included/unit_test.hpp>
19
20 #include <boost/mpl/assert.hpp>
21 #include <boost/mpl/if.hpp>
22 #include <boost/type_traits/is_same.hpp>
23
24 #include <boost/geometry/util/calculation_type.hpp>
25
26 #include <boost/geometry/geometries/point.hpp>
27 #include <boost/geometry/geometries/segment.hpp>
28 #include <boost/geometry/geometries/box.hpp>
29
30 #include <boost/geometry/strategies/strategies.hpp>
31 #include <boost/geometry/strategies/default_distance_result.hpp>
32 #include <boost/geometry/strategies/default_comparable_distance_result.hpp>
33
34 #if defined(HAVE_TTMATH)
35 #include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
36 #endif
37
38 namespace bg = ::boost::geometry;
39
40
41 template <typename DefaultResult, typename ExpectedResult>
42 struct assert_equal_types
43 {
assert_equal_typesassert_equal_types44 assert_equal_types()
45 {
46 static const bool are_same =
47 boost::is_same<DefaultResult, ExpectedResult>::type::value;
48
49 BOOST_MPL_ASSERT_MSG((are_same),
50 WRONG_DEFAULT_DISTANCE_RESULT,
51 (types<DefaultResult, ExpectedResult>));
52 }
53 };
54
55 //=========================================================================
56
57 template
58 <
59 typename Geometry1,
60 typename Geometry2,
61 typename ExpectedResult,
62 typename ExpectedComparableResult
63 >
test_distance_result()64 inline void test_distance_result()
65 {
66 typedef typename bg::default_distance_result
67 <
68 Geometry1, Geometry2
69 >::type result12;
70
71 typedef typename bg::default_distance_result
72 <
73 Geometry2, Geometry1
74 >::type result21;
75
76 typedef typename bg::default_comparable_distance_result
77 <
78 Geometry1, Geometry2
79 >::type comparable_result12;
80
81 typedef typename bg::default_comparable_distance_result
82 <
83 Geometry2, Geometry1
84 >::type comparable_result21;
85
86 assert_equal_types<result12, ExpectedResult>();
87 assert_equal_types<result21, ExpectedResult>();
88 assert_equal_types<comparable_result12, ExpectedComparableResult>();
89 assert_equal_types<comparable_result21, ExpectedComparableResult>();
90 }
91
92 //=========================================================================
93
94 template
95 <
96 typename CoordinateType1,
97 typename CoordinateType2,
98 std::size_t Dimension,
99 typename CoordinateSystem,
100 typename ExpectedResult,
101 typename ExpectedComparableResult = ExpectedResult
102 >
103 struct test_distance_result_segment
104 {
test_distance_result_segmenttest_distance_result_segment105 test_distance_result_segment()
106 {
107 typedef typename bg::model::point
108 <
109 CoordinateType1, Dimension, CoordinateSystem
110 > point1;
111
112 typedef typename bg::model::point
113 <
114 CoordinateType2, Dimension, CoordinateSystem
115 > point2;
116
117 typedef typename bg::model::segment<point1> segment1;
118 typedef typename bg::model::segment<point2> segment2;
119
120 test_distance_result
121 <
122 point1, point2, ExpectedResult, ExpectedComparableResult
123 >();
124
125 test_distance_result
126 <
127 point1, segment2, ExpectedResult, ExpectedComparableResult
128 >();
129
130 test_distance_result
131 <
132 point2, segment1, ExpectedResult, ExpectedComparableResult
133 >();
134 }
135 };
136
137 //=========================================================================
138
139 template
140 <
141 typename CoordinateType1,
142 typename CoordinateType2,
143 std::size_t Dimension,
144 typename ExpectedResult,
145 typename ExpectedComparableResult = ExpectedResult
146 >
147 struct test_distance_result_box
148 {
test_distance_result_boxtest_distance_result_box149 test_distance_result_box()
150 {
151 typedef typename bg::model::point
152 <
153 CoordinateType1, Dimension, bg::cs::cartesian
154 > point1;
155
156 typedef typename bg::model::point
157 <
158 CoordinateType2, Dimension, bg::cs::cartesian
159 > point2;
160
161 typedef typename bg::model::box<point1> box1;
162 typedef typename bg::model::box<point2> box2;
163
164 test_distance_result
165 <
166 point1, box2, ExpectedResult, ExpectedComparableResult
167 >();
168
169 test_distance_result
170 <
171 point2, box1, ExpectedResult, ExpectedComparableResult
172 >();
173
174 test_distance_result
175 <
176 box1, box2, ExpectedResult, ExpectedComparableResult
177 >();
178 }
179 };
180
181 //=========================================================================
182
183 template <std::size_t D, typename CoordinateSystem>
test_segment_all()184 inline void test_segment_all()
185 {
186 #if defined(HAVE_TTMATH)
187 typedef ttmath_big tt;
188 typedef bg::util::detail::default_integral::type default_integral;
189 #endif
190 typedef typename boost::mpl::if_
191 <
192 typename boost::is_same<CoordinateSystem, bg::cs::cartesian>::type,
193 double,
194 float
195 >::type float_return_type;
196
197 test_distance_result_segment<short, short, D, CoordinateSystem, double>();
198 test_distance_result_segment<int, int, D, CoordinateSystem, double>();
199 test_distance_result_segment<int, long, D, CoordinateSystem, double>();
200 test_distance_result_segment<long, long, D, CoordinateSystem, double>();
201
202 test_distance_result_segment<int, float, D, CoordinateSystem, float_return_type>();
203 test_distance_result_segment<float, float, D, CoordinateSystem, float_return_type>();
204
205 test_distance_result_segment<int, double, D, CoordinateSystem, double>();
206 test_distance_result_segment<double, int, D, CoordinateSystem, double>();
207 test_distance_result_segment<float, double, D, CoordinateSystem, double>();
208 test_distance_result_segment<double, float, D, CoordinateSystem, double>();
209 test_distance_result_segment<double, double, D, CoordinateSystem, double>();
210
211 #if defined(HAVE_TTMATH)
212 test_distance_result_segment<tt, int, D, CoordinateSystem, tt>();
213 test_distance_result_segment<tt, default_integral, D, CoordinateSystem, tt>();
214
215 test_distance_result_segment<tt, float, D, CoordinateSystem, tt>();
216 test_distance_result_segment<tt, double, D, CoordinateSystem, tt>();
217 test_distance_result_segment<tt, tt, D, CoordinateSystem, tt>();
218 #endif
219 }
220
221 //=========================================================================
222
223 template <std::size_t D>
test_box_all()224 inline void test_box_all()
225 {
226 #if defined(HAVE_TTMATH)
227 typedef ttmath_big tt;
228 #endif
229 typedef bg::util::detail::default_integral::type default_integral;
230
231 test_distance_result_box<short, short, D, double, default_integral>();
232 test_distance_result_box<int, int, D, double, default_integral>();
233 test_distance_result_box<int, long, D, double, default_integral>();
234 test_distance_result_box<long, long, D, double, default_integral>();
235
236 test_distance_result_box<int, float, D, double>();
237 test_distance_result_box<float, float, D, double>();
238
239 test_distance_result_box<int, double, D, double>();
240 test_distance_result_box<double, int, D, double>();
241 test_distance_result_box<float, double, D, double>();
242 test_distance_result_box<double, float, D, double>();
243 test_distance_result_box<double, double, D, double>();
244
245 #if defined(HAVE_TTMATH)
246 test_distance_result_box<tt, int, D, tt>();
247 test_distance_result_box<tt, default_integral, D, tt>();
248
249 test_distance_result_box<tt, float, D, tt>();
250 test_distance_result_box<tt, double, D, tt>();
251 test_distance_result_box<tt, tt, D, tt>();
252 #endif
253 }
254
255 //=========================================================================
256
BOOST_AUTO_TEST_CASE(test_point_point_or_point_segment)257 BOOST_AUTO_TEST_CASE( test_point_point_or_point_segment )
258 {
259 test_segment_all<2, bg::cs::cartesian>();
260 test_segment_all<3, bg::cs::cartesian>();
261 test_segment_all<4, bg::cs::cartesian>();
262 test_segment_all<2, bg::cs::spherical_equatorial<bg::degree> >();
263 }
264
BOOST_AUTO_TEST_CASE(test_point_box_or_box)265 BOOST_AUTO_TEST_CASE( test_point_box_or_box )
266 {
267 test_box_all<2>();
268 test_box_all<3>();
269 test_box_all<4>();
270 }
271