1 //
2 // Copyright 2018 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 namespace gil = boost::gil;
15
test_default_constructor()16 void test_default_constructor()
17 {
18 gil::point<int> p;
19 BOOST_TEST(p.x == 0);
20 BOOST_TEST(p.y == 0);
21 }
22
test_user_constructor()23 void test_user_constructor()
24 {
25 gil::point<int> p{1, 2};
26 BOOST_TEST(p.x == 1);
27 BOOST_TEST(p.y == 2);
28 }
29
test_copy_constructor()30 void test_copy_constructor()
31 {
32 gil::point<int> p1{1, 2};
33 gil::point<int> p2{p1};
34 BOOST_TEST(p1.x == p2.x);
35 BOOST_TEST(p1.y == p2.y);
36 }
37
test_copy_assignment_operator()38 void test_copy_assignment_operator()
39 {
40 gil::point<int> p1{1, 2};
41 gil::point<int> p2;
42 p2 = p1;
43 BOOST_TEST(p1.x == p2.x);
44 BOOST_TEST(p1.y == p2.y);
45 }
46
test_index_operator()47 void test_index_operator()
48 {
49 gil::point<int> p{1, 2};
50 BOOST_TEST(p[0] == 1);
51 BOOST_TEST(p[1] == 2);
52 }
53
test_op_addition()54 void test_op_addition()
55 {
56 gil::point<int> p1{1, 1};
57 gil::point<int> const p2{2, 4};
58 p1 += p2;
59 BOOST_TEST(p1.x == 3);
60 BOOST_TEST(p1.y == 5);
61 }
62
test_op_subtraction()63 void test_op_subtraction()
64 {
65 gil::point<int> p1{2, 4};
66 gil::point<int> const p2{1, 1};
67 p1 = p1 - p2;
68 BOOST_TEST(p1.x == 1);
69 BOOST_TEST(p1.y == 3);
70 p1 -= p2;
71 BOOST_TEST(p1.x == 0);
72 BOOST_TEST(p1.y == 2);
73 }
74
test_op_unary_minus()75 void test_op_unary_minus()
76 {
77 gil::point<int> p1{2, 4};
78 auto p2 = -p1;
79 BOOST_TEST(p2.x == -2);
80 BOOST_TEST(p2.y == -4);
81 p2 = -p2;
82 BOOST_TEST(p2.x == p1.x);
83 BOOST_TEST(p2.y == p1.y);
84 }
85
test_op_division()86 void test_op_division()
87 {
88 {
89 gil::point<int> p1{2, 4};
90 p1 /= 2;
91 static_assert(std::is_same<decltype((p1 / short{}).x), int>::value, "!int");
92 static_assert(std::is_same<decltype((p1 / int{}).x), int>::value, "!int");
93 static_assert(std::is_same<decltype((p1 / float{}).x), float>::value, "!float");
94 static_assert(std::is_same<decltype((p1 / double{}).x), double>::value, "!double");
95 BOOST_TEST(p1.x == 1);
96 BOOST_TEST(p1.y == 2);
97 }
98 // point / d
99 {
100 gil::point<int> p1{2, 4};
101 auto p2 = p1 / float{2};
102 static_assert(std::is_same<decltype((p2 / int{}).x), float>::value, "!float");
103 static_assert(std::is_same<decltype((p2 / float{}).x), float>::value, "!float");
104 static_assert(std::is_same<decltype((p2 / double{}).x), double>::value, "!double");
105 BOOST_TEST(p2.x >= 1.0); // means == but >= avoids compiler warning
106 BOOST_TEST(p2.y >= 2.0);
107 }
108 }
109
test_op_multiplication()110 void test_op_multiplication()
111 {
112 gil::point<int> p1{2, 4};
113 p1 *= 2;
114 BOOST_TEST(p1.x == 4);
115 BOOST_TEST(p1.y == 8);
116
117 // point * m
118 {
119 auto p2 = p1 * int{2};
120 static_assert(std::is_same<decltype(p2.x), int>::value, "!int");
121 static_assert(std::is_same<decltype(p2.y), int>::value, "!int");
122 BOOST_TEST(p2.x == 8);
123 BOOST_TEST(p2.y == 16);
124 }
125 // m * point
126 {
127 auto p2 = double{2} *p1;
128 static_assert(std::is_same<decltype(p2.x), double>::value, "!double");
129 static_assert(std::is_same<decltype(p2.y), double>::value, "!double");
130 BOOST_TEST(p2.x >= 8); // means == but >= avoids compiler warning
131 BOOST_TEST(p2.y >= 16);
132 }
133 }
134
test_bitwise_left_shift()135 void test_bitwise_left_shift()
136 {
137 gil::point<unsigned int> p{2, 4};
138 p = p << 1;
139 BOOST_TEST(p.x == 4);
140 BOOST_TEST(p.y == 8);
141 }
142
test_bitwise_right_shift()143 void test_bitwise_right_shift()
144 {
145 gil::point<unsigned int> p{2, 4};
146 p = p >> 1;
147 BOOST_TEST(p.x == 2 / 2);
148 BOOST_TEST(p.y == 4 / 2);
149 }
150
test_cmp_equal()151 void test_cmp_equal()
152 {
153 gil::point<int> p1{2, 4};
154 gil::point<int> p2{2, 4};
155 BOOST_TEST(p1 == p2);
156 }
157
test_cmp_not_equal()158 void test_cmp_not_equal()
159 {
160 gil::point<int> p1{1, 1};
161 gil::point<int> p2{2, 4};
162 BOOST_TEST(p1 != p2);
163 }
164
test_axis_value()165 void test_axis_value()
166 {
167 gil::point<int> p1{1, 2};
168 gil::point<int> const p2{1, 2};
169 BOOST_TEST(gil::axis_value<0>(p1) == p1.x);
170 BOOST_TEST(gil::axis_value<1>(p1) == p1.y);
171 BOOST_TEST(gil::axis_value<0>(p2) == p2.x);
172 BOOST_TEST(gil::axis_value<1>(p2) == p2.y);
173 }
174
main()175 int main()
176 {
177 test_default_constructor();
178 test_user_constructor();
179 test_copy_constructor();
180 test_copy_assignment_operator();
181
182 test_index_operator();
183
184 test_op_addition();
185 test_op_subtraction();
186 test_op_division();
187 test_op_multiplication();
188
189 test_bitwise_left_shift();
190 test_bitwise_right_shift();
191
192 test_cmp_equal();
193 test_cmp_not_equal();
194
195 test_axis_value();
196
197 return boost::report_errors();
198 }
199