test_attr.hpp 4.8 KB

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