9 #include <boost/test/unit_test.hpp>
15 #include <system_error>
16 #include <type_traits>
19 using namespace std::string_literals;
29 return {
static_cast<int>(
e), std::generic_category()};
44 BOOST_AUTO_TEST_SUITE(Utilities)
50 Result res = Result::success(42);
51 BOOST_CHECK_EQUAL(*res, 42);
52 BOOST_CHECK_EQUAL(res.value(), 42);
53 BOOST_CHECK(res.ok());
54 res = Result::success(
'e');
55 BOOST_CHECK_EQUAL(*res,
'e');
56 BOOST_CHECK_EQUAL(res.value(),
'e');
57 BOOST_CHECK(res.ok());
58 res = Result::failure(42);
59 BOOST_CHECK(!res.ok());
60 BOOST_CHECK_EQUAL(res.error(), 42);
61 BOOST_CHECK_THROW(res.value(), std::runtime_error);
62 res = Result::failure(
'e');
63 BOOST_CHECK(!res.ok());
64 BOOST_CHECK_EQUAL(res.error(),
'e');
65 BOOST_CHECK_THROW(res.value(), std::runtime_error);
72 BOOST_CHECK(!res1.ok());
73 BOOST_CHECK_EQUAL(res1.error(),
"hallo");
74 BOOST_CHECK_THROW(res1.value(), std::runtime_error);
75 res1 = Result::failure(
"hallo");
76 BOOST_CHECK(!res1.ok());
77 BOOST_CHECK_EQUAL(res1.error(),
"hallo");
78 BOOST_CHECK_THROW(res1.value(), std::runtime_error);
81 BOOST_CHECK(res2.ok());
82 BOOST_CHECK(*res2 == 4.5);
83 BOOST_CHECK(res2.value() == 4.5);
84 res2 = Result::success(4.5);
85 BOOST_CHECK(res2.ok());
86 BOOST_CHECK_EQUAL(*res2, 4.5);
87 BOOST_CHECK_EQUAL(res2.value(), 4.5);
92 auto err1 = MyError::Failure;
94 std::error_code ec = err1;
100 BOOST_CHECK(res.ok());
101 BOOST_CHECK_EQUAL(*res, 42.);
104 BOOST_CHECK(!res2.ok());
105 BOOST_CHECK_EQUAL(res2.error(), err1);
106 BOOST_CHECK_THROW(res2.value(), std::runtime_error);
113 BOOST_CHECK(res.ok());
114 BOOST_CHECK_EQUAL(*res, 42.);
115 BOOST_CHECK_EQUAL(res.value(), 42
u);
117 BOOST_CHECK(res.ok());
118 BOOST_CHECK_EQUAL(*res, 46.);
119 BOOST_CHECK_EQUAL(res.value(), 46
u);
122 BOOST_CHECK(!res2.ok());
123 BOOST_CHECK_EQUAL(res2.error(), ec);
124 BOOST_CHECK_EQUAL(res2.error(), err1);
126 res2 = MyError::SomethingElse;
127 BOOST_CHECK(!res2.ok());
128 BOOST_CHECK_EQUAL(res2.error(), MyError::SomethingElse);
129 BOOST_CHECK(res2.error() != MyError::Failure);
135 BOOST_CHECK(res.ok());
136 BOOST_CHECK_EQUAL(*res, 0.0);
139 BOOST_CHECK(res.ok());
140 BOOST_CHECK_EQUAL(*res, 1.0);
143 BOOST_CHECK(!res2.ok());
144 BOOST_CHECK_EQUAL(res2.error(),
"blubb");
146 BOOST_CHECK(!res2.ok());
147 BOOST_CHECK_EQUAL(res2.error(),
"sep");
153 BOOST_CHECK(!res.ok());
154 BOOST_CHECK_EQUAL(res.error(), 0.0);
157 BOOST_CHECK(res.ok());
158 BOOST_CHECK_EQUAL(res.value(),
"blibb");
161 BOOST_CHECK(res2.ok());
162 BOOST_CHECK_EQUAL(res2.value(),
"blibb");
165 BOOST_CHECK(!res2.ok());
166 BOOST_CHECK_EQUAL(res2.error(), 0.0);
171 Result res = Result::success(2);
172 BOOST_CHECK(res.ok());
173 BOOST_CHECK_EQUAL(res.value(), 2.0);
175 res = Result::failure(3);
176 BOOST_CHECK(!res.ok());
177 BOOST_CHECK_EQUAL(res.error(), 3);
179 Result res2 = Result::failure(2);
180 BOOST_CHECK(!res2.ok());
181 BOOST_CHECK_EQUAL(res2.error(), 2);
183 res2 = Result::success(3.3);
184 BOOST_CHECK(res2.ok());
185 BOOST_CHECK_EQUAL(res2.value(), 3.3);
192 BOOST_CHECK(res.ok());
193 BOOST_CHECK_EQUAL(*res, 42.);
194 BOOST_CHECK_EQUAL(res.value(), 42
u);
196 BOOST_CHECK(res.ok());
197 BOOST_CHECK_EQUAL(*res, 46.);
198 BOOST_CHECK_EQUAL(res.value(), 46
u);
201 BOOST_CHECK(!res2.ok());
202 BOOST_CHECK_EQUAL(res2.error(), ec);
203 BOOST_CHECK_EQUAL(res2.error(), err1);
205 res2 = MyError::SomethingElse;
206 BOOST_CHECK(!res2.ok());
207 BOOST_CHECK_EQUAL(res2.error(), MyError::SomethingElse);
208 BOOST_CHECK(res2.error() != MyError::Failure);
216 BOOST_CHECK(res.ok());
217 BOOST_CHECK_EQUAL(*res,
"hallo");
218 BOOST_CHECK_EQUAL(res.value(),
"hallo");
220 res =
"something else";
221 BOOST_CHECK(res.ok());
222 BOOST_CHECK_EQUAL(*res,
"something else");
223 BOOST_CHECK_EQUAL(res.value(),
"something else");
225 res = MyError::SomethingElse;
226 BOOST_CHECK(!res.ok());
227 BOOST_CHECK_EQUAL(res.error(), MyError::SomethingElse);
228 BOOST_CHECK(res.error() != MyError::Failure);
235 BOOST_CHECK(res.ok());
236 BOOST_CHECK_EQUAL(res.value(),
"hallo");
239 BOOST_CHECK(!res2.ok());
240 BOOST_CHECK_EQUAL(res2.error(), 4);
256 return MyError::Failure;
266 BOOST_CHECK(res.ok());
267 BOOST_CHECK_EQUAL((*res).num, res.value().num);
270 BOOST_CHECK(res.ok());
271 BOOST_CHECK_EQUAL((*res).num, res.value().num);
272 BOOST_CHECK_EQUAL((*res).num, 3);
275 BOOST_CHECK(res.ok());
276 BOOST_CHECK_EQUAL((*res).num, res.value().num);
277 BOOST_CHECK_EQUAL((*res).num, -4.);
280 BOOST_CHECK_EQUAL(n2.
num, 7);
284 BOOST_CHECK(n4r.ok());
285 BOOST_CHECK_EQUAL((*n4r).num, 8);
287 BOOST_CHECK_EQUAL(n4.
num, 8);
293 return MyError::SomethingElse;
302 BOOST_CHECK(res.ok());
304 Result res2 = Result::success();
305 BOOST_CHECK(res2.ok());
307 res = MyError::Failure;
308 BOOST_CHECK(!res.ok());
309 BOOST_CHECK_EQUAL(res.error(), MyError::Failure);
311 Result res3 = Result::failure(MyError::SomethingElse);
312 BOOST_CHECK(!res3.ok());
313 BOOST_CHECK_EQUAL(res3.error(), MyError::SomethingElse);
316 BOOST_CHECK(res4.ok());
319 BOOST_CHECK(!res5.ok());
320 BOOST_CHECK_EQUAL(res5.error(), MyError::SomethingElse);
326 Result res = Result::success(
false);
327 BOOST_CHECK(res.ok());
328 BOOST_CHECK_EQUAL(*res,
false);
330 res = Result::success(
true);
331 BOOST_CHECK(res.ok());
332 BOOST_CHECK_EQUAL(*res,
true);
334 res = Result::failure(MyError::Failure);
335 BOOST_CHECK(!res.ok());
336 BOOST_CHECK_EQUAL(res.error(), MyError::Failure);
339 BOOST_AUTO_TEST_SUITE_END()