| 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 | #include <boost/phoenix/core.hpp> |
| 12 | #include <boost/phoenix/operator.hpp> |
| 13 | |
| 14 | #include "test.hpp" |
| 15 | |
| 16 | #include <boost/cstdint.hpp> |
| 17 | #include <cwchar> |
| 18 | #include <streambuf> |
| 19 | #include <iostream> |
| 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("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 | { |
| 58 | BOOST_TEST(test("x" , stream('x'))); |
| 59 | BOOST_TEST(test("xyz" , stream("xyz" ))); |
| 60 | BOOST_TEST(test("xyz" , stream(std::string("xyz" )))); |
| 61 | BOOST_TEST(test("1" , stream(1))); |
| 62 | BOOST_TEST(test("1.1" , stream(1.1))); |
| 63 | BOOST_TEST(test("{1.2,2.4}" , stream(complex(1.2, 2.4)))); |
| 64 | } |
| 65 | |
| 66 | { |
| 67 | using namespace boost::spirit::ascii; |
| 68 | |
| 69 | BOOST_TEST(test("x" , lower[stream], 'X')); |
| 70 | BOOST_TEST(test("xyz" , lower[stream], "XYZ" )); |
| 71 | BOOST_TEST(test("xyz" , lower[stream], std::string("XYZ" ))); |
| 72 | BOOST_TEST(test("X" , upper[stream], 'x')); |
| 73 | BOOST_TEST(test("XYZ" , upper[stream], "xyz" )); |
| 74 | BOOST_TEST(test("XYZ" , upper[stream], std::string("xyz" ))); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | BOOST_TEST(test_delimited("x " , stream, 'x', ' ')); |
| 79 | BOOST_TEST(test_delimited("xyz " , stream, "xyz" , ' ')); |
| 80 | BOOST_TEST(test_delimited("xyz " , stream, std::string("xyz" ), ' ')); |
| 81 | BOOST_TEST(test_delimited("1 " , stream, 1, ' ')); |
| 82 | BOOST_TEST(test_delimited("1.1 " , stream, 1.1, ' ')); |
| 83 | BOOST_TEST(test_delimited("{1.2,2.4} " , stream, complex(1.2, 2.4), ' ')); |
| 84 | } |
| 85 | |
| 86 | { |
| 87 | typedef karma::stream_generator<utf8_char> utf8_stream_type; |
| 88 | utf8_stream_type const utf8_stream = utf8_stream_type(); |
| 89 | |
| 90 | BOOST_TEST(test_delimited("x " , utf8_stream, 'x', ' ')); |
| 91 | BOOST_TEST(test_delimited("xyz " , utf8_stream, "xyz" , ' ')); |
| 92 | BOOST_TEST(test_delimited("xyz " , utf8_stream, std::string("xyz" ), ' ')); |
| 93 | BOOST_TEST(test_delimited("1 " , utf8_stream, 1, ' ')); |
| 94 | BOOST_TEST(test_delimited("1.1 " , utf8_stream, 1.1, ' ')); |
| 95 | BOOST_TEST(test_delimited("{1.2,2.4} " , utf8_stream, complex(1.2, 2.4), ' ')); |
| 96 | |
| 97 | BOOST_TEST(test("x" , utf8_stream('x'))); |
| 98 | BOOST_TEST(test("xyz" , utf8_stream("xyz" ))); |
| 99 | BOOST_TEST(test("xyz" , utf8_stream(std::string("xyz" )))); |
| 100 | BOOST_TEST(test("1" , utf8_stream(1))); |
| 101 | BOOST_TEST(test("1.1" , utf8_stream(1.1))); |
| 102 | BOOST_TEST(test("{1.2,2.4}" , utf8_stream(complex(1.2, 2.4)))); |
| 103 | } |
| 104 | |
| 105 | { |
| 106 | using namespace boost::spirit::ascii; |
| 107 | |
| 108 | BOOST_TEST(test_delimited("x " , lower[stream], 'X', ' ')); |
| 109 | BOOST_TEST(test_delimited("xyz " , lower[stream], "XYZ" , ' ')); |
| 110 | BOOST_TEST(test_delimited("xyz " , lower[stream], std::string("XYZ" ), ' ')); |
| 111 | BOOST_TEST(test_delimited("X " , upper[stream], 'x', ' ')); |
| 112 | BOOST_TEST(test_delimited("XYZ " , upper[stream], "xyz" , ' ')); |
| 113 | BOOST_TEST(test_delimited("XYZ " , upper[stream], std::string("xyz" ), ' ')); |
| 114 | } |
| 115 | |
| 116 | { // lazy streams |
| 117 | namespace phx = boost::phoenix; |
| 118 | |
| 119 | std::basic_string<char> s("abc" ); |
| 120 | BOOST_TEST((test("abc" , stream(phx::val(s))))); |
| 121 | BOOST_TEST((test("abc" , stream(phx::ref(s))))); |
| 122 | } |
| 123 | |
| 124 | { |
| 125 | boost::optional<char> c; |
| 126 | BOOST_TEST(!test("" , stream, c)); |
| 127 | c = 'x'; |
| 128 | BOOST_TEST(test("x" , stream, c)); |
| 129 | } |
| 130 | |
| 131 | return boost::report_errors(); |
| 132 | } |
| 133 | |