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