]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. |
2 | ||
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #include <boost/leaf/result.hpp> | |
7 | #include <boost/leaf/capture.hpp> | |
8 | #include <boost/leaf/handle_errors.hpp> | |
9 | #include "lightweight_test.hpp" | |
10 | ||
11 | namespace leaf = boost::leaf; | |
12 | ||
13 | struct val | |
14 | { | |
15 | int id; | |
16 | ||
17 | friend bool operator==( val const & a, val const & b ) | |
18 | { | |
19 | return a.id==b.id; | |
20 | } | |
21 | ||
22 | friend std::ostream & operator<<( std::ostream & os, val const & v ) | |
23 | { | |
24 | return os << v.id; | |
25 | } | |
26 | }; | |
27 | ||
28 | struct base | |
29 | { | |
30 | }; | |
31 | ||
32 | struct derived: base | |
33 | { | |
34 | }; | |
35 | ||
36 | int main() | |
37 | { | |
38 | { | |
39 | leaf::result<val const> r1, r2; | |
40 | leaf::result<val const> & ref = r1; | |
41 | leaf::result<val const> const & cref = r1; | |
42 | leaf::result<val const> && rvref = std::move(r1); | |
43 | leaf::result<val const> const && rvcref = std::move(r2); | |
44 | ||
45 | static_assert(std::is_same<decltype(ref.value()), val const &>::value, "result type deduction bug"); | |
46 | static_assert(std::is_same<decltype(cref.value()), val const &>::value, "result type deduction bug"); | |
47 | static_assert(std::is_same<decltype(std::move(rvref.value())), val const &&>::value, "result type deduction bug"); | |
48 | static_assert(std::is_same<decltype(std::move(rvcref.value())), val const &&>::value, "result type deduction bug"); | |
49 | ||
50 | static_assert(std::is_same<decltype(*ref), val const &>::value, "result type deduction bug"); | |
51 | static_assert(std::is_same<decltype(*cref), val const &>::value, "result type deduction bug"); | |
52 | static_assert(std::is_same<decltype(std::move(*rvref)), val const &&>::value, "result type deduction bug"); | |
53 | static_assert(std::is_same<decltype(std::move(*rvcref)), val const &&>::value, "result type deduction bug"); | |
54 | ||
55 | auto & ref_id = ref->id; static_assert(std::is_same<decltype(ref_id), int const &>::value, "result type deduction bug"); | |
56 | auto & cref_id = cref->id; static_assert(std::is_same<decltype(cref_id), int const &>::value, "result type deduction bug"); | |
57 | auto & rvref_id = rvref->id; static_assert(std::is_same<decltype(rvref_id), int const &>::value, "result type deduction bug"); | |
58 | auto & rvcref_id = rvcref->id; static_assert(std::is_same<decltype(rvcref_id), int const &>::value, "result type deduction bug"); | |
59 | } | |
60 | ||
61 | { | |
62 | leaf::result<val> r1, r2; | |
63 | leaf::result<val> & ref = r1; | |
64 | leaf::result<val> const & cref = r1; | |
65 | leaf::result<val> && rvref = std::move(r1); | |
66 | leaf::result<val> const && rvcref = std::move(r2); | |
67 | ||
68 | static_assert(std::is_same<decltype(ref.value()), val &>::value, "result type deduction bug"); | |
69 | static_assert(std::is_same<decltype(cref.value()), val const &>::value, "result type deduction bug"); | |
70 | static_assert(std::is_same<decltype(std::move(rvref.value())), val &&>::value, "result type deduction bug"); | |
71 | static_assert(std::is_same<decltype(std::move(rvcref.value())), val const &&>::value, "result type deduction bug"); | |
72 | ||
73 | static_assert(std::is_same<decltype(*ref), val &>::value, "result type deduction bug"); | |
74 | static_assert(std::is_same<decltype(*cref), val const &>::value, "result type deduction bug"); | |
75 | static_assert(std::is_same<decltype(std::move(*rvref)), val &&>::value, "result type deduction bug"); | |
76 | static_assert(std::is_same<decltype(std::move(*rvcref)), val const &&>::value, "result type deduction bug"); | |
77 | ||
78 | auto & ref_id = ref->id; static_assert(std::is_same<decltype(ref_id), int &>::value, "result type deduction bug"); | |
79 | auto & cref_id = cref->id; static_assert(std::is_same<decltype(cref_id), int const &>::value, "result type deduction bug"); | |
80 | auto & rvref_id = rvref->id; static_assert(std::is_same<decltype(rvref_id), int &>::value, "result type deduction bug"); | |
81 | auto & rvcref_id = rvcref->id; static_assert(std::is_same<decltype(rvcref_id), int const &>::value, "result type deduction bug"); | |
82 | } | |
83 | ||
84 | { | |
85 | val v; | |
86 | leaf::result<val const &> r1(v), r2(v); | |
87 | leaf::result<val const &> & ref = r1; | |
88 | leaf::result<val const &> const & cref = r1; | |
89 | leaf::result<val const &> && rvref = std::move(r1); | |
90 | leaf::result<val const &> const && rvcref = std::move(r2); | |
91 | ||
92 | static_assert(std::is_same<decltype(ref.value()), val const &>::value, "result type deduction bug"); | |
93 | static_assert(std::is_same<decltype(cref.value()), val const &>::value, "result type deduction bug"); | |
94 | static_assert(std::is_same<decltype(std::move(rvref.value())), val const &&>::value, "result type deduction bug"); | |
95 | static_assert(std::is_same<decltype(std::move(rvcref.value())), val const &&>::value, "result type deduction bug"); | |
96 | ||
97 | static_assert(std::is_same<decltype(*ref), val const &>::value, "result type deduction bug"); | |
98 | static_assert(std::is_same<decltype(*cref), val const &>::value, "result type deduction bug"); | |
99 | static_assert(std::is_same<decltype(std::move(*rvref)), val const &&>::value, "result type deduction bug"); | |
100 | static_assert(std::is_same<decltype(std::move(*rvcref)), val const &&>::value, "result type deduction bug"); | |
101 | ||
102 | auto & ref_id = ref->id; static_assert(std::is_same<decltype(ref_id), int const &>::value, "result type deduction bug"); | |
103 | auto & cref_id = cref->id; static_assert(std::is_same<decltype(cref_id), int const &>::value, "result type deduction bug"); | |
104 | auto & rvref_id = rvref->id; static_assert(std::is_same<decltype(rvref_id), int const &>::value, "result type deduction bug"); | |
105 | auto & rvcref_id = rvcref->id; static_assert(std::is_same<decltype(rvcref_id), int const &>::value, "result type deduction bug"); | |
106 | } | |
107 | ||
108 | { | |
109 | val v; | |
110 | leaf::result<val &> r1(v), r2(v); | |
111 | leaf::result<val &> & ref = r1; | |
112 | leaf::result<val &> const & cref = r1; | |
113 | leaf::result<val &> && rvref = std::move(r1); | |
114 | leaf::result<val &> const && rvcref = std::move(r2); | |
115 | ||
116 | static_assert(std::is_same<decltype(ref.value()), val &>::value, "result type deduction bug"); | |
117 | static_assert(std::is_same<decltype(cref.value()), val &>::value, "result type deduction bug"); | |
118 | static_assert(std::is_same<decltype(std::move(rvref.value())), val &&>::value, "result type deduction bug"); | |
119 | static_assert(std::is_same<decltype(std::move(rvcref.value())), val &&>::value, "result type deduction bug"); | |
120 | ||
121 | static_assert(std::is_same<decltype(*ref), val &>::value, "result type deduction bug"); | |
122 | static_assert(std::is_same<decltype(*cref), val &>::value, "result type deduction bug"); | |
123 | static_assert(std::is_same<decltype(std::move(*rvref)), val &&>::value, "result type deduction bug"); | |
124 | static_assert(std::is_same<decltype(std::move(*rvcref)), val &&>::value, "result type deduction bug"); | |
125 | ||
126 | auto & ref_id = ref->id; static_assert(std::is_same<decltype(ref_id), int &>::value, "result type deduction bug"); | |
127 | auto & cref_id = cref->id; static_assert(std::is_same<decltype(cref_id), int &>::value, "result type deduction bug"); | |
128 | auto & rvref_id = rvref->id; static_assert(std::is_same<decltype(rvref_id), int &>::value, "result type deduction bug"); | |
129 | auto & rvcref_id = rvcref->id; static_assert(std::is_same<decltype(rvcref_id), int &>::value, "result type deduction bug"); | |
130 | } | |
131 | ||
132 | // Mutable: | |
133 | ||
134 | { | |
135 | val x = { 42 }; | |
136 | leaf::result<val const &> r(x); | |
137 | BOOST_TEST(r); | |
138 | val a = r.value(); | |
139 | BOOST_TEST_EQ(a, x); | |
140 | val b = *r; | |
141 | BOOST_TEST_EQ(b, x); | |
142 | } | |
143 | { | |
144 | val x = { 42 }; | |
145 | leaf::result<val const &> r(x); | |
146 | BOOST_TEST(r); | |
147 | val const & a = r.value(); | |
148 | BOOST_TEST_EQ(&a, &x); | |
149 | val const & b = *r; | |
150 | BOOST_TEST_EQ(&b, &x); | |
151 | } | |
152 | { | |
153 | val x = { 42 }; | |
154 | leaf::result<val const &> r(x); | |
155 | BOOST_TEST(r); | |
156 | auto & a = r.value(); | |
157 | BOOST_TEST_EQ(&a, &x); | |
158 | auto & b = *r; | |
159 | BOOST_TEST_EQ(&b, &x); | |
160 | } | |
161 | ||
162 | { | |
163 | val x = { 42 }; | |
164 | leaf::result<val &> r(x); | |
165 | BOOST_TEST(r); | |
166 | val a = r.value(); | |
167 | BOOST_TEST_EQ(a, x); | |
168 | val b = *r; | |
169 | BOOST_TEST_EQ(b, x); | |
170 | int id = x.id; | |
171 | BOOST_TEST_EQ(id+1, ++r->id); | |
172 | } | |
173 | { | |
174 | val x = { 42 }; | |
175 | leaf::result<val &> r(x); | |
176 | BOOST_TEST(r); | |
177 | val & a = r.value(); | |
178 | BOOST_TEST_EQ(&a, &x); | |
179 | val & b = *r; | |
180 | BOOST_TEST_EQ(&b, &x); | |
181 | int id = x.id; | |
182 | BOOST_TEST_EQ(id+1, ++r->id); | |
183 | } | |
184 | { | |
185 | val x = { 42 }; | |
186 | leaf::result<val &> r(x); | |
187 | BOOST_TEST(r); | |
188 | auto & a = r.value(); | |
189 | BOOST_TEST_EQ(&a, &x); | |
190 | auto & b = *r; | |
191 | BOOST_TEST_EQ(&b, &x); | |
192 | int id = x.id; | |
193 | BOOST_TEST_EQ(id+1, ++r->id); | |
194 | } | |
195 | ||
196 | // Const: | |
197 | ||
198 | { | |
199 | val x = { 42 }; | |
200 | leaf::result<val const &> const r(x); | |
201 | BOOST_TEST(r); | |
202 | val a = r.value(); | |
203 | BOOST_TEST_EQ(a, x); | |
204 | val b = *r; | |
205 | BOOST_TEST_EQ(b, x); | |
206 | } | |
207 | { | |
208 | val x = { 42 }; | |
209 | leaf::result<val const &> const r(x); | |
210 | BOOST_TEST(r); | |
211 | val const & a = r.value(); | |
212 | BOOST_TEST_EQ(&a, &x); | |
213 | val const & b = *r; | |
214 | BOOST_TEST_EQ(&b, &x); | |
215 | } | |
216 | { | |
217 | val x = { 42 }; | |
218 | leaf::result<val const &> const r(x); | |
219 | BOOST_TEST(r); | |
220 | auto & a = r.value(); | |
221 | BOOST_TEST_EQ(&a, &x); | |
222 | auto & b = *r; | |
223 | BOOST_TEST_EQ(&b, &x); | |
224 | } | |
225 | ||
226 | { | |
227 | val x = { 42 }; | |
228 | leaf::result<val &> const r(x); | |
229 | BOOST_TEST(r); | |
230 | val a = r.value(); | |
231 | BOOST_TEST_EQ(a, x); | |
232 | val b = *r; | |
233 | BOOST_TEST_EQ(b, x); | |
234 | int id = x.id; | |
235 | BOOST_TEST_EQ(id+1, ++r->id); | |
236 | } | |
237 | { | |
238 | val x = { 42 }; | |
239 | leaf::result<val &> const r(x); | |
240 | BOOST_TEST(r); | |
241 | val & a = r.value(); | |
242 | BOOST_TEST_EQ(&a, &x); | |
243 | val & b = *r; | |
244 | BOOST_TEST_EQ(&b, &x); | |
245 | } | |
246 | { | |
247 | val x = { 42 }; | |
248 | leaf::result<val &> const r(x); | |
249 | BOOST_TEST(r); | |
250 | auto & a = r.value(); | |
251 | BOOST_TEST_EQ(&a, &x); | |
252 | auto & b = *r; | |
253 | BOOST_TEST_EQ(&b, &x); | |
254 | int id = x.id; | |
255 | BOOST_TEST_EQ(id+1, ++r->id); | |
256 | } | |
257 | ||
258 | // Hierarchy | |
259 | ||
260 | { | |
261 | derived d; | |
262 | leaf::result<base &> r = d; | |
263 | BOOST_TEST_EQ(&r.value(), &d); | |
264 | BOOST_TEST_EQ(&*r, &d); | |
265 | BOOST_TEST_EQ(r.operator->(), &d); | |
266 | } | |
267 | ||
268 | { | |
269 | derived d; | |
270 | leaf::result<base *> r = &d; | |
271 | BOOST_TEST_EQ(r.value(), &d); | |
272 | BOOST_TEST_EQ(*r, &d); | |
273 | BOOST_TEST_EQ(*r.operator->(), &d); | |
274 | } | |
275 | ||
276 | return boost::report_errors(); | |
277 | } |