]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/outcome/test/tests/issue0203.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / outcome / test / tests / issue0203.cpp
CommitLineData
92f5a8d4 1/* Unit testing for outcomes
1e59de90 2(C) 2013-2022 Niall Douglas <http://www.nedproductions.biz/> (1 commit)
92f5a8d4
TL
3
4
5Boost Software License - Version 1.0 - August 17th, 2003
6
7Permission is hereby granted, free of charge, to any person or organization
8obtaining a copy of the software and accompanying documentation covered by
9this license (the "Software") to use, reproduce, display, distribute,
10execute, and transmit the Software, and to prepare derivative works of the
11Software, and to permit third-parties to whom the Software is furnished to
12do so, all subject to the following:
13
14The copyright notices in the Software and this entire statement, including
15the above license grant, this restriction and the following disclaimer,
16must be included in all copies of the Software, in whole or in part, and
17all derivative works of the Software, unless such copies or derivative
18works are solely in the form of machine-executable object code generated by
19a source language processor.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27DEALINGS IN THE SOFTWARE.
28*/
29
30#include <boost/outcome/std_result.hpp>
31#include <boost/outcome/try.hpp>
32#include <boost/test/unit_test.hpp>
33#include <boost/test/unit_test_monitor.hpp>
34
35namespace outcome = BOOST_OUTCOME_V2_NAMESPACE;
36
1e59de90
TL
37namespace stdalias = std;
38
92f5a8d4
TL
39enum class error
40{
41 test,
42 abcde
43};
44
45class error_category_impl : public std::error_category
46{
47public:
48 const char *name() const noexcept override { return "test"; }
49
50 std::string message(int code) const noexcept override
51 {
52 switch(static_cast<error>(code))
53 {
54 case error::test:
55 return "test";
56 case error::abcde:
57 return "abcde";
58 }
59 return "unknown";
60 }
61};
62const std::error_category &error_category() noexcept
63{
64 static error_category_impl instance;
65 return instance;
66}
67
1e59de90 68stdalias::error_code make_error_code(error error) noexcept
92f5a8d4
TL
69{
70 return {static_cast<int>(error), error_category()};
71}
72
73namespace std
74{
75 template <> struct is_error_code_enum<error> : true_type
76 {
77 };
78} // namespace std
79
80template <typename T> using enum_result = outcome::basic_result<T, error, outcome::policy::default_policy<T, error, void>>;
81
82enum_result<int> test()
83{
84 return 5;
85}
86
87outcome::std_result<int> abc()
88{
89 static_assert(std::is_error_code_enum<error>::value, "custom enum is not marked convertible to error code");
1e59de90
TL
90 static_assert(std::is_constructible<stdalias::error_code, error>::value, "error code is not explicitly constructible from custom enum");
91 static_assert(std::is_convertible<error, stdalias::error_code>::value, "error code is not implicitly constructible from custom enum");
92 stdalias::error_code ec = error::test; // custom enum is definitely convertible to error code
93 BOOST_OUTCOME_TRY(test()); // hence this should compile, as implicit conversions work here
92f5a8d4
TL
94 (void) ec;
95
96 // But explicit conversions are required between dissimilar basic_result, implicit conversions are disabled
97 static_assert(std::is_constructible<outcome::std_result<int>, enum_result<int>>::value, "basic_result with error code is not explicitly constructible from basic_result with custom enum");
98 static_assert(!std::is_convertible<enum_result<int>, outcome::std_result<int>>::value, "basic_result with error code is implicitly constructible from basic_result with custom enum");
99 return 5;
100}
101
102BOOST_OUTCOME_AUTO_TEST_CASE(issues_0203_test, "enum convertible to error code works as designed")
103{
104 BOOST_CHECK(abc().value() == 5);
105}