1 /*=============================================================================
2     Copyright (c) 2001-2010 Joel de Guzman
3     Copyright (c) 2001-2010 Hartmut Kaiser
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 
10 #include "real.hpp"
11 
12 int
main()13 main()
14 {
15     using spirit_test::test;
16     using spirit_test::test_attr;
17 
18     ///////////////////////////////////////////////////////////////////////////
19     //  strict real number tests
20     ///////////////////////////////////////////////////////////////////////////
21     {
22         using boost::spirit::qi::real_parser;
23         using boost::spirit::qi::parse;
24         using boost::spirit::qi::strict_ureal_policies;
25         using boost::spirit::qi::strict_real_policies;
26 
27         real_parser<double, strict_ureal_policies<double> > strict_udouble;
28         real_parser<double, strict_real_policies<double> > strict_double;
29         double  d;
30 
31         BOOST_TEST(!test("1234", strict_udouble));
32         BOOST_TEST(!test_attr("1234", strict_udouble, d));
33 
34         BOOST_TEST(test("1.2", strict_udouble));
35         BOOST_TEST(test_attr("1.2", strict_udouble, d) && compare(d, 1.2));
36 
37         BOOST_TEST(!test("-1234", strict_double));
38         BOOST_TEST(!test_attr("-1234", strict_double, d));
39 
40         BOOST_TEST(test("123.", strict_double));
41         BOOST_TEST(test_attr("123.", strict_double, d) && compare(d, 123));
42 
43         BOOST_TEST(test("3.E6", strict_double));
44         BOOST_TEST(test_attr("3.E6", strict_double, d) && compare(d, 3e6));
45 
46         real_parser<double, no_trailing_dot_policy<double> > notrdot_real;
47         real_parser<double, no_leading_dot_policy<double> > nolddot_real;
48 
49         BOOST_TEST(!test("1234.", notrdot_real));          //  Bad trailing dot
50         BOOST_TEST(!test(".1234", nolddot_real));          //  Bad leading dot
51     }
52 
53     ///////////////////////////////////////////////////////////////////////////
54     //  Special thousands separated numbers
55     ///////////////////////////////////////////////////////////////////////////
56     {
57         using boost::spirit::qi::real_parser;
58         using boost::spirit::qi::parse;
59         real_parser<double, ts_real_policies<double> > ts_real;
60         double  d;
61 
62         BOOST_TEST(test("123.01", ts_real));
63         BOOST_TEST(test_attr("123.01", ts_real, d)
64                 && compare(d, 123.01));
65 
66         BOOST_TEST(test("123,456,789.01", ts_real));
67         BOOST_TEST(test_attr("123,456,789.01", ts_real, d)
68                 && compare(d, 123456789.01));
69 
70         BOOST_TEST(test("12,345,678.90", ts_real));
71         BOOST_TEST(test_attr("12,345,678.90", ts_real, d)
72                 && compare(d, 12345678.90));
73 
74         BOOST_TEST(test("1,234,567.89", ts_real));
75         BOOST_TEST(test_attr("1,234,567.89", ts_real, d)
76                 && compare(d, 1234567.89));
77 
78         BOOST_TEST(!test("1234,567,890", ts_real));
79         BOOST_TEST(!test("1,234,5678,9", ts_real));
80         BOOST_TEST(!test("1,234,567.89e6", ts_real));
81         BOOST_TEST(!test("1,66", ts_real));
82     }
83 
84     return boost::report_errors();
85 }
86