2 // Copyright 2018 Mateusz Loskot <mateusz at loskot dot net>
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
8 #include <boost/gil/point.hpp>
10 #include <boost/core/lightweight_test.hpp>
12 #include <type_traits>
14 namespace gil = boost::gil;
16 void test_default_constructor()
23 void test_user_constructor()
25 gil::point<int> p{1, 2};
30 void test_copy_constructor()
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);
38 void test_copy_assignment_operator()
40 gil::point<int> p1{1, 2};
43 BOOST_TEST(p1.x == p2.x);
44 BOOST_TEST(p1.y == p2.y);
47 void test_index_operator()
49 gil::point<int> p{1, 2};
50 BOOST_TEST(p[0] == 1);
51 BOOST_TEST(p[1] == 2);
54 void test_op_addition()
56 gil::point<int> p1{1, 1};
57 gil::point<int> const p2{2, 4};
59 BOOST_TEST(p1.x == 3);
60 BOOST_TEST(p1.y == 5);
63 void test_op_subtraction()
65 gil::point<int> p1{2, 4};
66 gil::point<int> const p2{1, 1};
68 BOOST_TEST(p1.x == 1);
69 BOOST_TEST(p1.y == 3);
71 BOOST_TEST(p1.x == 0);
72 BOOST_TEST(p1.y == 2);
75 void test_op_unary_minus()
77 gil::point<int> p1{2, 4};
79 BOOST_TEST(p2.x == -2);
80 BOOST_TEST(p2.y == -4);
82 BOOST_TEST(p2.x == p1.x);
83 BOOST_TEST(p2.y == p1.y);
86 void test_op_division()
89 gil::point<int> p1{2, 4};
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);
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);
110 void test_op_multiplication()
112 gil::point<int> p1{2, 4};
114 BOOST_TEST(p1.x == 4);
115 BOOST_TEST(p1.y == 8);
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);
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);
135 void test_bitwise_left_shift()
137 gil::point<unsigned int> p{2, 4};
139 BOOST_TEST(p.x == 4);
140 BOOST_TEST(p.y == 8);
143 void test_bitwise_right_shift()
145 gil::point<unsigned int> p{2, 4};
147 BOOST_TEST(p.x == 2 / 2);
148 BOOST_TEST(p.y == 4 / 2);
151 void test_cmp_equal()
153 gil::point<int> p1{2, 4};
154 gil::point<int> p2{2, 4};
155 BOOST_TEST(p1 == p2);
158 void test_cmp_not_equal()
160 gil::point<int> p1{1, 1};
161 gil::point<int> p2{2, 4};
162 BOOST_TEST(p1 != p2);
165 void test_axis_value()
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);
177 test_default_constructor();
178 test_user_constructor();
179 test_copy_constructor();
180 test_copy_assignment_operator();
182 test_index_operator();
185 test_op_subtraction();
187 test_op_multiplication();
189 test_bitwise_left_shift();
190 test_bitwise_right_shift();
193 test_cmp_not_equal();
197 return boost::report_errors();