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