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 "int.hpp"
10 #include <boost/spirit/home/x3.hpp>
11 #include <boost/fusion/include/vector.hpp>
12 #include <boost/fusion/include/at.hpp>
13 
14 int
main()15 main()
16 {
17     using spirit_test::test;
18     using spirit_test::test_attr;
19 
20     ///////////////////////////////////////////////////////////////////////////
21     //  signed integer tests
22     ///////////////////////////////////////////////////////////////////////////
23     {
24         using boost::spirit::x3::int_;
25         int i;
26 
27         BOOST_TEST(test("123456", int_));
28         BOOST_TEST(test_attr("123456", int_, i));
29         BOOST_TEST(i == 123456);
30 
31         BOOST_TEST(test("+123456", int_));
32         BOOST_TEST(test_attr("+123456", int_, i));
33         BOOST_TEST(i == 123456);
34 
35         BOOST_TEST(test("-123456", int_));
36         BOOST_TEST(test_attr("-123456", int_, i));
37         BOOST_TEST(i == -123456);
38 
39         BOOST_TEST(test(max_int, int_));
40         BOOST_TEST(test_attr(max_int, int_, i));
41         BOOST_TEST(i == INT_MAX);
42 
43         BOOST_TEST(test(min_int, int_));
44         BOOST_TEST(test_attr(min_int, int_, i));
45         BOOST_TEST(i == INT_MIN);
46 
47         BOOST_TEST(!test(int_overflow, int_));
48         BOOST_TEST(!test_attr(int_overflow, int_, i));
49         BOOST_TEST(!test(int_underflow, int_));
50         BOOST_TEST(!test_attr(int_underflow, int_, i));
51 
52         BOOST_TEST(!test("-", int_));
53         BOOST_TEST(!test_attr("-", int_, i));
54 
55         BOOST_TEST(!test("+", int_));
56         BOOST_TEST(!test_attr("+", int_, i));
57 
58         // Bug report from Steve Nutt
59         BOOST_TEST(!test_attr("5368709120", int_, i));
60 
61         // with leading zeros
62         BOOST_TEST(test("0000000000123456", int_));
63         BOOST_TEST(test_attr("0000000000123456", int_, i));
64         BOOST_TEST(i == 123456);
65     }
66 
67     ///////////////////////////////////////////////////////////////////////////
68     //  long long tests
69     ///////////////////////////////////////////////////////////////////////////
70     {
71         using boost::spirit::x3::long_long;
72         boost::long_long_type ll;
73 
74         BOOST_TEST(test("1234567890123456789", long_long));
75         BOOST_TEST(test_attr("1234567890123456789", long_long, ll));
76         BOOST_TEST(ll == 1234567890123456789LL);
77 
78         BOOST_TEST(test("-1234567890123456789", long_long));
79         BOOST_TEST(test_attr("-1234567890123456789", long_long, ll));
80         BOOST_TEST(ll == -1234567890123456789LL);
81 
82         BOOST_TEST(test(max_long_long, long_long));
83         BOOST_TEST(test_attr(max_long_long, long_long, ll));
84         BOOST_TEST(ll == LLONG_MAX);
85 
86         BOOST_TEST(test(min_long_long, long_long));
87         BOOST_TEST(test_attr(min_long_long, long_long, ll));
88         BOOST_TEST(ll == LLONG_MIN);
89 
90         BOOST_TEST(!test(long_long_overflow, long_long));
91         BOOST_TEST(!test_attr(long_long_overflow, long_long, ll));
92         BOOST_TEST(!test(long_long_underflow, long_long));
93         BOOST_TEST(!test_attr(long_long_underflow, long_long, ll));
94     }
95 
96     ///////////////////////////////////////////////////////////////////////////
97     //  short_ and long_ tests
98     ///////////////////////////////////////////////////////////////////////////
99     {
100         using boost::spirit::x3::short_;
101         using boost::spirit::x3::long_;
102         int i;
103 
104         BOOST_TEST(test("12345", short_));
105         BOOST_TEST(test_attr("12345", short_, i));
106         BOOST_TEST(i == 12345);
107 
108         BOOST_TEST(test("1234567890", long_));
109         BOOST_TEST(test_attr("1234567890", long_, i));
110         BOOST_TEST(i == 1234567890);
111     }
112 
113     ///////////////////////////////////////////////////////////////////////////
114     // Check overflow is parse error
115     ///////////////////////////////////////////////////////////////////////////
116     {
117         boost::spirit::x3::int_parser<boost::int8_t> int8_;
118         char c;
119 
120         BOOST_TEST(!test_attr("999", int8_, c));
121 
122         int i;
123         using boost::spirit::x3::short_;
124         BOOST_TEST(!test_attr("32769", short_, i, false));
125         BOOST_TEST(!test_attr("41234", short_, i, false));
126     }
127 
128     ///////////////////////////////////////////////////////////////////////////
129     //  int_parser<unused_type> tests
130     ///////////////////////////////////////////////////////////////////////////
131     {
132         using boost::spirit::x3::int_parser;
133         using boost::spirit::x3::unused_type;
134         int_parser<unused_type> any_int;
135 
136         BOOST_TEST(test("123456", any_int));
137         BOOST_TEST(test("-123456", any_int));
138         BOOST_TEST(test("-1234567890123456789", any_int));
139     }
140 
141     ///////////////////////////////////////////////////////////////////////////
142     //  action tests
143     ///////////////////////////////////////////////////////////////////////////
144     {
145         using boost::spirit::x3::_attr;
146         using boost::spirit::x3::ascii::space;
147         using boost::spirit::x3::int_;
148         int n = 0, m = 0;
149 
150         auto f = [&](auto& ctx){ n = _attr(ctx); };
151 
152         BOOST_TEST(test("123", int_[f]));
153         BOOST_TEST(n == 123);
154         BOOST_TEST(test_attr("789", int_[f], m));
155         BOOST_TEST(n == 789 && m == 789);
156         BOOST_TEST(test("   456", int_[f], space));
157         BOOST_TEST(n == 456);
158     }
159 
160     ///////////////////////////////////////////////////////////////////////////
161     //  custom int tests
162     ///////////////////////////////////////////////////////////////////////////
163     {
164         using boost::spirit::x3::int_;
165         using boost::spirit::x3::int_parser;
166         custom_int i;
167 
168         BOOST_TEST(test_attr("-123456", int_, i));
169         int_parser<custom_int, 10, 1, 2> int2;
170         BOOST_TEST(test_attr("-12", int2, i));
171     }
172 
173     ///////////////////////////////////////////////////////////////////////////
174     //  single-element fusion vector tests
175     ///////////////////////////////////////////////////////////////////////////
176     {
177         using boost::spirit::x3::int_;
178         using boost::spirit::x3::int_parser;
179         boost::fusion::vector<int> i;
180 
181         BOOST_TEST(test_attr("-123456", int_, i));
182         BOOST_TEST(boost::fusion::at_c<0>(i) == -123456);
183     }
184 
185     return boost::report_errors();
186 }
187