1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2001-2011 Joel de Guzman
3 // Copyright (c) 2010 Bryce Lelbach
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #include <boost/config/warning_disable.hpp>
9 #include <boost/detail/lightweight_test.hpp>
10
11 #include <boost/mpl/print.hpp>
12
13 #include <boost/spirit/include/karma.hpp>
14 #include <boost/spirit/include/support_utree.hpp>
15
16 #include <sstream>
17
18 #include "test.hpp"
19
main()20 int main()
21 {
22 using spirit_test::test;
23 using spirit_test::test_delimited;
24 using boost::spirit::utree;
25 using boost::spirit::utree_type;
26 using boost::spirit::utf8_string_range_type;
27 using boost::spirit::utf8_string_type;
28 using boost::spirit::utf8_symbol_type;
29
30 using boost::spirit::karma::char_;
31 using boost::spirit::karma::bool_;
32 using boost::spirit::karma::int_;
33 using boost::spirit::karma::double_;
34 using boost::spirit::karma::string;
35 using boost::spirit::karma::space;
36 using boost::spirit::karma::rule;
37
38 typedef spirit_test::output_iterator<char>::type output_iterator;
39
40 // primitive data types
41 {
42 utree ut('x');
43 BOOST_TEST(test("x", char_, ut));
44
45 ut = false;
46 BOOST_TEST(test("false", bool_, ut));
47
48 ut = 123;
49 BOOST_TEST(test("123", int_, ut));
50
51 ut = 123.45;
52 BOOST_TEST(test("123.45", double_, ut));
53
54 ut = "abc";
55 BOOST_TEST(test("abc", string, ut));
56
57 ut = utf8_symbol_type("xyz");
58 BOOST_TEST(test("xyz", string, ut));
59 }
60
61 // sequences
62 {
63 using boost::spirit::karma::digit;
64 using boost::spirit::karma::repeat;
65
66 utree ut;
67 ut.push_back('x');
68 ut.push_back('y');
69 BOOST_TEST(test("xy", char_ << char_, ut));
70
71 ut.clear();
72 ut.push_back(123);
73 ut.push_back(456);
74 BOOST_TEST(test_delimited("123 456 ", int_ << int_, ut, space));
75
76 ut.clear();
77 ut.push_back(1.23);
78 ut.push_back(4.56);
79 BOOST_TEST(test_delimited("1.23 4.56 ", double_ << double_, ut, space));
80
81 ut.clear();
82 ut.push_back(1.23);
83 ut.push_back("ab");
84 BOOST_TEST(test("1.23ab", double_ << string, ut));
85
86 ut.clear();
87
88 rule<output_iterator, double()> r1 = double_;
89 rule<output_iterator, utree()> r2 = double_;
90
91 // ( 1.23 "a" "b" )
92 ut.push_back(1.23);
93 ut.push_back('a');
94 ut.push_back('b');
95 BOOST_TEST(test("1.23ab", double_ << *char_, ut));
96 BOOST_TEST(test("1.23ab", r1 << *char_, ut));
97 BOOST_TEST(test("1.23ab", r2 << *char_, ut));
98
99 // ( ( 1.23 ) "a" "b" )
100 ut.clear();
101 utree ut1;
102 ut1.push_back(1.23);
103 ut.push_back(ut1);
104 ut.push_back('a');
105 ut.push_back('b');
106 BOOST_TEST(test("1.23ab", r1 << *char_, ut));
107 BOOST_TEST(test("1.23ab", r2 << *char_, ut));
108
109 // ( "a" "b" 1.23 )
110 ut.clear();
111 ut.push_back('a');
112 ut.push_back('b');
113 ut.push_back(1.23);
114 BOOST_TEST(test("ab1.23", repeat(2)[~digit] << double_, ut));
115 BOOST_TEST(test("ab1.23", repeat(2)[~digit] << r1, ut));
116 BOOST_TEST(test("ab1.23", repeat(2)[~digit] << r2, ut));
117
118 // ( "a" "b" ( 1.23 ) )
119 ut.clear();
120 ut.push_back('a');
121 ut.push_back('b');
122 ut.push_back(ut1);
123 BOOST_TEST(test("ab1.23", repeat(2)[~digit] << r1, ut));
124 BOOST_TEST(test("ab1.23", repeat(2)[~digit] << r2, ut));
125 }
126
127 return boost::report_errors();
128 }
129