]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2011 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_PP_IS_ITERATING) | |
7 | ||
8 | #if !defined(BOOST_SPIRIT_KARMA_TEST_ATTR_APR_23_2009_0605PM) | |
9 | #define BOOST_SPIRIT_KARMA_TEST_ATTR_APR_23_2009_0605PM | |
10 | ||
11 | #include <cstring> | |
12 | #include <string> | |
13 | #include <iterator> | |
14 | #include <iostream> | |
15 | #include <typeinfo> | |
16 | ||
17 | #include <boost/spirit/include/karma_generate.hpp> | |
18 | #include <boost/spirit/include/karma_what.hpp> | |
19 | ||
1e59de90 | 20 | #include <boost/core/lightweight_test.hpp> |
7c673cae FG |
21 | #include <boost/preprocessor/iterate.hpp> |
22 | #include <boost/preprocessor/repetition/enum.hpp> | |
23 | #include <boost/preprocessor/repetition/enum_params.hpp> | |
24 | #include <boost/preprocessor/repetition/enum_binary_params.hpp> | |
25 | ||
26 | namespace spirit_test | |
27 | { | |
28 | /////////////////////////////////////////////////////////////////////////// | |
29 | template <typename Char> | |
30 | struct output_iterator | |
31 | { | |
32 | typedef std::basic_string<Char> string_type; | |
33 | typedef std::back_insert_iterator<string_type> type; | |
34 | }; | |
35 | ||
36 | /////////////////////////////////////////////////////////////////////////// | |
37 | template <typename Char, typename T> | |
38 | void print_if_failed(char const* func, bool result | |
39 | , std::basic_string<Char> const& generated, T const& expected) | |
40 | { | |
41 | if (!result) | |
42 | std::cerr << "in " << func << ": result is false" << std::endl; | |
43 | else if (generated != expected) | |
44 | std::cerr << "in " << func << ": generated \"" | |
45 | << std::string(generated.begin(), generated.end()) | |
46 | << "\"" << std::endl; | |
47 | } | |
48 | } | |
49 | ||
11fdf7f2 | 50 | #define BOOST_PP_FILENAME_1 "test_attr.hpp" |
7c673cae FG |
51 | #define BOOST_PP_ITERATION_LIMITS (1, SPIRIT_ARGUMENTS_LIMIT) |
52 | #include BOOST_PP_ITERATE() | |
53 | ||
54 | #endif | |
55 | ||
56 | /////////////////////////////////////////////////////////////////////////////// | |
57 | // | |
58 | // Preprocessor vertical repetition code | |
59 | // | |
60 | /////////////////////////////////////////////////////////////////////////////// | |
61 | #else // defined(BOOST_PP_IS_ITERATING) | |
62 | ||
63 | #define N BOOST_PP_ITERATION() | |
64 | ||
65 | namespace spirit_test | |
66 | { | |
67 | /////////////////////////////////////////////////////////////////////////// | |
68 | template <typename Char, typename Generator | |
69 | , BOOST_PP_ENUM_PARAMS(N, typename A)> | |
70 | inline bool test(Char const *expected, Generator const& g | |
71 | , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) | |
72 | { | |
73 | namespace karma = boost::spirit::karma; | |
74 | typedef std::basic_string<Char> string_type; | |
75 | ||
76 | // we don't care about the result of the "what" function. | |
77 | // we only care that all generators have it: | |
78 | karma::what(g); | |
79 | ||
80 | string_type generated; | |
81 | std::back_insert_iterator<string_type> outit(generated); | |
82 | bool result = karma::generate(outit, g, BOOST_PP_ENUM_PARAMS(N, attr)); | |
83 | ||
84 | print_if_failed("test", result, generated, expected); | |
85 | return result && generated == expected; | |
86 | } | |
87 | ||
88 | /////////////////////////////////////////////////////////////////////////// | |
89 | template <typename Char, typename Generator, typename Delimiter | |
90 | , BOOST_PP_ENUM_PARAMS(N, typename A)> | |
91 | inline bool test_delimited(Char const *expected, Generator const& g | |
92 | , Delimiter const& d, BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) | |
93 | { | |
94 | namespace karma = boost::spirit::karma; | |
95 | typedef std::basic_string<Char> string_type; | |
96 | ||
97 | // we don't care about the result of the "what" function. | |
98 | // we only care that all generators have it: | |
99 | karma::what(g); | |
100 | ||
101 | string_type generated; | |
102 | std::back_insert_iterator<string_type> outit(generated); | |
103 | bool result = karma::generate_delimited(outit, g, d | |
104 | , BOOST_PP_ENUM_PARAMS(N, attr)); | |
105 | ||
106 | print_if_failed("test_delimited", result, generated, expected); | |
107 | return result && generated == expected; | |
108 | } | |
109 | ||
110 | /////////////////////////////////////////////////////////////////////////// | |
111 | template <typename Char, typename Generator, typename Delimiter | |
112 | , BOOST_PP_ENUM_PARAMS(N, typename A)> | |
113 | inline bool test_predelimited(Char const *expected, Generator const& g | |
114 | , Delimiter const& d | |
115 | , BOOST_SCOPED_ENUM(boost::spirit::karma::delimit_flag) pre_delimit | |
116 | , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) | |
117 | { | |
118 | namespace karma = boost::spirit::karma; | |
119 | typedef std::basic_string<Char> string_type; | |
120 | ||
121 | // we don't care about the result of the "what" function. | |
122 | // we only care that all generators have it: | |
123 | karma::what(g); | |
124 | ||
125 | string_type generated; | |
126 | std::back_insert_iterator<string_type> outit(generated); | |
127 | bool result = karma::generate_delimited(outit, g, d | |
128 | , pre_delimit, BOOST_PP_ENUM_PARAMS(N, attr)); | |
129 | ||
130 | print_if_failed("test_predelimited", result, generated, expected); | |
131 | return result && generated == expected; | |
132 | } | |
133 | ||
134 | } // namespace spirit_test | |
135 | ||
136 | #undef N | |
137 | ||
138 | #endif |