1 // Copyright (c) 2001-2010 Hartmut Kaiser
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)
6 #if !defined(BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM)
7 #define BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM
15 #include <boost/spirit/include/karma_generate.hpp>
16 #include <boost/spirit/include/karma_what.hpp>
18 #include <boost/core/lightweight_test.hpp>
22 ///////////////////////////////////////////////////////////////////////////
26 void operator()(T const &) const
28 std::cout << typeid(T).name() << std::endl;
34 std::cout << typeid(T).name() << std::endl;
38 display_type const display = {};
40 ///////////////////////////////////////////////////////////////////////////
41 template <typename Char>
42 struct output_iterator
44 typedef std::basic_string<Char> string_type;
45 typedef std::back_insert_iterator<string_type> type;
48 ///////////////////////////////////////////////////////////////////////////
49 template <typename Char, typename T>
50 void print_if_failed(char const* func, bool result
51 , std::basic_string<Char> const& generated, T const& expected)
54 std::cerr << "in " << func << ": result is false" << std::endl;
55 else if (generated != expected)
56 std::cerr << "in " << func << ": generated \""
57 << std::string(generated.begin(), generated.end())
61 ///////////////////////////////////////////////////////////////////////////
62 template <typename Char, typename Generator>
63 inline bool test(Char const *expected, Generator const& g)
65 namespace karma = boost::spirit::karma;
66 typedef std::basic_string<Char> string_type;
68 // we don't care about the result of the "what" function.
69 // we only care that all generators have it:
72 string_type generated;
73 std::back_insert_iterator<string_type> outit(generated);
74 bool result = karma::generate(outit, g);
76 print_if_failed("test", result, generated, expected);
77 return result && generated == expected;
80 template <typename Char, typename Generator>
81 inline bool test(std::basic_string<Char> const& expected, Generator const& g)
83 namespace karma = boost::spirit::karma;
84 typedef std::basic_string<Char> string_type;
86 // we don't care about the result of the "what" function.
87 // we only care that all generators have it:
90 string_type generated;
91 std::back_insert_iterator<string_type> outit(generated);
92 bool result = karma::generate(outit, g);
94 print_if_failed("test", result, generated, expected);
95 return result && generated == expected;
98 ///////////////////////////////////////////////////////////////////////////
99 template <typename Char, typename Generator, typename Attribute>
100 inline bool test(Char const *expected, Generator const& g,
101 Attribute const &attr)
103 namespace karma = boost::spirit::karma;
104 typedef std::basic_string<Char> string_type;
106 // we don't care about the result of the "what" function.
107 // we only care that all generators have it:
110 string_type generated;
111 std::back_insert_iterator<string_type> outit(generated);
112 bool result = karma::generate(outit, g, attr);
114 print_if_failed("test", result, generated, expected);
115 return result && generated == expected;
118 template <typename Char, typename Generator, typename Attribute>
119 inline bool test(std::basic_string<Char> const& expected, Generator const& g,
120 Attribute const &attr)
122 namespace karma = boost::spirit::karma;
123 typedef std::basic_string<Char> string_type;
125 // we don't care about the result of the "what" function.
126 // we only care that all generators have it:
129 string_type generated;
130 std::back_insert_iterator<string_type> outit(generated);
131 bool result = karma::generate(outit, g, attr);
133 print_if_failed("test", result, generated, expected);
134 return result && generated == expected;
137 ///////////////////////////////////////////////////////////////////////////
138 template <typename Char, typename Generator, typename Delimiter>
139 inline bool test_delimited(Char const *expected, Generator const& g,
142 namespace karma = boost::spirit::karma;
143 typedef std::basic_string<Char> string_type;
145 // we don't care about the result of the "what" function.
146 // we only care that all generators have it:
149 string_type generated;
150 std::back_insert_iterator<string_type> outit(generated);
151 bool result = karma::generate_delimited(outit, g, d);
153 print_if_failed("test_delimited", result, generated, expected);
154 return result && generated == expected;
157 template <typename Char, typename Generator, typename Delimiter>
158 inline bool test_delimited(std::basic_string<Char> const& expected,
159 Generator const& g, Delimiter const& d)
161 namespace karma = boost::spirit::karma;
162 typedef std::basic_string<Char> string_type;
164 // we don't care about the result of the "what" function.
165 // we only care that all generators have it:
168 string_type generated;
169 std::back_insert_iterator<string_type> outit(generated);
170 bool result = karma::generate_delimited(outit, g, d);
172 print_if_failed("test_delimited", result, generated, expected);
173 return result && generated == expected;
176 ///////////////////////////////////////////////////////////////////////////
177 template <typename Char, typename Generator, typename Attribute,
179 inline bool test_delimited(Char const *expected, Generator const& g,
180 Attribute const &attr, Delimiter const& d)
182 namespace karma = boost::spirit::karma;
183 typedef std::basic_string<Char> string_type;
185 // we don't care about the result of the "what" function.
186 // we only care that all generators have it:
189 string_type generated;
190 std::back_insert_iterator<string_type> outit(generated);
191 bool result = karma::generate_delimited(outit, g, d, attr);
193 print_if_failed("test_delimited", result, generated, expected);
194 return result && generated == expected;
197 template <typename Char, typename Generator, typename Attribute,
199 inline bool test_delimited(std::basic_string<Char> const& expected,
200 Generator const& g, Attribute const &attr, Delimiter const& d)
202 namespace karma = boost::spirit::karma;
203 typedef std::basic_string<Char> string_type;
205 // we don't care about the result of the "what" function.
206 // we only care that all generators have it:
209 string_type generated;
210 std::back_insert_iterator<string_type> outit(generated);
211 bool result = karma::generate_delimited(outit, g, d, attr);
213 print_if_failed("test_delimited", result, generated, expected);
214 return result && generated == expected;
217 ///////////////////////////////////////////////////////////////////////////
218 template <typename Generator>
220 binary_test(char const *expected, std::size_t size,
223 namespace karma = boost::spirit::karma;
224 typedef std::basic_string<char> string_type;
226 // we don't care about the result of the "what" function.
227 // we only care that all generators have it:
230 string_type generated;
231 std::back_insert_iterator<string_type> outit(generated);
232 bool result = karma::generate(outit, g);
234 return result && !std::memcmp(generated.c_str(), expected, size);
237 ///////////////////////////////////////////////////////////////////////////
238 template <typename Generator, typename Attribute>
240 binary_test(char const *expected, std::size_t size,
241 Generator const& g, Attribute const &attr)
243 namespace karma = boost::spirit::karma;
244 typedef std::basic_string<char> string_type;
246 // we don't care about the result of the "what" function.
247 // we only care that all generators have it:
250 string_type generated;
251 std::back_insert_iterator<string_type> outit(generated);
252 bool result = karma::generate(outit, g, attr);
254 return result && !std::memcmp(generated.c_str(), expected, size);
257 ///////////////////////////////////////////////////////////////////////////
258 template <typename Generator, typename Delimiter>
260 binary_test_delimited(char const *expected, std::size_t size,
261 Generator const& g, Delimiter const& d)
263 namespace karma = boost::spirit::karma;
264 typedef std::basic_string<char> string_type;
266 // we don't care about the result of the "what" function.
267 // we only care that all generators have it:
270 string_type generated;
271 std::back_insert_iterator<string_type> outit(generated);
272 bool result = karma::generate_delimited(outit, g, d);
274 return result && !std::memcmp(generated.c_str(), expected, size);
277 ///////////////////////////////////////////////////////////////////////////
278 template <typename Generator, typename Attribute, typename Delimiter>
280 binary_test_delimited(char const *expected, std::size_t size,
281 Generator const& g, Attribute const &attr, Delimiter const& d)
283 namespace karma = boost::spirit::karma;
284 typedef std::basic_string<char> string_type;
286 // we don't care about the result of the "what" function.
287 // we only care that all generators have it:
290 string_type generated;
291 std::back_insert_iterator<string_type> outit(generated);
292 bool result = karma::generate_delimited(outit, g, d, attr);
294 return result && !std::memcmp(generated.c_str(), expected, size);
297 } // namespace spirit_test
299 #endif // !BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM