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