1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 //
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 <boost/config/warning_disable.hpp>
7 #include <boost/detail/lightweight_test.hpp>
8
9 #include <boost/spirit/include/karma_auxiliary.hpp>
10 #include <boost/spirit/include/karma_char.hpp>
11 #include <boost/spirit/include/karma_string.hpp>
12 #include <boost/spirit/include/karma_numeric.hpp>
13 #include <boost/spirit/include/karma_generate.hpp>
14 #include <boost/spirit/include/karma_operator.hpp>
15 #include <boost/spirit/include/karma_directive.hpp>
16 #include <boost/spirit/include/karma_nonterminal.hpp>
17 #include <boost/spirit/include/karma_auxiliary.hpp>
18
19 #include "test.hpp"
20
21 using namespace spirit_test;
22
23 ///////////////////////////////////////////////////////////////////////////////
24 int
main()25 main()
26 {
27 using namespace boost;
28 using namespace boost::spirit;
29 using namespace boost::spirit::ascii;
30
31 {
32 BOOST_TEST(test("x", char_('x') | char_('i')));
33 BOOST_TEST(test("xi", char_('x') << char_('i') | char_('i')));
34 BOOST_TEST(test("i", char_('i') | char_('x') << char_('i')));
35
36 BOOST_TEST(test("x", buffer[char_('x')] | char_('i')));
37
38 variant<int, char> v (10);
39 BOOST_TEST(test("10", char_ | int_, v));
40 BOOST_TEST(test("10", int_ | char_, v));
41 BOOST_TEST(test("a", lit('a') | char_ | int_, v));
42 BOOST_TEST(test("a", char_ | lit('a') | int_, v));
43 BOOST_TEST(test("10", int_ | lit('a') | char_, v));
44
45 v = 'c';
46 BOOST_TEST(test("c", char_ | int_, v));
47 BOOST_TEST(test("a", lit('a') | char_ | int_, v));
48 BOOST_TEST(test("c", char_ | lit('a') | int_, v));
49 BOOST_TEST(test("a", int_ | lit('a') | char_, v));
50 BOOST_TEST(test("c", int_ | char_ | lit('a'), v));
51 }
52
53 // testing for alignment/truncation problems on little endian systems
54 // (big endian systems will fail one of the other tests below)
55 {
56 // test optional attribute
57 optional<variant<int, char> > v;
58 BOOST_TEST(!test("", char_ | int_, v));
59 BOOST_TEST(!test("", int_ | char_, v));
60 BOOST_TEST(test("a", lit('a') | char_ | int_, v));
61 BOOST_TEST(test("a", char_ | lit('a') | int_, v));
62 BOOST_TEST(test("a", int_ | lit('a') | char_, v));
63
64 v = 10;
65 BOOST_TEST(test("10", char_ | int_, v));
66 BOOST_TEST(test("10", int_ | char_, v));
67 BOOST_TEST(test("a", lit('a') | char_ | int_, v));
68 BOOST_TEST(test("a", char_ | lit('a') | int_, v));
69 BOOST_TEST(test("10", int_ | lit('a') | char_, v));
70
71 v = 'c';
72 BOOST_TEST(test("c", char_ | int_, v));
73 BOOST_TEST(test("a", lit('a') | char_ | int_, v));
74 BOOST_TEST(test("c", char_ | lit('a') | int_, v));
75 BOOST_TEST(test("a", int_ | lit('a') | char_, v));
76 BOOST_TEST(test("c", int_ | char_ | lit('a'), v));
77 }
78
79 {
80 // more tests for optional attribute
81 optional<int> o;
82 BOOST_TEST(test("a", lit('a') | int_, o));
83 BOOST_TEST(test("a", int_ | lit('a'), o));
84
85 o = 10;
86 BOOST_TEST(test("a", lit('a') | int_, o));
87 BOOST_TEST(test("10", int_ | lit('a'), o));
88 }
89
90 {
91 int i = 10;
92 BOOST_TEST(test("a", lit('a') | int_, i));
93 BOOST_TEST(test("10", int_ | lit('a'), i));
94 }
95
96 {
97 optional<std::string> o;
98 BOOST_TEST(test("xyzzy", ("(" << string << ")") | lit("xyzzy"), o));
99
100 o = "plugh";
101 BOOST_TEST(test("(plugh)", ("(" << string << ")") | lit("xyzzy"), o));
102 }
103
104 {
105 BOOST_TEST(test("abc", string | int_, std::string("abc")));
106 BOOST_TEST(test("1234", string | int_, 1234));
107 BOOST_TEST(test("abc", int_ | string, std::string("abc")));
108 BOOST_TEST(test("1234", int_ | string, 1234));
109 }
110
111 {
112 // testing for alignment/truncation problems on little endian systems
113 // (big endian systems will fail one of the other tests below)
114 std::basic_string<wchar_t> generated;
115 std::back_insert_iterator<std::basic_string<wchar_t> > outit(generated);
116 boost::variant<int, char> v(10);
117 bool result = karma::generate_delimited(outit
118 , karma::int_ | karma::char_, karma::char_(' '), v);
119 BOOST_TEST(result && generated == L"10 ");
120 }
121
122 {
123 boost::optional<int> v;
124 BOOST_TEST(test("error", int_ | "error" << omit[-int_], v));
125 BOOST_TEST(!test("error", int_ | "error" << omit[int_], v));
126 BOOST_TEST(test("error", int_ | "error" << skip[int_], v));
127 v = 1;
128 BOOST_TEST(test("1", int_ | "error" << omit[-int_], v));
129 BOOST_TEST(test("1", int_ | "error" << omit[int_], v));
130 BOOST_TEST(test("1", int_ | "error" << skip[int_], v));
131 }
132
133 {
134 typedef spirit_test::output_iterator<char>::type outiter_type;
135 namespace karma = boost::spirit::karma;
136
137 karma::rule<outiter_type, int()> r = int_;
138 std::vector<int> v;
139 BOOST_TEST(test("", '>' << r % ',' | karma::eps, v));
140
141 v.push_back(1);
142 v.push_back(2);
143 v.push_back(3);
144 v.push_back(4);
145 BOOST_TEST(test(">1,2,3,4", '>' << r % ',' | karma::eps, v));
146 }
147
148 {
149 typedef spirit_test::output_iterator<char>::type outiter_type;
150 namespace karma = boost::spirit::karma;
151
152 karma::rule<outiter_type, boost::optional<int>()> r = int_;
153 boost::optional<int> o;
154 BOOST_TEST(test("error", r | "error", o));
155
156 o = 10;
157 BOOST_TEST(test("10", r | "error", o));
158 }
159
160 return boost::report_errors();
161 }
162
163