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