Files
boost_beast/test/beast/core/bind_handler.cpp
T

199 lines
5.0 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-20 08:01:46 -07:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2017-07-20 08:01:46 -07:00
// Test that header file is self-contained.
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/bind_handler.hpp>
2017-07-20 08:01:46 -07:00
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/detail/type_traits.hpp>
2018-11-11 14:07:55 -08:00
#include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
2018-11-22 20:49:30 -08:00
#include <boost/asio/io_context.hpp>
#include <boost/asio/strand.hpp>
2017-07-19 09:50:16 -07:00
#include <string>
2017-07-20 08:01:46 -07:00
2017-07-20 13:40:34 -07:00
namespace boost {
2017-07-20 08:01:46 -07:00
namespace beast {
2016-05-06 19:14:17 -04:00
class bind_handler_test : public unit_test::suite
2017-07-20 08:01:46 -07:00
{
public:
2017-07-19 09:50:16 -07:00
template<class... Args>
struct handler
{
void
2017-07-19 09:50:16 -07:00
operator()(Args const&...) const
{
}
};
2018-11-21 11:17:33 -08:00
struct copyable
{
template<class... Args>
void
operator()(Args&&...) const
{
}
};
2018-05-27 19:05:43 -07:00
struct move_only
{
move_only() = default;
move_only(move_only&&) = default;
move_only(move_only const&) = delete;
2018-11-21 11:17:33 -08:00
void operator()() const{};
2018-05-27 19:05:43 -07:00
};
void
2018-11-21 11:17:33 -08:00
callback(int v)
2018-05-27 19:05:43 -07:00
{
2018-11-21 11:17:33 -08:00
BEAST_EXPECT(v == 42);
}
#if 0
// This function should fail to compile
void
failStdBind()
{
std::bind(bind_handler(handler<>{}));
}
void
failStdBindFront()
{
std::bind(bind_front_handler(handler<>{}));
}
#endif
void
testBindHandler()
{
// invocation
{
auto f = bind_handler(std::bind(
&bind_handler_test::callback, this,
std::placeholders::_1), 42);
f();
}
// placeholders
{
namespace ph = std::placeholders;
bind_handler(handler<>{})();
bind_handler(handler<int>{}, 1)();
bind_handler(handler<int, std::string>{}, 1, "hello")();
bind_handler(handler<int>{}, ph::_1)(1);
bind_handler(handler<int, std::string>{}, ph::_1, ph::_2)(1, "hello");
}
// move-only
{
bind_handler([](move_only){}, move_only{})();
bind_handler([](move_only){}, std::placeholders::_1)(move_only{});
bind_handler([](move_only, move_only){}, move_only{}, std::placeholders::_1)(move_only{});
}
// asio_handler_invoke
{
// make sure things compile, also can set a
// breakpoint in asio_handler_invoke to make sure
// it is instantiated.
2018-11-30 14:58:38 -08:00
net::io_context ioc;
net::strand<
net::io_context::executor_type> s{
2018-11-21 11:17:33 -08:00
ioc.get_executor()};
test::stream ts{ioc};
2018-11-30 14:58:38 -08:00
net::post(s,
2018-11-21 11:17:33 -08:00
bind_handler(copyable{}, 42));
}
}
void
testBindFrontHandler()
{
// invocation
{
bind_front_handler(
std::bind(
&bind_handler_test::callback,
this,
42));
bind_front_handler(
std::bind(
&bind_handler_test::callback,
this,
std::placeholders::_1), 42);
bind_front_handler(
std::bind(
&bind_handler_test::callback,
this,
std::placeholders::_1))(42);
bind_front_handler(
bind_front_handler(
std::bind(
&bind_handler_test::callback,
this,
std::placeholders::_1)),
42);
bind_front_handler(
bind_front_handler(
std::bind(
&bind_handler_test::callback,
this,
std::placeholders::_1)))(42);
}
// move-only
{
bind_front_handler([]{});
}
// specializations
{
bind_front_handler(copyable{});
bind_front_handler(copyable{}, 1);
bind_front_handler(copyable{}, 1, 2);
bind_front_handler(copyable{}, 1, 2, 3);
bind_front_handler(copyable{}, 1, 2, 3, 4);
bind_front_handler(copyable{},
error_code{}, std::size_t(4));
}
// asio_handler_invoke
{
// make sure things compile, also can set a
// breakpoint in asio_handler_invoke to make sure
// it is instantiated.
2018-11-30 14:58:38 -08:00
net::io_context ioc;
net::strand<
net::io_context::executor_type> s{
2018-11-21 11:17:33 -08:00
ioc.get_executor()};
test::stream ts{ioc};
2018-11-30 14:58:38 -08:00
net::post(s,
2018-11-21 11:17:33 -08:00
bind_front_handler(copyable{}, 42));
}
2018-05-27 19:05:43 -07:00
}
2017-07-19 09:50:16 -07:00
void
run() override
2017-07-20 08:01:46 -07:00
{
2018-11-21 11:17:33 -08:00
testBindHandler();
testBindFrontHandler();
2017-07-20 08:01:46 -07:00
}
};
2017-08-01 17:01:57 -07:00
BEAST_DEFINE_TESTSUITE(beast,core,bind_handler);
2017-07-20 08:01:46 -07:00
} // beast
2017-07-20 13:40:34 -07:00
} // boost