1 //
2 // Copyright 2018-2020 Mateusz Loskot <mateusz at loskot dot net>
3 //
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 #include <boost/gil/point.hpp>
9
10 #include <boost/core/lightweight_test.hpp>
11
12 #include <type_traits>
13
14 #include "test_utility_output_stream.hpp"
15
16 namespace gil = boost::gil;
17
test_default_constructor()18 void test_default_constructor()
19 {
20 gil::point<int> p;
21 BOOST_TEST_EQ(p.x, 0);
22 BOOST_TEST_EQ(p.y, 0);
23 }
24
test_user_constructor()25 void test_user_constructor()
26 {
27 gil::point<int> p{1, 2};
28 BOOST_TEST_EQ(p.x, 1);
29 BOOST_TEST_EQ(p.y, 2);
30 }
31
test_copy_constructor()32 void test_copy_constructor()
33 {
34 gil::point<int> p1{1, 2};
35 gil::point<int> p2{p1};
36 BOOST_TEST_EQ(p1.x, p2.x);
37 BOOST_TEST_EQ(p1.y, p2.y);
38 }
39
test_copy_assignment_operator()40 void test_copy_assignment_operator()
41 {
42 gil::point<int> p1{1, 2};
43 gil::point<int> p2;
44 p2 = p1;
45 BOOST_TEST_EQ(p1.x, p2.x);
46 BOOST_TEST_EQ(p1.y, p2.y);
47 }
48
test_index_operator()49 void test_index_operator()
50 {
51 gil::point<int> p{1, 2};
52 BOOST_TEST_EQ(p[0], 1);
53 BOOST_TEST_EQ(p[1], 2);
54 }
55
test_addition_operator()56 void test_addition_operator()
57 {
58 gil::point<int> p1{1, 1};
59 gil::point<int> const p2{2, 4};
60 auto p3 = p1 + p2;
61 BOOST_TEST_EQ(p3.x, 3);
62 BOOST_TEST_EQ(p3.y, 5);
63 }
64
test_addition_assignment_operator()65 void test_addition_assignment_operator()
66 {
67 gil::point<int> p1{1, 1};
68 gil::point<int> const p2{2, 4};
69 p1 += p2;
70 BOOST_TEST_EQ(p1.x, 3);
71 BOOST_TEST_EQ(p1.y, 5);
72 }
73
test_subtraction_assignment_operator()74 void test_subtraction_assignment_operator()
75 {
76 gil::point<int> p1{2, 4};
77 gil::point<int> const p2{1, 1};
78 p1 -= p2;
79 BOOST_TEST_EQ(p1.x, 1);
80 BOOST_TEST_EQ(p1.y, 3);
81 }
82
test_subtraction_operator()83 void test_subtraction_operator()
84 {
85 gil::point<int> p1{2, 4};
86 gil::point<int> const p2{1, 1};
87 p1 = p1 - p2;
88 BOOST_TEST_EQ(p1.x, 1);
89 BOOST_TEST_EQ(p1.y, 3);
90 }
91
test_unary_minus_operator()92 void test_unary_minus_operator()
93 {
94 gil::point<int> p1{2, 4};
95 auto p2 = -p1;
96 BOOST_TEST_EQ(p2.x, -2);
97 BOOST_TEST_EQ(p2.y, -4);
98 p2 = -p2;
99 BOOST_TEST_EQ(p2.x, p1.x);
100 BOOST_TEST_EQ(p2.y, p1.y);
101 }
102
test_division_assignment_operator()103 void test_division_assignment_operator()
104 {
105 {
106 gil::point<int> p1{2, 4};
107 p1 /= 2;
108 static_assert(std::is_same<decltype((p1 / short{}).x), int>::value, "!int");
109 static_assert(std::is_same<decltype((p1 / int{}).x), int>::value, "!int");
110 static_assert(std::is_same<decltype((p1 / float{}).x), float>::value, "!float");
111 static_assert(std::is_same<decltype((p1 / double{}).x), double>::value, "!double");
112 BOOST_TEST_EQ(p1.x, 1);
113 BOOST_TEST_EQ(p1.y, 2);
114 }
115 // point / d
116 {
117 gil::point<int> p1{2, 4};
118 auto p2 = p1 / float{2};
119 static_assert(std::is_same<decltype((p2 / int{}).x), float>::value, "!float");
120 static_assert(std::is_same<decltype((p2 / float{}).x), float>::value, "!float");
121 static_assert(std::is_same<decltype((p2 / double{}).x), double>::value, "!double");
122 BOOST_TEST_GE(p2.x, 1.0); // means, but >= avoids compiler warning
123 BOOST_TEST_GE(p2.y, 2.0);
124 }
125 }
126
test_multiplication_operator()127 void test_multiplication_operator()
128 {
129 gil::point<int> p1{2, 4};
130 p1 *= 2;
131 BOOST_TEST_EQ(p1.x, 4);
132 BOOST_TEST_EQ(p1.y, 8);
133
134 // point * m
135 {
136 auto p2 = p1 * int{2};
137 static_assert(std::is_same<decltype(p2.x), int>::value, "!int");
138 static_assert(std::is_same<decltype(p2.y), int>::value, "!int");
139 BOOST_TEST_EQ(p2.x, 8);
140 BOOST_TEST_EQ(p2.y, 16);
141 }
142 // m * point
143 {
144 auto p2 = double{2} *p1;
145 static_assert(std::is_same<decltype(p2.x), double>::value, "!double");
146 static_assert(std::is_same<decltype(p2.y), double>::value, "!double");
147 BOOST_TEST_GE(p2.x, 8); // means, but >= avoids compiler warning
148 BOOST_TEST_GE(p2.y, 16);
149 }
150 }
151
test_multiplication_assignment_operator()152 void test_multiplication_assignment_operator()
153 {
154 gil::point<int> p1{2, 4};
155 // point * m
156 {
157 auto p2 = p1 * int{2};
158 static_assert(std::is_same<decltype(p2.x), int>::value, "!int");
159 static_assert(std::is_same<decltype(p2.y), int>::value, "!int");
160 BOOST_TEST_EQ(p2.x, 4);
161 BOOST_TEST_EQ(p2.y, 8);
162 }
163 // m * point
164 {
165 auto p2 = double{2} * p1;
166 static_assert(std::is_same<decltype(p2.x), double>::value, "!double");
167 static_assert(std::is_same<decltype(p2.y), double>::value, "!double");
168 BOOST_TEST_GE(p2.x, 4); // means, but >= avoids compiler warning
169 BOOST_TEST_GE(p2.y, 8);
170 }
171 }
172
test_bitwise_left_shift_operator()173 void test_bitwise_left_shift_operator()
174 {
175 gil::point<unsigned int> p{2, 4};
176 p = p << 1;
177 BOOST_TEST_EQ(p.x, 4u);
178 BOOST_TEST_EQ(p.y, 8u);
179 }
180
test_bitwise_right_shift_operator()181 void test_bitwise_right_shift_operator()
182 {
183 gil::point<unsigned int> p{2, 4};
184 p = p >> 1;
185 BOOST_TEST_EQ(p.x, 2u / 2);
186 BOOST_TEST_EQ(p.y, 4u / 2);
187 }
188
test_equal_to_operator()189 void test_equal_to_operator()
190 {
191 gil::point<int> p1{2, 4};
192 gil::point<int> p2{2, 4};
193 BOOST_TEST_EQ(p1, p2);
194 }
195
test_not_equal_to_operator()196 void test_not_equal_to_operator()
197 {
198 gil::point<int> p1{1, 1};
199 gil::point<int> p2{2, 4};
200 BOOST_TEST_NE(p1, p2);
201 }
202
test_axis_value()203 void test_axis_value()
204 {
205 gil::point<int> p1{1, 2};
206 gil::point<int> const p2{1, 2};
207 BOOST_TEST_EQ(gil::axis_value<0>(p1), p1.x);
208 BOOST_TEST_EQ(gil::axis_value<1>(p1), p1.y);
209 BOOST_TEST_EQ(gil::axis_value<0>(p2), p2.x);
210 BOOST_TEST_EQ(gil::axis_value<1>(p2), p2.y);
211 }
212
main()213 int main()
214 {
215 test_default_constructor();
216 test_user_constructor();
217 test_copy_constructor();
218 test_copy_assignment_operator();
219 test_index_operator();
220 test_addition_operator();
221 test_addition_assignment_operator();
222 test_subtraction_assignment_operator();
223 test_subtraction_operator();
224 test_unary_minus_operator();
225 test_division_assignment_operator();
226 test_multiplication_operator();
227 test_multiplication_assignment_operator();
228 test_bitwise_left_shift_operator();
229 test_bitwise_right_shift_operator();
230 test_equal_to_operator();
231 test_not_equal_to_operator();
232 test_axis_value();
233
234 return boost::report_errors();
235 }
236