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