summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/yap/test/comma.cpp
blob: af3814cf5112ca62ec09da380887cc7d6fee9d99 (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
// Copyright (C) 2016-2018 T. Zachary Laine
//
// 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/yap/expression.hpp>

#include <boost/mpl/assert.hpp>

#include <boost/test/minimal.hpp>

#include <sstream>


template<typename T>
using term = boost::yap::terminal<boost::yap::expression, T>;

template<typename T>
using term_ref = boost::yap::expression_ref<boost::yap::expression, term<T> &>;

template<typename T>
using term_cref =
    boost::yap::expression_ref<boost::yap::expression, term<T> const &>;

namespace yap = boost::yap;
namespace bh = boost::hana;


struct void_callable
{
    void operator()() { *called_ = (*call_count_)++; }
    int * call_count_;
    int * called_;
};

struct int_callable
{
    int operator()()
    {
        *called_ = (*call_count_)++;
        return 42;
    }
    int * call_count_;
    int * called_;
};

struct double_callable
{
    double operator()()
    {
        *called_ = (*call_count_)++;
        return 13.0;
    }
    int * call_count_;
    int * called_;
};


int test_main(int, char * [])
{
{
    {
        int call_count = 0;
        int int_called = -1;
        int double_called = -1;

        auto int_double_expr =
            (term<int_callable>{{&call_count, &int_called}}(),
             term<double_callable>{{&call_count, &double_called}}());

        BOOST_CHECK(evaluate(int_double_expr) == 13.0);
        BOOST_CHECK(int_called == 0);
        BOOST_CHECK(double_called == 1);
    }

    {
        int call_count = 0;
        int int_called = -1;
        int double_called = -1;

        auto double_int_expr =
            (term<double_callable>{{&call_count, &double_called}}(),
             term<int_callable>{{&call_count, &int_called}}());

        BOOST_CHECK(evaluate(double_int_expr) == 42);
        BOOST_CHECK(int_called == 1);
        BOOST_CHECK(double_called == 0);
    }
}

{
    {
        int call_count = 0;
        int void_called = -1;
        int int_called = -1;

        auto void_int_expr =
            (term<void_callable>{{&call_count, &void_called}}(),
             term<int_callable>{{&call_count, &int_called}}());

        BOOST_CHECK(evaluate(void_int_expr) == 42);
        BOOST_CHECK(void_called == 0);
        BOOST_CHECK(int_called == 1);
    }

    {
        int call_count = 0;
        int void_called = -1;
        int int_called = -1;

        auto int_void_expr =
            (term<int_callable>{{&call_count, &int_called}}(),
             term<void_callable>{{&call_count, &void_called}}());

        using eval_type = decltype(evaluate(int_void_expr));
        BOOST_MPL_ASSERT(
            (std::is_same<void, eval_type>));

        evaluate(int_void_expr);
        BOOST_CHECK(void_called == 1);
        BOOST_CHECK(int_called == 0);
    }
}

return 0;
}