]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/polygon/test/polygon_point_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / polygon / test / polygon_point_test.cpp
1 // Boost.Polygon library polygon_point_test.cpp file
2
3 // Copyright Andrii Sydorchuk 2012.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 // See http://www.boost.org for updates, documentation, and revision history.
9
10 #define BOOST_TEST_MODULE POLYGON_POINT_TEST
11 #include <boost/mpl/list.hpp>
12 #include <boost/test/test_case_template.hpp>
13
14 #include "boost/polygon/point_concept.hpp"
15 #include "boost/polygon/point_data.hpp"
16 #include "boost/polygon/point_traits.hpp"
17 using namespace boost::polygon;
18
19 typedef boost::mpl::list<int> test_types;
20
21 BOOST_AUTO_TEST_CASE_TEMPLATE(point_data_test, T, test_types) {
22 typedef point_data<T> point_type;
23
24 point_type point1(1, 2);
25 point_type point2;
26 point2 = point1;
27 BOOST_CHECK_EQUAL(point1.x(), 1);
28 BOOST_CHECK_EQUAL(point1.y(), 2);
29 BOOST_CHECK_EQUAL(point2.x(), 1);
30 BOOST_CHECK_EQUAL(point2.y(), 2);
31 BOOST_CHECK(point1 == point2);
32 BOOST_CHECK(!(point1 != point2));
33 BOOST_CHECK(!(point1 < point2));
34 BOOST_CHECK(!(point1 > point2));
35 BOOST_CHECK(point1 <= point2);
36 BOOST_CHECK(point1 >= point2);
37
38 point2.x(2);
39 point2.y(1);
40 BOOST_CHECK_EQUAL(point2.x(), 2);
41 BOOST_CHECK_EQUAL(point2.y(), 1);
42 BOOST_CHECK(!(point1 == point2));
43 BOOST_CHECK(point1 != point2);
44 BOOST_CHECK(point1 < point2);
45 BOOST_CHECK(!(point1 > point2));
46 BOOST_CHECK(point1 <= point2);
47 BOOST_CHECK(!(point1 >= point2));
48
49 point2.set(HORIZONTAL, 1);
50 point2.set(VERTICAL, 2);
51 BOOST_CHECK(point1 == point2);
52 }
53
54 BOOST_AUTO_TEST_CASE_TEMPLATE(point_traits_test, T, test_types) {
55 typedef point_data<T> point_type;
56
57 point_type point = point_mutable_traits<point_type>::construct(1, 2);
58 BOOST_CHECK_EQUAL(point_traits<point_type>::get(point, HORIZONTAL), 1);
59 BOOST_CHECK_EQUAL(point_traits<point_type>::get(point, VERTICAL), 2);
60
61 point_mutable_traits<point_type>::set(point, HORIZONTAL, 3);
62 point_mutable_traits<point_type>::set(point, VERTICAL, 4);
63 BOOST_CHECK_EQUAL(point_traits<point_type>::get(point, HORIZONTAL), 3);
64 BOOST_CHECK_EQUAL(point_traits<point_type>::get(point, VERTICAL), 4);
65 }
66
67 template <typename T>
68 struct Point {
69 T x;
70 T y;
71 };
72
73 namespace boost {
74 namespace polygon {
75 template <typename T>
76 struct geometry_concept< Point<T> > {
77 typedef point_concept type;
78 };
79
80 template <typename T>
81 struct point_traits< Point<T> > {
82 typedef T coordinate_type;
83
84 static coordinate_type get(const Point<T>& point, orientation_2d orient) {
85 return (orient == HORIZONTAL) ? point.x : point.y;
86 }
87 };
88
89 template <typename T>
90 struct point_mutable_traits< Point<T> > {
91 typedef T coordinate_type;
92
93 static void set(Point<T>& point, orientation_2d orient, T value) {
94 (orient == HORIZONTAL) ? point.x = value : point.y = value;
95 }
96
97 static Point<T> construct(coordinate_type x, coordinate_type y) {
98 Point<T> point;
99 point.x = x;
100 point.y = y;
101 return point;
102 }
103 };
104 } // polygon
105 } // boost
106
107 BOOST_AUTO_TEST_CASE_TEMPLATE(point_concept_test1, T, test_types) {
108 typedef Point<T> point_type;
109
110 point_type point1 = construct<point_type>(1, 2);
111 BOOST_CHECK_EQUAL(point1.x, 1);
112 BOOST_CHECK_EQUAL(point1.y, 2);
113
114 set(point1, HORIZONTAL, 3);
115 set(point1, VERTICAL, 4);
116 BOOST_CHECK_EQUAL(get(point1, HORIZONTAL), 3);
117 BOOST_CHECK_EQUAL(get(point1, VERTICAL), 4);
118
119 point_type point2;
120 assign(point2, point1);
121 BOOST_CHECK(equivalence(point1, point2));
122
123 x(point2, 1);
124 y(point2, 2);
125 BOOST_CHECK_EQUAL(x(point2), 1);
126 BOOST_CHECK_EQUAL(y(point2), 2);
127 }
128
129 BOOST_AUTO_TEST_CASE_TEMPLATE(point_concept_test2, T, test_types) {
130 typedef Point<T> point_type;
131
132 point_type point1 = construct<point_type>(1, 2);
133 point_type point2 = construct<point_type>(5, 5);
134 BOOST_CHECK_EQUAL(euclidean_distance(point1, point2, HORIZONTAL), 4);
135 BOOST_CHECK_EQUAL(euclidean_distance(point1, point2, VERTICAL), 3);
136 BOOST_CHECK_EQUAL(manhattan_distance(point1, point2), 7);
137 BOOST_CHECK_EQUAL(euclidean_distance(point1, point2), 5.0);
138 }
139
140 BOOST_AUTO_TEST_CASE_TEMPLATE(point_concept_test3, T, test_types) {
141 typedef Point<T> point_type;
142
143 point_type point = construct<point_type>(1, 2);
144 point_type shift = construct<point_type>(4, 3);
145 convolve(point, shift);
146 BOOST_CHECK_EQUAL(x(point), 5);
147 BOOST_CHECK_EQUAL(y(point), 5);
148
149 deconvolve(point, shift);
150 BOOST_CHECK_EQUAL(x(point), 1);
151 BOOST_CHECK_EQUAL(y(point), 2);
152
153 scale_up(point, 5);
154 BOOST_CHECK_EQUAL(x(point), 5);
155 BOOST_CHECK_EQUAL(y(point), 10);
156
157 scale_down(point, 5);
158 BOOST_CHECK_EQUAL(x(point), 1);
159 BOOST_CHECK_EQUAL(y(point), 2);
160
161 move(point, HORIZONTAL, 2);
162 move(point, VERTICAL, 3);
163 BOOST_CHECK_EQUAL(x(point), 3);
164 BOOST_CHECK_EQUAL(y(point), 5);
165 }
166
167 template<typename T>
168 struct Transformer {
169 void scale(T& x, T& y) const {
170 x *= 2;
171 y *= 2;
172 }
173
174 void transform(T& x, T& y) const {
175 T tmp = x;
176 x = y;
177 y = tmp;
178 }
179 };
180
181 BOOST_AUTO_TEST_CASE_TEMPLATE(point_concept_test4, T, test_types) {
182 typedef Point<T> point_type;
183
184 point_type point = construct<point_type>(1, 2);
185 scale(point, Transformer<T>());
186 BOOST_CHECK_EQUAL(x(point), 2);
187 BOOST_CHECK_EQUAL(y(point), 4);
188
189 transform(point, Transformer<T>());
190 BOOST_CHECK_EQUAL(x(point), 4);
191 BOOST_CHECK_EQUAL(y(point), 2);
192 }