1 // Copyright (c) 2001-2011 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_PP_IS_ITERATING)
8 #if !defined(BOOST_SPIRIT_KARMA_TEST_ATTR_APR_23_2009_0605PM)
9 #define BOOST_SPIRIT_KARMA_TEST_ATTR_APR_23_2009_0605PM
17 #include <boost/spirit/include/karma_generate.hpp>
18 #include <boost/spirit/include/karma_what.hpp>
20 #include <boost/preprocessor/iterate.hpp>
21 #include <boost/preprocessor/repetition/enum.hpp>
22 #include <boost/preprocessor/repetition/enum_params.hpp>
23 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
27 ///////////////////////////////////////////////////////////////////////////
28 template <typename Char>
29 struct output_iterator
31 typedef std::basic_string<Char> string_type;
32 typedef std::back_insert_iterator<string_type> type;
35 ///////////////////////////////////////////////////////////////////////////
36 template <typename Char, typename T>
37 void print_if_failed(char const* func, bool result
38 , std::basic_string<Char> const& generated, T const& expected)
41 std::cerr << "in " << func << ": result is false" << std::endl;
42 else if (generated != expected)
43 std::cerr << "in " << func << ": generated \""
44 << std::string(generated.begin(), generated.end())
49 #define BOOST_PP_FILENAME_1 "karma/test_attr.hpp"
50 #define BOOST_PP_ITERATION_LIMITS (1, SPIRIT_ARGUMENTS_LIMIT)
51 #include BOOST_PP_ITERATE()
55 ///////////////////////////////////////////////////////////////////////////////
57 // Preprocessor vertical repetition code
59 ///////////////////////////////////////////////////////////////////////////////
60 #else // defined(BOOST_PP_IS_ITERATING)
62 #define N BOOST_PP_ITERATION()
66 ///////////////////////////////////////////////////////////////////////////
67 template <typename Char, typename Generator
68 , BOOST_PP_ENUM_PARAMS(N, typename A)>
69 inline bool test(Char const *expected, Generator const& g
70 , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr))
72 namespace karma = boost::spirit::karma;
73 typedef std::basic_string<Char> string_type;
75 // we don't care about the result of the "what" function.
76 // we only care that all generators have it:
79 string_type generated;
80 std::back_insert_iterator<string_type> outit(generated);
81 bool result = karma::generate(outit, g, BOOST_PP_ENUM_PARAMS(N, attr));
83 print_if_failed("test", result, generated, expected);
84 return result && generated == expected;
87 ///////////////////////////////////////////////////////////////////////////
88 template <typename Char, typename Generator, typename Delimiter
89 , BOOST_PP_ENUM_PARAMS(N, typename A)>
90 inline bool test_delimited(Char const *expected, Generator const& g
91 , Delimiter const& d, BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr))
93 namespace karma = boost::spirit::karma;
94 typedef std::basic_string<Char> string_type;
96 // we don't care about the result of the "what" function.
97 // we only care that all generators have it:
100 string_type generated;
101 std::back_insert_iterator<string_type> outit(generated);
102 bool result = karma::generate_delimited(outit, g, d
103 , BOOST_PP_ENUM_PARAMS(N, attr));
105 print_if_failed("test_delimited", result, generated, expected);
106 return result && generated == expected;
109 ///////////////////////////////////////////////////////////////////////////
110 template <typename Char, typename Generator, typename Delimiter
111 , BOOST_PP_ENUM_PARAMS(N, typename A)>
112 inline bool test_predelimited(Char const *expected, Generator const& g
114 , BOOST_SCOPED_ENUM(boost::spirit::karma::delimit_flag) pre_delimit
115 , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr))
117 namespace karma = boost::spirit::karma;
118 typedef std::basic_string<Char> string_type;
120 // we don't care about the result of the "what" function.
121 // we only care that all generators have it:
124 string_type generated;
125 std::back_insert_iterator<string_type> outit(generated);
126 bool result = karma::generate_delimited(outit, g, d
127 , pre_delimit, BOOST_PP_ENUM_PARAMS(N, attr));
129 print_if_failed("test_predelimited", result, generated, expected);
130 return result && generated == expected;
133 } // namespace spirit_test