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_phoenix_attributes.hpp>
21
22 #include <boost/limits.hpp>
23 #include "test.hpp"
24 #include <sstream>
25
26 using namespace spirit_test;
27
28 template <typename T>
to_string(T v)29 std::string to_string(T v)
30 {
31 std::stringstream ss;
32 ss << v;
33 return ss.str();
34 }
35
36 ///////////////////////////////////////////////////////////////////////////////
37 struct test_minmax
38 {
39 template <typename T>
operator ()test_minmax40 void operator()(T) const
41 {
42 using namespace boost::spirit;
43 using namespace boost::phoenix;
44
45 T minval = (std::numeric_limits<T>::min)();
46 T maxval = (std::numeric_limits<T>::max)();
47
48 std::string expected_minval = to_string(minval);
49 std::string expected_maxval = to_string(maxval);
50
51 // create a correct generator type from the given integer type
52 typedef typename
53 boost::mpl::if_<
54 boost::mpl::bool_<std::numeric_limits<T>::is_signed>,
55 karma::int_generator<T>,
56 karma::uint_generator<T>
57 >::type
58 int_generator_type;
59
60 int_generator_type const gen = int_generator_type();
61
62 BOOST_TEST(test(expected_maxval, gen, maxval));
63 BOOST_TEST(test(expected_minval, gen, minval));
64 BOOST_TEST(test(expected_maxval, gen(maxval)));
65 BOOST_TEST(test(expected_minval, gen(minval)));
66 BOOST_TEST(test(expected_maxval, gen(maxval), maxval));
67 BOOST_TEST(test(expected_minval, gen(minval), minval));
68 BOOST_TEST(!test("", gen(maxval), maxval-1));
69 BOOST_TEST(!test("", gen(minval), minval+1));
70 BOOST_TEST(test(expected_maxval, lit(maxval)));
71 BOOST_TEST(test(expected_minval, lit(minval)));
72
73 BOOST_TEST(test_delimited(expected_maxval + " ", gen, maxval, char(' ')));
74 BOOST_TEST(test_delimited(expected_minval + " ", gen, minval, char(' ')));
75 BOOST_TEST(test_delimited(expected_maxval + " ", gen(maxval), char(' ')));
76 BOOST_TEST(test_delimited(expected_minval + " ", gen(minval), char(' ')));
77 BOOST_TEST(test_delimited(expected_maxval + " ", gen(maxval), maxval, char(' ')));
78 BOOST_TEST(test_delimited(expected_minval + " ", gen(minval), minval, char(' ')));
79 BOOST_TEST(!test_delimited("", gen(maxval), maxval-1, char(' ')));
80 BOOST_TEST(!test_delimited("", gen(minval), minval+1, char(' ')));
81 BOOST_TEST(test_delimited(expected_maxval + " ", lit(maxval), char(' ')));
82 BOOST_TEST(test_delimited(expected_minval + " ", lit(minval), char(' ')));
83
84 // action tests
85 BOOST_TEST(test(expected_maxval, gen[_1 = val(maxval)]));
86 BOOST_TEST(test(expected_minval, gen[_1 = val(minval)]));
87
88 // optional tests
89 boost::optional<T> optmin, optmax(maxval);
90
91 BOOST_TEST(!test("", gen, optmin));
92 BOOST_TEST(!test("", gen(minval), optmin));
93
94 optmin = minval;
95 BOOST_TEST(test(expected_minval, gen, optmin));
96 BOOST_TEST(test(expected_maxval, gen, optmax));
97 BOOST_TEST(test(expected_minval, gen(minval), optmin));
98 BOOST_TEST(test(expected_maxval, gen(maxval), optmax));
99
100 // we support Phoenix attributes only starting with V2.2
101 #if SPIRIT_VERSION >= 0x2020
102 // Phoenix expression tests (only supported while including
103 // karma_phoenix_attributes.hpp
104 namespace phoenix = boost::phoenix;
105
106 BOOST_TEST(test("1", gen, phoenix::val(1)));
107
108 T val = 1;
109 BOOST_TEST(test("1", gen, phoenix::ref(val)));
110 BOOST_TEST(test("2", gen, ++phoenix::ref(val)));
111 #endif
112 }
113 };
114
115 ///////////////////////////////////////////////////////////////////////////////
116 int
main()117 main()
118 {
119 using namespace boost::spirit;
120
121 // test boundary values
122 typedef boost::mpl::vector<
123 #ifdef BOOST_HAS_LONG_LONG
124 boost::long_long_type, boost::ulong_long_type,
125 #endif
126 short, unsigned short,
127 int, unsigned int,
128 long, unsigned long
129 > integer_types;
130 boost::mpl::for_each<integer_types>(test_minmax());
131
132 return boost::report_errors();
133 }
134
135