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