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

124 lines
2.4 KiB
C++
Raw Normal View History

//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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
//
// Test that header file is self-contained.
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/handler_ptr.hpp>
2017-02-06 10:56:37 -05:00
2018-11-11 11:52:37 -08:00
#include <boost/beast/experimental/unit_test/suite.hpp>
2017-02-06 10:56:37 -05:00
#include <exception>
2017-12-02 16:48:05 +01:00
#include <memory>
2017-02-06 10:56:37 -05:00
#include <utility>
2017-07-20 13:40:34 -07:00
namespace boost {
2017-02-06 10:56:37 -05:00
namespace beast {
class handler_ptr_test : public beast::unit_test::suite
{
public:
struct handler
{
2017-12-02 16:48:05 +01:00
std::unique_ptr<int> ptr;
2017-12-10 00:20:17 +01:00
void operator()(bool& b) const
2017-02-06 10:56:37 -05:00
{
b = true;
}
};
struct T
{
2017-12-10 00:20:17 +01:00
explicit T(handler const&)
2017-02-06 10:56:37 -05:00
{
}
~T()
{
}
};
void
2017-12-10 00:20:17 +01:00
testCtorExcept()
2017-02-06 10:56:37 -05:00
{
2017-12-10 00:20:17 +01:00
struct U
{
explicit U(handler const&)
{
throw std::exception{};
}
};
2017-12-02 16:48:05 +01:00
handler_ptr<T, handler> p1{handler{}};
2017-02-06 10:56:37 -05:00
try
{
2017-12-02 16:48:05 +01:00
handler_ptr<U, handler> p2{handler{}};
2017-02-06 10:56:37 -05:00
fail();
}
catch(std::exception const&)
{
pass();
}
catch(...)
{
2017-12-10 00:20:17 +01:00
fail("", __FILE__, __LINE__);
2017-02-06 10:56:37 -05:00
}
2017-12-10 00:20:17 +01:00
}
void
testMoveExcept()
{
struct throwing_handler
{
throwing_handler() = default;
throwing_handler(throwing_handler&&)
{
throw std::bad_alloc{};
}
void operator()() const
{
}
};
struct T
{
explicit T(throwing_handler const&) noexcept {}
};
try
{
throwing_handler h;
handler_ptr<T, throwing_handler> p{std::move(h)};
fail("", __FILE__, __LINE__);
}
catch (std::bad_alloc const&)
{
pass();
}
}
void
testInvoke()
{
handler_ptr<T, handler> p{handler{}};
2017-02-06 10:56:37 -05:00
bool b = false;
2018-05-18 21:28:44 +02:00
BEAST_EXPECT(p.has_value());
2017-12-10 00:20:17 +01:00
p.invoke(std::ref(b));
2018-05-18 21:28:44 +02:00
BEAST_EXPECT(! p.has_value());
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b);
2017-02-06 10:56:37 -05:00
}
2017-12-10 00:20:17 +01:00
void
run() override
{
testCtorExcept();
testMoveExcept();
testInvoke();
}
2017-02-06 10:56:37 -05:00
};
2017-08-01 17:01:57 -07:00
BEAST_DEFINE_TESTSUITE(beast,core,handler_ptr);
2017-02-06 10:56:37 -05:00
} // beast
2017-07-20 13:40:34 -07:00
} // boost