• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2001-2011 Hartmut Kaiser
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <boost/config/warning_disable.hpp>
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/mpl/vector.hpp>
9 #include <boost/mpl/for_each.hpp>
10 #include <boost/mpl/if.hpp>
11 #include <boost/mpl/bool.hpp>
12 
13 #include <boost/spirit/include/phoenix_core.hpp>
14 #include <boost/spirit/include/phoenix_operator.hpp>
15 
16 #include <boost/spirit/include/karma_char.hpp>
17 #include <boost/spirit/include/karma_numeric.hpp>
18 #include <boost/spirit/include/karma_directive.hpp>
19 #include <boost/spirit/include/karma_action.hpp>
20 #include <boost/spirit/include/karma_rule.hpp>
21 
22 #include <boost/limits.hpp>
23 #include "test.hpp"
24 
25 using namespace spirit_test;
26 
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 int
main()30 main()
31 {
32     using namespace boost::spirit;
33 
34     {
35         using namespace boost::spirit::ascii;
36 
37         karma::int_generator<int, 10, true> const signed_int =
38             karma::int_generator<int, 10, true>();
39 
40         ///////////////////////////////////////////////////////////////////////
41         BOOST_TEST(test(" 0", signed_int, 0));
42         BOOST_TEST(test("+123", signed_int, 123));
43         BOOST_TEST(test("-123", signed_int, -123));
44 
45         BOOST_TEST(test_delimited(" 0 ", signed_int, 0, char_(' ')));
46         BOOST_TEST(test_delimited("+123 ", signed_int, 123, char_(' ')));
47         BOOST_TEST(test_delimited("-123 ", signed_int, -123, char_(' ')));
48 
49         BOOST_TEST(test(" 0", lower[signed_int], 0));
50         BOOST_TEST(test("+123", lower[signed_int], 123));
51         BOOST_TEST(test("-123", lower[signed_int], -123));
52 
53         BOOST_TEST(test_delimited(" 0 ", lower[signed_int], 0, char_(' ')));
54         BOOST_TEST(test_delimited("+123 ", lower[signed_int], 123, char_(' ')));
55         BOOST_TEST(test_delimited("-123 ", lower[signed_int], -123, char_(' ')));
56 
57         BOOST_TEST(test(" 0", upper[signed_int], 0));
58         BOOST_TEST(test("+123", upper[signed_int], 123));
59         BOOST_TEST(test("-123", upper[signed_int], -123));
60 
61         BOOST_TEST(test_delimited(" 0 ", upper[signed_int], 0, char_(' ')));
62         BOOST_TEST(test_delimited("+123 ", upper[signed_int], 123, char_(' ')));
63         BOOST_TEST(test_delimited("-123 ", upper[signed_int], -123, char_(' ')));
64 
65         ///////////////////////////////////////////////////////////////////////
66         BOOST_TEST(test(" 0", signed_int(0)));
67         BOOST_TEST(test("+123", signed_int(123)));
68         BOOST_TEST(test("-123", signed_int(-123)));
69 
70         BOOST_TEST(test_delimited(" 0 ", signed_int(0), char_(' ')));
71         BOOST_TEST(test_delimited("+123 ", signed_int(123), char_(' ')));
72         BOOST_TEST(test_delimited("-123 ", signed_int(-123), char_(' ')));
73 
74         BOOST_TEST(test(" 0", lower[signed_int(0)]));
75         BOOST_TEST(test("+123", lower[signed_int(123)]));
76         BOOST_TEST(test("-123", lower[signed_int(-123)]));
77 
78         BOOST_TEST(test_delimited(" 0 ", lower[signed_int(0)], char_(' ')));
79         BOOST_TEST(test_delimited("+123 ", lower[signed_int(123)], char_(' ')));
80         BOOST_TEST(test_delimited("-123 ", lower[signed_int(-123)], char_(' ')));
81 
82         BOOST_TEST(test(" 0", upper[signed_int(0)]));
83         BOOST_TEST(test("+123", upper[signed_int(123)]));
84         BOOST_TEST(test("-123", upper[signed_int(-123)]));
85 
86         BOOST_TEST(test_delimited(" 0 ", upper[signed_int(0)], char_(' ')));
87         BOOST_TEST(test_delimited("+123 ", upper[signed_int(123)], char_(' ')));
88         BOOST_TEST(test_delimited("-123 ", upper[signed_int(-123)], char_(' ')));
89 
90         using namespace boost::phoenix;
91 
92         BOOST_TEST(test(" 0", signed_int(val(0))));
93         BOOST_TEST(test("+123", signed_int(val(123))));
94         BOOST_TEST(test("-123", signed_int(val(-123))));
95 
96         int i1 = 0, i2 = 123, i3 = -123;
97         BOOST_TEST(test(" 0", signed_int(ref(i1))));
98         BOOST_TEST(test("+123", signed_int(ref(i2))));
99         BOOST_TEST(test("-123", signed_int(ref(i3))));
100     }
101 
102     {
103         ///////////////////////////////////////////////////////////////////////
104         using namespace boost::spirit::ascii;
105 
106         BOOST_TEST(test("1234", uint_, 1234));
107         BOOST_TEST(test("ff", hex, 0xff));
108         BOOST_TEST(test("1234", oct, 01234));
109         BOOST_TEST(test("11110000", bin, 0xf0));
110 
111         BOOST_TEST(test_delimited("1234 ", uint_, 1234, char_(' ')));
112         BOOST_TEST(test_delimited("ff ", hex, 0xff, char_(' ')));
113         BOOST_TEST(test_delimited("1234 ", oct, 01234, char_(' ')));
114         BOOST_TEST(test_delimited("11110000 ", bin, 0xf0, char_(' ')));
115 
116         // test unsigned generator with signed integral value
117         BOOST_TEST(test("ff", hex, (signed char)'\xff'));
118         BOOST_TEST(test_delimited("ff ", hex, (signed char)'\xff', char_(' ')));
119 
120         BOOST_TEST(test("1234", lower[uint_], 1234));
121         BOOST_TEST(test("ff", lower[hex], 0xff));
122         BOOST_TEST(test("1234", lower[oct], 01234));
123         BOOST_TEST(test("11110000", lower[bin], 0xf0));
124 
125         BOOST_TEST(test_delimited("1234 ", lower[uint_], 1234, char_(' ')));
126         BOOST_TEST(test_delimited("ff ", lower[hex], 0xff, char_(' ')));
127         BOOST_TEST(test_delimited("1234 ", lower[oct], 01234, char_(' ')));
128         BOOST_TEST(test_delimited("11110000 ", lower[bin], 0xf0, char_(' ')));
129 
130         BOOST_TEST(test("1234", upper[uint_], 1234));
131         BOOST_TEST(test("FF", upper[hex], 0xff));
132         BOOST_TEST(test("1234", upper[oct], 01234));
133         BOOST_TEST(test("11110000", upper[bin], 0xf0));
134 
135         BOOST_TEST(test_delimited("1234 ", upper[uint_], 1234, char_(' ')));
136         BOOST_TEST(test_delimited("FF ", upper[hex], 0xff, char_(' ')));
137         BOOST_TEST(test_delimited("1234 ", upper[oct], 01234, char_(' ')));
138         BOOST_TEST(test_delimited("11110000 ", upper[bin], 0xf0, char_(' ')));
139 
140         // no generator transformation should occur for uint_'s
141         BOOST_TEST(test("1234", upper[upper[uint_]], 1234));
142         BOOST_TEST(test("1234", upper[lower[uint_]], 1234));
143         BOOST_TEST(test("1234", lower[upper[uint_]], 1234));
144         BOOST_TEST(test("1234", lower[lower[uint_]], 1234));
145 
146         BOOST_TEST(test_delimited("1234 ", upper[upper[uint_]], 1234, char_(' ')));
147         BOOST_TEST(test_delimited("1234 ", upper[lower[uint_]], 1234, char_(' ')));
148         BOOST_TEST(test_delimited("1234 ", lower[upper[uint_]], 1234, char_(' ')));
149         BOOST_TEST(test_delimited("1234 ", lower[lower[uint_]], 1234, char_(' ')));
150 
151         BOOST_TEST(test("FF", upper[upper[hex]], 0xff));
152         BOOST_TEST(test("FF", upper[lower[hex]], 0xff));
153         BOOST_TEST(test("ff", lower[upper[hex]], 0xff));
154         BOOST_TEST(test("ff", lower[lower[hex]], 0xff));
155 
156         BOOST_TEST(test_delimited("FF ", upper[upper[hex]], 0xff, char_(' ')));
157         BOOST_TEST(test_delimited("FF ", upper[lower[hex]], 0xff, char_(' ')));
158         BOOST_TEST(test_delimited("ff ", lower[upper[hex]], 0xff, char_(' ')));
159         BOOST_TEST(test_delimited("ff ", lower[lower[hex]], 0xff, char_(' ')));
160     }
161 
162     ///////////////////////////////////////////////////////////////////////////
163     {
164         using boost::spirit::karma::int_;
165         using boost::spirit::karma::_1;
166         using boost::spirit::karma::_val;
167         using boost::spirit::karma::space;
168 
169         int i = 123;
170         int j = 456;
171         BOOST_TEST(test("123", int_[_1 = _val], i));
172         BOOST_TEST(test_delimited("456 ", int_[_1 = _val], j, space));
173     }
174 
175     return boost::report_errors();
176 }
177 
178