1 //  Copyright (c) 2001-2011 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 <boost/spirit/include/version.hpp>
8 #include <boost/spirit/include/karma_phoenix_attributes.hpp>
9 #include <boost/spirit/include/phoenix_core.hpp>
10 #include <boost/spirit/include/phoenix_operator.hpp>
11 #include "real.hpp"
12 
13 ///////////////////////////////////////////////////////////////////////////////
14 // the customization points below have been added only recently
15 #if SPIRIT_VERSION >= 0x2050
16 // does not provide proper std::numeric_limits, we need to roll our own
17 namespace boost { namespace spirit { namespace traits
18 {
19     template <>
20     struct is_nan<boost::math::concepts::real_concept>
21     {
callboost::spirit::traits::is_nan22         static bool call(boost::math::concepts::real_concept n)
23         {
24             return test_nan(n.value());
25         }
26     };
27 
28     template <>
29     struct is_infinite<boost::math::concepts::real_concept>
30     {
callboost::spirit::traits::is_infinite31         static bool call(boost::math::concepts::real_concept n)
32         {
33             return test_infinite(n.value());
34         }
35     };
36 }}}
37 #endif
38 
39 ///////////////////////////////////////////////////////////////////////////////
main()40 int main()
41 {
42     using namespace boost::spirit;
43 
44     {
45         using namespace boost::spirit::ascii;
46 
47         ///////////////////////////////////////////////////////////////////////
48         typedef karma::real_generator<double, fixed_policy<double> > fixed_type;
49         fixed_type const fixed = fixed_type();
50 
51         BOOST_TEST(test("0.0", fixed, 0.0));
52         BOOST_TEST(test("1.0", fixed, 1.0));
53 
54         BOOST_TEST(test("0.0", fixed, 0.000012345));
55         BOOST_TEST(test("0.0", fixed, 0.00012345));
56         BOOST_TEST(test("0.001", fixed, 0.0012345));
57         BOOST_TEST(test("0.012", fixed, 0.012345));
58         BOOST_TEST(test("0.123", fixed, 0.12345));
59         BOOST_TEST(test("1.234", fixed, 1.2345));
60         BOOST_TEST(test("12.345", fixed, 12.345));
61         BOOST_TEST(test("123.45", fixed, 123.45));
62         BOOST_TEST(test("1234.5", fixed, 1234.5));
63         BOOST_TEST(test("12342.0", fixed, 12342.));
64         BOOST_TEST(test("123420.0", fixed, 123420.));
65         BOOST_TEST(test("123420000000000000000.0", fixed, 1.23420e20));
66 
67         BOOST_TEST(test("0.0", fixed, -0.000012345));
68         BOOST_TEST(test("0.0", fixed, -0.00012345));
69         BOOST_TEST(test("-0.001", fixed, -0.0012345));
70         BOOST_TEST(test("-0.012", fixed, -0.012345));
71         BOOST_TEST(test("-0.123", fixed, -0.12345));
72         BOOST_TEST(test("-1.234", fixed, -1.2345));
73         BOOST_TEST(test("-12.346", fixed, -12.346));
74         BOOST_TEST(test("-123.46", fixed, -123.46));
75         BOOST_TEST(test("-1234.5", fixed, -1234.5));
76         BOOST_TEST(test("-12342.0", fixed, -12342.));
77         BOOST_TEST(test("-123420.0", fixed, -123420.));
78         BOOST_TEST(test("-123420000000000000000.0", fixed, -1.23420e20));
79     }
80 
81 // support for using real_concept with a Karma generator has been implemented
82 // in Boost versions > 1.36 only, additionally real_concept is available only
83 // if BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS  is not defined
84 #if BOOST_VERSION > 103600 && !defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)
85     {
86         using boost::math::concepts::real_concept;
87         typedef karma::real_generator<real_concept> custom_type;
88         custom_type const custom = custom_type();
89 
90         BOOST_TEST(test("0.0", custom, real_concept(0.0)));
91         BOOST_TEST(test("1.0", custom, real_concept(1.0)));
92         BOOST_TEST(test("1.0", custom, real_concept(1.0001)));
93         BOOST_TEST(test("1.001", custom, real_concept(1.001)));
94         BOOST_TEST(test("1.01", custom, real_concept(1.010)));
95         BOOST_TEST(test("1.1", custom, real_concept(1.100)));
96 
97         BOOST_TEST(test("1.234e-04", custom, real_concept(0.00012345)));
98         BOOST_TEST(test("0.001", custom, real_concept(0.0012345)));
99         BOOST_TEST(test("0.012", custom, real_concept(0.012345)));
100         BOOST_TEST(test("0.123", custom, real_concept(0.12345)));
101         BOOST_TEST(test("1.234", custom, real_concept(1.2345)));
102         BOOST_TEST(test("12.346", custom, real_concept(12.346)));
103         BOOST_TEST(test("123.46", custom, real_concept(123.46)));
104         BOOST_TEST(test("1234.5", custom, real_concept(1234.5)));
105         BOOST_TEST(test("12342.0", custom, real_concept(12342.)));
106         BOOST_TEST(test("1.234e05", custom, real_concept(123420.)));
107 
108         BOOST_TEST(test("-1.0", custom, real_concept(-1.0)));
109         BOOST_TEST(test("-1.234", custom, real_concept(-1.2345)));
110         BOOST_TEST(test("-1.235", custom, real_concept(-1.2346)));
111         BOOST_TEST(test("-1234.2", custom, real_concept(-1234.2)));
112 
113         BOOST_TEST(test("1.0", custom(real_concept(1.0))));
114         BOOST_TEST(test("1.0", custom(real_concept(1.0001))));
115         BOOST_TEST(test("1.001", custom(real_concept(1.001))));
116         BOOST_TEST(test("1.01", custom(real_concept(1.010))));
117         BOOST_TEST(test("1.1", custom(real_concept(1.100))));
118 
119         BOOST_TEST(test("1.234e-04", custom(real_concept(0.00012345))));
120         BOOST_TEST(test("0.001", custom(real_concept(0.0012345))));
121         BOOST_TEST(test("0.012", custom(real_concept(0.012345))));
122         BOOST_TEST(test("0.123", custom(real_concept(0.12345))));
123         BOOST_TEST(test("1.234", custom(real_concept(1.2345))));
124         BOOST_TEST(test("12.346", custom(real_concept(12.346))));
125         BOOST_TEST(test("123.46", custom(real_concept(123.46))));
126         BOOST_TEST(test("1234.5", custom(real_concept(1234.5))));
127         BOOST_TEST(test("12342.0", custom(real_concept(12342.))));
128         BOOST_TEST(test("1.234e05", custom(real_concept(123420.))));
129     }
130 #endif
131 
132 // this appears to be broken on Apple Tiger x86 with gcc4.0.1
133 #if (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__ != 40001) || \
134     !defined(__APPLE__)
135     {
136         ///////////////////////////////////////////////////////////////////////
137         typedef karma::real_generator<double, bordercase_policy<double> >
138             bordercase_type;
139         bordercase_type const bordercase = bordercase_type();
140 
141 //         BOOST_TEST(test("-5.7222349715140557e307",
142 //             bordercase(-5.7222349715140557e307)));
143 
144         BOOST_TEST(test("1.7976931348623158e308",
145             bordercase(1.7976931348623158e308)));       // DBL_MAX
146         BOOST_TEST(test("-1.7976931348623158e308",
147             bordercase(-1.7976931348623158e308)));      // -DBL_MAX
148         BOOST_TEST(test("2.2250738585072014e-308",
149             bordercase(2.2250738585072014e-308)));      // DBL_MIN
150         BOOST_TEST(test("-2.2250738585072014e-308",
151             bordercase(-2.2250738585072014e-308)));     // -DBL_MIN
152     }
153 #endif
154 
155     {
156         boost::optional<double> v;
157         BOOST_TEST(!test("", double_, v));
158         BOOST_TEST(!test("", double_(1.0), v));
159 
160         v = 1.0;
161         BOOST_TEST(test("1.0", double_, v));
162         BOOST_TEST(test("1.0", double_(1.0), v));
163     }
164 
165 // we support Phoenix attributes only starting with V2.2
166 #if SPIRIT_VERSION >= 0x2020
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 #endif
178 
179     // test for denormalized numbers
180     {
181         BOOST_TEST(test("4.941e-324", double_, std::numeric_limits<double>::denorm_min()));
182     }
183     return boost::report_errors();
184 }
185 
186