test_limits.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/free.hpp>
  12. #include <boost/type_erasure/builtin.hpp>
  13. #include <boost/mpl/vector.hpp>
  14. #include <boost/mpl/transform.hpp>
  15. #include <boost/mpl/back_inserter.hpp>
  16. #include <boost/mpl/range_c.hpp>
  17. #define BOOST_TEST_MAIN
  18. #include <boost/test/unit_test.hpp>
  19. using namespace boost::type_erasure;
  20. namespace mpl = boost::mpl;
  21. template<int N>
  22. struct tester
  23. {
  24. int value;
  25. };
  26. int func() { return 0; }
  27. template<int N, class... T>
  28. int func(tester<N> t0, T... t)
  29. {
  30. return t0.value + func(t...);
  31. }
  32. BOOST_TYPE_ERASURE_FREE((has_func), func)
  33. template<class T = _self>
  34. struct common : mpl::vector<
  35. copy_constructible<T>
  36. > {};
  37. BOOST_AUTO_TEST_CASE(test_arity)
  38. {
  39. tester<0> t = { 1 };
  40. any<mpl::vector<common<>, has_func<int(_self, _self, _self, _self, _self, _self)> > > x(t);
  41. int i = func(x, x, x, x, x, x);
  42. BOOST_TEST(i == 6);
  43. }
  44. BOOST_AUTO_TEST_CASE(test_null_arity)
  45. {
  46. any<mpl::vector<common<>, has_func<int(_self, _self, _self, _self, _self, _self)>, relaxed> > x;
  47. BOOST_CHECK_THROW(func(x, x, x, x, x , x), boost::type_erasure::bad_function_call);
  48. }
  49. template<class T0, class... T>
  50. struct my_concept
  51. {
  52. static int apply(T0 t0) { return func(t0); }
  53. };
  54. BOOST_AUTO_TEST_CASE(test_template_arity)
  55. {
  56. typedef my_concept<_self, int, int, int, int, int, int> concept1;
  57. tester<0> t = { 1 };
  58. any<mpl::vector<common<>, concept1> > x(t);
  59. int i = call(concept1(), x);
  60. BOOST_TEST(i == 1);
  61. }
  62. template<class T>
  63. struct make_funcN
  64. {
  65. typedef has_func<int(_self, tester<T::value>)> type;
  66. };
  67. BOOST_AUTO_TEST_CASE(test_vtable_size)
  68. {
  69. tester<0> t = { 1 };
  70. any<mpl::vector<
  71. common<>,
  72. mpl::transform<mpl::range_c<int, 1, 60>,
  73. make_funcN<mpl::_1>,
  74. mpl::back_inserter< boost::mp11::mp_list<> >
  75. >::type
  76. > > x(t);
  77. tester<7> t1 = { 2 };
  78. int i = func(x, t1);
  79. BOOST_TEST(i == 3);
  80. }