test_comparison.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright 2016-2018 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. #include "test_comparison.hpp"
  9. #include <boost/core/lightweight_test.hpp>
  10. #include "any_types.hpp"
  11. #include "base_types.hpp"
  12. #include "function_types.hpp"
  13. #include "test_utilities.hpp"
  14. using namespace test_utilities;
  15. template<typename PolyCollection,typename ValueFactory,typename... Types>
  16. void test_comparison()
  17. {
  18. {
  19. PolyCollection p1,p2;
  20. const PolyCollection& cp1=p1;
  21. const PolyCollection& cp2=p2;
  22. BOOST_TEST(cp1==cp1);
  23. BOOST_TEST(!(cp1!=cp1));
  24. BOOST_TEST(cp1==cp2);
  25. BOOST_TEST(!(cp1!=cp2));
  26. }
  27. {
  28. PolyCollection p1,p2;
  29. const PolyCollection& cp1=p1;
  30. const PolyCollection& cp2=p2;
  31. ValueFactory v;
  32. fill<
  33. constraints<is_not_equality_comparable>,
  34. Types...
  35. >(p1,v,2);
  36. BOOST_TEST(!(cp1==cp2));
  37. BOOST_TEST(cp1!=cp2);
  38. }
  39. {
  40. PolyCollection p1,p2;
  41. const PolyCollection& cp1=p1;
  42. const PolyCollection& cp2=p2;
  43. ValueFactory v;
  44. p1.template register_types<Types...>();
  45. fill<
  46. constraints<is_not_equality_comparable>,
  47. Types...
  48. >(p1,v,2);
  49. BOOST_TEST(!(cp1==cp2));
  50. BOOST_TEST(cp1!=cp2);
  51. }
  52. {
  53. PolyCollection p1,p2;
  54. const PolyCollection& cp1=p1;
  55. const PolyCollection& cp2=p2;
  56. ValueFactory v;
  57. fill<
  58. constraints<is_not_equality_comparable>,
  59. Types...
  60. >(p1,v,1);
  61. fill<
  62. constraints<is_not_equality_comparable>,
  63. Types...
  64. >(p2,v,2);
  65. BOOST_TEST(!(cp1==cp2));
  66. BOOST_TEST(cp1!=cp2);
  67. }
  68. {
  69. using not_equality_comparable=
  70. boost::poly_collection::not_equality_comparable;
  71. PolyCollection p1,p2;
  72. const PolyCollection& cp1=p1;
  73. const PolyCollection& cp2=p2;
  74. ValueFactory v;
  75. fill<
  76. constraints<is_not_equality_comparable>,
  77. Types...
  78. >(p1,v,2);
  79. fill<
  80. constraints<is_not_equality_comparable>,
  81. Types...
  82. >(p2,v,2);
  83. check_throw<not_equality_comparable>(
  84. [&]{(void)(cp1==cp2);},
  85. [&]{(void)(cp1!=cp2);});
  86. }
  87. {
  88. PolyCollection p1,p2;
  89. const PolyCollection& cp1=p1;
  90. const PolyCollection& cp2=p2;
  91. ValueFactory v;
  92. fill<
  93. constraints<is_not_equality_comparable>,
  94. Types...
  95. >(p1,v,2);
  96. fill<
  97. constraints<is_equality_comparable,is_copy_constructible>,
  98. Types...
  99. >(p2,v,2);
  100. p1.insert(p2.begin(),p2.end());
  101. BOOST_TEST(!(cp1==cp2));
  102. BOOST_TEST(cp1!=cp2);
  103. }
  104. {
  105. PolyCollection p1,p2;
  106. const PolyCollection& cp1=p1;
  107. const PolyCollection& cp2=p2;
  108. ValueFactory v;
  109. p1.template register_types<Types...>();
  110. fill<
  111. constraints<is_equality_comparable,is_copy_constructible>,
  112. Types...
  113. >(p2,v,2);
  114. p1.insert(p2.begin(),p2.end());
  115. BOOST_TEST(cp1==cp2);
  116. BOOST_TEST(!(cp1!=cp2));
  117. p1.erase(p1.begin());
  118. BOOST_TEST(!(cp1==cp2));
  119. BOOST_TEST(cp1!=cp2);
  120. }
  121. }
  122. void test_stateless_lambda_comparability_check()
  123. {
  124. /* https://svn.boost.org/trac10/ticket/13012 */
  125. {
  126. boost::function_collection<void()> c1,c2;
  127. c1.insert([]{});
  128. BOOST_TEST(c1!=c2);
  129. }
  130. {
  131. boost::function_collection<int(int)> c1,c2;
  132. c1.insert([](int){return 0;});
  133. BOOST_TEST(c1!=c2);
  134. }
  135. }
  136. void test_comparison()
  137. {
  138. test_comparison<
  139. any_types::collection,auto_increment,
  140. any_types::t1,any_types::t2,any_types::t3,
  141. any_types::t4,any_types::t5>();
  142. test_comparison<
  143. base_types::collection,auto_increment,
  144. base_types::t1,base_types::t2,base_types::t3,
  145. base_types::t4,base_types::t5>();
  146. test_comparison<
  147. function_types::collection,auto_increment,
  148. function_types::t1,function_types::t2,function_types::t3,
  149. function_types::t4,function_types::t5>();
  150. test_stateless_lambda_comparability_check();
  151. }