• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2016-2018 T. Zachary Laine
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/yap/expression.hpp>
7 
8 #include <boost/mpl/assert.hpp>
9 
10 #include <boost/test/minimal.hpp>
11 
12 
13 template<typename T>
14 using term = boost::yap::terminal<boost::yap::expression, T>;
15 
16 template<typename T>
17 using ref = boost::yap::expression_ref<boost::yap::expression, T>;
18 
19 namespace yap = boost::yap;
20 namespace bh = boost::hana;
21 
22 
23 template<boost::yap::expr_kind Kind, typename Tuple>
24 struct user_expr
25 {
26     static boost::yap::expr_kind const kind = Kind;
27 
28     Tuple elements;
29 };
30 
31 BOOST_YAP_USER_BINARY_OPERATOR(plus, user_expr, user_expr)
32 
33 template<typename T>
34 using user_term = boost::yap::terminal<user_expr, T>;
35 
36 template<typename T>
37 using user_ref = boost::yap::expression_ref<user_expr, T>;
38 
39 
test_main(int,char * [])40 int test_main(int, char * [])
41 {
42     {
43         term<double> unity = {{1.0}};
44         using plus_expr_type = yap::expression<
45             yap::expr_kind::plus,
46             bh::tuple<ref<term<double> &>, term<int>>>;
47 
48         plus_expr_type plus_expr = unity + term<int>{{1}};
49 
50         {
51             ref<term<double> &> ref = bh::front(plus_expr.elements);
52             BOOST_MPL_ASSERT((std::is_same<
53                               decltype(yap::deref(std::move(ref))),
54                               term<double> &>));
55             BOOST_CHECK(yap::value(ref) == 1.0);
56         }
57 
58         {
59             ref<term<double> &> ref = bh::front(plus_expr.elements);
60             BOOST_MPL_ASSERT(
61                 (std::is_same<decltype(yap::deref(ref)), term<double> &>));
62             BOOST_CHECK(yap::value(ref) == 1.0);
63         }
64 
65         {
66             ref<term<double> &> const ref = bh::front(plus_expr.elements);
67             BOOST_MPL_ASSERT(
68                 (std::is_same<decltype(yap::deref(ref)), term<double> &>));
69             BOOST_CHECK(yap::value(ref) == 1.0);
70         }
71 
72         {
73             term<double> const unity = {{1.0}};
74             yap::expression<
75                 yap::expr_kind::plus,
76                 bh::tuple<ref<term<double> const &>, term<int>>>
77                 plus_expr = unity + term<int>{{1}};
78 
79             {
80                 ref<term<double> const &> ref = bh::front(plus_expr.elements);
81                 BOOST_MPL_ASSERT((std::is_same<
82                                   decltype(yap::deref(std::move(ref))),
83                                   term<double> const &>));
84                 BOOST_CHECK(yap::value(ref) == 1.0);
85             }
86 
87             {
88                 ref<term<double> const &> ref = bh::front(plus_expr.elements);
89                 BOOST_MPL_ASSERT((std::is_same<
90                                   decltype(yap::deref(ref)),
91                                   term<double> const &>));
92                 BOOST_CHECK(yap::value(ref) == 1.0);
93             }
94 
95             {
96                 ref<term<double> const &> const ref =
97                     bh::front(plus_expr.elements);
98                 BOOST_MPL_ASSERT((std::is_same<
99                                   decltype(yap::deref(ref)),
100                                   term<double> const &>));
101                 BOOST_CHECK(yap::value(ref) == 1.0);
102             }
103         }
104     }
105 
106     {
107         user_term<double> unity = {{1.0}};
108         using plus_expr_type = user_expr<
109             yap::expr_kind::plus,
110             bh::tuple<user_ref<user_term<double> &>, user_term<int>>>;
111 
112         plus_expr_type plus_expr = unity + user_term<int>{{1}};
113 
114         {
115             user_ref<user_term<double> &> ref = bh::front(plus_expr.elements);
116             BOOST_MPL_ASSERT((std::is_same<
117                               decltype(yap::deref(std::move(ref))),
118                               user_term<double> &>));
119             BOOST_CHECK(yap::value(ref) == 1.0);
120         }
121 
122         {
123             user_ref<user_term<double> &> ref = bh::front(plus_expr.elements);
124             BOOST_MPL_ASSERT(
125                 (std::is_same<decltype(yap::deref(ref)), user_term<double> &>));
126             BOOST_CHECK(yap::value(ref) == 1.0);
127         }
128 
129         {
130             user_ref<user_term<double> &> const ref =
131                 bh::front(plus_expr.elements);
132             BOOST_MPL_ASSERT(
133                 (std::is_same<decltype(yap::deref(ref)), user_term<double> &>));
134             BOOST_CHECK(yap::value(ref) == 1.0);
135         }
136 
137         {
138             user_term<double> const unity = {{1.0}};
139             user_expr<
140                 yap::expr_kind::plus,
141                 bh::tuple<user_ref<user_term<double> const &>, user_term<int>>>
142                 plus_expr = unity + user_term<int>{{1}};
143 
144             {
145                 user_ref<user_term<double> const &> ref =
146                     bh::front(plus_expr.elements);
147                 BOOST_MPL_ASSERT((std::is_same<
148                                   decltype(yap::deref(std::move(ref))),
149                                   user_term<double> const &>));
150                 BOOST_CHECK(yap::value(ref) == 1.0);
151             }
152 
153             {
154                 user_ref<user_term<double> const &> ref =
155                     bh::front(plus_expr.elements);
156                 BOOST_MPL_ASSERT((std::is_same<
157                                   decltype(yap::deref(ref)),
158                                   user_term<double> const &>));
159                 BOOST_CHECK(yap::value(ref) == 1.0);
160             }
161 
162             {
163                 user_ref<user_term<double> const &> const ref =
164                     bh::front(plus_expr.elements);
165                 BOOST_MPL_ASSERT((std::is_same<
166                                   decltype(yap::deref(ref)),
167                                   user_term<double> const &>));
168                 BOOST_CHECK(yap::value(ref) == 1.0);
169             }
170         }
171     }
172 
173     return 0;
174 }
175