forked from boostorg/unordered
Refactor scoped allocator test to use a custom non-default-constructible Allocator instead of Intrerprocess which required a dep on Filesystem
This commit is contained in:
@ -61,7 +61,7 @@ test-suite unordered
|
||||
[ run unordered/swap_tests.cpp ]
|
||||
[ run unordered/detail_tests.cpp ]
|
||||
[ run unordered/deduction_tests.cpp ]
|
||||
[ run unordered/scoped_allocator.cpp /boost/filesystem//boost_filesystem/<warnings>off ]
|
||||
[ run unordered/scoped_allocator.cpp ]
|
||||
[ run unordered/transparent_tests.cpp ]
|
||||
[ run unordered/reserve_tests.cpp ]
|
||||
[ run unordered/contains_tests.cpp ]
|
||||
|
@ -3,6 +3,21 @@
|
||||
// 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)
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
|
||||
#if BOOST_CXX_VERSION <= 199711L
|
||||
|
||||
BOOST_PRAGMA_MESSAGE(
|
||||
"scoped allocator adaptor tests only work under C++11 and above")
|
||||
int main() {}
|
||||
|
||||
#else
|
||||
|
||||
// This test is based on a user-submitted issue found here:
|
||||
// https://github.com/boostorg/unordered/issues/22
|
||||
//
|
||||
|
||||
// clang-format off
|
||||
#include "../helpers/prefix.hpp"
|
||||
#include <boost/unordered_set.hpp>
|
||||
@ -12,67 +27,55 @@
|
||||
|
||||
#include "../helpers/test.hpp"
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
|
||||
#include <boost/container/scoped_allocator.hpp>
|
||||
#include <boost/container/uses_allocator.hpp>
|
||||
|
||||
#include <boost/core/ignore_unused.hpp>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <boost/interprocess/allocators/node_allocator.hpp>
|
||||
#include <boost/interprocess/managed_shared_memory.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <scoped_allocator>
|
||||
|
||||
// This test is based on a user-submitted issue found here:
|
||||
// https://github.com/boostorg/unordered/issues/22
|
||||
//
|
||||
namespace test {
|
||||
template <class T> struct allocator
|
||||
{
|
||||
typedef T value_type;
|
||||
|
||||
#if BOOST_CXX_VERSION <= 199711L
|
||||
allocator() = delete;
|
||||
allocator(int) {}
|
||||
allocator(allocator const&) = default;
|
||||
allocator(allocator&&) = default;
|
||||
|
||||
BOOST_PRAGMA_MESSAGE(
|
||||
"scoped allocator adaptor tests only work under C++11 and above")
|
||||
int main() {}
|
||||
template <class U> allocator(allocator<U> const&) {}
|
||||
|
||||
#else
|
||||
BOOST_ATTRIBUTE_NODISCARD T* allocate(std::size_t n)
|
||||
{
|
||||
return static_cast<T*>(::operator new(n * sizeof(T)));
|
||||
}
|
||||
|
||||
namespace bi = boost::interprocess;
|
||||
void deallocate(T* p, std::size_t) noexcept { ::operator delete(p); }
|
||||
|
||||
template <typename T> struct node_alloc
|
||||
{
|
||||
typedef bi::node_allocator<T, bi::managed_shared_memory::segment_manager>
|
||||
type;
|
||||
};
|
||||
bool operator==(allocator const&) const { return true; }
|
||||
bool operator!=(allocator const&) const { return false; }
|
||||
};
|
||||
} // namespace test
|
||||
|
||||
typedef std::vector<boost::uint64_t, node_alloc<boost::uint64_t>::type>
|
||||
typedef std::vector<boost::uint64_t, test::allocator<boost::uint64_t> >
|
||||
vector_type;
|
||||
|
||||
typedef std::pair<boost::uint64_t, vector_type> pair_type;
|
||||
|
||||
typedef boost::container::scoped_allocator_adaptor<node_alloc<pair_type>::type,
|
||||
node_alloc<boost::uint64_t>::type>
|
||||
typedef std::scoped_allocator_adaptor<test::allocator<pair_type>,
|
||||
test::allocator<boost::uint64_t> >
|
||||
allocator_type;
|
||||
|
||||
typedef boost::unordered_map<const boost::uint64_t, vector_type,
|
||||
boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>
|
||||
map_type;
|
||||
|
||||
std::string make_uuid() { return boost::filesystem::unique_path().string(); }
|
||||
|
||||
UNORDERED_AUTO_TEST (scoped_allocator) {
|
||||
std::string uuid = make_uuid();
|
||||
|
||||
bi::managed_shared_memory s(bi::create_only, uuid.c_str(), 65536);
|
||||
|
||||
allocator_type alloc(node_alloc<pair_type>::type(s.get_segment_manager()),
|
||||
node_alloc<boost::uint64_t>::type(s.get_segment_manager()));
|
||||
allocator_type alloc(
|
||||
test::allocator<pair_type>(1337), test::allocator<boost::uint64_t>(7331));
|
||||
|
||||
map_type map(alloc);
|
||||
|
||||
@ -81,8 +84,6 @@ UNORDERED_AUTO_TEST (scoped_allocator) {
|
||||
}
|
||||
|
||||
BOOST_TEST(map.size() == 10);
|
||||
|
||||
bi::shared_memory_object::remove(uuid.c_str());
|
||||
}
|
||||
|
||||
RUN_TESTS()
|
||||
|
Reference in New Issue
Block a user