• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
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 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 // Test that header file is self-contained.
11 #include <boost/beast/websocket/detail/decorator.hpp>
12 
13 #include <boost/beast/_experimental/unit_test/suite.hpp>
14 
15 namespace boost {
16 namespace beast {
17 namespace websocket {
18 namespace detail {
19 
20 class decorator_test : public unit_test::suite
21 {
22 public:
23     struct req_t
24     {
25         bool pass_ = false;
26 
27         req_t() = default;
28 
~req_tboost::beast::websocket::detail::decorator_test::req_t29         ~req_t()
30         {
31             BEAST_EXPECT(pass_);
32         }
33 
req_tboost::beast::websocket::detail::decorator_test::req_t34         req_t(req_t&& other) noexcept
35             : pass_(boost::exchange(other.pass_, true))
36         {
37         }
38 
39         void
operator ()boost::beast::websocket::detail::decorator_test::req_t40         operator()(request_type&)
41         {
42             BEAST_EXPECT(! pass_);
43             pass_ = true;
44         }
45     };
46 
47     struct res_t
48     {
49         bool pass_ = false;
50 
51         res_t() = default;
52 
~res_tboost::beast::websocket::detail::decorator_test::res_t53         ~res_t()
54         {
55             BEAST_EXPECT(pass_);
56         }
57 
res_tboost::beast::websocket::detail::decorator_test::res_t58         res_t(res_t&& other) noexcept
59             : pass_(boost::exchange(other.pass_, true))
60         {
61         }
62 
63         void
operator ()boost::beast::websocket::detail::decorator_test::res_t64         operator()(response_type&)
65         {
66             BEAST_EXPECT(! pass_);
67             pass_ = true;
68         }
69     };
70 
71     struct both_t : res_t , req_t
72     {
73         using req_t::operator();
74         using res_t::operator();
75     };
76 
77     struct big_t : both_t
78     {
79         using both_t::operator();
80         char buf[2048];
81     };
82 
83     struct goldi // just right
84     {
85         struct incomplete;
86         std::shared_ptr<incomplete> sp1;
87         std::shared_ptr<incomplete> sp2;
88         void* param;
89 
operator ()boost::beast::websocket::detail::decorator_test::goldi90         void operator()(request_type &) const
91         {
92         }
93     };
94 
95     void
dec_req(request_type &)96     dec_req(request_type&)
97     {
98     }
99 
100     void
testDecorator()101     testDecorator()
102     {
103         request_type req;
104         response_type res;
105 
106         decorator{}(req);
107         decorator{}(res);
108         decorator{req_t{}}(req);
109         decorator{res_t{}}(res);
110 
111         {
112             decorator d(both_t{});
113             d(req);
114             d(res);
115         }
116 
117         {
118             decorator d(big_t{});
119             d(req);
120             d(res);
121         }
122 
123         {
124             decorator d1{req_t{}};
125             decorator d2{std::move(d1)};
126             d2(req);
127             decorator d3;
128             d3 = std::move(d2);
129         }
130 
131         {
132             decorator d{goldi{}};
133             bool is_inline = d.vtable_ ==
134                 decorator::vtable_impl<goldi, true>::get();
135             BEAST_EXPECT(is_inline);
136         }
137     }
138 
139     void
run()140     run() override
141     {
142         log << "sizeof(decorator)==" << sizeof(decorator) << "\n";
143         testDecorator();
144     }
145 };
146 
147 BEAST_DEFINE_TESTSUITE(beast,websocket,decorator);
148 
149 } // detail
150 } // websocket
151 } // beast
152 } // boost
153