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/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 using namespace boost::placeholders;
77 BOOST_TEST(test("42", int_[boost::bind(&read1, _1)]));
78 BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read1, _1)], ' '));
79 BOOST_TEST(test("42", int_[boost::bind(&read2, _1, _2)]));
80 BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read2, _1, _2)], ' '));
81 BOOST_TEST(test("42", int_[boost::bind(&read3, _1, _2, _3)]));
82 BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read3, _1, _2, _3)], ' '));
83 }
84
85 {
86 namespace lambda = boost::lambda;
87 {
88 std::stringstream strm("42");
89 BOOST_TEST(test("42", int_[strm >> lambda::_1]));
90 }
91 {
92 std::stringstream strm("42");
93 BOOST_TEST(test_delimited("42 ", int_[strm >> lambda::_1], ' '));
94 }
95 }
96
97 {
98 BOOST_TEST(test("{42}", '{' << int_[&read1] << '}'));
99 BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read1] << '}', ' '));
100 BOOST_TEST(test("{42}", '{' << int_[&read2] << '}'));
101 BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read2] << '}', ' '));
102 BOOST_TEST(test("{42}", '{' << int_[&read3] << '}'));
103 BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read3] << '}', ' '));
104 }
105
106 {
107 BOOST_TEST(test("{42}", '{' << int_[read_action()] << '}'));
108 BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[read_action()] << '}', ' '));
109 }
110
111 {
112 using namespace boost::placeholders;
113 BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read1, _1)] << '}'));
114 BOOST_TEST(test_delimited("{ 42 } ",
115 '{' << int_[boost::bind(&read1, _1)] << '}', ' '));
116 BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read2, _1, _2)] << '}'));
117 BOOST_TEST(test_delimited("{ 42 } ",
118 '{' << int_[boost::bind(&read2, _1, _2)] << '}', ' '));
119 BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read3, _1, _2, _3)] << '}'));
120 BOOST_TEST(test_delimited("{ 42 } ",
121 '{' << int_[boost::bind(&read3, _1, _2, _3)] << '}', ' '));
122 }
123
124 {
125 namespace lambda = boost::lambda;
126 {
127 std::stringstream strm("42");
128 BOOST_TEST(test("{42}", '{' << int_[strm >> lambda::_1] << '}'));
129 }
130 {
131 std::stringstream strm("42");
132 BOOST_TEST(test_delimited("{ 42 } ",
133 '{' << int_[strm >> lambda::_1] << '}', ' '));
134 }
135 }
136
137 return boost::report_errors();
138 }
139
140