summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/qi/uint1.cpp
blob: 87c2d41502cc8b9808809af43b141e60f6454217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*=============================================================================
    Copyright (c) 2001-2011 Joel de Guzman
    Copyright (c) 2001-2011 Hartmut Kaiser
    Copyright (c) 2011      Bryce Lelbach

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/

#include "uint.hpp"

int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    ///////////////////////////////////////////////////////////////////////////
    //  unsigned tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::qi::uint_;
        unsigned u;

        BOOST_TEST(test("123456", uint_));
        BOOST_TEST(test_attr("123456", uint_, u));
        BOOST_TEST(u == 123456);

        BOOST_TEST(test(max_unsigned, uint_));
        BOOST_TEST(test_attr(max_unsigned, uint_, u));
        BOOST_TEST(u == UINT_MAX);

        BOOST_TEST(!test(unsigned_overflow, uint_));
        BOOST_TEST(!test_attr(unsigned_overflow, uint_, u));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  binary tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::qi::bin;
        unsigned u;

        BOOST_TEST(test("11111110", bin));
        BOOST_TEST(test_attr("11111110", bin, u));
        BOOST_TEST(u == 0xFE);

        BOOST_TEST(test(max_binary, bin));
        BOOST_TEST(test_attr(max_binary, bin, u));
        BOOST_TEST(u == UINT_MAX);

        BOOST_TEST(!test(binary_overflow, bin));
        BOOST_TEST(!test_attr(binary_overflow, bin, u));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  octal tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::qi::oct;
        unsigned u;

        BOOST_TEST(test("12545674515", oct));
        BOOST_TEST(test_attr("12545674515", oct, u));
        BOOST_TEST(u == 012545674515);

        BOOST_TEST(test(max_octal, oct));
        BOOST_TEST(test_attr(max_octal, oct, u));
        BOOST_TEST(u == UINT_MAX);

        BOOST_TEST(!test(octal_overflow, oct));
        BOOST_TEST(!test_attr(octal_overflow, oct, u));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  hex tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::qi::hex;
        unsigned u;

        BOOST_TEST(test("95BC8DF", hex));
        BOOST_TEST(test_attr("95BC8DF", hex, u));
        BOOST_TEST(u == 0x95BC8DF);

        BOOST_TEST(test("abcdef12", hex));
        BOOST_TEST(test_attr("abcdef12", hex, u));
        BOOST_TEST(u == 0xabcdef12);

        BOOST_TEST(test(max_hex, hex));
        BOOST_TEST(test_attr(max_hex, hex, u));
        BOOST_TEST(u == UINT_MAX);

        BOOST_TEST(!test(hex_overflow, hex));
        BOOST_TEST(!test_attr(hex_overflow, hex, u));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  limited fieldwidth
    ///////////////////////////////////////////////////////////////////////////
    {
        unsigned u;
        using boost::spirit::qi::uint_parser;

        uint_parser<unsigned, 10, 1, 3> uint3;
        BOOST_TEST(test("123456", uint3, false));
        BOOST_TEST(test_attr("123456", uint3, u, false));
        BOOST_TEST(u == 123);

        uint_parser<unsigned, 10, 2, 4> uint4;
        BOOST_TEST(test("123456", uint4, false));
        BOOST_TEST(test_attr("123456", uint4, u, false));
        BOOST_TEST(u == 1234);

        char const * first = "0000000";
        char const * last  = first + std::strlen(first);
        uint_parser<unsigned, 10, 4, 4> uint_exact4;
        BOOST_TEST(boost::spirit::qi::parse(first, last, uint_exact4, u)
            && first != last && (last-first == 3) && u == 0);

        first = "0001400";
        last  = first + std::strlen(first);
        BOOST_TEST(boost::spirit::qi::parse(first, last, uint_exact4, u)
            && first != last && (last-first == 3) && u == 1);

        BOOST_TEST(!test("1", uint4));
        BOOST_TEST(!test_attr("1", uint4, u));
        BOOST_TEST(test_attr("014567", uint4, u, false) && u == 145);

        uint_parser<boost::uint8_t, 10, 1, 3> uchar_3;
        unsigned char uc;
        BOOST_TEST(test_attr("255", uchar_3, uc, true));
        BOOST_TEST(uc == 255);
        BOOST_TEST(!test_attr("256", uchar_3, uc, false));
        BOOST_TEST(!test_attr("257", uchar_3, uc, false));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  action tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::phoenix::ref;
        using boost::spirit::qi::_1;
        using boost::spirit::qi::uint_;
        using boost::spirit::ascii::space;
        int n;

        BOOST_TEST(test("123", uint_[ref(n) = _1]));
        BOOST_TEST(n == 123);
        BOOST_TEST(test("   456", uint_[ref(n) = _1], space));
        BOOST_TEST(n == 456);
    }

    ///////////////////////////////////////////////////////////////////////////
    // Check overflow is parse error
    ///////////////////////////////////////////////////////////////////////////
    {
        boost::spirit::qi::uint_parser<boost::uint8_t> uint8_;
        boost::uint8_t u8;

        BOOST_TEST(!test_attr("999", uint8_, u8));
        BOOST_TEST(!test_attr("256", uint8_, u8));
        BOOST_TEST(test_attr("255", uint8_, u8));

        boost::spirit::qi::uint_parser<boost::uint16_t> uint16_;
        boost::uint16_t u16;

        BOOST_TEST(!test_attr("99999", uint16_, u16));
        BOOST_TEST(!test_attr("65536", uint16_, u16));
        BOOST_TEST(test_attr("65535", uint16_, u16));

        boost::spirit::qi::uint_parser<boost::uint32_t> uint32_;
        boost::uint32_t u32;

        BOOST_TEST(!test_attr("9999999999", uint32_, u32));
        BOOST_TEST(!test_attr("4294967296", uint32_, u32));
        BOOST_TEST(test_attr("4294967295", uint32_, u32));

        boost::spirit::qi::uint_parser<boost::int8_t> u_int8_;

        BOOST_TEST(!test_attr("999", u_int8_, u8));
        BOOST_TEST(!test_attr("-1", u_int8_, u8));
        BOOST_TEST(!test_attr("128", u_int8_, u8));
        BOOST_TEST(test_attr("127", u_int8_, u8));
        BOOST_TEST(test_attr("0", u_int8_, u8));

        boost::spirit::qi::uint_parser<boost::int16_t> u_int16_;

        BOOST_TEST(!test_attr("99999", u_int16_, u16));
        BOOST_TEST(!test_attr("-1", u_int16_, u16));
        BOOST_TEST(!test_attr("32768", u_int16_, u16));
        BOOST_TEST(test_attr("32767", u_int16_, u16));
        BOOST_TEST(test_attr("0", u_int16_, u16));

        boost::spirit::qi::uint_parser<boost::int32_t> u_int32_;

        BOOST_TEST(!test_attr("9999999999", u_int32_, u32));
        BOOST_TEST(!test_attr("-1", u_int32_, u32));
        BOOST_TEST(!test_attr("2147483648", u_int32_, u32));
        BOOST_TEST(test_attr("2147483647", u_int32_, u32));
        BOOST_TEST(test_attr("0", u_int32_, u32));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  custom uint tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::qi::uint_;
        using boost::spirit::qi::uint_parser;
        custom_uint u;

        BOOST_TEST(test_attr("123456", uint_, u));
        uint_parser<custom_uint, 10, 1, 2> uint2;
        BOOST_TEST(test_attr("12", uint2, u));
    }

    return boost::report_errors();
}