]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/match_member_test.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / leaf / test / match_member_test.cpp
1 // Copyright 2018-2022 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 #if __cplusplus < 201703L
7
8 #include <iostream>
9
10 int main()
11 {
12 std::cout << "Unit test not applicable." << std::endl;
13 return 0;
14 }
15
16 #else
17
18 #ifdef BOOST_LEAF_TEST_SINGLE_HEADER
19 # include "leaf.hpp"
20 #else
21 # include <boost/leaf/handle_errors.hpp>
22 # include <boost/leaf/pred.hpp>
23 # include <boost/leaf/result.hpp>
24 #endif
25
26 #include "_test_ec.hpp"
27 #include "lightweight_test.hpp"
28 #include <exception>
29
30 namespace leaf = boost::leaf;
31
32 enum class my_error { e1=1, e2, e3 };
33
34 struct e_my_error { my_error value; };
35
36 #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
37 struct e_error_code { std::error_code value; };
38 #endif
39
40 struct my_exception: std::exception
41 {
42 int value;
43 };
44
45 template <class M, class E>
46 bool test(E const & e )
47 {
48 if( M::evaluate(e) )
49 {
50 M m{e};
51 BOOST_TEST_EQ(&e, &m.matched);
52 return true;
53 }
54 else
55 return false;
56 }
57
58 int main()
59 {
60 {
61 e_my_error e = { my_error::e1 };
62
63 BOOST_TEST(( test<leaf::match_member<&e_my_error::value, my_error::e1>>(e) ));
64 BOOST_TEST(( !test<leaf::match_member<&e_my_error::value, my_error::e2>>(e) ));
65 BOOST_TEST(( test<leaf::match_member<&e_my_error::value, my_error::e2, my_error::e1>>(e) ));
66 }
67
68 #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
69 {
70 e_error_code e = { errc_a::a0 };
71
72 BOOST_TEST(( test<leaf::match_member<&e_error_code::value, errc_a::a0>>(e) ));
73 BOOST_TEST(( !test<leaf::match_member<&e_error_code::value, errc_a::a2>>(e) ));
74 BOOST_TEST(( test<leaf::match_member<&e_error_code::value, errc_a::a2, errc_a::a0>>(e) ));
75 }
76 #endif
77
78 {
79 int r = leaf::try_handle_all(
80 []() -> leaf::result<int>
81 {
82 return leaf::new_error(e_my_error{my_error::e1});
83 },
84
85 []( leaf::match_member<&e_my_error::value, my_error::e1> )
86 {
87 return 1;
88 },
89
90 []
91 {
92 return 2;
93 } );
94 BOOST_TEST_EQ(r, 1);
95 }
96
97 {
98 int r = leaf::try_handle_all(
99 []() -> leaf::result<int>
100 {
101 return leaf::new_error(e_my_error{my_error::e1});
102 },
103
104 []( leaf::match_member<&e_my_error::value, my_error::e2> )
105 {
106 return 1;
107 },
108
109 []
110 {
111 return 2;
112 } );
113 BOOST_TEST_EQ(r, 2);
114 }
115
116 return boost::report_errors();
117 }
118
119 #endif