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 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> 24 25 namespace spirit_test 26 { 27 /////////////////////////////////////////////////////////////////////////// 28 template <typename Char> 29 struct output_iterator 30 { 31 typedef std::basic_string<Char> string_type; 32 typedef std::back_insert_iterator<string_type> type; 33 }; 34 35 /////////////////////////////////////////////////////////////////////////// 36 template <typename Char, typename T> print_if_failed(char const * func,bool result,std::basic_string<Char> const & generated,T const & expected)37 void print_if_failed(char const* func, bool result 38 , std::basic_string<Char> const& generated, T const& expected) 39 { 40 if (!result) 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()) 45 << "\"" << std::endl; 46 } 47 } 48 49 #define BOOST_PP_FILENAME_1 "test_attr.hpp" 50 #define BOOST_PP_ITERATION_LIMITS (1, SPIRIT_ARGUMENTS_LIMIT) 51 #include BOOST_PP_ITERATE() 52 53 #endif 54 55 /////////////////////////////////////////////////////////////////////////////// 56 // 57 // Preprocessor vertical repetition code 58 // 59 /////////////////////////////////////////////////////////////////////////////// 60 #else // defined(BOOST_PP_IS_ITERATING) 61 62 #define N BOOST_PP_ITERATION() 63 64 namespace spirit_test 65 { 66 /////////////////////////////////////////////////////////////////////////// 67 template <typename Char, typename Generator 68 , BOOST_PP_ENUM_PARAMS(N, typename A)> test(Char const * expected,Generator const & g,BOOST_PP_ENUM_BINARY_PARAMS (N,A,const & attr))69 inline bool test(Char const *expected, Generator const& g 70 , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) 71 { 72 namespace karma = boost::spirit::karma; 73 typedef std::basic_string<Char> string_type; 74 75 // we don't care about the result of the "what" function. 76 // we only care that all generators have it: 77 karma::what(g); 78 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)); 82 83 print_if_failed("test", result, generated, expected); 84 return result && generated == expected; 85 } 86 87 /////////////////////////////////////////////////////////////////////////// 88 template <typename Char, typename Generator, typename Delimiter 89 , 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))90 inline bool test_delimited(Char const *expected, Generator const& g 91 , Delimiter const& d, BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) 92 { 93 namespace karma = boost::spirit::karma; 94 typedef std::basic_string<Char> string_type; 95 96 // we don't care about the result of the "what" function. 97 // we only care that all generators have it: 98 karma::what(g); 99 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)); 104 105 print_if_failed("test_delimited", result, generated, expected); 106 return result && generated == expected; 107 } 108 109 /////////////////////////////////////////////////////////////////////////// 110 template <typename Char, typename Generator, typename Delimiter 111 , 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))112 inline bool test_predelimited(Char const *expected, Generator const& g 113 , Delimiter const& d 114 , BOOST_SCOPED_ENUM(boost::spirit::karma::delimit_flag) pre_delimit 115 , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) 116 { 117 namespace karma = boost::spirit::karma; 118 typedef std::basic_string<Char> string_type; 119 120 // we don't care about the result of the "what" function. 121 // we only care that all generators have it: 122 karma::what(g); 123 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)); 128 129 print_if_failed("test_predelimited", result, generated, expected); 130 return result && generated == expected; 131 } 132 133 } // namespace spirit_test 134 135 #undef N 136 137 #endif 138