summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/proto/test/switch.cpp
blob: be5881f54b59af367c66165de8ef1b11e33cc8f9 (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
///////////////////////////////////////////////////////////////////////////////
// new_switch.cpp
//
//  Copyright 2011 Eric Niebler
//  Copyright Pierre Esterie & Joel Falcou.
//  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/proto/core.hpp>
#include <boost/proto/transform.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/proto/debug.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/bool.hpp>

namespace proto = boost::proto;

struct MyCases
{
    template<typename Tag>
    struct case_
      : proto::not_<proto::_>
    {};
};

template<>
struct MyCases::case_<proto::tag::shift_right>
  : proto::_
{};

template<>
struct MyCases::case_<proto::tag::plus>
  : proto::_
{};

struct ArityOf;

struct ArityOfCases
{
    template<typename ArityOf>
    struct case_
      : proto::not_<proto::_>
    {};
};


template<>
struct ArityOfCases::case_<boost::mpl::long_<1> >
  : boost::proto::when<boost::proto::_, boost::mpl::false_()>
{};

template<>
struct ArityOfCases::case_<boost::mpl::long_<2> >
  : boost::proto::when<boost::proto::_, boost::mpl::true_()>
{};

struct ArityOf
  : boost::proto::switch_<
        ArityOfCases
      , proto::arity_of<proto::_>()
    >
{};

void test_switch()
{
    // Tests for backward compatibility
    proto::assert_matches<proto::switch_<MyCases> >(proto::lit(1) >> 'a');
    proto::assert_matches<proto::switch_<MyCases> >(proto::lit(1) + 'a');
    proto::assert_matches_not<proto::switch_<MyCases> >(proto::lit(1) << 'a');

    //Test new matching on the Transform result type
    ArityOf ar;

    proto::assert_matches_not<ArityOf>(proto::lit(1));
    proto::assert_matches<ArityOf>(proto::lit(1) + 2);
    proto::assert_matches<ArityOf>(!proto::lit(1));
    BOOST_CHECK_EQUAL(ar(!proto::lit(1)), false);
    BOOST_CHECK_EQUAL(ar(proto::lit(1) + 2), true);
}

using namespace boost::unit_test;
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite(int argc, char* argv[])
{
    test_suite *test = BOOST_TEST_SUITE("test proto::switch_<>");

    test->add(BOOST_TEST_CASE(&test_switch));

    return test;
}