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