test.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Copyright (c) 2001-2010 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_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM)
  6. #define BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM
  7. #include <cstring>
  8. #include <string>
  9. #include <iterator>
  10. #include <iostream>
  11. #include <typeinfo>
  12. #include <boost/spirit/include/karma_generate.hpp>
  13. #include <boost/spirit/include/karma_what.hpp>
  14. namespace spirit_test
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. struct display_type
  18. {
  19. template<typename T>
  20. void operator()(T const &) const
  21. {
  22. std::cout << typeid(T).name() << std::endl;
  23. }
  24. template<typename T>
  25. static void print()
  26. {
  27. std::cout << typeid(T).name() << std::endl;
  28. }
  29. };
  30. display_type const display = {};
  31. ///////////////////////////////////////////////////////////////////////////
  32. template <typename Char>
  33. struct output_iterator
  34. {
  35. typedef std::basic_string<Char> string_type;
  36. typedef std::back_insert_iterator<string_type> type;
  37. };
  38. ///////////////////////////////////////////////////////////////////////////
  39. template <typename Char, typename T>
  40. void print_if_failed(char const* func, bool result
  41. , std::basic_string<Char> const& generated, T const& expected)
  42. {
  43. if (!result)
  44. std::cerr << "in " << func << ": result is false" << std::endl;
  45. else if (generated != expected)
  46. std::cerr << "in " << func << ": generated \""
  47. << std::string(generated.begin(), generated.end())
  48. << "\"" << std::endl;
  49. }
  50. ///////////////////////////////////////////////////////////////////////////
  51. template <typename Char, typename Generator>
  52. inline bool test(Char const *expected, Generator const& g)
  53. {
  54. namespace karma = boost::spirit::karma;
  55. typedef std::basic_string<Char> string_type;
  56. // we don't care about the result of the "what" function.
  57. // we only care that all generators have it:
  58. karma::what(g);
  59. string_type generated;
  60. std::back_insert_iterator<string_type> outit(generated);
  61. bool result = karma::generate(outit, g);
  62. print_if_failed("test", result, generated, expected);
  63. return result && generated == expected;
  64. }
  65. template <typename Char, typename Generator>
  66. inline bool test(std::basic_string<Char> const& expected, Generator const& g)
  67. {
  68. namespace karma = boost::spirit::karma;
  69. typedef std::basic_string<Char> string_type;
  70. // we don't care about the result of the "what" function.
  71. // we only care that all generators have it:
  72. karma::what(g);
  73. string_type generated;
  74. std::back_insert_iterator<string_type> outit(generated);
  75. bool result = karma::generate(outit, g);
  76. print_if_failed("test", result, generated, expected);
  77. return result && generated == expected;
  78. }
  79. ///////////////////////////////////////////////////////////////////////////
  80. template <typename Char, typename Generator, typename Attribute>
  81. inline bool test(Char const *expected, Generator const& g,
  82. Attribute const &attr)
  83. {
  84. namespace karma = boost::spirit::karma;
  85. typedef std::basic_string<Char> string_type;
  86. // we don't care about the result of the "what" function.
  87. // we only care that all generators have it:
  88. karma::what(g);
  89. string_type generated;
  90. std::back_insert_iterator<string_type> outit(generated);
  91. bool result = karma::generate(outit, g, attr);
  92. print_if_failed("test", result, generated, expected);
  93. return result && generated == expected;
  94. }
  95. template <typename Char, typename Generator, typename Attribute>
  96. inline bool test(std::basic_string<Char> const& expected, Generator const& g,
  97. Attribute const &attr)
  98. {
  99. namespace karma = boost::spirit::karma;
  100. typedef std::basic_string<Char> string_type;
  101. // we don't care about the result of the "what" function.
  102. // we only care that all generators have it:
  103. karma::what(g);
  104. string_type generated;
  105. std::back_insert_iterator<string_type> outit(generated);
  106. bool result = karma::generate(outit, g, attr);
  107. print_if_failed("test", result, generated, expected);
  108. return result && generated == expected;
  109. }
  110. ///////////////////////////////////////////////////////////////////////////
  111. template <typename Char, typename Generator, typename Delimiter>
  112. inline bool test_delimited(Char const *expected, Generator const& g,
  113. Delimiter const& d)
  114. {
  115. namespace karma = boost::spirit::karma;
  116. typedef std::basic_string<Char> string_type;
  117. // we don't care about the result of the "what" function.
  118. // we only care that all generators have it:
  119. karma::what(g);
  120. string_type generated;
  121. std::back_insert_iterator<string_type> outit(generated);
  122. bool result = karma::generate_delimited(outit, g, d);
  123. print_if_failed("test_delimited", result, generated, expected);
  124. return result && generated == expected;
  125. }
  126. template <typename Char, typename Generator, typename Delimiter>
  127. inline bool test_delimited(std::basic_string<Char> const& expected,
  128. Generator const& g, Delimiter const& d)
  129. {
  130. namespace karma = boost::spirit::karma;
  131. typedef std::basic_string<Char> string_type;
  132. // we don't care about the result of the "what" function.
  133. // we only care that all generators have it:
  134. karma::what(g);
  135. string_type generated;
  136. std::back_insert_iterator<string_type> outit(generated);
  137. bool result = karma::generate_delimited(outit, g, d);
  138. print_if_failed("test_delimited", result, generated, expected);
  139. return result && generated == expected;
  140. }
  141. ///////////////////////////////////////////////////////////////////////////
  142. template <typename Char, typename Generator, typename Attribute,
  143. typename Delimiter>
  144. inline bool test_delimited(Char const *expected, Generator const& g,
  145. Attribute const &attr, Delimiter const& d)
  146. {
  147. namespace karma = boost::spirit::karma;
  148. typedef std::basic_string<Char> string_type;
  149. // we don't care about the result of the "what" function.
  150. // we only care that all generators have it:
  151. karma::what(g);
  152. string_type generated;
  153. std::back_insert_iterator<string_type> outit(generated);
  154. bool result = karma::generate_delimited(outit, g, d, attr);
  155. print_if_failed("test_delimited", result, generated, expected);
  156. return result && generated == expected;
  157. }
  158. template <typename Char, typename Generator, typename Attribute,
  159. typename Delimiter>
  160. inline bool test_delimited(std::basic_string<Char> const& expected,
  161. Generator const& g, Attribute const &attr, Delimiter const& d)
  162. {
  163. namespace karma = boost::spirit::karma;
  164. typedef std::basic_string<Char> string_type;
  165. // we don't care about the result of the "what" function.
  166. // we only care that all generators have it:
  167. karma::what(g);
  168. string_type generated;
  169. std::back_insert_iterator<string_type> outit(generated);
  170. bool result = karma::generate_delimited(outit, g, d, attr);
  171. print_if_failed("test_delimited", result, generated, expected);
  172. return result && generated == expected;
  173. }
  174. ///////////////////////////////////////////////////////////////////////////
  175. template <typename Generator>
  176. inline bool
  177. binary_test(char const *expected, std::size_t size,
  178. Generator const& g)
  179. {
  180. namespace karma = boost::spirit::karma;
  181. typedef std::basic_string<char> string_type;
  182. // we don't care about the result of the "what" function.
  183. // we only care that all generators have it:
  184. karma::what(g);
  185. string_type generated;
  186. std::back_insert_iterator<string_type> outit(generated);
  187. bool result = karma::generate(outit, g);
  188. return result && !std::memcmp(generated.c_str(), expected, size);
  189. }
  190. ///////////////////////////////////////////////////////////////////////////
  191. template <typename Generator, typename Attribute>
  192. inline bool
  193. binary_test(char const *expected, std::size_t size,
  194. Generator const& g, Attribute const &attr)
  195. {
  196. namespace karma = boost::spirit::karma;
  197. typedef std::basic_string<char> string_type;
  198. // we don't care about the result of the "what" function.
  199. // we only care that all generators have it:
  200. karma::what(g);
  201. string_type generated;
  202. std::back_insert_iterator<string_type> outit(generated);
  203. bool result = karma::generate(outit, g, attr);
  204. return result && !std::memcmp(generated.c_str(), expected, size);
  205. }
  206. ///////////////////////////////////////////////////////////////////////////
  207. template <typename Generator, typename Delimiter>
  208. inline bool
  209. binary_test_delimited(char const *expected, std::size_t size,
  210. Generator const& g, Delimiter const& d)
  211. {
  212. namespace karma = boost::spirit::karma;
  213. typedef std::basic_string<char> string_type;
  214. // we don't care about the result of the "what" function.
  215. // we only care that all generators have it:
  216. karma::what(g);
  217. string_type generated;
  218. std::back_insert_iterator<string_type> outit(generated);
  219. bool result = karma::generate_delimited(outit, g, d);
  220. return result && !std::memcmp(generated.c_str(), expected, size);
  221. }
  222. ///////////////////////////////////////////////////////////////////////////
  223. template <typename Generator, typename Attribute, typename Delimiter>
  224. inline bool
  225. binary_test_delimited(char const *expected, std::size_t size,
  226. Generator const& g, Attribute const &attr, Delimiter const& d)
  227. {
  228. namespace karma = boost::spirit::karma;
  229. typedef std::basic_string<char> string_type;
  230. // we don't care about the result of the "what" function.
  231. // we only care that all generators have it:
  232. karma::what(g);
  233. string_type generated;
  234. std::back_insert_iterator<string_type> outit(generated);
  235. bool result = karma::generate_delimited(outit, g, d, attr);
  236. return result && !std::memcmp(generated.c_str(), expected, size);
  237. }
  238. } // namespace spirit_test
  239. #endif // !BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM