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_MANIP_ATTR_APR_24_2009_0834AM)
9 #define BOOST_SPIRIT_KARMA_TEST_MANIP_ATTR_APR_24_2009_0834AM
10 
11 #include <cstring>
12 #include <string>
13 #include <iterator>
14 #include <iostream>
15 #include <typeinfo>
16 
17 #include <boost/spirit/include/karma_stream.hpp>
18 
19 #include <boost/preprocessor/iterate.hpp>
20 #include <boost/preprocessor/repetition/enum.hpp>
21 #include <boost/preprocessor/repetition/enum_params.hpp>
22 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
23 
24 namespace spirit_test
25 {
26     ///////////////////////////////////////////////////////////////////////////
27     template <typename Char, typename T>
print_if_failed(char const * func,bool result,std::basic_string<Char> const & generated,T const & expected)28     void print_if_failed(char const* func, bool result
29       , std::basic_string<Char> const& generated, T const& expected)
30     {
31         if (!result)
32             std::cerr << "in " << func << ": result is false" << std::endl;
33         else if (generated != expected)
34             std::cerr << "in " << func << ": generated \""
35                 << std::string(generated.begin(), generated.end())
36                 << "\"" << std::endl;
37     }
38 }
39 
40 #define BOOST_PP_FILENAME_1 "karma/test_manip_attr.hpp"
41 #define BOOST_PP_ITERATION_LIMITS (1, SPIRIT_ARGUMENTS_LIMIT)
42 #include BOOST_PP_ITERATE()
43 
44 #endif
45 
46 ///////////////////////////////////////////////////////////////////////////////
47 //
48 //  Preprocessor vertical repetition code
49 //
50 ///////////////////////////////////////////////////////////////////////////////
51 #else // defined(BOOST_PP_IS_ITERATING)
52 
53 #define N BOOST_PP_ITERATION()
54 
55 namespace spirit_test
56 {
57     ///////////////////////////////////////////////////////////////////////////
58     template <typename Char, typename Generator
59       , BOOST_PP_ENUM_PARAMS(N, typename A)>
test(Char const * expected,Generator const & g,BOOST_PP_ENUM_BINARY_PARAMS (N,A,const & attr))60     inline bool test(Char const *expected, Generator const& g
61       , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr))
62     {
63         namespace karma = boost::spirit::karma;
64 
65         std::ostringstream ostrm;
66         ostrm << karma::format(g, BOOST_PP_ENUM_PARAMS(N, attr));
67 
68         print_if_failed("test", ostrm.good(), ostrm.str(), expected);
69         return ostrm.good() && ostrm.str() == expected;
70     }
71 
72     ///////////////////////////////////////////////////////////////////////////
73     template <typename Char, typename Generator, typename Delimiter
74       , BOOST_PP_ENUM_PARAMS(N, typename A)>
test_delimited(Char const * expected,Generator const & g,Delimiter const & d,BOOST_PP_ENUM_BINARY_PARAMS (N,A,const & attr))75     inline bool test_delimited(Char const *expected, Generator const& g
76       , Delimiter const& d, BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr))
77     {
78         namespace karma = boost::spirit::karma;
79 
80         std::ostringstream ostrm;
81         ostrm << karma::format_delimited(g, d, BOOST_PP_ENUM_PARAMS(N, attr));
82 
83         print_if_failed("test_delimited", ostrm.good(), ostrm.str(), expected);
84         return ostrm.good() && ostrm.str() == expected;
85     }
86 
87     ///////////////////////////////////////////////////////////////////////////
88     template <typename Char, typename Generator, typename Delimiter
89       , BOOST_PP_ENUM_PARAMS(N, typename A)>
test_predelimited(Char const * expected,Generator const & g,Delimiter const & d,BOOST_SCOPED_ENUM (boost::spirit::karma::delimit_flag)pre_delimit,BOOST_PP_ENUM_BINARY_PARAMS (N,A,const & attr))90     inline bool test_predelimited(Char const *expected, Generator const& g
91       , Delimiter const& d
92       , BOOST_SCOPED_ENUM(boost::spirit::karma::delimit_flag) pre_delimit
93       , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr))
94     {
95         namespace karma = boost::spirit::karma;
96 
97         std::ostringstream ostrm;
98         ostrm << karma::format_delimited(g, d, pre_delimit
99           , BOOST_PP_ENUM_PARAMS(N, attr));
100 
101         print_if_failed("test_predelimited", ostrm.good(), ostrm.str(), expected);
102         return ostrm.good() && ostrm.str() == expected;
103     }
104 
105 }   // namespace spirit_test
106 
107 #undef N
108 
109 #endif
110