test_less.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 Steven Watanabe
  4. //
  5. // Distributed under the Boost Software License Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // $Id$
  10. #include <boost/type_erasure/any.hpp>
  11. #include <boost/type_erasure/builtin.hpp>
  12. #include <boost/type_erasure/operators.hpp>
  13. #include <boost/type_erasure/any_cast.hpp>
  14. #include <boost/type_erasure/tuple.hpp>
  15. #include <boost/mpl/vector.hpp>
  16. #define BOOST_TEST_MAIN
  17. #include <boost/test/unit_test.hpp>
  18. using namespace boost::type_erasure;
  19. BOOST_AUTO_TEST_CASE(test_basic)
  20. {
  21. typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<> > test_concept;
  22. any<test_concept> x(1);
  23. any<test_concept> y(2);
  24. BOOST_CHECK((x < y));
  25. BOOST_CHECK(!(y < x));
  26. BOOST_CHECK(!(x < x));
  27. BOOST_CHECK(!(x > y));
  28. BOOST_CHECK((y > x));
  29. BOOST_CHECK(!(x > x));
  30. BOOST_CHECK((x <= y));
  31. BOOST_CHECK(!(y <= x));
  32. BOOST_CHECK((x <= x));
  33. BOOST_CHECK(!(x >= y));
  34. BOOST_CHECK((y >= x));
  35. BOOST_CHECK((x >= x));
  36. }
  37. BOOST_AUTO_TEST_CASE(test_mixed_less)
  38. {
  39. typedef boost::mpl::vector<copy_constructible<_a>, copy_constructible<_b>, less_than_comparable<_a, _b> > test_concept;
  40. tuple<test_concept, _a, _b> t(1, 2.0);
  41. any<test_concept, _a> x(get<0>(t));
  42. any<test_concept, _b> y(get<1>(t));
  43. BOOST_CHECK((x < y));
  44. BOOST_CHECK((y > x));
  45. BOOST_CHECK(!(y <= x));
  46. BOOST_CHECK(!(x >= y));
  47. }
  48. BOOST_AUTO_TEST_CASE(test_mixed_equal)
  49. {
  50. typedef boost::mpl::vector<copy_constructible<_a>, copy_constructible<_b>, less_than_comparable<_a, _b> > test_concept;
  51. tuple<test_concept, _a, _b> t(1, 1);
  52. any<test_concept, _a> x(get<0>(t));
  53. any<test_concept, _b> y(get<1>(t));
  54. BOOST_CHECK(!(x < y));
  55. BOOST_CHECK(!(y > x));
  56. BOOST_CHECK((y <= x));
  57. BOOST_CHECK((x >= y));
  58. }
  59. BOOST_AUTO_TEST_CASE(test_mixed_greater)
  60. {
  61. typedef boost::mpl::vector<copy_constructible<_a>, copy_constructible<_b>, less_than_comparable<_a, _b> > test_concept;
  62. tuple<test_concept, _a, _b> t(2.0, 1);
  63. any<test_concept, _a> x(get<0>(t));
  64. any<test_concept, _b> y(get<1>(t));
  65. BOOST_CHECK(!(x < y));
  66. BOOST_CHECK(!(y > x));
  67. BOOST_CHECK((y <= x));
  68. BOOST_CHECK((x >= y));
  69. }
  70. BOOST_AUTO_TEST_CASE(test_fixed_lhs_less)
  71. {
  72. typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<int, _self> > test_concept;
  73. int x(1);
  74. any<test_concept> y(2.0);
  75. BOOST_CHECK((x < y));
  76. BOOST_CHECK((y > x));
  77. BOOST_CHECK(!(y <= x));
  78. BOOST_CHECK(!(x >= y));
  79. }
  80. BOOST_AUTO_TEST_CASE(test_fixed_lhs_equal)
  81. {
  82. typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<int, _self> > test_concept;
  83. int x(1);
  84. any<test_concept> y(1);
  85. BOOST_CHECK(!(x < y));
  86. BOOST_CHECK(!(y > x));
  87. BOOST_CHECK((y <= x));
  88. BOOST_CHECK((x >= y));
  89. }
  90. BOOST_AUTO_TEST_CASE(test_fixed_lhs_greater)
  91. {
  92. typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<int, _self> > test_concept;
  93. int x(1);
  94. any<test_concept> y(0.5);
  95. BOOST_CHECK(!(x < y));
  96. BOOST_CHECK(!(y > x));
  97. BOOST_CHECK((y <= x));
  98. BOOST_CHECK((x >= y));
  99. }
  100. BOOST_AUTO_TEST_CASE(test_fixed_rhs_less)
  101. {
  102. typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<_self, int> > test_concept;
  103. any<test_concept> x(1.0);
  104. int y(2);
  105. BOOST_CHECK((x < y));
  106. BOOST_CHECK((y > x));
  107. BOOST_CHECK(!(y <= x));
  108. BOOST_CHECK(!(x >= y));
  109. }
  110. BOOST_AUTO_TEST_CASE(test_fixed_rhs_equal)
  111. {
  112. typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<_self, int> > test_concept;
  113. any<test_concept> x(1);
  114. int y(1);
  115. BOOST_CHECK(!(x < y));
  116. BOOST_CHECK(!(y > x));
  117. BOOST_CHECK((y <= x));
  118. BOOST_CHECK((x >= y));
  119. }
  120. BOOST_AUTO_TEST_CASE(test_fixed_rhs_greater)
  121. {
  122. typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<_self, int> > test_concept;
  123. any<test_concept> x(2.0);
  124. int y(1);
  125. BOOST_CHECK(!(x < y));
  126. BOOST_CHECK(!(y > x));
  127. BOOST_CHECK((y <= x));
  128. BOOST_CHECK((x >= y));
  129. }
  130. BOOST_AUTO_TEST_CASE(test_relaxed)
  131. {
  132. typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<>, relaxed> test_concept;
  133. any<test_concept> x(1);
  134. any<test_concept> y(2);
  135. any<test_concept> z(std::string("test"));
  136. BOOST_CHECK((x < y));
  137. BOOST_CHECK(!(y < x));
  138. BOOST_CHECK(!(x < x));
  139. BOOST_CHECK(!(x > y));
  140. BOOST_CHECK((y > x));
  141. BOOST_CHECK(!(x > x));
  142. BOOST_CHECK((x <= y));
  143. BOOST_CHECK(!(y <= x));
  144. BOOST_CHECK((x <= x));
  145. BOOST_CHECK(!(x >= y));
  146. BOOST_CHECK((y >= x));
  147. BOOST_CHECK((x >= x));
  148. bool expected = x < z;
  149. BOOST_CHECK_EQUAL((x < z), expected);
  150. BOOST_CHECK_EQUAL(!(z < x), expected);
  151. BOOST_CHECK_EQUAL(!(x > z), expected);
  152. BOOST_CHECK_EQUAL((z > x), expected);
  153. BOOST_CHECK_EQUAL((x <= z), expected);
  154. BOOST_CHECK_EQUAL(!(z <= x), expected);
  155. BOOST_CHECK_EQUAL(!(x >= z), expected);
  156. BOOST_CHECK_EQUAL((z >= x), expected);
  157. BOOST_CHECK_EQUAL((y < z), expected);
  158. BOOST_CHECK_EQUAL(!(z < y), expected);
  159. BOOST_CHECK_EQUAL(!(y > z), expected);
  160. BOOST_CHECK_EQUAL((z > y), expected);
  161. BOOST_CHECK_EQUAL((y <= z), expected);
  162. BOOST_CHECK_EQUAL(!(z <= y), expected);
  163. BOOST_CHECK_EQUAL(!(y >= z), expected);
  164. BOOST_CHECK_EQUAL((z >= y), expected);
  165. }
  166. BOOST_AUTO_TEST_CASE(test_overload)
  167. {
  168. typedef boost::mpl::vector<
  169. copy_constructible<_a>,
  170. copy_constructible<_b>,
  171. less_than_comparable<_a>,
  172. less_than_comparable<_a, int>,
  173. less_than_comparable<int, _a>,
  174. less_than_comparable<_b>,
  175. less_than_comparable<_b, int>,
  176. less_than_comparable<int, _b>,
  177. less_than_comparable<_a, _b>
  178. > test_concept;
  179. tuple<test_concept, _a, _b> t(1, 2);
  180. any<test_concept, _a> x(get<0>(t));
  181. any<test_concept, _b> y(get<1>(t));
  182. BOOST_CHECK(!(x < x));
  183. BOOST_CHECK(x <= x);
  184. BOOST_CHECK(!(x > x));
  185. BOOST_CHECK(x >= x);
  186. BOOST_CHECK(!(x < 1));
  187. BOOST_CHECK(x <= 1);
  188. BOOST_CHECK(!(x > 1));
  189. BOOST_CHECK(x >= 1);
  190. BOOST_CHECK(!(1 < x));
  191. BOOST_CHECK(1 <= x);
  192. BOOST_CHECK(!(1 > x));
  193. BOOST_CHECK(1 >= x);
  194. BOOST_CHECK(!(y < y));
  195. BOOST_CHECK(y <= y);
  196. BOOST_CHECK(!(y > y));
  197. BOOST_CHECK(y >= y);
  198. BOOST_CHECK(!(y < 2));
  199. BOOST_CHECK(y <= 2);
  200. BOOST_CHECK(!(y > 2));
  201. BOOST_CHECK(y >= 2);
  202. BOOST_CHECK(!(2 < y));
  203. BOOST_CHECK(2 <= y);
  204. BOOST_CHECK(!(2 > y));
  205. BOOST_CHECK(2 >= y);
  206. BOOST_CHECK(x < y);
  207. BOOST_CHECK(y > x);
  208. BOOST_CHECK(!(y <= x));
  209. BOOST_CHECK(!(x >= y));
  210. }