summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/yap/test/value.cpp
blob: 5ca933afcd61dd936359b09c6f4757526b55216a (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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>


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

template<typename T>
using ref = boost::yap::expression_ref<boost::yap::expression, T>;

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


template<boost::yap::expr_kind Kind, typename Tuple>
struct user_expr
{
    static boost::yap::expr_kind const kind = Kind;

    Tuple elements;
};

BOOST_YAP_USER_BINARY_OPERATOR(plus, user_expr, user_expr)

template<typename T>
using user_term = boost::yap::terminal<user_expr, T>;

template<typename T>
using user_ref = boost::yap::expression_ref<user_expr, T>;


int test_main(int, char * [])
{
    {
        {
            BOOST_MPL_ASSERT((std::is_same<decltype(yap::value(1.0)), double &&>));
            BOOST_CHECK(yap::value(1.0) == 1.0);
        }

        {
            double d = 2.0;
            BOOST_MPL_ASSERT((std::is_same<decltype(yap::value(d)), double &>));
            BOOST_CHECK(yap::value(d) == 2.0);
        }

        {
            double const d = 3.0;
            BOOST_MPL_ASSERT(
                (std::is_same<decltype(yap::value(d)), double const &>));
            BOOST_CHECK(yap::value(d) == 3.0);
        }
    }

    {
        {
            term<double> td = {{1.0}};
            BOOST_MPL_ASSERT(
                (std::is_same<decltype(yap::value(std::move(td))), double &&>));
            BOOST_CHECK(yap::value(std::move(td)) == 1.0);
        }

        {
            term<double> td = {{2.0}};
            BOOST_MPL_ASSERT((std::is_same<decltype(yap::value(td)), double &>));
            BOOST_CHECK(yap::value(td) == 2.0);
        }

        {
            term<double> const td = {{3.0}};
            BOOST_MPL_ASSERT(
                (std::is_same<decltype(yap::value(td)), double const &>));
            BOOST_CHECK(yap::value(td) == 3.0);
        }

        term<double> unity = {{1.0}};
        using plus_expr_type = yap::expression<
            yap::expr_kind::plus,
            bh::tuple<ref<term<double> &>, term<int>>>;
        plus_expr_type plus_expr = unity + term<int>{{1}};

        {
            ref<term<double> &> ref = bh::front(plus_expr.elements);
            BOOST_MPL_ASSERT(
                (std::is_same<decltype(yap::value(std::move(ref))), double &>));
        }

        {
            ref<term<double> &> ref = bh::front(plus_expr.elements);
            BOOST_MPL_ASSERT((std::is_same<decltype(yap::value(ref)), double &>));
        }

        {
            ref<term<double> &> const ref = bh::front(plus_expr.elements);
            BOOST_MPL_ASSERT((std::is_same<decltype(yap::value(ref)), double &>));
        }

        {
            term<double> const unity = {{1.0}};
            yap::expression<
                yap::expr_kind::plus,
                bh::tuple<ref<term<double> const &>, term<int>>>
                plus_expr = unity + term<int>{{1}};

            {
                ref<term<double> const &> ref = bh::front(plus_expr.elements);
                BOOST_MPL_ASSERT((std::is_same<
                                  decltype(yap::value(std::move(ref))),
                                  double const &>));
            }

            {
                ref<term<double> const &> ref = bh::front(plus_expr.elements);
                BOOST_MPL_ASSERT(
                    (std::is_same<decltype(yap::value(ref)), double const &>));
            }

            {
                ref<term<double> const &> const ref = bh::front(plus_expr.elements);
                BOOST_MPL_ASSERT(
                    (std::is_same<decltype(yap::value(ref)), double const &>));
            }
        }
    }

    {
        {
            user_term<double> td = {{1.0}};
            BOOST_MPL_ASSERT(
                (std::is_same<decltype(yap::value(std::move(td))), double &&>));
            BOOST_CHECK(yap::value(std::move(td)) == 1.0);
        }

        {
            user_term<double> td = {{2.0}};
            BOOST_MPL_ASSERT((std::is_same<decltype(yap::value(td)), double &>));
            BOOST_CHECK(yap::value(td) == 2.0);
        }

        {
            user_term<double> const td = {{3.0}};
            BOOST_MPL_ASSERT(
                (std::is_same<decltype(yap::value(td)), double const &>));
            BOOST_CHECK(yap::value(td) == 3.0);
        }

        user_term<double> unity = {{1.0}};
        using plus_expr_type = user_expr<
            yap::expr_kind::plus,
            bh::tuple<user_ref<user_term<double> &>, user_term<int>>>;
        plus_expr_type plus_expr = unity + user_term<int>{{1}};

        {
            user_ref<user_term<double> &> ref = bh::front(plus_expr.elements);
            BOOST_MPL_ASSERT(
                (std::is_same<decltype(yap::value(std::move(ref))), double &>));
        }

        {
            user_ref<user_term<double> &> ref = bh::front(plus_expr.elements);
            BOOST_MPL_ASSERT((std::is_same<decltype(yap::value(ref)), double &>));
        }

        {
            user_ref<user_term<double> &> const ref = bh::front(plus_expr.elements);
            BOOST_MPL_ASSERT((std::is_same<decltype(yap::value(ref)), double &>));
        }

        {
            user_term<double> const unity = {{1.0}};
            user_expr<
                yap::expr_kind::plus,
                bh::tuple<user_ref<user_term<double> const &>, user_term<int>>>
                plus_expr = unity + user_term<int>{{1}};

            {
                user_ref<user_term<double> const &> ref =
                    bh::front(plus_expr.elements);
                BOOST_MPL_ASSERT((std::is_same<
                                  decltype(yap::value(std::move(ref))),
                                  double const &>));
            }

            {
                user_ref<user_term<double> const &> ref =
                    bh::front(plus_expr.elements);
                BOOST_MPL_ASSERT(
                    (std::is_same<decltype(yap::value(ref)), double const &>));
            }

            {
                user_ref<user_term<double> const &> const ref =
                    bh::front(plus_expr.elements);
                BOOST_MPL_ASSERT(
                    (std::is_same<decltype(yap::value(ref)), double const &>));
            }
        }
    }

    return 0;
}