string.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
  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. #define BOOST_TEST_MODULE string
  6. #define BOOST_METAPARSE_LIMIT_STRING_SIZE 64
  7. #include <boost/metaparse/string.hpp>
  8. #include <boost/mpl/equal.hpp>
  9. #include <boost/mpl/equal_to.hpp>
  10. #include <boost/mpl/assert.hpp>
  11. #include <boost/mpl/at.hpp>
  12. #include <boost/mpl/char.hpp>
  13. #include <boost/mpl/int.hpp>
  14. #include <boost/mpl/size.hpp>
  15. #include <boost/mpl/back.hpp>
  16. #include <boost/mpl/begin_end.hpp>
  17. #include <boost/mpl/deref.hpp>
  18. #include <boost/mpl/advance.hpp>
  19. #include <boost/mpl/next.hpp>
  20. #include <boost/mpl/prior.hpp>
  21. #include <boost/mpl/distance.hpp>
  22. #include <boost/mpl/clear.hpp>
  23. #include <boost/mpl/empty.hpp>
  24. #include <boost/mpl/front.hpp>
  25. #include <boost/mpl/is_sequence.hpp>
  26. #include <boost/mpl/pop_front.hpp>
  27. #include <boost/mpl/pop_back.hpp>
  28. #include <boost/mpl/push_front.hpp>
  29. #include <boost/mpl/push_back.hpp>
  30. #include <boost/mpl/string.hpp>
  31. #include <boost/mpl/assert.hpp>
  32. #include <boost/test/unit_test.hpp>
  33. #include <string>
  34. BOOST_AUTO_TEST_CASE(test_string)
  35. {
  36. using boost::mpl::equal;
  37. using boost::mpl::equal_to;
  38. using boost::mpl::char_;
  39. using boost::mpl::int_;
  40. using boost::mpl::at;
  41. using boost::mpl::at_c;
  42. using boost::mpl::size;
  43. using boost::mpl::back;
  44. using boost::mpl::deref;
  45. using boost::mpl::advance;
  46. using boost::mpl::next;
  47. using boost::mpl::prior;
  48. using boost::mpl::distance;
  49. using boost::mpl::clear;
  50. using boost::mpl::empty;
  51. using boost::mpl::front;
  52. using boost::mpl::is_sequence;
  53. using boost::mpl::pop_front;
  54. using boost::mpl::pop_back;
  55. using boost::mpl::begin;
  56. using boost::mpl::end;
  57. using boost::mpl::c_str;
  58. using boost::mpl::push_front;
  59. using boost::mpl::push_back;
  60. using namespace boost;
  61. // string type
  62. ///////////////////////
  63. typedef metaparse::string<'H','e','l','l','o'> hello;
  64. typedef metaparse::string<> empty_string;
  65. typedef begin<hello>::type begin_hello;
  66. typedef end<hello>::type end_hello;
  67. // test_value_of_empty_string
  68. BOOST_REQUIRE_EQUAL(std::string(), c_str<empty_string>::type::value);
  69. // test_value
  70. BOOST_REQUIRE_EQUAL(std::string("Hello"), c_str<hello>::type::value);
  71. // equal_to
  72. BOOST_MPL_ASSERT((equal_to<hello, hello>));
  73. BOOST_MPL_ASSERT_NOT((equal_to<hello, empty_string>));
  74. // at
  75. BOOST_MPL_ASSERT((equal_to<char_<'e'>, at<hello, int_<1> >::type>));
  76. BOOST_MPL_ASSERT((equal_to<char_<'e'>, at_c<hello, 1>::type>));
  77. // size
  78. BOOST_MPL_ASSERT((equal_to<int_<5>, size<hello>::type>));
  79. // is_sequence
  80. BOOST_MPL_ASSERT((is_sequence<hello>));
  81. // front
  82. BOOST_MPL_ASSERT((equal_to<char_<'H'>, front<hello>::type>));
  83. // push_front
  84. BOOST_MPL_ASSERT((
  85. equal_to<
  86. metaparse::string<'x','H','e','l','l','o'>,
  87. push_front<hello, char_<'x'> >::type
  88. >
  89. ));
  90. // back
  91. BOOST_MPL_ASSERT((equal_to<char_<'o'>, back<hello>::type>));
  92. // push_back
  93. BOOST_MPL_ASSERT((
  94. equal_to<
  95. metaparse::string<'H','e','l','l','o', 'x'>,
  96. push_back<hello, char_<'x'> >::type
  97. >
  98. ));
  99. // clear
  100. BOOST_MPL_ASSERT((equal_to<empty_string, clear<hello>::type>));
  101. // empty
  102. BOOST_MPL_ASSERT_NOT((empty<hello>::type));
  103. BOOST_MPL_ASSERT((empty<empty_string>::type));
  104. // string_iterator
  105. BOOST_MPL_ASSERT((equal_to<begin_hello, begin_hello>));
  106. BOOST_MPL_ASSERT_NOT((equal_to<begin_hello, end_hello>));
  107. BOOST_MPL_ASSERT((
  108. equal_to<begin<empty_string>::type, end<empty_string>::type>
  109. ));
  110. BOOST_MPL_ASSERT((equal_to<char_<'H'>, deref<begin_hello>::type>));
  111. BOOST_MPL_ASSERT((equal_to<end_hello, advance<begin_hello, int_<5> >::type>));
  112. BOOST_MPL_ASSERT((
  113. equal_to<char_<'e'>, deref<next<begin_hello>::type>::type>
  114. ));
  115. BOOST_MPL_ASSERT((equal_to<char_<'o'>, deref<prior<end_hello>::type>::type>));
  116. BOOST_MPL_ASSERT((equal_to<int_<5>, distance<begin_hello, end_hello>::type>));
  117. // pop_front
  118. BOOST_MPL_ASSERT((
  119. equal_to<metaparse::string<'e','l','l','o'>, pop_front<hello>::type>
  120. ));
  121. // pop_back
  122. BOOST_MPL_ASSERT((
  123. equal_to<metaparse::string<'H','e','l','l'>, pop_back<hello>::type>
  124. ));
  125. #if BOOST_METAPARSE_STD >= 2011
  126. // BOOST_METAPARSE_STRING macro
  127. ///////////////////////
  128. // test_empty_string
  129. BOOST_MPL_ASSERT((equal<BOOST_METAPARSE_STRING(""), empty_string>));
  130. // test_string_creation
  131. BOOST_MPL_ASSERT((equal<BOOST_METAPARSE_STRING("Hello"), hello>));
  132. #endif
  133. }