1 /*=============================================================================
2     Copyright (c) 2001-2015 Joel de Guzman
3     Copyright (c) 2001-2011 Hartmut Kaiser
4     Copyright (c)      2019 Christian Mazakas
5 
6     Distributed under the Boost Software License, Version 1.0. (See accompanying
7     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 
10 #define BOOST_SPIRIT_X3_UNICODE
11 
12 #include <boost/detail/lightweight_test.hpp>
13 #include <boost/spirit/home/x3.hpp>
14 
15 #include <boost/utility/string_view.hpp>
16 
17 #include <iostream>
18 #include <vector>
19 #include <algorithm>
20 
21 #include "test.hpp"
22 
23 int
main()24 main()
25 {
26     using spirit_test::test;
27 
28     {
29         using namespace boost::spirit::x3::ascii;
30 
31         BOOST_TEST(test("x", 'x'));
32         BOOST_TEST(test(L"x", L'x'));
33         BOOST_TEST(!test("y", 'x'));
34         BOOST_TEST(!test(L"y", L'x'));
35 
36         BOOST_TEST(test("x", char_));
37         BOOST_TEST(test("x", char_('x')));
38         BOOST_TEST(!test("x", char_('y')));
39         BOOST_TEST(test("x", char_('a', 'z')));
40         BOOST_TEST(!test("x", char_('0', '9')));
41 
42         BOOST_TEST(test("0", char_('0', '9')));
43         BOOST_TEST(test("9", char_('0', '9')));
44         BOOST_TEST(!test("0", ~char_('0', '9')));
45         BOOST_TEST(!test("9", ~char_('0', '9')));
46 
47         BOOST_TEST(!test("x", ~char_));
48         BOOST_TEST(!test("x", ~char_('x')));
49         BOOST_TEST(test(" ", ~char_('x')));
50         BOOST_TEST(test("X", ~char_('x')));
51         BOOST_TEST(!test("x", ~char_('b', 'y')));
52         BOOST_TEST(test("a", ~char_('b', 'y')));
53         BOOST_TEST(test("z", ~char_('b', 'y')));
54 
55         BOOST_TEST(test("x", ~~char_));
56         BOOST_TEST(test("x", ~~char_('x')));
57         BOOST_TEST(!test(" ", ~~char_('x')));
58         BOOST_TEST(!test("X", ~~char_('x')));
59         BOOST_TEST(test("x", ~~char_('b', 'y')));
60         BOOST_TEST(!test("a", ~~char_('b', 'y')));
61         BOOST_TEST(!test("z", ~~char_('b', 'y')));
62     }
63 
64     {
65         using namespace boost::spirit::x3::ascii;
66 
67         BOOST_TEST(test("   x", 'x', space));
68         BOOST_TEST(test(L"   x", L'x', space));
69 
70         BOOST_TEST(test("   x", char_, space));
71         BOOST_TEST(test("   x", char_('x'), space));
72         BOOST_TEST(!test("   x", char_('y'), space));
73         BOOST_TEST(test("   x", char_('a', 'z'), space));
74         BOOST_TEST(!test("   x", char_('0', '9'), space));
75     }
76 
77     {
78         using namespace boost::spirit::x3::standard_wide;
79 
80         BOOST_TEST(test(L"x", char_));
81         BOOST_TEST(test(L"x", char_(L'x')));
82         BOOST_TEST(!test(L"x", char_(L'y')));
83         BOOST_TEST(test(L"x", char_(L'a', L'z')));
84         BOOST_TEST(!test(L"x", char_(L'0', L'9')));
85 
86         BOOST_TEST(!test(L"x", ~char_));
87         BOOST_TEST(!test(L"x", ~char_(L'x')));
88         BOOST_TEST(test(L" ", ~char_(L'x')));
89         BOOST_TEST(test(L"X", ~char_(L'x')));
90         BOOST_TEST(!test(L"x", ~char_(L'b', L'y')));
91         BOOST_TEST(test(L"a", ~char_(L'b', L'y')));
92         BOOST_TEST(test(L"z", ~char_(L'b', L'y')));
93 
94         BOOST_TEST(test(L"x", ~~char_));
95         BOOST_TEST(test(L"x", ~~char_(L'x')));
96         BOOST_TEST(!test(L" ", ~~char_(L'x')));
97         BOOST_TEST(!test(L"X", ~~char_(L'x')));
98         BOOST_TEST(test(L"x", ~~char_(L'b', L'y')));
99         BOOST_TEST(!test(L"a", ~~char_(L'b', L'y')));
100         BOOST_TEST(!test(L"z", ~~char_(L'b', L'y')));
101     }
102 
103     // unicode (normal ASCII)
104     {
105         using namespace boost::spirit::x3::unicode;
106 
107         BOOST_TEST(test(U"abcd", +char_(U"abcd")));
108         BOOST_TEST(!test(U"abcd", +char_(U"qwer")));
109 
110         auto const sub_delims = char_(U"!$&'()*+,;=");
111 
112         auto const delims =
113             std::vector<boost::u32string_view>{U"!", U"$", U"&", U"'", U"(", U")", U"*", U"+",
114                                                U",", U";", U"="};
115 
116         auto const matched_all_sub_delims =
117             std::all_of(delims.begin(), delims.end(), [&](auto const delim) -> bool {
118                 return test(delim, sub_delims);
119             });
120 
121         BOOST_TEST(matched_all_sub_delims);
122     }
123 
124     // unicode (escaped Unicode char literals)
125     {
126         using namespace boost::spirit::x3::unicode;
127 
128         auto const chars = char_(U"\u0024\u00a2\u0939\u20ac\U00010348");
129 
130         auto const test_strings =
131             std::vector<boost::u32string_view>{U"\u0024", U"\u00a2", U"\u0939", U"\u20ac",
132                                                U"\U00010348"};
133 
134         auto const bad_test_strings = std::vector<boost::u32string_view>{U"a", U"B", U"c", U"\u0409"};
135 
136         auto const all_matched =
137             std::all_of(test_strings.begin(), test_strings.end(), [&](auto const test_str) -> bool {
138                 return test(test_str, chars);
139             });
140 
141         auto const none_matched =
142             std::all_of(bad_test_strings.begin(), bad_test_strings.end(), [&](auto const bad_test_str) -> bool {
143                 return !test(bad_test_str, chars);
144             });
145 
146         BOOST_TEST(all_matched);
147         BOOST_TEST(none_matched);
148     }
149 
150 
151     {   // single char strings!
152         namespace ascii = boost::spirit::x3::ascii;
153          namespace wide = boost::spirit::x3::standard_wide;
154 
155         BOOST_TEST(test("x", "x"));
156         BOOST_TEST(test(L"x", L"x"));
157         BOOST_TEST(test("x", ascii::char_("x")));
158         BOOST_TEST(test(L"x", wide::char_(L"x")));
159 
160         BOOST_TEST(test("x", ascii::char_("a", "z")));
161         BOOST_TEST(test(L"x", wide::char_(L"a", L"z")));
162     }
163 
164     {
165         // chsets
166         namespace ascii = boost::spirit::x3::ascii;
167         namespace wide = boost::spirit::x3::standard_wide;
168 
169         BOOST_TEST(test("x", ascii::char_("a-z")));
170         BOOST_TEST(!test("1", ascii::char_("a-z")));
171         BOOST_TEST(test("1", ascii::char_("a-z0-9")));
172 
173         BOOST_TEST(test("x", wide::char_(L"a-z")));
174         BOOST_TEST(!test("1", wide::char_(L"a-z")));
175         BOOST_TEST(test("1", wide::char_(L"a-z0-9")));
176 
177         std::string set = "a-z0-9";
178         BOOST_TEST(test("x", ascii::char_(set)));
179 
180 #ifdef SPIRIT_NO_COMPILE_CHECK
181         test("", ascii::char_(L"a-z0-9"));
182 #endif
183     }
184 
185     {
186         namespace ascii = boost::spirit::x3::ascii;
187         char const* input = "\x80";
188 
189         // ascii > 7 bits (this should fail, not assert!)
190         BOOST_TEST(!test(input, ascii::char_));
191         BOOST_TEST(!test(input, ascii::char_('a')));
192         BOOST_TEST(!test(input, ascii::alnum));
193         BOOST_TEST(!test(input, ascii::char_("a-z")));
194         BOOST_TEST(!test(input, ascii::char_('0', '9')));
195     }
196 
197     return boost::report_errors();
198 }
199