]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // |
b32b8144 | 2 | // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) |
7c673cae FG |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
b32b8144 FG |
7 | // Official repository: https://github.com/boostorg/beast |
8 | // | |
7c673cae FG |
9 | |
10 | // Test that header file is self-contained. | |
b32b8144 | 11 | #include <boost/beast/core/handler_ptr.hpp> |
7c673cae | 12 | |
b32b8144 | 13 | #include <boost/beast/unit_test/suite.hpp> |
7c673cae | 14 | #include <exception> |
11fdf7f2 | 15 | #include <memory> |
7c673cae FG |
16 | #include <utility> |
17 | ||
b32b8144 | 18 | namespace boost { |
7c673cae FG |
19 | namespace beast { |
20 | ||
21 | class handler_ptr_test : public beast::unit_test::suite | |
22 | { | |
23 | public: | |
24 | struct handler | |
25 | { | |
11fdf7f2 TL |
26 | std::unique_ptr<int> ptr; |
27 | void operator()(bool& b) const | |
7c673cae FG |
28 | { |
29 | b = true; | |
30 | } | |
31 | }; | |
32 | ||
33 | struct T | |
34 | { | |
11fdf7f2 | 35 | explicit T(handler const&) |
7c673cae FG |
36 | { |
37 | } | |
38 | ||
39 | ~T() | |
40 | { | |
41 | } | |
42 | }; | |
43 | ||
7c673cae | 44 | void |
11fdf7f2 | 45 | testCtorExcept() |
7c673cae | 46 | { |
11fdf7f2 TL |
47 | struct U |
48 | { | |
49 | explicit U(handler const&) | |
50 | { | |
51 | throw std::exception{}; | |
52 | } | |
53 | }; | |
54 | handler_ptr<T, handler> p1{handler{}}; | |
7c673cae FG |
55 | try |
56 | { | |
11fdf7f2 | 57 | handler_ptr<U, handler> p2{handler{}}; |
7c673cae FG |
58 | fail(); |
59 | } | |
60 | catch(std::exception const&) | |
61 | { | |
62 | pass(); | |
63 | } | |
64 | catch(...) | |
65 | { | |
11fdf7f2 TL |
66 | fail("", __FILE__, __LINE__); |
67 | } | |
68 | } | |
69 | ||
70 | void | |
71 | testMoveExcept() | |
72 | { | |
73 | struct throwing_handler | |
74 | { | |
75 | throwing_handler() = default; | |
76 | throwing_handler(throwing_handler&&) | |
77 | { | |
78 | throw std::bad_alloc{}; | |
79 | } | |
80 | void operator()() const | |
81 | { | |
82 | } | |
83 | }; | |
84 | struct T | |
85 | { | |
86 | explicit T(throwing_handler const&) noexcept {} | |
87 | }; | |
88 | try | |
89 | { | |
90 | throwing_handler h; | |
91 | handler_ptr<T, throwing_handler> p{std::move(h)}; | |
92 | fail("", __FILE__, __LINE__); | |
93 | } | |
94 | catch (std::bad_alloc const&) | |
95 | { | |
96 | pass(); | |
7c673cae | 97 | } |
11fdf7f2 TL |
98 | } |
99 | ||
100 | void | |
101 | testInvoke() | |
102 | { | |
103 | handler_ptr<T, handler> p{handler{}}; | |
7c673cae | 104 | bool b = false; |
11fdf7f2 | 105 | p.invoke(std::ref(b)); |
7c673cae FG |
106 | BEAST_EXPECT(b); |
107 | } | |
11fdf7f2 TL |
108 | |
109 | void | |
110 | run() override | |
111 | { | |
112 | testCtorExcept(); | |
113 | testMoveExcept(); | |
114 | testInvoke(); | |
115 | } | |
7c673cae FG |
116 | }; |
117 | ||
b32b8144 | 118 | BEAST_DEFINE_TESTSUITE(beast,core,handler_ptr); |
7c673cae FG |
119 | |
120 | } // beast | |
b32b8144 | 121 | } // boost |