point.cpp 4.5 KB

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