1// Copyright (c) 2001-2020 Hartmut Kaiser
2// Copyright (c) 2011 Bryce Lelbach
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7#include "real.hpp"
8
9#include <boost/spirit/include/version.hpp>
10#include <boost/spirit/include/karma_phoenix_attributes.hpp>
11#include <boost/phoenix/core.hpp>
12#include <boost/phoenix/operator.hpp>
13
14///////////////////////////////////////////////////////////////////////////////
15#ifndef BOOST_SPIRIT_NO_MATH_REAL_CONCEPT
16// does not provide proper std::numeric_limits, we need to roll our own
17namespace boost { namespace spirit { namespace traits
18{
19 template <>
20 struct is_nan<boost::math::concepts::real_concept>
21 {
22 static bool call(boost::math::concepts::real_concept n)
23 {
24 return test_nan(n: n.value());
25 }
26 };
27
28 template <>
29 struct is_infinite<boost::math::concepts::real_concept>
30 {
31 static bool call(boost::math::concepts::real_concept n)
32 {
33 return test_infinite(n: n.value());
34 }
35 };
36}}}
37#endif
38
39struct double_prec16_policy : boost::spirit::karma::real_policies<double>
40{
41 static unsigned int precision(double) { return 16; }
42};
43
44///////////////////////////////////////////////////////////////////////////////
45int main()
46{
47 using namespace boost::spirit;
48
49 {
50 using namespace boost::spirit::ascii;
51
52 ///////////////////////////////////////////////////////////////////////
53 typedef karma::real_generator<double, fixed_policy<double> > fixed_type;
54 fixed_type const fixed = fixed_type();
55
56 BOOST_TEST(test("0.0", fixed, 0.0));
57 BOOST_TEST(test("1.0", fixed, 1.0));
58
59 BOOST_TEST(test("0.0", fixed, 0.000012345));
60 BOOST_TEST(test("0.0", fixed, 0.00012345));
61 BOOST_TEST(test("0.001", fixed, 0.0012345));
62 BOOST_TEST(test("0.012", fixed, 0.012345));
63 BOOST_TEST(test("0.123", fixed, 0.12345));
64 BOOST_TEST(test("1.234", fixed, 1.2345));
65 BOOST_TEST(test("12.345", fixed, 12.345));
66 BOOST_TEST(test("123.45", fixed, 123.45));
67 BOOST_TEST(test("1234.5", fixed, 1234.5));
68 BOOST_TEST(test("12342.0", fixed, 12342.));
69 BOOST_TEST(test("123420.0", fixed, 123420.));
70 BOOST_TEST(test("123420000000000000000.0", fixed, 1.23420e20));
71
72 BOOST_TEST(test("0.0", fixed, -0.000012345));
73 BOOST_TEST(test("0.0", fixed, -0.00012345));
74 BOOST_TEST(test("-0.001", fixed, -0.0012345));
75 BOOST_TEST(test("-0.012", fixed, -0.012345));
76 BOOST_TEST(test("-0.123", fixed, -0.12345));
77 BOOST_TEST(test("-1.234", fixed, -1.2345));
78 BOOST_TEST(test("-12.346", fixed, -12.346));
79 BOOST_TEST(test("-123.46", fixed, -123.46));
80 BOOST_TEST(test("-1234.5", fixed, -1234.5));
81 BOOST_TEST(test("-12342.0", fixed, -12342.));
82 BOOST_TEST(test("-123420.0", fixed, -123420.));
83 BOOST_TEST(test("-123420000000000000000.0", fixed, -1.23420e20));
84 }
85
86#ifndef BOOST_SPIRIT_NO_MATH_REAL_CONCEPT
87 {
88 using boost::math::concepts::real_concept;
89 typedef karma::real_generator<real_concept> custom_type;
90 custom_type const custom = custom_type();
91
92 BOOST_TEST(test("0.0", custom, real_concept(0.0)));
93 BOOST_TEST(test("1.0", custom, real_concept(1.0)));
94 BOOST_TEST(test("1.0", custom, real_concept(1.0001)));
95 BOOST_TEST(test("1.001", custom, real_concept(1.001)));
96 BOOST_TEST(test("1.01", custom, real_concept(1.010)));
97 BOOST_TEST(test("1.1", custom, real_concept(1.100)));
98
99 BOOST_TEST(test("1.234e-04", custom, real_concept(0.00012345)));
100 BOOST_TEST(test("0.001", custom, real_concept(0.0012345)));
101 BOOST_TEST(test("0.012", custom, real_concept(0.012345)));
102 BOOST_TEST(test("0.123", custom, real_concept(0.12345)));
103 BOOST_TEST(test("1.234", custom, real_concept(1.2345)));
104 BOOST_TEST(test("12.346", custom, real_concept(12.346)));
105 BOOST_TEST(test("123.46", custom, real_concept(123.46)));
106 BOOST_TEST(test("1234.5", custom, real_concept(1234.5)));
107 BOOST_TEST(test("12342.0", custom, real_concept(12342.)));
108 BOOST_TEST(test("1.234e05", custom, real_concept(123420.)));
109
110 BOOST_TEST(test("-1.0", custom, real_concept(-1.0)));
111 BOOST_TEST(test("-1.234", custom, real_concept(-1.2345)));
112 BOOST_TEST(test("-1.235", custom, real_concept(-1.2346)));
113 BOOST_TEST(test("-1234.2", custom, real_concept(-1234.2)));
114
115 BOOST_TEST(test("1.0", custom(real_concept(1.0))));
116 BOOST_TEST(test("1.0", custom(real_concept(1.0001))));
117 BOOST_TEST(test("1.001", custom(real_concept(1.001))));
118 BOOST_TEST(test("1.01", custom(real_concept(1.010))));
119 BOOST_TEST(test("1.1", custom(real_concept(1.100))));
120
121 BOOST_TEST(test("1.234e-04", custom(real_concept(0.00012345))));
122 BOOST_TEST(test("0.001", custom(real_concept(0.0012345))));
123 BOOST_TEST(test("0.012", custom(real_concept(0.012345))));
124 BOOST_TEST(test("0.123", custom(real_concept(0.12345))));
125 BOOST_TEST(test("1.234", custom(real_concept(1.2345))));
126 BOOST_TEST(test("12.346", custom(real_concept(12.346))));
127 BOOST_TEST(test("123.46", custom(real_concept(123.46))));
128 BOOST_TEST(test("1234.5", custom(real_concept(1234.5))));
129 BOOST_TEST(test("12342.0", custom(real_concept(12342.))));
130 BOOST_TEST(test("1.234e05", custom(real_concept(123420.))));
131 }
132#endif
133
134// this appears to be broken on Apple Tiger x86 with gcc4.0.1
135#if (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__ != 40001) || \
136 !defined(__APPLE__)
137 {
138 ///////////////////////////////////////////////////////////////////////
139 typedef karma::real_generator<double, bordercase_policy<double> >
140 bordercase_type;
141 bordercase_type const bordercase = bordercase_type();
142
143// BOOST_TEST(test("-5.7222349715140557e307",
144// bordercase(-5.7222349715140557e307)));
145
146 BOOST_TEST(test("1.7976931348623158e308",
147 bordercase(1.7976931348623158e308))); // DBL_MAX
148 BOOST_TEST(test("-1.7976931348623158e308",
149 bordercase(-1.7976931348623158e308))); // -DBL_MAX
150 BOOST_TEST(test("2.2250738585072014e-308",
151 bordercase(2.2250738585072014e-308))); // DBL_MIN
152 BOOST_TEST(test("-2.2250738585072014e-308",
153 bordercase(-2.2250738585072014e-308))); // -DBL_MIN
154 }
155#endif
156
157 {
158 boost::optional<double> v;
159 BOOST_TEST(!test("", double_, v));
160 BOOST_TEST(!test("", double_(1.0), v));
161
162 v = 1.0;
163 BOOST_TEST(test("1.0", double_, v));
164 BOOST_TEST(test("1.0", double_(1.0), v));
165 }
166
167 { // Phoenix expression tests (requires to include
168 // karma_phoenix_attributes.hpp)
169 namespace phoenix = boost::phoenix;
170
171 BOOST_TEST(test("1.0", double_, phoenix::val(1.0)));
172
173 double d = 1.2;
174 BOOST_TEST(test("1.2", double_, phoenix::ref(d)));
175 BOOST_TEST(test("2.2", double_, ++phoenix::ref(d)));
176 }
177
178 // test for denormalized numbers
179 {
180 BOOST_TEST(test("4.941e-324", double_, std::numeric_limits<double>::denorm_min()));
181 }
182
183 // test for #628: spirit::karma::generate generates 10.0e-04, but expecting 1.0e-03
184 {
185 BOOST_TEST(test("1.0", double_, 0.99999999999999829));
186 BOOST_TEST(test("0.1", double_, 0.099999999999999829));
187 BOOST_TEST(test("0.01", double_, 0.0099999999999999829));
188 BOOST_TEST(test("1.0e-03", double_, 0.00099999999999999829));
189 BOOST_TEST(test("1.0e-04", double_, 0.00009999999999999982));
190 BOOST_TEST(test("1.0e-05", double_, 0.00000999999999999998));
191 }
192
193 // test for #735: off by a magnitude due to log10 rounding up
194 {
195 BOOST_TEST(test("0.0999999999999998",
196 karma::real_generator<double, double_prec16_policy>(),
197 0.099999999999999770
198 ));
199 }
200
201 return boost::report_errors();
202}
203
204

source code of boost/libs/spirit/test/karma/real3.cpp