]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/leaf/test/handle_some_other_result_test.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / leaf / test / handle_some_other_result_test.cpp
CommitLineData
20effc67
TL
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 "_test_res.hpp"
9#include "lightweight_test.hpp"
10
11namespace leaf = boost::leaf;
12
13template <int> struct info { int value; };
14
15template <class ResType>
16ResType f( bool succeed )
17{
18 if( succeed )
19 return 42;
20 else
21 return make_error_code(errc_a::a0);
22}
23
24template <class ResType>
25ResType g( bool succeed )
26{
27 if( auto r = f<ResType>(succeed) )
28 return r;
29 else
30 return leaf::error_id(r.error()).load(info<42>{42}).to_error_code();
31}
32
33template <class ResType>
34void test()
35{
36 {
37 ResType r = leaf::try_handle_some(
38 []
39 {
40 return g<ResType>(true);
41 } );
42 BOOST_TEST(r);
43 BOOST_TEST_EQ(r.value(), 42);
44 }
45 {
46 int called = 0;
47 ResType r = leaf::try_handle_some(
48 [&]
49 {
50 auto r = g<ResType>(false);
51 BOOST_TEST(!r);
52 auto ec = r.error();
53 BOOST_TEST_EQ(ec.message(), "LEAF error");
54 BOOST_TEST(!std::strcmp(ec.category().name(),"LEAF error"));
55 return r;
56 },
57 [&]( info<42> const & x, leaf::match<leaf::condition<cond_x>, cond_x::x00> ec )
58 {
59 called = 1;
60 BOOST_TEST_EQ(x.value, 42);
61 return ec.matched;
62 } );
63 BOOST_TEST(!r);
64 BOOST_TEST_EQ(r.error(), make_error_code(errc_a::a0));
65 BOOST_TEST(called);
66 }
67}
68
69int main()
70{
71 test<test_res<int, std::error_code>>();
72 test<test_res<int const, std::error_code>>();
73 test<test_res<int, std::error_code> const>();
74 test<test_res<int const, std::error_code> const>();
75 return boost::report_errors();
76}