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