stream.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include <cwchar>
  6. #include <streambuf>
  7. #include <iostream>
  8. #include <boost/config/warning_disable.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/cstdint.hpp>
  11. #include <boost/spirit/include/karma_char.hpp>
  12. #include <boost/spirit/include/karma_string.hpp>
  13. #include <boost/spirit/include/karma_stream.hpp>
  14. #include <boost/spirit/include/karma_directive.hpp>
  15. #include <boost/spirit/include/phoenix_core.hpp>
  16. #include <boost/spirit/include/phoenix_operator.hpp>
  17. #include "test.hpp"
  18. using namespace spirit_test;
  19. // a simple complex number representation z = a + bi
  20. struct complex
  21. {
  22. complex (double a, double b)
  23. : a(a), b(b)
  24. {}
  25. double a;
  26. double b;
  27. template <typename Char>
  28. friend std::basic_ostream<Char>&
  29. operator<< (std::basic_ostream<Char>& os, complex z)
  30. {
  31. os << "{" << z.a << "," << z.b << "}";
  32. return os;
  33. }
  34. };
  35. ///////////////////////////////////////////////////////////////////////////////
  36. int
  37. main()
  38. {
  39. using namespace boost::spirit;
  40. {
  41. BOOST_TEST(test("x", stream, 'x'));
  42. BOOST_TEST(test("xyz", stream, "xyz"));
  43. BOOST_TEST(test("xyz", stream, std::string("xyz")));
  44. BOOST_TEST(test("1", stream, 1));
  45. BOOST_TEST(test("1.1", stream, 1.1));
  46. BOOST_TEST(test("{1.2,2.4}", stream, complex(1.2, 2.4)));
  47. }
  48. {
  49. BOOST_TEST(test("x", stream('x')));
  50. BOOST_TEST(test("xyz", stream("xyz")));
  51. BOOST_TEST(test("xyz", stream(std::string("xyz"))));
  52. BOOST_TEST(test("1", stream(1)));
  53. BOOST_TEST(test("1.1", stream(1.1)));
  54. BOOST_TEST(test("{1.2,2.4}", stream(complex(1.2, 2.4))));
  55. }
  56. {
  57. using namespace boost::spirit::ascii;
  58. BOOST_TEST(test("x", lower[stream], 'X'));
  59. BOOST_TEST(test("xyz", lower[stream], "XYZ"));
  60. BOOST_TEST(test("xyz", lower[stream], std::string("XYZ")));
  61. BOOST_TEST(test("X", upper[stream], 'x'));
  62. BOOST_TEST(test("XYZ", upper[stream], "xyz"));
  63. BOOST_TEST(test("XYZ", upper[stream], std::string("xyz")));
  64. }
  65. {
  66. BOOST_TEST(test_delimited("x ", stream, 'x', ' '));
  67. BOOST_TEST(test_delimited("xyz ", stream, "xyz", ' '));
  68. BOOST_TEST(test_delimited("xyz ", stream, std::string("xyz"), ' '));
  69. BOOST_TEST(test_delimited("1 ", stream, 1, ' '));
  70. BOOST_TEST(test_delimited("1.1 ", stream, 1.1, ' '));
  71. BOOST_TEST(test_delimited("{1.2,2.4} ", stream, complex(1.2, 2.4), ' '));
  72. }
  73. {
  74. typedef karma::stream_generator<utf8_char> utf8_stream_type;
  75. utf8_stream_type const utf8_stream = utf8_stream_type();
  76. BOOST_TEST(test_delimited("x ", utf8_stream, 'x', ' '));
  77. BOOST_TEST(test_delimited("xyz ", utf8_stream, "xyz", ' '));
  78. BOOST_TEST(test_delimited("xyz ", utf8_stream, std::string("xyz"), ' '));
  79. BOOST_TEST(test_delimited("1 ", utf8_stream, 1, ' '));
  80. BOOST_TEST(test_delimited("1.1 ", utf8_stream, 1.1, ' '));
  81. BOOST_TEST(test_delimited("{1.2,2.4} ", utf8_stream, complex(1.2, 2.4), ' '));
  82. BOOST_TEST(test("x", utf8_stream('x')));
  83. BOOST_TEST(test("xyz", utf8_stream("xyz")));
  84. BOOST_TEST(test("xyz", utf8_stream(std::string("xyz"))));
  85. BOOST_TEST(test("1", utf8_stream(1)));
  86. BOOST_TEST(test("1.1", utf8_stream(1.1)));
  87. BOOST_TEST(test("{1.2,2.4}", utf8_stream(complex(1.2, 2.4))));
  88. }
  89. {
  90. using namespace boost::spirit::ascii;
  91. BOOST_TEST(test_delimited("x ", lower[stream], 'X', ' '));
  92. BOOST_TEST(test_delimited("xyz ", lower[stream], "XYZ", ' '));
  93. BOOST_TEST(test_delimited("xyz ", lower[stream], std::string("XYZ"), ' '));
  94. BOOST_TEST(test_delimited("X ", upper[stream], 'x', ' '));
  95. BOOST_TEST(test_delimited("XYZ ", upper[stream], "xyz", ' '));
  96. BOOST_TEST(test_delimited("XYZ ", upper[stream], std::string("xyz"), ' '));
  97. }
  98. { // lazy streams
  99. namespace phx = boost::phoenix;
  100. std::basic_string<char> s("abc");
  101. BOOST_TEST((test("abc", stream(phx::val(s)))));
  102. BOOST_TEST((test("abc", stream(phx::ref(s)))));
  103. }
  104. {
  105. boost::optional<char> c;
  106. BOOST_TEST(!test("", stream, c));
  107. c = 'x';
  108. BOOST_TEST(test("x", stream, c));
  109. }
  110. return boost::report_errors();
  111. }