Skip to content

Commit

Permalink
Porting Beast test code from Boost 1.72
Browse files Browse the repository at this point in the history
This patch adds the relevant files needed for the
Beast stream test code, which have not yet been modified.
  • Loading branch information
windowsair authored and laudrup committed Jan 26, 2024
1 parent aced755 commit c172aa5
Show file tree
Hide file tree
Showing 15 changed files with 3,133 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/utils/allocator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_DETAIL_ALLOCATOR_HPP
#define BOOST_BEAST_DETAIL_ALLOCATOR_HPP

#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_ALLOCATOR
#include <boost/container/allocator_traits.hpp>
#else
#include <memory>
#endif

namespace boost {
namespace beast {
namespace detail {

// This is a workaround for allocator_traits
// implementations which falsely claim C++11
// compatibility.

#ifdef BOOST_NO_CXX11_ALLOCATOR
template<class Alloc>
using allocator_traits = boost::container::allocator_traits<Alloc>;

#else
template<class Alloc>
using allocator_traits = std::allocator_traits<Alloc>;

#endif

} // detail
} // beast
} // boost

#endif
144 changes: 144 additions & 0 deletions test/utils/empty_value.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
Copyright 2018 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_CORE_EMPTY_VALUE_HPP
#define BOOST_CORE_EMPTY_VALUE_HPP

#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#include <utility>
#endif

#if defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION >= 40700)
#define BOOST_DETAIL_EMPTY_VALUE_BASE
#elif defined(BOOST_INTEL) && defined(_MSC_VER) && (_MSC_VER >= 1800)
#define BOOST_DETAIL_EMPTY_VALUE_BASE
#elif defined(BOOST_MSVC) && (BOOST_MSVC >= 1800)
#define BOOST_DETAIL_EMPTY_VALUE_BASE
#elif defined(BOOST_CLANG) && !defined(__CUDACC__)
#if __has_feature(is_empty) && __has_feature(is_final)
#define BOOST_DETAIL_EMPTY_VALUE_BASE
#endif
#endif

namespace boost {

template<class T>
struct use_empty_value_base {
enum {
#if defined(BOOST_DETAIL_EMPTY_VALUE_BASE)
value = __is_empty(T) && !__is_final(T)
#else
value = false
#endif
};
};

struct empty_init_t { };

namespace empty_ {

template<class T, unsigned N = 0,
bool E = boost::use_empty_value_base<T>::value>
class empty_value {
public:
typedef T type;

#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
empty_value() = default;
#else
empty_value() { }
#endif

empty_value(boost::empty_init_t)
: value_() { }

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class... Args>
explicit empty_value(boost::empty_init_t, Args&&... args)
: value_(std::forward<Args>(args)...) { }
#else
template<class U>
empty_value(boost::empty_init_t, U&& value)
: value_(std::forward<U>(value)) { }
#endif
#else
template<class U>
empty_value(boost::empty_init_t, const U& value)
: value_(value) { }

template<class U>
empty_value(boost::empty_init_t, U& value)
: value_(value) { }
#endif

const T& get() const BOOST_NOEXCEPT {
return value_;
}

T& get() BOOST_NOEXCEPT {
return value_;
}

private:
T value_;
};

#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
template<class T, unsigned N>
class empty_value<T, N, true>
: T {
public:
typedef T type;

#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
empty_value() = default;
#else
empty_value() { }
#endif

empty_value(boost::empty_init_t)
: T() { }

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class... Args>
explicit empty_value(boost::empty_init_t, Args&&... args)
: T(std::forward<Args>(args)...) { }
#else
template<class U>
empty_value(boost::empty_init_t, U&& value)
: T(std::forward<U>(value)) { }
#endif
#else
template<class U>
empty_value(boost::empty_init_t, const U& value)
: T(value) { }

template<class U>
empty_value(boost::empty_init_t, U& value)
: T(value) { }
#endif

const T& get() const BOOST_NOEXCEPT {
return *this;
}

T& get() BOOST_NOEXCEPT {
return *this;
}
};
#endif

} /* empty_ */

using empty_::empty_value;

} /* boost */

#endif
40 changes: 40 additions & 0 deletions test/utils/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_TEST_ERROR_HPP
#define BOOST_BEAST_TEST_ERROR_HPP

#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/error.hpp>

namespace boost {
namespace beast {
namespace test {

/// Error codes returned from unit testing algorithms
enum class error
{
/** The test stream generated a simulated testing error
This error is returned by a @ref fail_count object
when it generates a simulated error.
*/
test_failure = 1
};

} // test
} // beast
} // boost

#include <boost/beast/_experimental/test/impl/error.hpp>
#ifdef BOOST_BEAST_HEADER_ONLY
#include <boost/beast/_experimental/test/impl/error.ipp>
#endif

#endif
70 changes: 70 additions & 0 deletions test/utils/fail_count.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_TEST_FAIL_COUNT_HPP
#define BOOST_BEAST_TEST_FAIL_COUNT_HPP

#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/_experimental/test/error.hpp>
#include <cstdlib>

namespace boost {
namespace beast {
namespace test {

/** A countdown to simulated failure
On the Nth operation, the class will fail with the specified
error code, or the default error code of @ref error::test_failure.
Instances of this class may be used to build objects which
are specifically designed to aid in writing unit tests, for
interfaces which can throw exceptions or return `error_code`
values representing failure.
*/
class fail_count
{
std::size_t n_;
std::size_t i_ = 0;
error_code ec_;

public:
fail_count(fail_count&&) = default;

/** Construct a counter
@param n The 0-based index of the operation to fail on or after
@param ev An optional error code to use when generating a simulated failure
*/
BOOST_BEAST_DECL
explicit
fail_count(
std::size_t n,
error_code ev = error::test_failure);

/// Throw an exception on the Nth failure
BOOST_BEAST_DECL
void
fail();

/// Set an error code on the Nth failure
BOOST_BEAST_DECL
bool
fail(error_code& ec);
};

} // test
} // beast
} // boost

#ifdef BOOST_BEAST_HEADER_ONLY
#include <boost/beast/_experimental/test/impl/fail_count.ipp>
#endif

#endif
Loading

0 comments on commit c172aa5

Please sign in to comment.