1 /*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
3 Copyright (c) 2001-2011 Joel de Guzman
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
9 #include <boost/config/warning_disable.hpp>
10 #include <boost/detail/lightweight_test.hpp>
11
12 #include <boost/spirit/include/karma_char.hpp>
13 #include <boost/spirit/include/karma_numeric.hpp>
14 #include <boost/spirit/include/karma_action.hpp>
15 #include <boost/spirit/include/karma_generate.hpp>
16 #include <boost/spirit/include/karma_operator.hpp>
17 #include <boost/bind.hpp>
18 #include <boost/lambda/lambda.hpp>
19
20 #include <sstream>
21 #include "test.hpp"
22
23 using namespace spirit_test;
24 using boost::spirit::unused_type;
25
read1(int & i)26 void read1(int& i)
27 {
28 i = 42;
29 }
30
read2(int & i,unused_type)31 void read2(int& i, unused_type)
32 {
33 i = 42;
34 }
35
read3(int & i,unused_type,bool &)36 void read3(int& i, unused_type, bool&)
37 {
38 i = 42;
39 }
40
read3_fail(int & i,unused_type,bool & pass)41 void read3_fail(int& i, unused_type, bool& pass)
42 {
43 i = 42;
44 pass = false;
45 }
46
47 struct read_action
48 {
operator ()read_action49 void operator()(int& i, unused_type, unused_type) const
50 {
51 i = 42;
52 }
53 };
54
55 ///////////////////////////////////////////////////////////////////////////////
main()56 int main()
57 {
58 using boost::spirit::karma::int_;
59 {
60 BOOST_TEST(test("42", int_[&read1]));
61 BOOST_TEST(test_delimited("42 ", int_[&read1], ' '));
62 BOOST_TEST(test("42", int_[&read2]));
63 BOOST_TEST(test_delimited("42 ", int_[&read2], ' '));
64 BOOST_TEST(test("42", int_[&read3]));
65 BOOST_TEST(test_delimited("42 ", int_[&read3], ' '));
66 BOOST_TEST(!test("42", int_[&read3_fail]));
67 BOOST_TEST(!test_delimited("42 ", int_[&read3_fail], ' '));
68 }
69
70 {
71 BOOST_TEST(test("42", int_[read_action()]));
72 BOOST_TEST(test_delimited("42 ", int_[read_action()], ' '));
73 }
74
75 {
76 BOOST_TEST(test("42", int_[boost::bind(&read1, _1)]));
77 BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read1, _1)], ' '));
78 BOOST_TEST(test("42", int_[boost::bind(&read2, _1, _2)]));
79 BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read2, _1, _2)], ' '));
80 BOOST_TEST(test("42", int_[boost::bind(&read3, _1, _2, _3)]));
81 BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read3, _1, _2, _3)], ' '));
82 }
83
84 {
85 namespace lambda = boost::lambda;
86 {
87 std::stringstream strm("42");
88 BOOST_TEST(test("42", int_[strm >> lambda::_1]));
89 }
90 {
91 std::stringstream strm("42");
92 BOOST_TEST(test_delimited("42 ", int_[strm >> lambda::_1], ' '));
93 }
94 }
95
96 {
97 BOOST_TEST(test("{42}", '{' << int_[&read1] << '}'));
98 BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read1] << '}', ' '));
99 BOOST_TEST(test("{42}", '{' << int_[&read2] << '}'));
100 BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read2] << '}', ' '));
101 BOOST_TEST(test("{42}", '{' << int_[&read3] << '}'));
102 BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read3] << '}', ' '));
103 }
104
105 {
106 BOOST_TEST(test("{42}", '{' << int_[read_action()] << '}'));
107 BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[read_action()] << '}', ' '));
108 }
109
110 {
111 BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read1, _1)] << '}'));
112 BOOST_TEST(test_delimited("{ 42 } ",
113 '{' << int_[boost::bind(&read1, _1)] << '}', ' '));
114 BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read2, _1, _2)] << '}'));
115 BOOST_TEST(test_delimited("{ 42 } ",
116 '{' << int_[boost::bind(&read2, _1, _2)] << '}', ' '));
117 BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read3, _1, _2, _3)] << '}'));
118 BOOST_TEST(test_delimited("{ 42 } ",
119 '{' << int_[boost::bind(&read3, _1, _2, _3)] << '}', ' '));
120 }
121
122 {
123 namespace lambda = boost::lambda;
124 {
125 std::stringstream strm("42");
126 BOOST_TEST(test("{42}", '{' << int_[strm >> lambda::_1] << '}'));
127 }
128 {
129 std::stringstream strm("42");
130 BOOST_TEST(test_delimited("{ 42 } ",
131 '{' << int_[strm >> lambda::_1] << '}', ' '));
132 }
133 }
134
135 return boost::report_errors();
136 }
137
138