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