• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(L"x", wstream, L'x'));
52         BOOST_TEST(test(L"xyz", wstream, L"xyz"));
53         BOOST_TEST(test(L"xyz", wstream, std::basic_string<wchar_t>(L"xyz")));
54         BOOST_TEST(test(L"1", wstream, 1));
55         BOOST_TEST(test(L"1.1", wstream, 1.1));
56         BOOST_TEST(test(L"{1.2,2.4}", wstream, complex(1.2, 2.4)));
57     }
58 
59     {
60         BOOST_TEST(test(L"x", wstream(L'x')));
61         BOOST_TEST(test(L"xyz", wstream(L"xyz")));
62         BOOST_TEST(test(L"xyz", wstream(std::basic_string<wchar_t>(L"xyz"))));
63         BOOST_TEST(test(L"1", wstream(1)));
64         BOOST_TEST(test(L"1.1", wstream(1.1)));
65         BOOST_TEST(test(L"{1.2,2.4}", wstream(complex(1.2, 2.4))));
66     }
67 
68     {
69         using namespace boost::spirit::ascii;
70 
71         BOOST_TEST(test(L"x", lower[wstream], L'X'));
72         BOOST_TEST(test(L"xyz", lower[wstream], L"XYZ"));
73         BOOST_TEST(test(L"xyz", lower[wstream], std::basic_string<wchar_t>(L"XYZ")));
74         BOOST_TEST(test(L"X", upper[wstream], L'x'));
75         BOOST_TEST(test(L"XYZ", upper[wstream], L"xyz"));
76         BOOST_TEST(test(L"XYZ", upper[wstream], std::basic_string<wchar_t>(L"xyz")));
77     }
78 
79     {
80         BOOST_TEST(test_delimited(L"x ", wstream, L'x', L' '));
81         BOOST_TEST(test_delimited(L"xyz ", wstream, L"xyz", L' '));
82         BOOST_TEST(test_delimited(L"xyz ", wstream, std::basic_string<wchar_t>(L"xyz"), L' '));
83         BOOST_TEST(test_delimited(L"1 ", wstream, 1, ' '));
84         BOOST_TEST(test_delimited(L"1.1 ", wstream, 1.1, ' '));
85         BOOST_TEST(test_delimited(L"{1.2,2.4} ", wstream, complex(1.2, 2.4), ' '));
86     }
87 
88     {
89         using namespace boost::spirit::ascii;
90 
91         BOOST_TEST(test_delimited(L"x ", lower[wstream], L'X', L' '));
92         BOOST_TEST(test_delimited(L"xyz ", lower[wstream], L"XYZ", L' '));
93         BOOST_TEST(test_delimited(L"xyz ", lower[wstream], std::basic_string<wchar_t>(L"XYZ"), L' '));
94         BOOST_TEST(test_delimited(L"X ", upper[wstream], L'x', L' '));
95         BOOST_TEST(test_delimited(L"XYZ ", upper[wstream], L"xyz", ' '));
96         BOOST_TEST(test_delimited(L"XYZ ", upper[wstream], std::basic_string<wchar_t>(L"xyz"), L' '));
97     }
98 
99     {   // lazy streams
100         namespace phx = boost::phoenix;
101 
102         std::basic_string<wchar_t> ws(L"abc");
103         BOOST_TEST((test(L"abc", wstream(phx::val(ws)))));
104         BOOST_TEST((test(L"abc", wstream(phx::ref(ws)))));
105     }
106 
107     {
108         boost::optional<wchar_t> c;
109         BOOST_TEST(!test(L"", wstream, c));
110         c = L'x';
111         BOOST_TEST(test(L"x", wstream, c));
112     }
113 
114     return boost::report_errors();
115 }
116