1 /*=============================================================================
2 Copyright (c) 2001-2010 Joel de Guzman
3 Copyright (c) 2001-2010 Hartmut Kaiser
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8
9 #define BOOST_SPIRIT_UNICODE
10
11 #include <boost/detail/lightweight_test.hpp>
12 #include <boost/spirit/include/qi_char.hpp>
13 #include <boost/spirit/include/qi_action.hpp>
14 #include <boost/spirit/include/support_argument.hpp>
15 #include <boost/spirit/include/support_attributes.hpp>
16 #include <boost/spirit/include/phoenix_core.hpp>
17 #include <boost/spirit/include/phoenix_operator.hpp>
18 #include <boost/type_traits/is_same.hpp>
19 #include <boost/static_assert.hpp>
20
21 #include <iostream>
22 #include "test.hpp"
23
24 int
main()25 main()
26 {
27 using spirit_test::test;
28 using spirit_test::test_attr;
29
30 {
31 using namespace boost::spirit::ascii;
32 BOOST_TEST(test("1", alnum));
33 BOOST_TEST(!test(" ", alnum));
34 BOOST_TEST(!test("1", alpha));
35 BOOST_TEST(test("x", alpha));
36 BOOST_TEST(test(" ", blank));
37 BOOST_TEST(!test("x", blank));
38 BOOST_TEST(test("1", digit));
39 BOOST_TEST(!test("x", digit));
40 BOOST_TEST(test("a", lower));
41 BOOST_TEST(!test("A", lower));
42 BOOST_TEST(test("!", punct));
43 BOOST_TEST(!test("x", punct));
44 BOOST_TEST(test(" ", space));
45 BOOST_TEST(test("\n", space));
46 BOOST_TEST(test("\r", space));
47 BOOST_TEST(test("\t", space));
48 BOOST_TEST(test("A", upper));
49 BOOST_TEST(!test("a", upper));
50 BOOST_TEST(test("A", xdigit));
51 BOOST_TEST(test("0", xdigit));
52 BOOST_TEST(test("f", xdigit));
53 BOOST_TEST(!test("g", xdigit));
54
55 // should fail, not assert!
56 // $$$ Removing this test for now $$$
57 // BOOST_TEST(!test("\265", space));
58 }
59
60 {
61 using namespace boost::spirit::ascii;
62 BOOST_TEST(!test("1", ~alnum));
63 BOOST_TEST(test(" ", ~alnum));
64 BOOST_TEST(test("1", ~alpha));
65 BOOST_TEST(!test("x", ~alpha));
66 BOOST_TEST(!test(" ", ~blank));
67 BOOST_TEST(test("x", ~blank));
68 BOOST_TEST(!test("1", ~digit));
69 BOOST_TEST(test("x", ~digit));
70 BOOST_TEST(!test("a", ~lower));
71 BOOST_TEST(test("A", ~lower));
72 BOOST_TEST(!test("!", ~punct));
73 BOOST_TEST(test("x", ~punct));
74 BOOST_TEST(!test(" ", ~space));
75 BOOST_TEST(!test("\n", ~space));
76 BOOST_TEST(!test("\r", ~space));
77 BOOST_TEST(!test("\t", ~space));
78 BOOST_TEST(!test("A", ~upper));
79 BOOST_TEST(test("a", ~upper));
80 BOOST_TEST(!test("A", ~xdigit));
81 BOOST_TEST(!test("0", ~xdigit));
82 BOOST_TEST(!test("f", ~xdigit));
83 BOOST_TEST(test("g", ~xdigit));
84 }
85
86 {
87 // we use the hoisted qi namespace this time
88 using namespace boost::spirit::qi::iso8859_1;
89 BOOST_TEST(test("1", alnum));
90 BOOST_TEST(!test(" ", alnum));
91 BOOST_TEST(!test("1", alpha));
92 BOOST_TEST(test("x", alpha));
93 BOOST_TEST(test(" ", blank));
94 BOOST_TEST(!test("x", blank));
95 BOOST_TEST(test("1", digit));
96 BOOST_TEST(!test("x", digit));
97 BOOST_TEST(test("a", lower));
98 BOOST_TEST(!test("A", lower));
99 BOOST_TEST(test("!", punct));
100 BOOST_TEST(!test("x", punct));
101 BOOST_TEST(test(" ", space));
102 BOOST_TEST(test("\n", space));
103 BOOST_TEST(test("\r", space));
104 BOOST_TEST(test("\t", space));
105 BOOST_TEST(test("A", upper));
106 BOOST_TEST(!test("a", upper));
107 BOOST_TEST(test("A", xdigit));
108 BOOST_TEST(test("0", xdigit));
109 BOOST_TEST(test("f", xdigit));
110 BOOST_TEST(!test("g", xdigit));
111
112 // test extended ASCII characters
113 BOOST_TEST(test("\xE9", alpha));
114 BOOST_TEST(test("\xE9", lower));
115 BOOST_TEST(!test("\xE9", upper));
116 }
117
118 {
119 using namespace boost::spirit::standard;
120 BOOST_TEST(test("1", alnum));
121 BOOST_TEST(!test(" ", alnum));
122 BOOST_TEST(!test("1", alpha));
123 BOOST_TEST(test("x", alpha));
124 BOOST_TEST(test(" ", blank));
125 BOOST_TEST(!test("x", blank));
126 BOOST_TEST(test("1", digit));
127 BOOST_TEST(!test("x", digit));
128 BOOST_TEST(test("a", lower));
129 BOOST_TEST(!test("A", lower));
130 BOOST_TEST(test("!", punct));
131 BOOST_TEST(!test("x", punct));
132 BOOST_TEST(test(" ", space));
133 BOOST_TEST(test("\n", space));
134 BOOST_TEST(test("\r", space));
135 BOOST_TEST(test("\t", space));
136 BOOST_TEST(test("A", upper));
137 BOOST_TEST(!test("a", upper));
138 BOOST_TEST(test("A", xdigit));
139 BOOST_TEST(test("0", xdigit));
140 BOOST_TEST(test("f", xdigit));
141 BOOST_TEST(!test("g", xdigit));
142 }
143
144 {
145 using namespace boost::spirit::standard_wide;
146 BOOST_TEST(test(L"1", alnum));
147 BOOST_TEST(!test(L" ", alnum));
148 BOOST_TEST(!test(L"1", alpha));
149 BOOST_TEST(test(L"x", alpha));
150 BOOST_TEST(test(L" ", blank));
151 BOOST_TEST(!test(L"x", blank));
152 BOOST_TEST(test(L"1", digit));
153 BOOST_TEST(!test(L"x", digit));
154 BOOST_TEST(test(L"a", lower));
155 BOOST_TEST(!test(L"A", lower));
156 BOOST_TEST(test(L"!", punct));
157 BOOST_TEST(!test(L"x", punct));
158 BOOST_TEST(test(L" ", space));
159 BOOST_TEST(test(L"\n", space));
160 BOOST_TEST(test(L"\r", space));
161 BOOST_TEST(test(L"\t", space));
162 BOOST_TEST(test(L"A", upper));
163 BOOST_TEST(!test(L"a", upper));
164 BOOST_TEST(test(L"A", xdigit));
165 BOOST_TEST(test(L"0", xdigit));
166 BOOST_TEST(test(L"f", xdigit));
167 BOOST_TEST(!test(L"g", xdigit));
168 }
169
170 {
171 using namespace boost::spirit::unicode;
172 BOOST_TEST(test(L"1", alnum));
173 BOOST_TEST(!test(L" ", alnum));
174 BOOST_TEST(!test(L"1", alpha));
175 BOOST_TEST(test(L"x", alpha));
176 BOOST_TEST(test(L" ", blank));
177 BOOST_TEST(!test(L"x", blank));
178 BOOST_TEST(test(L"1", digit));
179 BOOST_TEST(!test(L"x", digit));
180 BOOST_TEST(test(L"a", lower));
181 BOOST_TEST(!test(L"A", lower));
182 BOOST_TEST(test(L"!", punct));
183 BOOST_TEST(!test(L"x", punct));
184 BOOST_TEST(test(L" ", space));
185 BOOST_TEST(test(L"\n", space));
186 BOOST_TEST(test(L"\r", space));
187 BOOST_TEST(test(L"\t", space));
188 BOOST_TEST(test(L"A", upper));
189 BOOST_TEST(!test(L"a", upper));
190 BOOST_TEST(test(L"A", xdigit));
191 BOOST_TEST(test(L"0", xdigit));
192 BOOST_TEST(test(L"f", xdigit));
193 BOOST_TEST(!test(L"g", xdigit));
194 }
195
196 { // test attribute extraction
197 using boost::spirit::qi::domain;
198 using boost::spirit::traits::attribute_of;
199 using boost::spirit::iso8859_1::alpha;
200 using boost::spirit::iso8859_1::alpha_type;
201 using boost::spirit::result_of::compile;
202
203 BOOST_STATIC_ASSERT((
204 boost::is_same<
205 attribute_of<compile<domain, alpha_type>::type>::type
206 , unsigned char>::value));
207
208 int attr = 0;
209 BOOST_TEST(test_attr("a", alpha, attr));
210 BOOST_TEST(attr == 'a');
211 }
212
213 { // test attribute extraction
214 using boost::spirit::iso8859_1::alpha;
215 using boost::spirit::iso8859_1::space;
216 char attr = 0;
217 BOOST_TEST(test_attr(" a", alpha, attr, space));
218 BOOST_TEST(attr == 'a');
219 }
220
221 { // test action
222
223 using namespace boost::spirit::ascii;
224 using boost::phoenix::ref;
225 using boost::spirit::_1;
226 char ch;
227
228 BOOST_TEST(test("x", alnum[ref(ch) = _1]));
229 BOOST_TEST(ch == 'x');
230 BOOST_TEST(test(" A", alnum[ref(ch) = _1], space));
231 BOOST_TEST(ch == 'A');
232 }
233
234 return boost::report_errors();
235 }
236