1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2011 Bryce Lelbach
5
6 Use, modification and distribution is subject to the Boost Software
7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10
11 #include "real.hpp"
12
13 int
main()14 main()
15 {
16 using spirit_test::test;
17 using spirit_test::test_attr;
18 ///////////////////////////////////////////////////////////////////////////
19 // signed real number tests
20 ///////////////////////////////////////////////////////////////////////////
21 {
22 using boost::spirit::qi::double_;
23 using boost::spirit::qi::float_;
24 using boost::spirit::qi::parse;
25 double d;
26 float f;
27
28 BOOST_TEST(test("-1234", double_));
29 BOOST_TEST(test_attr("-1234", double_, d) && compare(d, -1234));
30
31 BOOST_TEST(test("-1.2e3", double_));
32 BOOST_TEST(test_attr("-1.2e3", double_, d) && compare(d, -1.2e3));
33
34 BOOST_TEST(test("+1.2e3", double_));
35 BOOST_TEST(test_attr("+1.2e3", double_, d) && compare(d, 1.2e3));
36
37 BOOST_TEST(test("-0.1", double_));
38 BOOST_TEST(test_attr("-0.1", double_, d) && compare(d, -0.1));
39
40 BOOST_TEST(test("-1.2e-3", double_));
41 BOOST_TEST(test_attr("-1.2e-3", double_, d) && compare(d, -1.2e-3));
42
43 BOOST_TEST(test("-1.e2", double_));
44 BOOST_TEST(test_attr("-1.e2", double_, d) && compare(d, -1.e2));
45
46 BOOST_TEST(test("-.2e3", double_));
47 BOOST_TEST(test_attr("-.2e3", double_, d) && compare(d, -.2e3));
48
49 BOOST_TEST(test("-2e3", double_));
50 BOOST_TEST(test_attr("-2e3", double_, d) && compare(d, -2e3));
51
52 BOOST_TEST(!test("-e3", double_));
53 BOOST_TEST(!test_attr("-e3", double_, d));
54
55 BOOST_TEST(!test("-1.2e", double_));
56 BOOST_TEST(!test_attr("-1.2e", double_, d));
57
58 BOOST_TEST(test_attr("-5.7222349715140557e+307", double_, d));
59 BOOST_TEST(d == -5.7222349715140557e+307); // exact!
60
61 BOOST_TEST(test_attr("2.0332938517515416e-308", double_, d));
62 BOOST_TEST(d == 2.0332938517515416e-308); // exact!
63
64 BOOST_TEST(test_attr("20332938517515416e291", double_, d));
65 BOOST_TEST(d == 20332938517515416e291); // exact!
66
67 BOOST_TEST(test_attr("2.0332938517515416e307", double_, d));
68 BOOST_TEST(d == 2.0332938517515416e307); // exact!
69
70 BOOST_TEST(test_attr("1.7976931348623157e+308", double_, d)); // DBL_MAX
71 BOOST_TEST(d == 1.7976931348623157e+308); // exact!
72
73 BOOST_TEST(test_attr("2.2204460492503131e-16", double_, d)); // DBL_EPSILON
74 BOOST_TEST(d == 2.2204460492503131e-16); // exact!
75
76 BOOST_TEST(test_attr("2.2250738585072014e-308", double_, d)); // DBL_MIN
77 BOOST_TEST(d == 2.2250738585072014e-308); // exact!
78
79 BOOST_TEST(test_attr("4.9406564584124654e-324", double_, d)); // DBL_DENORM_MIN
80 BOOST_TEST(d == 4.9406564584124654e-324); // exact!
81
82 BOOST_TEST(test_attr("219721.03839999999", double_, d));
83 BOOST_TEST(d == 219721.03839999999); // exact!
84
85 BOOST_TEST(test_attr("-5.7222349715140557e+307", double_, d));
86 BOOST_TEST(d == -5.7222349715140557e+307); // exact!
87
88 BOOST_TEST(test_attr("2.2204460492503131e-16", double_, d));
89 BOOST_TEST(d == 2.2204460492503131e-16); // exact!
90
91 // floats...
92
93 BOOST_TEST(test_attr("3.402823466e+38", float_, f)); // FLT_MAX
94 BOOST_TEST(f == 3.402823466e+38F); // exact!
95
96 BOOST_TEST(test_attr("1.192092896e-07", float_, f)); // FLT_EPSILON
97 BOOST_TEST(f == 1.192092896e-07F); // exact!
98
99 BOOST_TEST(test_attr("1.175494351e-38", float_, f)); // FLT_MIN
100 BOOST_TEST(f == 1.175494351e-38F); // exact!
101
102 BOOST_TEST(test_attr("219721.03839999999", float_, f));
103 BOOST_TEST(f == 219721.03839999999f); // inexact
104
105 BOOST_TEST(test_attr("2.2204460492503131e-16", float_, f));
106 BOOST_TEST(f == 2.2204460492503131e-16f); // inexact
107
108 // big exponents!
109 // fail, but do not assert!
110 BOOST_TEST(!test_attr("123e1234000000", double_, d));
111 BOOST_TEST(!test_attr("123e-1234000000", double_, d));
112
113 using boost::math::fpclassify;
114 using boost::spirit::detail::signbit; // Boost version is broken
115
116 BOOST_TEST(test("-inf", double_));
117 BOOST_TEST(test("-infinity", double_));
118 BOOST_TEST(test_attr("-inf", double_, d) &&
119 FP_INFINITE == fpclassify(d) && signbit(d));
120 BOOST_TEST(test_attr("-infinity", double_, d) &&
121 FP_INFINITE == fpclassify(d) && signbit(d));
122 BOOST_TEST(test("-INF", double_));
123 BOOST_TEST(test("-INFINITY", double_));
124 BOOST_TEST(test_attr("-INF", double_, d) &&
125 FP_INFINITE == fpclassify(d) && signbit(d));
126 BOOST_TEST(test_attr("-INFINITY", double_, d) &&
127 FP_INFINITE == fpclassify(d) && signbit(d));
128
129 BOOST_TEST(test("-nan", double_));
130 BOOST_TEST(test_attr("-nan", double_, d) &&
131 FP_NAN == fpclassify(d) && signbit(d));
132 BOOST_TEST(test("-NAN", double_));
133 BOOST_TEST(test_attr("-NAN", double_, d) &&
134 FP_NAN == fpclassify(d) && signbit(d));
135
136 BOOST_TEST(test("-nan(...)", double_));
137 BOOST_TEST(test_attr("-nan(...)", double_, d) &&
138 FP_NAN == fpclassify(d) && signbit(d));
139 BOOST_TEST(test("-NAN(...)", double_));
140 BOOST_TEST(test_attr("-NAN(...)", double_, d) &&
141 FP_NAN == fpclassify(d) && signbit(d));
142
143 BOOST_TEST(!test("1e999", double_));
144 BOOST_TEST(!test("1e-999", double_));
145 BOOST_TEST(test_attr("2.1111111e-303", double_, d) &&
146 compare(d, 2.1111111e-303));
147 BOOST_TEST(!test_attr("1.1234e", double_, d) && compare(d, 1.1234));
148
149 // https://svn.boost.org/trac10/ticket/11608
150 BOOST_TEST(test_attr("1267650600228229401496703205376", double_, d) &&
151 compare(d, 1.2676506002282294e+30));
152
153 BOOST_TEST(test_attr("12676506.00228229401496703205376", double_, d) &&
154 compare(d, 12676506.00228229, 0.00000001));
155
156 BOOST_TEST(test_attr("12676506.00228229401496703205376E6", double_, d) &&
157 compare(d, 12676506002282.29, 0.01));
158
159 char const* long_number = "17976931348623157081452742373170435679807056"
160 "7525844996598917476803157260780028538760589558632766878171540458953"
161 "5143824642343213268894641827684675467035375169860499105765512820762"
162 "4549009038932894407586850845513394230458323690322294816580855933212"
163 "3348274797826204144723168738177180919299881250404026184124858368.00"
164 "00000000000000";
165
166 BOOST_TEST(test_attr(long_number, double_, d) &&
167 compare(d, 1.7976931348623157e+308));
168 }
169
170 return boost::report_errors();
171 }
172