diff --git a/CHANGELOG.md b/CHANGELOG.md index 07f40a4d..39378647 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Refactor base64 * Use static_string for WebSocket handshakes * Simplify get_lowest_layer test +* Add test_allocator to extras/test -------------------------------------------------------------------------------- diff --git a/extras/beast/test/test_allocator.hpp b/extras/beast/test/test_allocator.hpp new file mode 100644 index 00000000..851b0712 --- /dev/null +++ b/extras/beast/test/test_allocator.hpp @@ -0,0 +1,138 @@ +// +// Copyright (c) 2013-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) +// + +#ifndef BEAST_TEST_TEST_ALLOCATOR_HPP +#define BEAST_TEST_TEST_ALLOCATOR_HPP + +#include +#include +#include + +namespace beast { +namespace test { + +struct test_allocator_info +{ + std::size_t ncopy = 0; + std::size_t nmove = 0; + std::size_t nselect = 0; +}; + +template +class test_allocator; + +template +struct test_allocator_base +{ +}; + +template +struct test_allocator_base +{ + static + test_allocator + select_on_container_copy_construction( + test_allocator const& a) + { + return test_allocator{}; + } +}; + +template +class test_allocator : public test_allocator_base< + T, Assign, Move, Swap, Select> +{ + std::size_t id_; + std::shared_ptr info_; + + template + friend class test_allocator; + +public: + using value_type = T; + using propagate_on_container_copy_assignment = + std::integral_constant; + using propagate_on_container_move_assignment = + std::integral_constant; + using propagate_on_container_swap = + std::integral_constant; + + template + struct rebind + { + using other = test_allocator< + U, Assign, Move, Swap, Select>; + }; + + test_allocator() + : id_([] + { + static std::atomic< + std::size_t> sid(0); + return ++sid; + }()) + , info_(std::make_shared()) + { + } + + test_allocator(test_allocator const& u) noexcept + : id_(u.id_) + , info_(u.info_) + { + ++info_->ncopy; + } + + template + test_allocator(test_allocator< + U, Assign, Move, Swap, Select> const& u) noexcept + : id_(u.id_) + , info_(u.info_) + { + ++info_->ncopy; + } + + test_allocator(test_allocator&& t) + : id_(t.id_) + , info_(t.info_) + { + ++info_->nmove; + } + + value_type* + allocate(std::size_t n) + { + return static_cast( + ::operator new (n*sizeof(value_type))); + } + + void + deallocate(value_type* p, std::size_t) noexcept + { + ::operator delete(p); + } + + std::size_t + id() const + { + return id_; + } + + test_allocator_info const* + operator->() const + { + return info_.get(); + } +}; + +} // test +} // beast + +#endif diff --git a/test/core/streambuf.cpp b/test/core/streambuf.cpp index ee162672..6d110cbc 100644 --- a/test/core/streambuf.cpp +++ b/test/core/streambuf.cpp @@ -11,6 +11,7 @@ #include "buffer_test.hpp" #include #include +#include #include #include #include @@ -22,123 +23,6 @@ namespace beast { static_assert(is_DynamicBuffer::value, ""); -struct test_allocator_info -{ - std::size_t ncopy = 0; - std::size_t nmove = 0; - std::size_t nselect = 0; -}; - -template -class test_allocator; - -template -struct test_allocator_base -{ -}; - -template -struct test_allocator_base -{ - static - test_allocator - select_on_container_copy_construction( - test_allocator const& a) - { - return test_allocator{}; - } -}; - -template -class test_allocator : public test_allocator_base< - T, Assign, Move, Swap, Select> -{ - std::size_t id_; - std::shared_ptr info_; - - template - friend class test_allocator; - -public: - using value_type = T; - using propagate_on_container_copy_assignment = - std::integral_constant; - using propagate_on_container_move_assignment = - std::integral_constant; - using propagate_on_container_swap = - std::integral_constant; - - template - struct rebind - { - using other = test_allocator< - U, Assign, Move, Swap, Select>; - }; - - test_allocator() - : id_([] - { - static std::atomic< - std::size_t> sid(0); - return ++sid; - }()) - , info_(std::make_shared()) - { - } - - test_allocator(test_allocator const& u) noexcept - : id_(u.id_) - , info_(u.info_) - { - ++info_->ncopy; - } - - template - test_allocator(test_allocator< - U, Assign, Move, Swap, Select> const& u) noexcept - : id_(u.id_) - , info_(u.info_) - { - ++info_->ncopy; - } - - test_allocator(test_allocator&& t) - : id_(t.id_) - , info_(t.info_) - { - ++info_->nmove; - } - - value_type* - allocate(std::size_t n) - { - return static_cast( - ::operator new (n*sizeof(value_type))); - } - - void - deallocate(value_type* p, std::size_t) noexcept - { - ::operator delete(p); - } - - std::size_t - id() const - { - return id_; - } - - test_allocator_info const* - operator->() const - { - return info_.get(); - } -}; - class basic_streambuf_test : public beast::unit_test::suite { public: @@ -218,8 +102,10 @@ public: } } - void testAllocator() + void + testAllocator() { + using test::test_allocator; // VFALCO This needs work { using alloc_type =