test_member.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2012 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/member.hpp>
  13. #include <boost/mpl/vector.hpp>
  14. #define BOOST_TEST_MAIN
  15. #include <boost/test/unit_test.hpp>
  16. using namespace boost::type_erasure;
  17. BOOST_TYPE_ERASURE_MEMBER((ns)(ns2)(has_fun), fun, 0)
  18. struct model {
  19. explicit model(int v) : val(v) {}
  20. int f1() { return val; }
  21. int f1(int i) { return val + i; }
  22. int val;
  23. };
  24. BOOST_TYPE_ERASURE_MEMBER((global_has_f1_0), f1, 0)
  25. BOOST_AUTO_TEST_CASE(test_global_has_f1_0) {
  26. typedef ::boost::mpl::vector<
  27. global_has_f1_0<int()>,
  28. copy_constructible<> > concept_type;
  29. model m(10);
  30. any<concept_type> x(m);
  31. BOOST_CHECK_EQUAL(x.f1(), 10);
  32. }
  33. BOOST_AUTO_TEST_CASE(test_global_has_f1_0_ref) {
  34. typedef ::boost::mpl::vector<
  35. global_has_f1_0<int()>,
  36. copy_constructible<> > concept_type;
  37. model m(10);
  38. const any<concept_type, _self&> x(m);
  39. BOOST_CHECK_EQUAL(x.f1(), 10);
  40. }
  41. BOOST_TYPE_ERASURE_MEMBER((ns1)(ns2)(ns_has_f1_0), f1, 0)
  42. BOOST_AUTO_TEST_CASE(test_ns_has_f1_0) {
  43. typedef ::boost::mpl::vector<
  44. ns1::ns2::ns_has_f1_0<int()>,
  45. copy_constructible<> > concept_type;
  46. model m(10);
  47. any<concept_type> x(m);
  48. BOOST_CHECK_EQUAL(x.f1(), 10);
  49. }
  50. struct model_const {
  51. explicit model_const(int v) : val(v) {}
  52. int f1() const { return val; }
  53. int f1(int i) const { return val + i; }
  54. int val;
  55. };
  56. BOOST_AUTO_TEST_CASE(test_global_has_f1_0_const) {
  57. typedef ::boost::mpl::vector<
  58. ns1::ns2::ns_has_f1_0<int(), const _self>,
  59. copy_constructible<> > concept_type;
  60. model_const m(10);
  61. any<concept_type> x(m);
  62. BOOST_CHECK_EQUAL(x.f1(), 10);
  63. }
  64. BOOST_AUTO_TEST_CASE(test_global_has_f1_0_const_ref) {
  65. typedef ::boost::mpl::vector<
  66. ns1::ns2::ns_has_f1_0<int(), const _self>,
  67. copy_constructible<> > concept_type;
  68. model_const m(10);
  69. const any<concept_type, _self&> x(m);
  70. BOOST_CHECK_EQUAL(x.f1(), 10);
  71. }
  72. BOOST_AUTO_TEST_CASE(test_global_has_f1_0_const_cref) {
  73. typedef ::boost::mpl::vector<
  74. ns1::ns2::ns_has_f1_0<int(), const _self>,
  75. copy_constructible<> > concept_type;
  76. model_const m(10);
  77. const any<concept_type, const _self&> x(m);
  78. BOOST_CHECK_EQUAL(x.f1(), 10);
  79. }
  80. BOOST_AUTO_TEST_CASE(test_global_has_f1_0_void) {
  81. typedef ::boost::mpl::vector<
  82. global_has_f1_0<void()>,
  83. copy_constructible<> > concept_type;
  84. model m(10);
  85. any<concept_type> x(m);
  86. x.f1();
  87. }
  88. BOOST_TYPE_ERASURE_MEMBER((global_has_f1_1), f1, 1)
  89. BOOST_AUTO_TEST_CASE(test_global_has_f1_overload) {
  90. typedef ::boost::mpl::vector<
  91. global_has_f1_0<int()>,
  92. global_has_f1_1<int(int)>,
  93. copy_constructible<> > concept_type;
  94. model m(10);
  95. any<concept_type> x(m);
  96. BOOST_CHECK_EQUAL(x.f1(), 10);
  97. BOOST_CHECK_EQUAL(x.f1(5), 15);
  98. }
  99. BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const) {
  100. typedef ::boost::mpl::vector<
  101. global_has_f1_0<int(), const _self>,
  102. global_has_f1_1<int(int), const _self>,
  103. copy_constructible<> > concept_type;
  104. model_const m(10);
  105. any<concept_type> x(m);
  106. BOOST_CHECK_EQUAL(x.f1(), 10);
  107. BOOST_CHECK_EQUAL(x.f1(5), 15);
  108. }
  109. struct model_overload_const_non_const {
  110. int f1() { return 1; }
  111. int f1() const { return 2; }
  112. };
  113. BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const_non_const) {
  114. typedef ::boost::mpl::vector<
  115. global_has_f1_0<int(), _self>,
  116. global_has_f1_0<int(), const _self>,
  117. copy_constructible<> > concept_type;
  118. model_overload_const_non_const m;
  119. any<concept_type> x1(m);
  120. BOOST_CHECK_EQUAL(x1.f1(), 1);
  121. const any<concept_type> x2(m);
  122. BOOST_CHECK_EQUAL(x2.f1(), 2);
  123. }
  124. BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const_non_const_ref) {
  125. typedef ::boost::mpl::vector<
  126. global_has_f1_0<int(), const _self>, // FIXME: This is order sensitive
  127. global_has_f1_0<int(), _self>,
  128. copy_constructible<> > concept_type;
  129. model_overload_const_non_const m;
  130. any<concept_type, _self&> x1(m);
  131. BOOST_CHECK_EQUAL(x1.f1(), 1);
  132. const any<concept_type, _self&> x2(m);
  133. BOOST_CHECK_EQUAL(x2.f1(), 1);
  134. }
  135. BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const_non_const_cref) {
  136. typedef ::boost::mpl::vector<
  137. global_has_f1_0<int(), _self>,
  138. global_has_f1_0<int(), const _self>,
  139. copy_constructible<> > concept_type;
  140. model_overload_const_non_const m;
  141. any<concept_type, const _self&> x1(m);
  142. BOOST_CHECK_EQUAL(x1.f1(), 2);
  143. const any<concept_type, const _self&> x2(m);
  144. BOOST_CHECK_EQUAL(x2.f1(), 2);
  145. }
  146. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  147. BOOST_AUTO_TEST_CASE(test_global_has_f1_rv) {
  148. typedef ::boost::mpl::vector<
  149. global_has_f1_1<int(int&&)>,
  150. copy_constructible<> > concept_type;
  151. model m(10);
  152. any<concept_type> x(m);
  153. BOOST_CHECK_EQUAL(x.f1(5), 15);
  154. }
  155. BOOST_AUTO_TEST_CASE(test_global_has_f1_rv_const) {
  156. typedef ::boost::mpl::vector<
  157. global_has_f1_1<int(int&&), const _self>,
  158. copy_constructible<> > concept_type;
  159. model_const m(10);
  160. const any<concept_type> x(m);
  161. BOOST_CHECK_EQUAL(x.f1(5), 15);
  162. }
  163. #endif
  164. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && \
  165. !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
  166. !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  167. !defined(BOOST_NO_CXX11_DECLTYPE) && \
  168. !BOOST_WORKAROUND(BOOST_MSVC, == 1800)
  169. namespace ns3 {
  170. BOOST_TYPE_ERASURE_MEMBER(f1);
  171. }
  172. BOOST_AUTO_TEST_CASE(test_simple_member_0) {
  173. typedef ::boost::mpl::vector<
  174. ns3::has_f1<int()>,
  175. copy_constructible<> > concept_type;
  176. model m(10);
  177. any<concept_type> x(m);
  178. BOOST_CHECK_EQUAL(x.f1(), 10);
  179. }
  180. BOOST_AUTO_TEST_CASE(test_simple_member_1) {
  181. typedef ::boost::mpl::vector<
  182. ns3::has_f1<int(int)>,
  183. copy_constructible<> > concept_type;
  184. model m(10);
  185. any<concept_type> x(m);
  186. BOOST_CHECK_EQUAL(x.f1(5), 15);
  187. }
  188. namespace ns3 {
  189. BOOST_TYPE_ERASURE_MEMBER(has_f1_ex, f1);
  190. }
  191. BOOST_AUTO_TEST_CASE(test_named_member_1) {
  192. typedef ::boost::mpl::vector<
  193. ns3::has_f1_ex<int(int)>,
  194. copy_constructible<> > concept_type;
  195. model m(10);
  196. any<concept_type> x(m);
  197. BOOST_CHECK_EQUAL(x.f1(5), 15);
  198. }
  199. BOOST_AUTO_TEST_CASE(test_named_member_1_const) {
  200. typedef ::boost::mpl::vector<
  201. ns3::has_f1_ex<int(int) const>,
  202. copy_constructible<> > concept_type;
  203. model_const m(10);
  204. const any<concept_type> x(m);
  205. BOOST_CHECK_EQUAL(x.f1(5), 15);
  206. }
  207. #endif