• 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 
9 #include <boost/assign/std/vector.hpp>
10 
11 #include <boost/spirit/include/karma_operator.hpp>
12 #include <boost/spirit/include/karma_char.hpp>
13 #include <boost/spirit/include/karma_string.hpp>
14 #include <boost/spirit/include/karma_numeric.hpp>
15 #include <boost/spirit/include/karma_directive.hpp>
16 #include <boost/spirit/include/karma_operator.hpp>
17 #include <boost/spirit/include/karma_action.hpp>
18 #include <boost/spirit/include/karma_nonterminal.hpp>
19 #include <boost/spirit/include/karma_auxiliary.hpp>
20 #include <boost/spirit/include/karma_directive.hpp>
21 #include <boost/spirit/include/karma_phoenix_attributes.hpp>
22 #include <boost/spirit/include/support_argument.hpp>
23 #include <boost/spirit/include/phoenix_core.hpp>
24 #include <boost/spirit/include/phoenix_operator.hpp>
25 #include <boost/fusion/include/std_pair.hpp>
26 
27 #include <string>
28 #include <iostream>
29 #include <vector>
30 
31 #include "test.hpp"
32 
33 using namespace spirit_test;
34 
35 ///////////////////////////////////////////////////////////////////////////////
main()36 int main()
37 {
38     using namespace boost::spirit::ascii;
39     using boost::spirit::karma::repeat;
40     using boost::spirit::karma::inf;
41     using boost::spirit::karma::int_;
42     using boost::spirit::karma::hex;
43     using boost::spirit::karma::_1;
44 
45     {
46         std::string str("aBcdeFGH");
47         BOOST_TEST(test("abcdefgh", lower[repeat(8)[char_]], str));
48         BOOST_TEST(test_delimited("A B C D E F G H ", upper[repeat(8)[char_]], str, space));
49     }
50 
51    {
52        std::string s1 = "aaaaa";
53        BOOST_TEST(test("aaaaa", char_ << repeat(2)[char_ << char_], s1));
54        s1 = "aaa";
55        BOOST_TEST(test("aaa", char_ << repeat(1, 2)[char_ << char_], s1));
56        s1 = "aa";
57        BOOST_TEST(!test("", char_ << repeat(1)[char_ << char_], s1));
58    }
59 
60     { // actions
61         namespace phx = boost::phoenix;
62 
63         std::vector<char> v;
64         v.push_back('a');
65         v.push_back('a');
66         v.push_back('a');
67         v.push_back('a');
68         BOOST_TEST(test("aaaa", repeat(4)[char_][_1 = phx::ref(v)]));
69     }
70 
71     { // more actions
72         namespace phx = boost::phoenix;
73 
74         std::vector<int> v;
75         v.push_back(123);
76         v.push_back(456);
77         v.push_back(789);
78         BOOST_TEST(test_delimited("123 456 789 ", repeat(3)[int_][_1 = phx::ref(v)], space));
79     }
80 
81     // failing sub-generators
82     {
83         using boost::spirit::karma::strict;
84         using boost::spirit::karma::relaxed;
85 
86         using namespace boost::assign;
87         namespace karma = boost::spirit::karma;
88 
89         typedef std::pair<char, char> data;
90         std::vector<data> v2, v3;
91         v2 += std::make_pair('a', 'a'),
92               std::make_pair('b', 'b'),
93               std::make_pair('c', 'c'),
94               std::make_pair('d', 'd'),
95               std::make_pair('e', 'e'),
96               std::make_pair('f', 'f'),
97               std::make_pair('g', 'g');
98         v3 += std::make_pair('a', 'a'),
99               std::make_pair('b', 'b'),
100               std::make_pair('c', 'c'),
101               std::make_pair('d', 'd');
102 
103         karma::rule<spirit_test::output_iterator<char>::type, data()> r;
104 
105         r = &char_('d') << char_;
106         BOOST_TEST(test("d", repeat[r], v2));
107         BOOST_TEST(test("d", relaxed[repeat[r]], v2));
108         BOOST_TEST(test("", strict[repeat[r]], v2));
109 
110         r = !char_('d') << char_;
111         BOOST_TEST(test("abcefg", repeat(6)[r], v2));
112         BOOST_TEST(!test("", repeat(5)[r], v2));
113         BOOST_TEST(test("abcefg", relaxed[repeat(6)[r]], v2));
114         BOOST_TEST(!test("", relaxed[repeat(5)[r]], v2));
115         BOOST_TEST(!test("", strict[repeat(6)[r]], v2));
116         BOOST_TEST(!test("", strict[repeat(5)[r]], v2));
117 
118         r = !char_('c') << char_;
119         BOOST_TEST(test("abd", repeat(3)[r], v2));
120         BOOST_TEST(test("abd", relaxed[repeat(3)[r]], v2));
121         BOOST_TEST(!test("", strict[repeat(3)[r]], v2));
122 
123         r = !char_('a') << char_;
124         BOOST_TEST(test("bcdef", repeat(3, 5)[r], v2));
125         BOOST_TEST(test("bcd", repeat(3, 5)[r], v3));
126         BOOST_TEST(!test("", repeat(4, 5)[r], v3));
127         BOOST_TEST(test("bcdef", relaxed[repeat(3, 5)[r]], v2));
128         BOOST_TEST(test("bcd", relaxed[repeat(3, 5)[r]], v3));
129         BOOST_TEST(!test("", relaxed[repeat(4, 5)[r]], v3));
130         BOOST_TEST(!test("", strict[repeat(3, 5)[r]], v2));
131         BOOST_TEST(!test("", strict[repeat(3, 5)[r]], v3));
132         BOOST_TEST(!test("", strict[repeat(4, 5)[r]], v3));
133 
134         BOOST_TEST(test("bcd", repeat(3, inf)[r], v3));
135         BOOST_TEST(test("bcdefg", repeat(3, inf)[r], v2));
136         BOOST_TEST(!test("", repeat(4, inf)[r], v3));
137 
138         r = !char_('g') << char_;
139         BOOST_TEST(test("abcde", repeat(3, 5)[r], v2));
140         BOOST_TEST(test("abcd", repeat(3, 5)[r], v3));
141         BOOST_TEST(!test("", repeat(4, 5)[r], v3));
142         BOOST_TEST(test("abcde", relaxed[repeat(3, 5)[r]], v2));
143         BOOST_TEST(test("abcd", relaxed[repeat(3, 5)[r]], v3));
144         BOOST_TEST(!test("", relaxed[repeat(4, 5)[r]], v3));
145         BOOST_TEST(test("abcde", strict[repeat(3, 5)[r]], v2));
146         BOOST_TEST(test("abcd", strict[repeat(3, 5)[r]], v3));
147         BOOST_TEST(!test("", strict[repeat(5)[r]], v3));
148     }
149 
150     return boost::report_errors();
151 }
152 
153