From 1e553df5b6603c57ee98cb239eb29ea205466ec3 Mon Sep 17 00:00:00 2001 From: LeonineKing1199 Date: Mon, 15 Nov 2021 11:22:40 -0800 Subject: [PATCH] =?UTF-8?q?Add=20explicit=20tests=20around=20use=20of=20`s?= =?UTF-8?q?coped=5Fallocator=5Fadaptor`=20to=20emulat=E2=80=A6=20(#31)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add explicit tests around use of `scoped_allocator_adaptor` to emulate issue https://github.com/boostorg/unordered/issues/22 * Refine test to only run in C++11 mode and later with possibility of re-introducing C++03 support later * Update test to use `` * Refactor test to use `UNORDERED_AUTO_TEST` * Cleanup how the scoped allocator test is conditionally compiled * Update test to generate a UUID for the name of the shared memory segment so tests can run safely in parallel * Update test jamfile to relocate the Filesystem link dependency directly to the test that requires it --- test/Jamfile.v2 | 1 + test/unordered/scoped_allocator.cpp | 90 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 test/unordered/scoped_allocator.cpp 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