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/test/minimal.hpp>
9
10 #include <sstream>
11
12
13 template<typename T>
14 using term = boost::yap::terminal<boost::yap::expression, T>;
15
16 template<typename T>
17 using term_ref = boost::yap::expression_ref<boost::yap::expression, term<T> &>;
18
19 template<typename T>
20 using term_cref =
21 boost::yap::expression_ref<boost::yap::expression, term<T> const &>;
22
23 namespace yap = boost::yap;
24 namespace bh = boost::hana;
25
26
test_main(int,char * [])27 int test_main(int, char * [])
28 {
29 {
30 term<uint32_t> x{{2u}};
31 term<uint32_t> const cx{{3u}};
32
33 {
34 yap::expression<
35 yap::expr_kind::unary_plus,
36 bh::tuple<term<uint32_t>>>
37 term_expr = +term<uint32_t>{{1u}};
38 yap::expression<
39 yap::expr_kind::unary_plus,
40 bh::tuple<term_ref<uint32_t>>>
41 term_ref_expr = +x;
42 yap::expression<
43 yap::expr_kind::unary_plus,
44 bh::tuple<term_cref<uint32_t>>>
45 term_const_ref_expr = +cx;
46
47 BOOST_CHECK(evaluate(term_expr) == 1u);
48 BOOST_CHECK(evaluate(term_ref_expr) == 2u);
49 BOOST_CHECK(evaluate(term_const_ref_expr) == 3u);
50 }
51
52 {
53 yap::expression<yap::expr_kind::negate, bh::tuple<term<uint32_t>>>
54 term_expr = -term<uint32_t>{{1u}};
55 yap::expression<
56 yap::expr_kind::negate,
57 bh::tuple<term_ref<uint32_t>>>
58 term_ref_expr = -x;
59 yap::expression<
60 yap::expr_kind::negate,
61 bh::tuple<term_cref<uint32_t>>>
62 term_const_ref_expr = -cx;
63
64 BOOST_CHECK(evaluate(term_expr) == 0u - 1u);
65 BOOST_CHECK(evaluate(term_ref_expr) == 0u - 2u);
66 BOOST_CHECK(evaluate(term_const_ref_expr) == 0u - 3u);
67 }
68
69 {
70 yap::expression<
71 yap::expr_kind::complement,
72 bh::tuple<term<uint32_t>>>
73 term_expr = ~term<uint32_t>{{1u}};
74 yap::expression<
75 yap::expr_kind::complement,
76 bh::tuple<term_ref<uint32_t>>>
77 term_ref_expr = ~x;
78 yap::expression<
79 yap::expr_kind::complement,
80 bh::tuple<term_cref<uint32_t>>>
81 term_const_ref_expr = ~cx;
82
83 BOOST_CHECK(evaluate(term_expr) == 0xfffffffe);
84 BOOST_CHECK(evaluate(term_ref_expr) == 0xfffffffd);
85 BOOST_CHECK(evaluate(term_const_ref_expr) == 0xfffffffc);
86 }
87
88 {
89 yap::expression<
90 yap::expr_kind::logical_not,
91 bh::tuple<term<uint32_t>>>
92 term_expr = !term<uint32_t>{{1u}};
93 yap::expression<
94 yap::expr_kind::logical_not,
95 bh::tuple<term_ref<uint32_t>>>
96 term_ref_expr = !x;
97 yap::expression<
98 yap::expr_kind::logical_not,
99 bh::tuple<term_cref<uint32_t>>>
100 term_const_ref_expr = !cx;
101
102 BOOST_CHECK(evaluate(term_expr) == false);
103 BOOST_CHECK(evaluate(term_ref_expr) == false);
104 BOOST_CHECK(evaluate(term_const_ref_expr) == false);
105 }
106
107 {
108 yap::expression<yap::expr_kind::pre_inc, bh::tuple<term<uint32_t>>>
109 term_expr = ++term<uint32_t>{{1u}};
110 yap::expression<
111 yap::expr_kind::pre_inc,
112 bh::tuple<term_ref<uint32_t>>>
113 term_ref_expr = ++x;
114 yap::expression<
115 yap::expr_kind::pre_inc,
116 bh::tuple<term_cref<uint32_t>>>
117 term_const_ref_expr = ++cx;
118 (void)term_const_ref_expr;
119
120 BOOST_CHECK(evaluate(term_expr) == 2u);
121 BOOST_CHECK(evaluate(term_ref_expr) == 3u);
122 }
123 }
124
125 {
126 {
127 uint32_t i = 1, j = 2, k = 3;
128 term<uint32_t &> x{{j}};
129 term<uint32_t &> const cx{{k}};
130
131 yap::expression<
132 yap::expr_kind::address_of,
133 bh::tuple<term<uint32_t &>>>
134 term_expr = &term<uint32_t &>{{i}};
135 yap::expression<
136 yap::expr_kind::address_of,
137 bh::tuple<term_ref<uint32_t &>>>
138 term_ref_expr = &x;
139 yap::expression<
140 yap::expr_kind::address_of,
141 bh::tuple<term_cref<uint32_t &>>>
142 term_const_ref_expr = &cx;
143
144 BOOST_CHECK(evaluate(term_expr) == &i);
145 BOOST_CHECK(evaluate(term_ref_expr) == &j);
146 BOOST_CHECK(evaluate(term_const_ref_expr) == &k);
147 }
148
149 {
150 uint32_t i = 1, j = 2, k = 3;
151 term<uint32_t *> x{{&j}};
152 term<uint32_t *> const cx{{&k}};
153
154 yap::expression<
155 yap::expr_kind::dereference,
156 bh::tuple<term<uint32_t *>>>
157 term_expr = *term<uint32_t *>{{&i}};
158 yap::expression<
159 yap::expr_kind::dereference,
160 bh::tuple<term_ref<uint32_t *>>>
161 term_ref_expr = *x;
162 yap::expression<
163 yap::expr_kind::dereference,
164 bh::tuple<term_cref<uint32_t *>>>
165 term_const_ref_expr = *cx;
166
167 BOOST_CHECK(evaluate(term_expr) == i);
168 BOOST_CHECK(evaluate(term_ref_expr) == j);
169 BOOST_CHECK(evaluate(term_const_ref_expr) == k);
170 }
171 }
172
173 return 0;
174 }
175