summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/karma/alternative1.cpp
blob: 461e4b06698b24c7f063210fa63c172fecebe0bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//  Copyright (c) 2001-2011 Hartmut Kaiser
// 
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/config/warning_disable.hpp>
#include <boost/detail/lightweight_test.hpp>

#include <boost/spirit/include/karma_auxiliary.hpp>
#include <boost/spirit/include/karma_char.hpp>
#include <boost/spirit/include/karma_string.hpp>
#include <boost/spirit/include/karma_numeric.hpp>
#include <boost/spirit/include/karma_generate.hpp>
#include <boost/spirit/include/karma_operator.hpp>
#include <boost/spirit/include/karma_directive.hpp>
#include <boost/spirit/include/karma_nonterminal.hpp>
#include <boost/spirit/include/karma_auxiliary.hpp>

#include "test.hpp"

using namespace spirit_test;

///////////////////////////////////////////////////////////////////////////////
int
main()
{
    using namespace boost;
    using namespace boost::spirit;
    using namespace boost::spirit::ascii;

    {
        BOOST_TEST(test("x", char_('x') | char_('i')));
        BOOST_TEST(test("xi", char_('x') << char_('i') | char_('i')));
        BOOST_TEST(test("i", char_('i') | char_('x') << char_('i')));

        BOOST_TEST(test("x", buffer[char_('x')] | char_('i')));

        variant<int, char> v (10);
        BOOST_TEST(test("10", char_ | int_, v));
        BOOST_TEST(test("10", int_ | char_, v));
        BOOST_TEST(test("a", lit('a') | char_ | int_, v));
        BOOST_TEST(test("a", char_ | lit('a') | int_, v));
        BOOST_TEST(test("10", int_ | lit('a') | char_, v));

        v = 'c';
        BOOST_TEST(test("c", char_ | int_, v));
        BOOST_TEST(test("a", lit('a') | char_ | int_, v));
        BOOST_TEST(test("c", char_ | lit('a') | int_, v));
        BOOST_TEST(test("a", int_ | lit('a') | char_, v));
        BOOST_TEST(test("c", int_ | char_ | lit('a'), v));
    }

    // testing for alignment/truncation problems on little endian systems
    // (big endian systems will fail one of the other tests below)
    {
        // test optional attribute
        optional<variant<int, char> > v;
        BOOST_TEST(!test("", char_ | int_, v));
        BOOST_TEST(!test("", int_ | char_, v));
        BOOST_TEST(test("a", lit('a') | char_ | int_, v));
        BOOST_TEST(test("a", char_ | lit('a') | int_, v));
        BOOST_TEST(test("a", int_ | lit('a') | char_, v));

        v = 10;
        BOOST_TEST(test("10", char_ | int_, v));
        BOOST_TEST(test("10", int_ | char_, v));
        BOOST_TEST(test("a", lit('a') | char_ | int_, v));
        BOOST_TEST(test("a", char_ | lit('a') | int_, v));
        BOOST_TEST(test("10", int_ | lit('a') | char_, v));

        v = 'c';
        BOOST_TEST(test("c", char_ | int_, v));
        BOOST_TEST(test("a", lit('a') | char_ | int_, v));
        BOOST_TEST(test("c", char_ | lit('a') | int_, v));
        BOOST_TEST(test("a", int_ | lit('a') | char_, v));
        BOOST_TEST(test("c", int_ | char_ | lit('a'), v));
    }

    {
        // more tests for optional attribute
        optional<int> o;
        BOOST_TEST(test("a", lit('a') | int_, o));
        BOOST_TEST(test("a", int_ | lit('a'), o));

        o = 10;
        BOOST_TEST(test("a", lit('a') | int_, o));
        BOOST_TEST(test("10", int_ | lit('a'), o));
    }

    {
        int i = 10;
        BOOST_TEST(test("a", lit('a') | int_, i));
        BOOST_TEST(test("10", int_ | lit('a'), i));
    }

    {
        optional<std::string> o;
        BOOST_TEST(test("xyzzy", ("(" << string << ")") | lit("xyzzy"), o));

        o = "plugh";
        BOOST_TEST(test("(plugh)", ("(" << string << ")") | lit("xyzzy"), o));
    }

    {
        BOOST_TEST(test("abc", string | int_, std::string("abc")));
        BOOST_TEST(test("1234", string | int_, 1234));
        BOOST_TEST(test("abc", int_ | string, std::string("abc")));
        BOOST_TEST(test("1234", int_ | string, 1234));
    }

    {
        // testing for alignment/truncation problems on little endian systems
        // (big endian systems will fail one of the other tests below)
        std::basic_string<wchar_t> generated;
        std::back_insert_iterator<std::basic_string<wchar_t> > outit(generated);
        boost::variant<int, char> v(10);
        bool result = karma::generate_delimited(outit
          , karma::int_ | karma::char_, karma::char_(' '), v);
        BOOST_TEST(result && generated == L"10 ");
    }

    {
        boost::optional<int> v;
        BOOST_TEST(test("error", int_ | "error" << omit[-int_], v));
        BOOST_TEST(!test("error", int_ | "error" << omit[int_], v));
        BOOST_TEST(test("error", int_ | "error" << skip[int_], v));
        v = 1;
        BOOST_TEST(test("1", int_ | "error" << omit[-int_], v));
        BOOST_TEST(test("1", int_ | "error" << omit[int_], v));
        BOOST_TEST(test("1", int_ | "error" << skip[int_], v));
    }

    {
        typedef spirit_test::output_iterator<char>::type outiter_type;
        namespace karma = boost::spirit::karma;

        karma::rule<outiter_type, int()> r = int_;
        std::vector<int> v;
        BOOST_TEST(test("", '>' << r % ',' | karma::eps, v));

        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        BOOST_TEST(test(">1,2,3,4", '>' << r % ',' | karma::eps, v));
    }

    {
        typedef spirit_test::output_iterator<char>::type outiter_type;
        namespace karma = boost::spirit::karma;

        karma::rule<outiter_type, boost::optional<int>()> r = int_;
        boost::optional<int> o;
        BOOST_TEST(test("error", r | "error", o));

        o = 10;
        BOOST_TEST(test("10", r | "error", o));
    }

    return boost::report_errors();
}