switch.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // new_switch.cpp
  3. //
  4. // Copyright 2011 Eric Niebler
  5. // Copyright Pierre Esterie & Joel Falcou.
  6. // Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #include <boost/proto/core.hpp>
  10. #include <boost/proto/transform.hpp>
  11. #include <boost/detail/workaround.hpp>
  12. #include <boost/proto/debug.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/mpl/long.hpp>
  15. #include <boost/mpl/bool.hpp>
  16. namespace proto = boost::proto;
  17. struct MyCases
  18. {
  19. template<typename Tag>
  20. struct case_
  21. : proto::not_<proto::_>
  22. {};
  23. };
  24. template<>
  25. struct MyCases::case_<proto::tag::shift_right>
  26. : proto::_
  27. {};
  28. template<>
  29. struct MyCases::case_<proto::tag::plus>
  30. : proto::_
  31. {};
  32. struct ArityOf;
  33. struct ArityOfCases
  34. {
  35. template<typename ArityOf>
  36. struct case_
  37. : proto::not_<proto::_>
  38. {};
  39. };
  40. template<>
  41. struct ArityOfCases::case_<boost::mpl::long_<1> >
  42. : boost::proto::when<boost::proto::_, boost::mpl::false_()>
  43. {};
  44. template<>
  45. struct ArityOfCases::case_<boost::mpl::long_<2> >
  46. : boost::proto::when<boost::proto::_, boost::mpl::true_()>
  47. {};
  48. struct ArityOf
  49. : boost::proto::switch_<
  50. ArityOfCases
  51. , proto::arity_of<proto::_>()
  52. >
  53. {};
  54. void test_switch()
  55. {
  56. // Tests for backward compatibility
  57. proto::assert_matches<proto::switch_<MyCases> >(proto::lit(1) >> 'a');
  58. proto::assert_matches<proto::switch_<MyCases> >(proto::lit(1) + 'a');
  59. proto::assert_matches_not<proto::switch_<MyCases> >(proto::lit(1) << 'a');
  60. //Test new matching on the Transform result type
  61. ArityOf ar;
  62. proto::assert_matches_not<ArityOf>(proto::lit(1));
  63. proto::assert_matches<ArityOf>(proto::lit(1) + 2);
  64. proto::assert_matches<ArityOf>(!proto::lit(1));
  65. BOOST_CHECK_EQUAL(ar(!proto::lit(1)), false);
  66. BOOST_CHECK_EQUAL(ar(proto::lit(1) + 2), true);
  67. }
  68. using namespace boost::unit_test;
  69. ///////////////////////////////////////////////////////////////////////////////
  70. // init_unit_test_suite
  71. //
  72. test_suite* init_unit_test_suite(int argc, char* argv[])
  73. {
  74. test_suite *test = BOOST_TEST_SUITE("test proto::switch_<>");
  75. test->add(BOOST_TEST_CASE(&test_switch));
  76. return test;
  77. }