contains_test.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Boost.Function library
  2. // Copyright Douglas Gregor 2004. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/function.hpp>
  7. #include <boost/core/lightweight_test.hpp>
  8. #include <boost/ref.hpp>
  9. #define BOOST_CHECK BOOST_TEST
  10. static int forty_two() { return 42; }
  11. struct Seventeen
  12. {
  13. int operator()() const { return 17; }
  14. };
  15. struct ReturnInt
  16. {
  17. explicit ReturnInt(int value) : value(value) {}
  18. int operator()() const { return value; }
  19. int value;
  20. };
  21. bool operator==(const ReturnInt& x, const ReturnInt& y)
  22. { return x.value == y.value; }
  23. bool operator!=(const ReturnInt& x, const ReturnInt& y)
  24. { return x.value != y.value; }
  25. namespace contain_test {
  26. struct ReturnIntFE
  27. {
  28. explicit ReturnIntFE(int value) : value(value) {}
  29. int operator()() const { return value; }
  30. int value;
  31. };
  32. }
  33. #ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  34. namespace contain_test {
  35. # ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  36. bool function_equal(const ReturnIntFE& x, const ReturnIntFE& y)
  37. { return x.value == y.value; }
  38. # else
  39. bool function_equal_impl(const ReturnIntFE& x, const ReturnIntFE& y, int)
  40. { return x.value == y.value; }
  41. # endif // #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  42. }
  43. #else // BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  44. namespace boost {
  45. # ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  46. bool
  47. function_equal(const contain_test::ReturnIntFE& x,
  48. const contain_test::ReturnIntFE& y)
  49. { return x.value == y.value; }
  50. # else
  51. bool
  52. function_equal_impl(const contain_test::ReturnIntFE& x,
  53. const contain_test::ReturnIntFE& y, int)
  54. { return x.value == y.value; }
  55. # endif
  56. }
  57. #endif
  58. static void target_test()
  59. {
  60. boost::function0<int> f;
  61. f = &forty_two;
  62. BOOST_CHECK(*f.target<int (*)()>() == &forty_two);
  63. BOOST_CHECK(!f.target<Seventeen>());
  64. f = Seventeen();
  65. BOOST_CHECK(!f.target<int (*)()>());
  66. BOOST_CHECK(f.target<Seventeen>());
  67. Seventeen this_seventeen;
  68. f = boost::ref(this_seventeen);
  69. BOOST_CHECK(!f.target<int (*)()>());
  70. BOOST_CHECK(f.target<Seventeen>());
  71. BOOST_CHECK(f.target<Seventeen>() == &this_seventeen);
  72. const Seventeen const_seventeen = this_seventeen;
  73. f = boost::ref(const_seventeen);
  74. BOOST_CHECK(!f.target<int (*)()>());
  75. BOOST_CHECK(f.target<const Seventeen>());
  76. BOOST_CHECK(f.target<const Seventeen>() == &const_seventeen);
  77. BOOST_CHECK(f.target<const volatile Seventeen>());
  78. BOOST_CHECK(!f.target<Seventeen>());
  79. BOOST_CHECK(!f.target<volatile Seventeen>());
  80. }
  81. static void equal_test()
  82. {
  83. boost::function0<int> f;
  84. f = &forty_two;
  85. BOOST_CHECK(f == &forty_two);
  86. BOOST_CHECK(f != ReturnInt(17));
  87. #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  88. BOOST_CHECK(&forty_two == f);
  89. BOOST_CHECK(ReturnInt(17) != f);
  90. #endif
  91. BOOST_CHECK(f.contains(&forty_two));
  92. f = ReturnInt(17);
  93. BOOST_CHECK(f != &forty_two);
  94. BOOST_CHECK(f == ReturnInt(17));
  95. BOOST_CHECK(f != ReturnInt(16));
  96. #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  97. BOOST_CHECK(&forty_two != f);
  98. BOOST_CHECK(ReturnInt(17) == f);
  99. BOOST_CHECK(ReturnInt(16) != f);
  100. #endif
  101. BOOST_CHECK(f.contains(ReturnInt(17)));
  102. f = contain_test::ReturnIntFE(17);
  103. BOOST_CHECK(f != &forty_two);
  104. BOOST_CHECK(f == contain_test::ReturnIntFE(17));
  105. BOOST_CHECK(f != contain_test::ReturnIntFE(16));
  106. #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  107. BOOST_CHECK(&forty_two != f);
  108. BOOST_CHECK(contain_test::ReturnIntFE(17) == f);
  109. BOOST_CHECK(contain_test::ReturnIntFE(16) != f);
  110. #endif
  111. BOOST_CHECK(f.contains(contain_test::ReturnIntFE(17)));
  112. #if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
  113. boost::function<int(void)> g;
  114. g = &forty_two;
  115. BOOST_CHECK(g == &forty_two);
  116. BOOST_CHECK(g != ReturnInt(17));
  117. # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  118. BOOST_CHECK(&forty_two == g);
  119. BOOST_CHECK(ReturnInt(17) != g);
  120. # endif
  121. g = ReturnInt(17);
  122. BOOST_CHECK(g != &forty_two);
  123. BOOST_CHECK(g == ReturnInt(17));
  124. BOOST_CHECK(g != ReturnInt(16));
  125. # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  126. BOOST_CHECK(&forty_two != g);
  127. BOOST_CHECK(ReturnInt(17) == g);
  128. BOOST_CHECK(ReturnInt(16) != g);
  129. # endif
  130. #endif
  131. }
  132. static void ref_equal_test()
  133. {
  134. {
  135. ReturnInt ri(17);
  136. boost::function0<int> f = boost::ref(ri);
  137. // References and values are equal
  138. BOOST_CHECK(f == boost::ref(ri));
  139. BOOST_CHECK(f == ri);
  140. BOOST_CHECK(boost::ref(ri) == f);
  141. BOOST_CHECK(!(f != boost::ref(ri)));
  142. BOOST_CHECK(!(f != ri));
  143. BOOST_CHECK(!(boost::ref(ri) != f));
  144. #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  145. BOOST_CHECK(ri == f);
  146. BOOST_CHECK(!(ri != f));
  147. #endif
  148. // Values equal, references inequal
  149. ReturnInt ri2(17);
  150. BOOST_CHECK(f == ri2);
  151. BOOST_CHECK(f != boost::ref(ri2));
  152. BOOST_CHECK(boost::ref(ri2) != f);
  153. BOOST_CHECK(!(f != ri2));
  154. BOOST_CHECK(!(f == boost::ref(ri2)));
  155. BOOST_CHECK(!(boost::ref(ri2) == f));
  156. #if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  157. BOOST_CHECK(ri2 == f);
  158. BOOST_CHECK(!(ri2 != f));
  159. #endif
  160. }
  161. #if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
  162. {
  163. ReturnInt ri(17);
  164. boost::function<int(void)> f = boost::ref(ri);
  165. // References and values are equal
  166. BOOST_CHECK(f == boost::ref(ri));
  167. BOOST_CHECK(f == ri);
  168. BOOST_CHECK(boost::ref(ri) == f);
  169. BOOST_CHECK(!(f != boost::ref(ri)));
  170. BOOST_CHECK(!(f != ri));
  171. BOOST_CHECK(!(boost::ref(ri) != f));
  172. # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  173. BOOST_CHECK(ri == f);
  174. BOOST_CHECK(!(ri != f));
  175. # endif
  176. // Values equal, references inequal
  177. ReturnInt ri2(17);
  178. BOOST_CHECK(f == ri2);
  179. BOOST_CHECK(f != boost::ref(ri2));
  180. BOOST_CHECK(boost::ref(ri2) != f);
  181. BOOST_CHECK(!(f != ri2));
  182. BOOST_CHECK(!(f == boost::ref(ri2)));
  183. BOOST_CHECK(!(boost::ref(ri2) == f));
  184. # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  185. BOOST_CHECK(ri2 == f);
  186. BOOST_CHECK(!(ri2 != f));
  187. # endif
  188. }
  189. #endif
  190. }
  191. int main()
  192. {
  193. target_test();
  194. equal_test();
  195. ref_equal_test();
  196. return boost::report_errors();
  197. }