9 #include <boost/test/unit_test.hpp>
13 #include <system_error>
17 using namespace std::string_literals;
20 enum class MyError { Failure = 1, SomethingElse = 2 };
23 return {
static_cast<int>(
e), std::generic_category()};
30 struct is_error_code_enum<
MyError> : std::true_type {};
37 BOOST_AUTO_TEST_SUITE(Utilities)
43 Result res = Result::success(42);
44 BOOST_CHECK_EQUAL(*res, 42);
45 BOOST_CHECK_EQUAL(res.value(), 42);
46 BOOST_CHECK(res.ok());
47 res = Result::success(
'e');
48 BOOST_CHECK_EQUAL(*res,
'e');
49 BOOST_CHECK_EQUAL(res.value(),
'e');
50 BOOST_CHECK(res.ok());
51 res = Result::failure(42);
52 BOOST_CHECK(!res.ok());
53 BOOST_CHECK_EQUAL(res.error(), 42);
54 BOOST_CHECK_THROW(res.value(), std::runtime_error);
55 res = Result::failure(
'e');
56 BOOST_CHECK(!res.ok());
57 BOOST_CHECK_EQUAL(res.error(),
'e');
58 BOOST_CHECK_THROW(res.value(), std::runtime_error);
65 BOOST_CHECK(!res1.ok());
66 BOOST_CHECK_EQUAL(res1.error(),
"hallo");
67 BOOST_CHECK_THROW(res1.value(), std::runtime_error);
68 res1 = Result::failure(
"hallo");
69 BOOST_CHECK(!res1.ok());
70 BOOST_CHECK_EQUAL(res1.error(),
"hallo");
71 BOOST_CHECK_THROW(res1.value(), std::runtime_error);
74 BOOST_CHECK(res2.ok());
75 BOOST_CHECK(*res2 == 4.5);
76 BOOST_CHECK(res2.value() == 4.5);
77 res2 = Result::success(4.5);
78 BOOST_CHECK(res2.ok());
79 BOOST_CHECK_EQUAL(*res2, 4.5);
80 BOOST_CHECK_EQUAL(res2.value(), 4.5);
85 auto err1 = MyError::Failure;
87 std::error_code ec = err1;
93 BOOST_CHECK(res.ok());
94 BOOST_CHECK_EQUAL(*res, 42.);
97 BOOST_CHECK(!res2.ok());
98 BOOST_CHECK_EQUAL(res2.error(), err1);
99 BOOST_CHECK_THROW(res2.value(), std::runtime_error);
106 BOOST_CHECK(res.ok());
107 BOOST_CHECK_EQUAL(*res, 42.);
108 BOOST_CHECK_EQUAL(res.value(), 42
u);
110 BOOST_CHECK(res.ok());
111 BOOST_CHECK_EQUAL(*res, 46.);
112 BOOST_CHECK_EQUAL(res.value(), 46
u);
115 BOOST_CHECK(!res2.ok());
116 BOOST_CHECK_EQUAL(res2.error(), ec);
117 BOOST_CHECK_EQUAL(res2.error(), err1);
119 res2 = MyError::SomethingElse;
120 BOOST_CHECK(!res2.ok());
121 BOOST_CHECK_EQUAL(res2.error(), MyError::SomethingElse);
122 BOOST_CHECK(res2.error() != MyError::Failure);
128 BOOST_CHECK(res.ok());
129 BOOST_CHECK_EQUAL(*res, 0.0);
132 BOOST_CHECK(res.ok());
133 BOOST_CHECK_EQUAL(*res, 1.0);
136 BOOST_CHECK(!res2.ok());
137 BOOST_CHECK_EQUAL(res2.error(),
"blubb");
139 BOOST_CHECK(!res2.ok());
140 BOOST_CHECK_EQUAL(res2.error(),
"sep");
146 BOOST_CHECK(!res.ok());
147 BOOST_CHECK_EQUAL(res.error(), 0.0);
150 BOOST_CHECK(res.ok());
151 BOOST_CHECK_EQUAL(res.value(),
"blibb");
154 BOOST_CHECK(res2.ok());
155 BOOST_CHECK_EQUAL(res2.value(),
"blibb");
158 BOOST_CHECK(!res2.ok());
159 BOOST_CHECK_EQUAL(res2.error(), 0.0);
164 Result res = Result::success(2);
165 BOOST_CHECK(res.ok());
166 BOOST_CHECK_EQUAL(res.value(), 2.0);
168 res = Result::failure(3);
169 BOOST_CHECK(!res.ok());
170 BOOST_CHECK_EQUAL(res.error(), 3);
172 Result res2 = Result::failure(2);
173 BOOST_CHECK(!res2.ok());
174 BOOST_CHECK_EQUAL(res2.error(), 2);
176 res2 = Result::success(3.3);
177 BOOST_CHECK(res2.ok());
178 BOOST_CHECK_EQUAL(res2.value(), 3.3);
185 BOOST_CHECK(res.ok());
186 BOOST_CHECK_EQUAL(*res, 42.);
187 BOOST_CHECK_EQUAL(res.value(), 42
u);
189 BOOST_CHECK(res.ok());
190 BOOST_CHECK_EQUAL(*res, 46.);
191 BOOST_CHECK_EQUAL(res.value(), 46
u);
194 BOOST_CHECK(!res2.ok());
195 BOOST_CHECK_EQUAL(res2.error(), ec);
196 BOOST_CHECK_EQUAL(res2.error(), err1);
198 res2 = MyError::SomethingElse;
199 BOOST_CHECK(!res2.ok());
200 BOOST_CHECK_EQUAL(res2.error(), MyError::SomethingElse);
201 BOOST_CHECK(res2.error() != MyError::Failure);
209 BOOST_CHECK(res.ok());
210 BOOST_CHECK_EQUAL(*res,
"hallo");
211 BOOST_CHECK_EQUAL(res.value(),
"hallo");
213 res =
"something else";
214 BOOST_CHECK(res.ok());
215 BOOST_CHECK_EQUAL(*res,
"something else");
216 BOOST_CHECK_EQUAL(res.value(),
"something else");
218 res = MyError::SomethingElse;
219 BOOST_CHECK(!res.ok());
220 BOOST_CHECK_EQUAL(res.error(), MyError::SomethingElse);
221 BOOST_CHECK(res.error() != MyError::Failure);
228 BOOST_CHECK(res.ok());
229 BOOST_CHECK_EQUAL(res.value(),
"hallo");
232 BOOST_CHECK(!res2.ok());
233 BOOST_CHECK_EQUAL(res2.error(), 4);
249 return MyError::Failure;
258 Result res = std::move(n);
259 BOOST_CHECK(res.ok());
260 BOOST_CHECK_EQUAL((*res).num, res.value().num);
263 BOOST_CHECK(res.ok());
264 BOOST_CHECK_EQUAL((*res).num, res.value().num);
265 BOOST_CHECK_EQUAL((*res).num, 3);
268 BOOST_CHECK(res.ok());
269 BOOST_CHECK_EQUAL((*res).num, res.value().num);
270 BOOST_CHECK_EQUAL((*res).num, -4.);
273 BOOST_CHECK_EQUAL(n2.
num, 7);
277 BOOST_CHECK(n4r.ok());
278 BOOST_CHECK_EQUAL((*n4r).num, 8);
279 NoCopy n4 = std::move(n4r.value());
280 BOOST_CHECK_EQUAL(n4.
num, 8);
286 return MyError::SomethingElse;
295 BOOST_CHECK(res.ok());
297 Result res2 = Result::success();
298 BOOST_CHECK(res2.ok());
300 res = MyError::Failure;
301 BOOST_CHECK(!res.ok());
302 BOOST_CHECK_EQUAL(res.error(), MyError::Failure);
304 Result res3 = Result::failure(MyError::SomethingElse);
305 BOOST_CHECK(!res3.ok());
306 BOOST_CHECK_EQUAL(res3.error(), MyError::SomethingElse);
309 BOOST_CHECK(res4.ok());
312 BOOST_CHECK(!res5.ok());
313 BOOST_CHECK_EQUAL(res5.error(), MyError::SomethingElse);
319 Result res = Result::success(
false);
320 BOOST_CHECK(res.ok());
321 BOOST_CHECK_EQUAL(*res,
false);
323 res = Result::success(
true);
324 BOOST_CHECK(res.ok());
325 BOOST_CHECK_EQUAL(*res,
true);
327 res = Result::failure(MyError::Failure);
328 BOOST_CHECK(!res.ok());
329 BOOST_CHECK_EQUAL(res.error(), MyError::Failure);
332 BOOST_AUTO_TEST_SUITE_END()