list.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/spirit/include/qi_operator.hpp>
  12. #include <boost/spirit/include/qi_char.hpp>
  13. #include <boost/spirit/include/qi_string.hpp>
  14. #include <boost/spirit/include/qi_numeric.hpp>
  15. #include <boost/spirit/include/qi_directive.hpp>
  16. #include <boost/spirit/include/qi_action.hpp>
  17. #include <boost/spirit/include/support_argument.hpp>
  18. #include <boost/spirit/include/phoenix_core.hpp>
  19. #include <boost/spirit/include/phoenix_operator.hpp>
  20. #include <boost/spirit/include/phoenix_object.hpp>
  21. #include <boost/spirit/include/phoenix_container.hpp>
  22. #include <boost/fusion/include/std_pair.hpp>
  23. #include <string>
  24. #include <iostream>
  25. #include "test.hpp"
  26. using namespace spirit_test;
  27. int
  28. main()
  29. {
  30. using namespace boost::spirit::ascii;
  31. {
  32. BOOST_TEST(test("a,b,c,d,e,f,g,h", char_ % ','));
  33. BOOST_TEST(test("a,b,c,d,e,f,g,h,", char_ % ',', false));
  34. }
  35. {
  36. BOOST_TEST(test("a, b, c, d, e, f, g, h", char_ % ',', space));
  37. BOOST_TEST(test("a, b, c, d, e, f, g, h,", char_ % ',', space, false));
  38. }
  39. {
  40. std::string s;
  41. BOOST_TEST(test_attr("a,b,c,d,e,f,g,h", char_ % ',', s));
  42. BOOST_TEST(s == "abcdefgh");
  43. BOOST_TEST(!test("a,b,c,d,e,f,g,h,", char_ % ','));
  44. }
  45. {
  46. std::string s;
  47. BOOST_TEST(test_attr("ab,cd,ef,gh", (char_ >> char_) % ',', s));
  48. BOOST_TEST(s == "abcdefgh");
  49. BOOST_TEST(!test("ab,cd,ef,gh,", (char_ >> char_) % ','));
  50. BOOST_TEST(!test("ab,cd,ef,g", (char_ >> char_) % ','));
  51. s.clear();
  52. BOOST_TEST(test_attr("ab,cd,efg", (char_ >> char_) % ',' >> char_, s));
  53. BOOST_TEST(s == "abcdefg");
  54. }
  55. {
  56. using boost::spirit::int_;
  57. std::vector<int> v;
  58. BOOST_TEST(test_attr("1,2", int_ % ',', v));
  59. BOOST_TEST(2 == v.size() && 1 == v[0] && 2 == v[1]);
  60. }
  61. {
  62. using boost::spirit::int_;
  63. std::vector<int> v;
  64. BOOST_TEST(test_attr("(1,2)", '(' >> int_ % ',' >> ')', v));
  65. BOOST_TEST(2 == v.size() && 1 == v[0] && 2 == v[1]);
  66. }
  67. {
  68. std::vector<std::string> v;
  69. BOOST_TEST(test_attr("a,b,c,d", +alpha % ',', v));
  70. BOOST_TEST(4 == v.size() && "a" == v[0] && "b" == v[1]
  71. && "c" == v[2] && "d" == v[3]);
  72. }
  73. {
  74. std::vector<boost::optional<char> > v;
  75. BOOST_TEST(test_attr("#a,#", ('#' >> -alpha) % ',', v));
  76. BOOST_TEST(2 == v.size() &&
  77. !!v[0] && 'a' == boost::get<char>(v[0]) && !v[1]);
  78. std::vector<char> v2;
  79. BOOST_TEST(test_attr("#a,#", ('#' >> -alpha) % ',', v2));
  80. BOOST_TEST(1 == v2.size() && 'a' == v2[0]);
  81. }
  82. {
  83. typedef std::set<std::pair<std::string, std::string> > set_type;
  84. set_type s;
  85. BOOST_TEST(test_attr("k1=v1&k2=v2",
  86. (*(char_ - '=') >> '=' >> *(char_ - '&')) % '&', s));
  87. set_type::const_iterator it = s.begin();
  88. BOOST_TEST(s.size() == 2);
  89. BOOST_TEST(it != s.end() && (*it).first == "k1" && (*it).second == "v1");
  90. BOOST_TEST(++it != s.end() && (*it).first == "k2" && (*it).second == "v2");
  91. }
  92. {
  93. typedef std::map<std::string, std::string> map_type;
  94. map_type m;
  95. BOOST_TEST(test_attr("k1=v1&k2=v2",
  96. (*(char_ - '=') >> '=' >> *(char_ - '&')) % '&', m));
  97. map_type::const_iterator it = m.begin();
  98. BOOST_TEST(m.size() == 2);
  99. BOOST_TEST(it != m.end() && (*it).first == "k1" && (*it).second == "v1");
  100. BOOST_TEST(++it != m.end() && (*it).first == "k2" && (*it).second == "v2");
  101. }
  102. { // actions
  103. namespace phx = boost::phoenix;
  104. using boost::phoenix::begin;
  105. using boost::phoenix::end;
  106. using boost::phoenix::construct;
  107. using boost::spirit::qi::_1;
  108. std::string s;
  109. BOOST_TEST(test("a,b,c,d,e,f,g,h", (char_ % ',')
  110. [phx::ref(s) = construct<std::string>(begin(_1), end(_1))]));
  111. }
  112. return boost::report_errors();
  113. }