| 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 "int.hpp" |
| 11 | |
| 12 | int |
| 13 | main() |
| 14 | { |
| 15 | using spirit_test::test; |
| 16 | using spirit_test::test_attr; |
| 17 | |
| 18 | /////////////////////////////////////////////////////////////////////////// |
| 19 | // signed integer tests |
| 20 | /////////////////////////////////////////////////////////////////////////// |
| 21 | { |
| 22 | using boost::spirit::int_; |
| 23 | int i; |
| 24 | |
| 25 | BOOST_TEST(test("123456" , int_)); |
| 26 | BOOST_TEST(test_attr("123456" , int_, i)); |
| 27 | BOOST_TEST(i == 123456); |
| 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(max_int, int_)); |
| 38 | BOOST_TEST(test_attr(max_int, int_, i)); |
| 39 | BOOST_TEST(i == INT_MAX); |
| 40 | |
| 41 | BOOST_TEST(test(min_int, int_)); |
| 42 | BOOST_TEST(test_attr(min_int, int_, i)); |
| 43 | BOOST_TEST(i == INT_MIN); |
| 44 | |
| 45 | BOOST_TEST(!test(int_overflow, int_)); |
| 46 | BOOST_TEST(!test_attr(int_overflow, int_, i)); |
| 47 | BOOST_TEST(!test(int_underflow, int_)); |
| 48 | BOOST_TEST(!test_attr(int_underflow, int_, i)); |
| 49 | |
| 50 | BOOST_TEST(!test("-" , int_)); |
| 51 | BOOST_TEST(!test_attr("-" , int_, i)); |
| 52 | |
| 53 | BOOST_TEST(!test("+" , int_)); |
| 54 | BOOST_TEST(!test_attr("+" , int_, i)); |
| 55 | |
| 56 | // Bug report from Steve Nutt |
| 57 | BOOST_TEST(!test_attr("5368709120" , int_, i)); |
| 58 | |
| 59 | // with leading zeros |
| 60 | BOOST_TEST(test("0000000000123456" , int_)); |
| 61 | BOOST_TEST(test_attr("0000000000123456" , int_, i)); |
| 62 | BOOST_TEST(i == 123456); |
| 63 | } |
| 64 | |
| 65 | /////////////////////////////////////////////////////////////////////////// |
| 66 | // long long tests |
| 67 | /////////////////////////////////////////////////////////////////////////// |
| 68 | #ifdef BOOST_HAS_LONG_LONG |
| 69 | { |
| 70 | using boost::spirit::long_long; |
| 71 | boost::long_long_type ll; |
| 72 | |
| 73 | BOOST_TEST(test("1234567890123456789" , long_long)); |
| 74 | BOOST_TEST(test_attr("1234567890123456789" , long_long, ll)); |
| 75 | BOOST_TEST(ll == 1234567890123456789LL); |
| 76 | |
| 77 | BOOST_TEST(test("-1234567890123456789" , long_long)); |
| 78 | BOOST_TEST(test_attr("-1234567890123456789" , long_long, ll)); |
| 79 | BOOST_TEST(ll == -1234567890123456789LL); |
| 80 | |
| 81 | BOOST_TEST(test(max_long_long, long_long)); |
| 82 | BOOST_TEST(test_attr(max_long_long, long_long, ll)); |
| 83 | BOOST_TEST(ll == LONG_LONG_MAX); |
| 84 | |
| 85 | BOOST_TEST(test(min_long_long, long_long)); |
| 86 | BOOST_TEST(test_attr(min_long_long, long_long, ll)); |
| 87 | BOOST_TEST(ll == LONG_LONG_MIN); |
| 88 | |
| 89 | BOOST_TEST(!test(long_long_overflow, long_long)); |
| 90 | BOOST_TEST(!test_attr(long_long_overflow, long_long, ll)); |
| 91 | BOOST_TEST(!test(long_long_underflow, long_long)); |
| 92 | BOOST_TEST(!test_attr(long_long_underflow, long_long, ll)); |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | /////////////////////////////////////////////////////////////////////////// |
| 97 | // short_ and long_ tests |
| 98 | /////////////////////////////////////////////////////////////////////////// |
| 99 | { |
| 100 | using boost::spirit::short_; |
| 101 | using boost::spirit::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::qi::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::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::qi::int_parser; |
| 133 | using boost::spirit::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::phoenix::ref; |
| 146 | using boost::spirit::_1; |
| 147 | using boost::spirit::ascii::space; |
| 148 | using boost::spirit::int_; |
| 149 | int n = 0, m = 0; |
| 150 | |
| 151 | BOOST_TEST(test("123" , int_[ref(n) = _1])); |
| 152 | BOOST_TEST(n == 123); |
| 153 | BOOST_TEST(test_attr("789" , int_[ref(n) = _1], m)); |
| 154 | BOOST_TEST(n == 789 && m == 789); |
| 155 | BOOST_TEST(test(" 456" , int_[ref(n) = _1], space)); |
| 156 | BOOST_TEST(n == 456); |
| 157 | } |
| 158 | |
| 159 | /////////////////////////////////////////////////////////////////////////// |
| 160 | // custom int tests |
| 161 | /////////////////////////////////////////////////////////////////////////// |
| 162 | { |
| 163 | using boost::spirit::qi::int_; |
| 164 | using boost::spirit::qi::int_parser; |
| 165 | custom_int i; |
| 166 | |
| 167 | BOOST_TEST(test_attr("-123456" , int_, i)); |
| 168 | int_parser<custom_int, 10, 1, 2> int2; |
| 169 | BOOST_TEST(test_attr("-12" , int2, i)); |
| 170 | } |
| 171 | |
| 172 | return boost::report_errors(); |
| 173 | } |
| 174 | |