• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <boost/phoenix/core/reference.hpp>
14 
15 int
main()16 main()
17 {
18     using spirit_test::test;
19     using spirit_test::test_attr;
20 
21     ///////////////////////////////////////////////////////////////////////////
22     //  parameterized signed real number tests
23     ///////////////////////////////////////////////////////////////////////////
24     {
25         using boost::spirit::qi::double_;
26         double d;
27 
28         BOOST_TEST(test("+1234", double_(1234)));
29         BOOST_TEST(!test("+1234", double_(-1234)));
30         BOOST_TEST(test_attr("+1234", double_(1234), d));
31         BOOST_TEST(compare(d, 1234));
32         BOOST_TEST(!test_attr("+1234", double_(-1234), d));
33 
34         BOOST_TEST(test("-1234", double_(-1234)));
35         BOOST_TEST(!test("-1234", double_(1234)));
36         BOOST_TEST(test_attr("-1234", double_(-1234), d));
37         BOOST_TEST(compare(d, -1234));
38         BOOST_TEST(!test_attr("-1234", double_(1234), d));
39 
40         BOOST_TEST(test("+1.2e3", double_(1.2e3)));
41         BOOST_TEST(!test("+1.2e3", double_(-1.2e3)));
42         BOOST_TEST(test_attr("+1.2e3", double_(1.2e3), d));
43         BOOST_TEST(compare(d, 1.2e3));
44         BOOST_TEST(!test_attr("+1.2e3", double_(-1.2e3), d));
45 
46         BOOST_TEST(test("-1.2e3", double_(-1.2e3)));
47         BOOST_TEST(!test("-1.2e3", double_(1.2e3)));
48         BOOST_TEST(test_attr("-1.2e3", double_(-1.2e3), d));
49         BOOST_TEST(compare(d, -1.2e3));
50         BOOST_TEST(!test_attr("-1.2e3", double_(1.2e3), d));
51     }
52 
53     ///////////////////////////////////////////////////////////////////////////
54     //  parameterized unsigned real number tests
55     ///////////////////////////////////////////////////////////////////////////
56     {
57         using boost::spirit::qi::real_parser;
58         using boost::spirit::qi::ureal_policies;
59         double d;
60 
61         real_parser<double, ureal_policies<double> > udouble;
62 
63         BOOST_TEST(test("1234", udouble(1234)));
64         BOOST_TEST(!test("1234", udouble(4321)));
65         BOOST_TEST(test_attr("1234", udouble(1234), d));
66         BOOST_TEST(compare(d, 1234));
67         BOOST_TEST(!test_attr("1234", udouble(4321), d));
68 
69         BOOST_TEST(test("1.2e3", udouble(1.2e3)));
70         BOOST_TEST(!test("1.2e3", udouble(3.2e1)));
71         BOOST_TEST(test_attr("1.2e3", udouble(1.2e3), d));
72         BOOST_TEST(compare(d, 1.2e3));
73         BOOST_TEST(!test_attr("1.2e3", udouble(3.2e1), d));
74     }
75 
76     ///////////////////////////////////////////////////////////////////////////
77     //  parameterized custom data type
78     ///////////////////////////////////////////////////////////////////////////
79     {
80         using boost::math::concepts::real_concept;
81         using boost::spirit::qi::real_parser;
82         using boost::spirit::qi::real_policies;
83 
84         real_parser<real_concept, real_policies<real_concept> > custom_real;
85         real_concept d;
86 
87         BOOST_TEST(test("-1234", custom_real(-1234)));
88         BOOST_TEST(!test("-1234", custom_real(4321)));
89         BOOST_TEST(test_attr("-1234", custom_real(-1234), d));
90         BOOST_TEST(compare(d, -1234));
91         BOOST_TEST(!test_attr("-1234", custom_real(-4321), d));
92 
93         BOOST_TEST(test("1.2e3", custom_real(1.2e3)));
94         BOOST_TEST(!test("1.2e3", custom_real(-1.2e3)));
95         BOOST_TEST(test_attr("1.2e3", custom_real(1.2e3), d));
96         BOOST_TEST(compare(d, 1.2e3));
97         BOOST_TEST(!test_attr("1.2e3", custom_real(-3.2e1), d));
98     }
99 
100     ///////////////////////////////////////////////////////////////////////////
101     //  parameterized lazy tests
102     ///////////////////////////////////////////////////////////////////////////
103     {
104         using boost::phoenix::ref;
105         using boost::spirit::qi::double_;
106         double n = 1.2e3, m = 3.2e1;
107 
108         BOOST_TEST(test("1.2e3", double_(ref(n))));
109         BOOST_TEST(!test("1.2e3", double_(ref(m))));
110     }
111 
112     ///////////////////////////////////////////////////////////////////////////
113     //  literal real number tests
114     ///////////////////////////////////////////////////////////////////////////
115     {
116         using boost::spirit::qi::lit;
117 
118         BOOST_TEST(test("+1.2e3", lit(1.2e3)));
119         BOOST_TEST(!test("+1.2e3", lit(-1.2e3)));
120         BOOST_TEST(test("-1.2e3", lit(-1.2e3)));
121         BOOST_TEST(!test("-1.2e3", lit(1.2e3)));
122         BOOST_TEST(test("1.2e3", lit(1.2e3)));
123         BOOST_TEST(!test("1.2e3", lit(3.2e1)));
124     }
125 
126     ///////////////////////////////////////////////////////////////////////////
127     //  literal lazy tests
128     ///////////////////////////////////////////////////////////////////////////
129     {
130         using boost::spirit::qi::lit;
131         using boost::phoenix::ref;
132         double n = 1.2e3, m = 3.2e1;
133 
134         BOOST_TEST(test("1.2e3", lit(ref(n))));
135         BOOST_TEST(!test("1.2e3", lit(ref(m))));
136     }
137 
138     return boost::report_errors();
139 }
140