wstream.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(L"x", wstream, L'x'));
  42. BOOST_TEST(test(L"xyz", wstream, L"xyz"));
  43. BOOST_TEST(test(L"xyz", wstream, std::basic_string<wchar_t>(L"xyz")));
  44. BOOST_TEST(test(L"1", wstream, 1));
  45. BOOST_TEST(test(L"1.1", wstream, 1.1));
  46. BOOST_TEST(test(L"{1.2,2.4}", wstream, complex(1.2, 2.4)));
  47. }
  48. {
  49. BOOST_TEST(test(L"x", wstream(L'x')));
  50. BOOST_TEST(test(L"xyz", wstream(L"xyz")));
  51. BOOST_TEST(test(L"xyz", wstream(std::basic_string<wchar_t>(L"xyz"))));
  52. BOOST_TEST(test(L"1", wstream(1)));
  53. BOOST_TEST(test(L"1.1", wstream(1.1)));
  54. BOOST_TEST(test(L"{1.2,2.4}", wstream(complex(1.2, 2.4))));
  55. }
  56. {
  57. using namespace boost::spirit::ascii;
  58. BOOST_TEST(test(L"x", lower[wstream], L'X'));
  59. BOOST_TEST(test(L"xyz", lower[wstream], L"XYZ"));
  60. BOOST_TEST(test(L"xyz", lower[wstream], std::basic_string<wchar_t>(L"XYZ")));
  61. BOOST_TEST(test(L"X", upper[wstream], L'x'));
  62. BOOST_TEST(test(L"XYZ", upper[wstream], L"xyz"));
  63. BOOST_TEST(test(L"XYZ", upper[wstream], std::basic_string<wchar_t>(L"xyz")));
  64. }
  65. {
  66. BOOST_TEST(test_delimited(L"x ", wstream, L'x', L' '));
  67. BOOST_TEST(test_delimited(L"xyz ", wstream, L"xyz", L' '));
  68. BOOST_TEST(test_delimited(L"xyz ", wstream, std::basic_string<wchar_t>(L"xyz"), L' '));
  69. BOOST_TEST(test_delimited(L"1 ", wstream, 1, ' '));
  70. BOOST_TEST(test_delimited(L"1.1 ", wstream, 1.1, ' '));
  71. BOOST_TEST(test_delimited(L"{1.2,2.4} ", wstream, complex(1.2, 2.4), ' '));
  72. }
  73. {
  74. using namespace boost::spirit::ascii;
  75. BOOST_TEST(test_delimited(L"x ", lower[wstream], L'X', L' '));
  76. BOOST_TEST(test_delimited(L"xyz ", lower[wstream], L"XYZ", L' '));
  77. BOOST_TEST(test_delimited(L"xyz ", lower[wstream], std::basic_string<wchar_t>(L"XYZ"), L' '));
  78. BOOST_TEST(test_delimited(L"X ", upper[wstream], L'x', L' '));
  79. BOOST_TEST(test_delimited(L"XYZ ", upper[wstream], L"xyz", ' '));
  80. BOOST_TEST(test_delimited(L"XYZ ", upper[wstream], std::basic_string<wchar_t>(L"xyz"), L' '));
  81. }
  82. { // lazy streams
  83. namespace phx = boost::phoenix;
  84. std::basic_string<wchar_t> ws(L"abc");
  85. BOOST_TEST((test(L"abc", wstream(phx::val(ws)))));
  86. BOOST_TEST((test(L"abc", wstream(phx::ref(ws)))));
  87. }
  88. {
  89. boost::optional<wchar_t> c;
  90. BOOST_TEST(!test(L"", wstream, c));
  91. c = L'x';
  92. BOOST_TEST(test(L"x", wstream, c));
  93. }
  94. return boost::report_errors();
  95. }