1 /*=============================================================================
2     Copyright (c) 2001-2015 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     ///////////////////////////////////////////////////////////////////////////
20     //  thousand separated numbers
21     ///////////////////////////////////////////////////////////////////////////
22     {
23         using boost::spirit::x3::uint_parser;
24         using boost::spirit::x3::parse;
25 
26         uint_parser<unsigned, 10, 1, 3> uint3;
27         uint_parser<unsigned, 10, 3, 3> uint3_3;
28 
29     #define r (uint3 >> *(',' >> uint3_3))
30 
31         BOOST_TEST(test("1,234,567,890", r));
32         BOOST_TEST(test("12,345,678,900", r));
33         BOOST_TEST(test("123,456,789,000", r));
34         BOOST_TEST(!test("1000,234,567,890", r));
35         BOOST_TEST(!test("1,234,56,890", r));
36         BOOST_TEST(!test("1,66", r));
37     }
38 
39     ///////////////////////////////////////////////////////////////////////////
40     //  unsigned real number tests
41     ///////////////////////////////////////////////////////////////////////////
42     {
43         using boost::spirit::x3::real_parser;
44         using boost::spirit::x3::parse;
45         using boost::spirit::x3::ureal_policies;
46 
47         real_parser<double, ureal_policies<double> > udouble;
48         double d;
49 
50         BOOST_TEST(test("1234", udouble));
51         BOOST_TEST(test_attr("1234", udouble, d) && compare(d, 1234));
52 
53         BOOST_TEST(test("1.2e3", udouble));
54         BOOST_TEST(test_attr("1.2e3", udouble, d) && compare(d, 1.2e3));
55 
56         BOOST_TEST(test("1.2e-3", udouble));
57         BOOST_TEST(test_attr("1.2e-3", udouble, d) && compare(d, 1.2e-3));
58 
59         BOOST_TEST(test("1.e2", udouble));
60         BOOST_TEST(test_attr("1.e2", udouble, d) && compare(d, 1.e2));
61 
62         BOOST_TEST(test("1.", udouble));
63         BOOST_TEST(test_attr("1.", udouble, d) && compare(d, 1.));
64 
65         BOOST_TEST(test(".2e3", udouble));
66         BOOST_TEST(test_attr(".2e3", udouble, d) && compare(d, .2e3));
67 
68         BOOST_TEST(test("2e3", udouble));
69         BOOST_TEST(test_attr("2e3", udouble, d) && compare(d, 2e3));
70 
71         BOOST_TEST(test("2", udouble));
72         BOOST_TEST(test_attr("2", udouble, d) && compare(d, 2));
73 
74         using boost::math::fpclassify;
75         BOOST_TEST(test("inf", udouble));
76         BOOST_TEST(test("infinity", udouble));
77         BOOST_TEST(test("INF", udouble));
78         BOOST_TEST(test("INFINITY", udouble));
79 
80         BOOST_TEST(test_attr("inf", udouble, d)
81                 && FP_INFINITE == fpclassify(d));
82         BOOST_TEST(test_attr("INF", udouble, d)
83                 && FP_INFINITE == fpclassify(d));
84         BOOST_TEST(test_attr("infinity", udouble, d)
85                 && FP_INFINITE == fpclassify(d));
86         BOOST_TEST(test_attr("INFINITY", udouble, d)
87                 && FP_INFINITE == fpclassify(d));
88 
89         BOOST_TEST(test("nan", udouble));
90         BOOST_TEST(test_attr("nan", udouble, d)
91                 && FP_NAN == fpclassify(d));
92         BOOST_TEST(test("NAN", udouble));
93         BOOST_TEST(test_attr("NAN", udouble, d)
94                 && FP_NAN == fpclassify(d));
95         BOOST_TEST(test("nan(...)", udouble));
96         BOOST_TEST(test_attr("nan(...)", udouble, d)
97                 && FP_NAN == fpclassify(d));
98         BOOST_TEST(test("NAN(...)", udouble));
99         BOOST_TEST(test_attr("NAN(...)", udouble, d)
100                 && FP_NAN == fpclassify(d));
101 
102         BOOST_TEST(!test("e3", udouble));
103         BOOST_TEST(!test_attr("e3", udouble, d));
104 
105         BOOST_TEST(!test("-1.2e3", udouble));
106         BOOST_TEST(!test_attr("-1.2e3", udouble, d));
107 
108         BOOST_TEST(!test("+1.2e3", udouble));
109         BOOST_TEST(!test_attr("+1.2e3", udouble, d));
110 
111         BOOST_TEST(!test("1.2e", udouble));
112         BOOST_TEST(!test_attr("1.2e", udouble, d));
113 
114         BOOST_TEST(!test("-.3", udouble));
115         BOOST_TEST(!test_attr("-.3", udouble, d));
116     }
117 
118     return boost::report_errors();
119 }
120