1 /*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2011 Bryce Lelbach
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 #include "uint.hpp"
10 #include <boost/spirit/home/x3.hpp>
11
12 int
main()13 main()
14 {
15 using spirit_test::test;
16 using spirit_test::test_attr;
17 ///////////////////////////////////////////////////////////////////////////
18 // unsigned tests
19 ///////////////////////////////////////////////////////////////////////////
20 {
21 using boost::spirit::x3::uint_;
22 unsigned u;
23
24 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(uint_);
25
26 BOOST_TEST(test("123456", uint_));
27 BOOST_TEST(test_attr("123456", uint_, u));
28 BOOST_TEST(u == 123456);
29
30 BOOST_TEST(test(max_unsigned, uint_));
31 BOOST_TEST(test_attr(max_unsigned, uint_, u));
32 BOOST_TEST(u == UINT_MAX);
33
34 BOOST_TEST(!test(unsigned_overflow, uint_));
35 BOOST_TEST(!test_attr(unsigned_overflow, uint_, u));
36 }
37
38 ///////////////////////////////////////////////////////////////////////////
39 // binary tests
40 ///////////////////////////////////////////////////////////////////////////
41 {
42 using boost::spirit::x3::bin;
43 unsigned u;
44
45 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(bin);
46
47 BOOST_TEST(test("11111110", bin));
48 BOOST_TEST(test_attr("11111110", bin, u));
49 BOOST_TEST(u == 0xFE);
50
51 BOOST_TEST(test(max_binary, bin));
52 BOOST_TEST(test_attr(max_binary, bin, u));
53 BOOST_TEST(u == UINT_MAX);
54
55 BOOST_TEST(!test(binary_overflow, bin));
56 BOOST_TEST(!test_attr(binary_overflow, bin, u));
57 }
58
59 ///////////////////////////////////////////////////////////////////////////
60 // octal tests
61 ///////////////////////////////////////////////////////////////////////////
62 {
63 using boost::spirit::x3::oct;
64 unsigned u;
65
66 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(oct);
67
68 BOOST_TEST(test("12545674515", oct));
69 BOOST_TEST(test_attr("12545674515", oct, u));
70 BOOST_TEST(u == 012545674515);
71
72 BOOST_TEST(test(max_octal, oct));
73 BOOST_TEST(test_attr(max_octal, oct, u));
74 BOOST_TEST(u == UINT_MAX);
75
76 BOOST_TEST(!test(octal_overflow, oct));
77 BOOST_TEST(!test_attr(octal_overflow, oct, u));
78 }
79
80 ///////////////////////////////////////////////////////////////////////////
81 // hex tests
82 ///////////////////////////////////////////////////////////////////////////
83 {
84 using boost::spirit::x3::hex;
85 unsigned u;
86
87 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(hex);
88
89 BOOST_TEST(test("95BC8DF", hex));
90 BOOST_TEST(test_attr("95BC8DF", hex, u));
91 BOOST_TEST(u == 0x95BC8DF);
92
93 BOOST_TEST(test("abcdef12", hex));
94 BOOST_TEST(test_attr("abcdef12", hex, u));
95 BOOST_TEST(u == 0xabcdef12);
96
97 BOOST_TEST(test(max_hex, hex));
98 BOOST_TEST(test_attr(max_hex, hex, u));
99 BOOST_TEST(u == UINT_MAX);
100
101 BOOST_TEST(!test(hex_overflow, hex));
102 BOOST_TEST(!test_attr(hex_overflow, hex, u));
103 }
104
105 ///////////////////////////////////////////////////////////////////////////
106 // limited fieldwidth
107 ///////////////////////////////////////////////////////////////////////////
108 {
109 unsigned u;
110 using boost::spirit::x3::uint_parser;
111
112 constexpr uint_parser<unsigned, 10, 1, 3> uint3{};
113 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(uint3);
114 BOOST_TEST(test("123456", uint3, false));
115 BOOST_TEST(test_attr("123456", uint3, u, false));
116 BOOST_TEST(u == 123);
117
118 constexpr uint_parser<unsigned, 10, 2, 4> uint4{};
119 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(uint4);
120 BOOST_TEST(test("123456", uint4, false));
121 BOOST_TEST(test_attr("123456", uint4, u, false));
122 BOOST_TEST(u == 1234);
123
124 char const * first = "0000000";
125 char const * last = first + std::strlen(first);
126 constexpr uint_parser<unsigned, 10, 4, 4> uint_exact4{};
127 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(uint_exact4);
128 BOOST_TEST(boost::spirit::x3::parse(first, last, uint_exact4, u)
129 && first != last && (last-first == 3) && u == 0);
130
131 first = "0001400";
132 last = first + std::strlen(first);
133 BOOST_TEST(boost::spirit::x3::parse(first, last, uint_exact4, u)
134 && first != last && (last-first == 3) && u == 1);
135
136 BOOST_TEST(!test("1", uint4));
137 BOOST_TEST(!test_attr("1", uint4, u));
138 BOOST_TEST(test_attr("014567", uint4, u, false) && u == 145);
139 }
140
141 ///////////////////////////////////////////////////////////////////////////
142 // action tests
143 ///////////////////////////////////////////////////////////////////////////
144 {
145 using boost::spirit::x3::_attr;
146 using boost::spirit::x3::uint_;
147 using boost::spirit::x3::ascii::space;
148 int n;
149
150 auto f = [&](auto& ctx){ n = _attr(ctx); };
151
152 BOOST_TEST(test("123", uint_[f]));
153 BOOST_TEST(n == 123);
154 BOOST_TEST(test(" 456", uint_[f], space));
155 BOOST_TEST(n == 456);
156 }
157
158 ///////////////////////////////////////////////////////////////////////////
159 // Check overflow is parse error
160 ///////////////////////////////////////////////////////////////////////////
161 {
162 boost::spirit::x3::uint_parser<boost::uint8_t> uint8_;
163 boost::uint8_t u8;
164
165 BOOST_TEST(!test_attr("999", uint8_, u8));
166 BOOST_TEST(!test_attr("256", uint8_, u8));
167 BOOST_TEST(test_attr("255", uint8_, u8));
168
169 boost::spirit::x3::uint_parser<boost::uint16_t> uint16_;
170 boost::uint16_t u16;
171
172 BOOST_TEST(!test_attr("99999", uint16_, u16));
173 BOOST_TEST(!test_attr("65536", uint16_, u16));
174 BOOST_TEST(test_attr("65535", uint16_, u16));
175
176 boost::spirit::x3::uint_parser<boost::uint32_t> uint32_;
177 boost::uint32_t u32;
178
179 BOOST_TEST(!test_attr("9999999999", uint32_, u32));
180 BOOST_TEST(!test_attr("4294967296", uint32_, u32));
181 BOOST_TEST(test_attr("4294967295", uint32_, u32));
182
183 boost::spirit::x3::uint_parser<boost::int8_t> u_int8_;
184
185 BOOST_TEST(!test_attr("999", u_int8_, u8));
186 BOOST_TEST(!test_attr("-1", u_int8_, u8));
187 BOOST_TEST(!test_attr("128", u_int8_, u8));
188 BOOST_TEST(test_attr("127", u_int8_, u8));
189 BOOST_TEST(test_attr("0", u_int8_, u8));
190
191 boost::spirit::x3::uint_parser<boost::int16_t> u_int16_;
192
193 BOOST_TEST(!test_attr("99999", u_int16_, u16));
194 BOOST_TEST(!test_attr("-1", u_int16_, u16));
195 BOOST_TEST(!test_attr("32768", u_int16_, u16));
196 BOOST_TEST(test_attr("32767", u_int16_, u16));
197 BOOST_TEST(test_attr("0", u_int16_, u16));
198
199 boost::spirit::x3::uint_parser<boost::int32_t> u_int32_;
200
201 BOOST_TEST(!test_attr("9999999999", u_int32_, u32));
202 BOOST_TEST(!test_attr("-1", u_int32_, u32));
203 BOOST_TEST(!test_attr("2147483648", u_int32_, u32));
204 BOOST_TEST(test_attr("2147483647", u_int32_, u32));
205 BOOST_TEST(test_attr("0", u_int32_, u32));
206 }
207
208 ///////////////////////////////////////////////////////////////////////////
209 // custom uint tests
210 ///////////////////////////////////////////////////////////////////////////
211 {
212 using boost::spirit::x3::uint_;
213 using boost::spirit::x3::uint_parser;
214 custom_uint u;
215
216 BOOST_TEST(test_attr("123456", uint_, u));
217 uint_parser<custom_uint, 10, 1, 2> uint2;
218 BOOST_TEST(test_attr("12", uint2, u));
219 }
220
221 return boost::report_errors();
222 }
223