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