diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 0bc6ed49..e87886aa 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -64,6 +64,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 ] [ run unordered/compile_set.cpp : : : BOOST_UNORDERED_USE_MOVE diff --git a/test/unordered/scoped_allocator.cpp b/test/unordered/scoped_allocator.cpp new file mode 100644 index 00000000..cd61b9f3 --- /dev/null +++ b/test/unordered/scoped_allocator.cpp @@ -0,0 +1,90 @@ + +// Copyright 2021 Christian Mazakas. +// 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) + +// clang-format off +#include "../helpers/prefix.hpp" +#include +#include +#include "../helpers/postfix.hpp" +// clang-format on + +#include "../helpers/test.hpp" + +#include +#include + +#include + +#include +#include + +#include + +#include + +#include +#include + +#include +#include +#include + +// This test is based on a user-submitted issue found here: +// https://github.com/boostorg/unordered/issues/22 +// + +#if BOOST_CXX_VERSION <= 199711L + +BOOST_PRAGMA_MESSAGE( + "scoped allocator adaptor tests only work under C++11 and above") +int main() {} + +#else + +namespace bi = boost::interprocess; + +template struct node_alloc +{ + typedef bi::node_allocator + type; +}; + +typedef std::vector::type> + vector_type; + +typedef std::pair pair_type; + +typedef boost::container::scoped_allocator_adaptor::type, + node_alloc::type> + allocator_type; + +typedef boost::unordered_map, std::equal_to, 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::type(s.get_segment_manager()), + node_alloc::type(s.get_segment_manager())); + + map_type map(alloc); + + for (unsigned i = 0; i < 10; ++i) { + boost::ignore_unused(map[i]); + } + + BOOST_TEST(map.size() == 10); + + bi::shared_memory_object::remove(uuid.c_str()); +} + +RUN_TESTS() + +#endif