summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/karma/omit.cpp
blob: 9a349697e44db7c1f667cbac4f243260c3ee0369 (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
//  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.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

#include <iostream>
#include "test.hpp"

using namespace spirit_test;

int main()
{
    using boost::spirit::karma::_1;
    using boost::spirit::karma::_a;
    using boost::spirit::karma::double_;
    using boost::spirit::karma::int_;
    using boost::spirit::karma::omit;
    using boost::spirit::karma::skip;
    using boost::spirit::karma::rule;
    using boost::spirit::karma::locals;

    typedef spirit_test::output_iterator<char>::type outiter_type;
    typedef std::pair<std::vector<int>, int> attribute_type;

    rule<outiter_type, attribute_type(), locals<int> > r;

    attribute_type a;
    a.first.push_back(0x01);
    a.first.push_back(0x02);
    a.first.push_back(0x04);
    a.first.push_back(0x08);
    a.second = 0;

    // omit[] is supposed to execute the embedded generator
    {
        std::pair<double, double> p (1.0, 2.0);
        BOOST_TEST(test("2.0", omit[double_] << double_, p));

        r %= omit[ *(int_[_a = _a + _1]) ] << int_[_1 = _a];
        BOOST_TEST(test("15", r, a));
    }

    // even if omit[] never fails, it has to honor the result of the 
    // embedded generator
    {
        typedef std::pair<double, double> attribute_type;
        rule<outiter_type, attribute_type()> r;

        r %= omit[double_(1.0) << double_] | "42";

        attribute_type p1 (1.0, 2.0);
        BOOST_TEST(test("", r, p1));

        attribute_type p2 (10.0, 2.0);
        BOOST_TEST(test("42", r, p2));
     }

    // skip[] is not supposed to execute the embedded generator
    {
        using boost::spirit::karma::double_;
        using boost::spirit::karma::skip;

        std::pair<double, double> p (1.0, 2.0);
        BOOST_TEST(test("2.0", skip[double_] << double_, p));

        r %= skip[ *(int_[_a = _a + _1]) ] << int_[_1 = _a];
        BOOST_TEST(test("0", r, a));
    }
    
    return boost::report_errors();
}