1 //  Copyright (c) 2001-2010 Hartmut Kaiser
2 //
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)
5 
6 #if !defined(BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM)
7 #define BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM
8 
9 #include <cstring>
10 #include <string>
11 #include <iterator>
12 #include <iostream>
13 #include <typeinfo>
14 
15 #include <boost/spirit/include/karma_generate.hpp>
16 #include <boost/spirit/include/karma_what.hpp>
17 
18 namespace spirit_test
19 {
20     ///////////////////////////////////////////////////////////////////////////
21     struct display_type
22     {
23         template<typename T>
operator ()spirit_test::display_type24         void operator()(T const &) const
25         {
26             std::cout << typeid(T).name() << std::endl;
27         }
28 
29         template<typename T>
printspirit_test::display_type30         static void print()
31         {
32             std::cout << typeid(T).name() << std::endl;
33         }
34     };
35 
36     display_type const display = {};
37 
38     ///////////////////////////////////////////////////////////////////////////
39     template <typename Char>
40     struct output_iterator
41     {
42         typedef std::basic_string<Char> string_type;
43         typedef std::back_insert_iterator<string_type> type;
44     };
45 
46     ///////////////////////////////////////////////////////////////////////////
47     template <typename Char, typename T>
print_if_failed(char const * func,bool result,std::basic_string<Char> const & generated,T const & expected)48     void print_if_failed(char const* func, bool result
49       , std::basic_string<Char> const& generated, T const& expected)
50     {
51         if (!result)
52             std::cerr << "in " << func << ": result is false" << std::endl;
53         else if (generated != expected)
54             std::cerr << "in " << func << ": generated \""
55                 << std::string(generated.begin(), generated.end())
56                 << "\"" << std::endl;
57     }
58 
59     ///////////////////////////////////////////////////////////////////////////
60     template <typename Char, typename Generator>
test(Char const * expected,Generator const & g)61     inline bool test(Char const *expected, Generator const& g)
62     {
63         namespace karma = boost::spirit::karma;
64         typedef std::basic_string<Char> string_type;
65 
66         // we don't care about the result of the "what" function.
67         // we only care that all generators have it:
68         karma::what(g);
69 
70         string_type generated;
71         std::back_insert_iterator<string_type> outit(generated);
72         bool result = karma::generate(outit, g);
73 
74         print_if_failed("test", result, generated, expected);
75         return result && generated == expected;
76     }
77 
78     template <typename Char, typename Generator>
test(std::basic_string<Char> const & expected,Generator const & g)79     inline bool test(std::basic_string<Char> const& expected, Generator const& g)
80     {
81         namespace karma = boost::spirit::karma;
82         typedef std::basic_string<Char> string_type;
83 
84         // we don't care about the result of the "what" function.
85         // we only care that all generators have it:
86         karma::what(g);
87 
88         string_type generated;
89         std::back_insert_iterator<string_type> outit(generated);
90         bool result = karma::generate(outit, g);
91 
92         print_if_failed("test", result, generated, expected);
93         return result && generated == expected;
94     }
95 
96     ///////////////////////////////////////////////////////////////////////////
97     template <typename Char, typename Generator, typename Attribute>
test(Char const * expected,Generator const & g,Attribute const & attr)98     inline bool test(Char const *expected, Generator const& g,
99         Attribute const &attr)
100     {
101         namespace karma = boost::spirit::karma;
102         typedef std::basic_string<Char> string_type;
103 
104         // we don't care about the result of the "what" function.
105         // we only care that all generators have it:
106         karma::what(g);
107 
108         string_type generated;
109         std::back_insert_iterator<string_type> outit(generated);
110         bool result = karma::generate(outit, g, attr);
111 
112         print_if_failed("test", result, generated, expected);
113         return result && generated == expected;
114     }
115 
116     template <typename Char, typename Generator, typename Attribute>
test(std::basic_string<Char> const & expected,Generator const & g,Attribute const & attr)117     inline bool test(std::basic_string<Char> const& expected, Generator const& g,
118         Attribute const &attr)
119     {
120         namespace karma = boost::spirit::karma;
121         typedef std::basic_string<Char> string_type;
122 
123         // we don't care about the result of the "what" function.
124         // we only care that all generators have it:
125         karma::what(g);
126 
127         string_type generated;
128         std::back_insert_iterator<string_type> outit(generated);
129         bool result = karma::generate(outit, g, attr);
130 
131         print_if_failed("test", result, generated, expected);
132         return result && generated == expected;
133     }
134 
135     ///////////////////////////////////////////////////////////////////////////
136     template <typename Char, typename Generator, typename Delimiter>
test_delimited(Char const * expected,Generator const & g,Delimiter const & d)137     inline bool test_delimited(Char const *expected, Generator const& g,
138         Delimiter const& d)
139     {
140         namespace karma = boost::spirit::karma;
141         typedef std::basic_string<Char> string_type;
142 
143         // we don't care about the result of the "what" function.
144         // we only care that all generators have it:
145         karma::what(g);
146 
147         string_type generated;
148         std::back_insert_iterator<string_type> outit(generated);
149         bool result = karma::generate_delimited(outit, g, d);
150 
151         print_if_failed("test_delimited", result, generated, expected);
152         return result && generated == expected;
153     }
154 
155     template <typename Char, typename Generator, typename Delimiter>
test_delimited(std::basic_string<Char> const & expected,Generator const & g,Delimiter const & d)156     inline bool test_delimited(std::basic_string<Char> const& expected,
157         Generator const& g, Delimiter const& d)
158     {
159         namespace karma = boost::spirit::karma;
160         typedef std::basic_string<Char> string_type;
161 
162         // we don't care about the result of the "what" function.
163         // we only care that all generators have it:
164         karma::what(g);
165 
166         string_type generated;
167         std::back_insert_iterator<string_type> outit(generated);
168         bool result = karma::generate_delimited(outit, g, d);
169 
170         print_if_failed("test_delimited", result, generated, expected);
171         return result && generated == expected;
172     }
173 
174     ///////////////////////////////////////////////////////////////////////////
175     template <typename Char, typename Generator, typename Attribute,
176         typename Delimiter>
test_delimited(Char const * expected,Generator const & g,Attribute const & attr,Delimiter const & d)177     inline bool test_delimited(Char const *expected, Generator const& g,
178         Attribute const &attr, Delimiter const& d)
179     {
180         namespace karma = boost::spirit::karma;
181         typedef std::basic_string<Char> string_type;
182 
183         // we don't care about the result of the "what" function.
184         // we only care that all generators have it:
185         karma::what(g);
186 
187         string_type generated;
188         std::back_insert_iterator<string_type> outit(generated);
189         bool result = karma::generate_delimited(outit, g, d, attr);
190 
191         print_if_failed("test_delimited", result, generated, expected);
192         return result && generated == expected;
193     }
194 
195     template <typename Char, typename Generator, typename Attribute,
196         typename Delimiter>
test_delimited(std::basic_string<Char> const & expected,Generator const & g,Attribute const & attr,Delimiter const & d)197     inline bool test_delimited(std::basic_string<Char> const& expected,
198         Generator const& g, Attribute const &attr, Delimiter const& d)
199     {
200         namespace karma = boost::spirit::karma;
201         typedef std::basic_string<Char> string_type;
202 
203         // we don't care about the result of the "what" function.
204         // we only care that all generators have it:
205         karma::what(g);
206 
207         string_type generated;
208         std::back_insert_iterator<string_type> outit(generated);
209         bool result = karma::generate_delimited(outit, g, d, attr);
210 
211         print_if_failed("test_delimited", result, generated, expected);
212         return result && generated == expected;
213     }
214 
215     ///////////////////////////////////////////////////////////////////////////
216     template <typename Generator>
217     inline bool
binary_test(char const * expected,std::size_t size,Generator const & g)218     binary_test(char const *expected, std::size_t size,
219         Generator const& g)
220     {
221         namespace karma = boost::spirit::karma;
222         typedef std::basic_string<char> string_type;
223 
224         // we don't care about the result of the "what" function.
225         // we only care that all generators have it:
226         karma::what(g);
227 
228         string_type generated;
229         std::back_insert_iterator<string_type> outit(generated);
230         bool result = karma::generate(outit, g);
231 
232         return result && !std::memcmp(generated.c_str(), expected, size);
233     }
234 
235     ///////////////////////////////////////////////////////////////////////////
236     template <typename Generator, typename Attribute>
237     inline bool
binary_test(char const * expected,std::size_t size,Generator const & g,Attribute const & attr)238     binary_test(char const *expected, std::size_t size,
239         Generator const& g, Attribute const &attr)
240     {
241         namespace karma = boost::spirit::karma;
242         typedef std::basic_string<char> string_type;
243 
244         // we don't care about the result of the "what" function.
245         // we only care that all generators have it:
246         karma::what(g);
247 
248         string_type generated;
249         std::back_insert_iterator<string_type> outit(generated);
250         bool result = karma::generate(outit, g, attr);
251 
252         return result && !std::memcmp(generated.c_str(), expected, size);
253     }
254 
255     ///////////////////////////////////////////////////////////////////////////
256     template <typename Generator, typename Delimiter>
257     inline bool
binary_test_delimited(char const * expected,std::size_t size,Generator const & g,Delimiter const & d)258     binary_test_delimited(char const *expected, std::size_t size,
259         Generator const& g, Delimiter const& d)
260     {
261         namespace karma = boost::spirit::karma;
262         typedef std::basic_string<char> string_type;
263 
264         // we don't care about the result of the "what" function.
265         // we only care that all generators have it:
266         karma::what(g);
267 
268         string_type generated;
269         std::back_insert_iterator<string_type> outit(generated);
270         bool result = karma::generate_delimited(outit, g, d);
271 
272         return result && !std::memcmp(generated.c_str(), expected, size);
273     }
274 
275     ///////////////////////////////////////////////////////////////////////////
276     template <typename Generator, typename Attribute, typename Delimiter>
277     inline bool
binary_test_delimited(char const * expected,std::size_t size,Generator const & g,Attribute const & attr,Delimiter const & d)278     binary_test_delimited(char const *expected, std::size_t size,
279         Generator const& g, Attribute const &attr, Delimiter const& d)
280     {
281         namespace karma = boost::spirit::karma;
282         typedef std::basic_string<char> string_type;
283 
284         // we don't care about the result of the "what" function.
285         // we only care that all generators have it:
286         karma::what(g);
287 
288         string_type generated;
289         std::back_insert_iterator<string_type> outit(generated);
290         bool result = karma::generate_delimited(outit, g, d, attr);
291 
292         return result && !std::memcmp(generated.c_str(), expected, size);
293     }
294 
295 }   // namespace spirit_test
296 
297 #endif // !BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM
298