mirror of
https://github.com/boostorg/unordered.git
synced 2025-07-30 03:17:15 +02:00
Write test pmr_emplace_erase to check basic functionality with pmr allocators
This commit is contained in:
@ -119,6 +119,7 @@ local FCA_TESTS =
|
|||||||
transparent_tests
|
transparent_tests
|
||||||
unnecessary_copy_tests
|
unnecessary_copy_tests
|
||||||
fancy_pointer_noleak
|
fancy_pointer_noleak
|
||||||
|
pmr_allocator_tests
|
||||||
;
|
;
|
||||||
|
|
||||||
for local test in $(FCA_TESTS)
|
for local test in $(FCA_TESTS)
|
||||||
@ -226,6 +227,7 @@ local FOA_TESTS =
|
|||||||
uses_allocator
|
uses_allocator
|
||||||
hash_is_avalanching_test
|
hash_is_avalanching_test
|
||||||
fancy_pointer_noleak
|
fancy_pointer_noleak
|
||||||
|
pmr_allocator_tests
|
||||||
;
|
;
|
||||||
|
|
||||||
for local test in $(FOA_TESTS)
|
for local test in $(FOA_TESTS)
|
||||||
@ -330,6 +332,7 @@ local CFOA_TESTS =
|
|||||||
rw_spinlock_test8
|
rw_spinlock_test8
|
||||||
reentrancy_check_test
|
reentrancy_check_test
|
||||||
explicit_alloc_ctor_tests
|
explicit_alloc_ctor_tests
|
||||||
|
pmr_allocator_tests
|
||||||
;
|
;
|
||||||
|
|
||||||
for local test in $(CFOA_TESTS)
|
for local test in $(CFOA_TESTS)
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
#define BOOST_UNORDERED_TEST_CFOA_HELPERS_HPP
|
#define BOOST_UNORDERED_TEST_CFOA_HELPERS_HPP
|
||||||
|
|
||||||
#include "../helpers/generators.hpp"
|
#include "../helpers/generators.hpp"
|
||||||
|
#include "../helpers/helpers.hpp"
|
||||||
|
#include "../helpers/pmr.hpp"
|
||||||
#include "../helpers/test.hpp"
|
#include "../helpers/test.hpp"
|
||||||
#include "common_helpers.hpp"
|
#include "common_helpers.hpp"
|
||||||
|
|
||||||
|
8
test/cfoa/pmr_allocator_tests.cpp
Normal file
8
test/cfoa/pmr_allocator_tests.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
// Copyright (C) 2024 Braden Ganetsky
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
#define BOOST_UNORDERED_CFOA_TESTS
|
||||||
|
#include <boost/unordered/concurrent_flat_map.hpp>
|
||||||
|
#include <boost/unordered/concurrent_flat_set.hpp>
|
||||||
|
#include "../unordered/pmr_allocator_tests.cpp"
|
@ -51,6 +51,11 @@ namespace test {
|
|||||||
static_cast<typename std::iterator_traits<Iterator>::difference_type>(x));
|
static_cast<typename std::iterator_traits<Iterator>::difference_type>(x));
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Container>
|
||||||
|
using is_map =
|
||||||
|
std::integral_constant<bool, !std::is_same<typename Container::key_type,
|
||||||
|
typename Container::value_type>::value>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
183
test/unordered/pmr_allocator_tests.cpp
Normal file
183
test/unordered/pmr_allocator_tests.cpp
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
//
|
||||||
|
// Copyright 2024 Braden Ganetsky.
|
||||||
|
// 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 "../helpers/helpers.hpp"
|
||||||
|
#include "../helpers/pmr.hpp"
|
||||||
|
#include "../helpers/test.hpp"
|
||||||
|
#include "../helpers/unordered.hpp"
|
||||||
|
#include <boost/config/pragma_message.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#ifdef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
|
||||||
|
|
||||||
|
BOOST_PRAGMA_MESSAGE(
|
||||||
|
"Test skipped because C++17 header <memory_resource> is not available.")
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
namespace pmr_allocator_tests {
|
||||||
|
|
||||||
|
using pmr_string = std::basic_string<char, std::char_traits<char>,
|
||||||
|
std::pmr::polymorphic_allocator<char> >;
|
||||||
|
|
||||||
|
#if defined(BOOST_UNORDERED_CFOA_TESTS)
|
||||||
|
static boost::unordered::pmr::concurrent_flat_map<std::string, std::string>*
|
||||||
|
test_string_flat_map;
|
||||||
|
static boost::unordered::pmr::concurrent_flat_map<pmr_string, pmr_string>*
|
||||||
|
test_pmr_string_flat_map;
|
||||||
|
static boost::unordered::pmr::concurrent_flat_set<std::string>*
|
||||||
|
test_string_flat_set;
|
||||||
|
static boost::unordered::pmr::concurrent_flat_set<pmr_string>*
|
||||||
|
test_pmr_string_flat_set;
|
||||||
|
#define PMR_ALLOCATOR_TESTS_ARGS \
|
||||||
|
((test_string_flat_map)(test_pmr_string_flat_map)(test_string_flat_set)(test_pmr_string_flat_set))
|
||||||
|
#elif defined(BOOST_UNORDERED_FOA_TESTS)
|
||||||
|
static boost::unordered::pmr::unordered_flat_map<std::string, std::string>*
|
||||||
|
test_string_flat_map;
|
||||||
|
static boost::unordered::pmr::unordered_flat_map<pmr_string, pmr_string>*
|
||||||
|
test_pmr_string_flat_map;
|
||||||
|
static boost::unordered::pmr::unordered_node_map<std::string, std::string>*
|
||||||
|
test_string_node_map;
|
||||||
|
static boost::unordered::pmr::unordered_node_map<pmr_string, pmr_string>*
|
||||||
|
test_pmr_string_node_map;
|
||||||
|
static boost::unordered::pmr::unordered_flat_set<std::string>*
|
||||||
|
test_string_flat_set;
|
||||||
|
static boost::unordered::pmr::unordered_flat_set<pmr_string>*
|
||||||
|
test_pmr_string_flat_set;
|
||||||
|
static boost::unordered::pmr::unordered_node_set<std::string>*
|
||||||
|
test_string_node_set;
|
||||||
|
static boost::unordered::pmr::unordered_node_set<pmr_string>*
|
||||||
|
test_pmr_string_node_set;
|
||||||
|
#define PMR_ALLOCATOR_TESTS_ARGS \
|
||||||
|
((test_string_flat_map)(test_pmr_string_flat_map)(test_string_node_map)(test_pmr_string_node_map)(test_string_flat_set)(test_pmr_string_flat_set)(test_string_node_set)(test_pmr_string_node_set))
|
||||||
|
#else
|
||||||
|
static boost::unordered::pmr::unordered_map<std::string, std::string>*
|
||||||
|
test_string_map;
|
||||||
|
static boost::unordered::pmr::unordered_map<pmr_string, pmr_string>*
|
||||||
|
test_pmr_string_map;
|
||||||
|
static boost::unordered::pmr::unordered_multimap<std::string, std::string>*
|
||||||
|
test_string_multimap;
|
||||||
|
static boost::unordered::pmr::unordered_multimap<pmr_string, pmr_string>*
|
||||||
|
test_pmr_string_multimap;
|
||||||
|
static boost::unordered::pmr::unordered_set<std::string>* test_string_set;
|
||||||
|
static boost::unordered::pmr::unordered_set<pmr_string>* test_pmr_string_set;
|
||||||
|
static boost::unordered::pmr::unordered_multiset<std::string>*
|
||||||
|
test_string_multiset;
|
||||||
|
static boost::unordered::pmr::unordered_multiset<pmr_string>*
|
||||||
|
test_pmr_string_multiset;
|
||||||
|
#define PMR_ALLOCATOR_TESTS_ARGS \
|
||||||
|
((test_string_map)(test_pmr_string_map)(test_string_multimap)(test_pmr_string_multimap)(test_string_set)(test_pmr_string_set)(test_string_multiset)(test_pmr_string_multiset))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <class X>
|
||||||
|
typename std::enable_if<!test::is_map<X>::value, std::size_t>::type
|
||||||
|
emplace_strings(X& x)
|
||||||
|
{
|
||||||
|
std::string_view sv =
|
||||||
|
"this is a string that's longer than the SBO threshold";
|
||||||
|
x.emplace(sv);
|
||||||
|
// Return how many chars were allocated using a pmr allocator
|
||||||
|
return std::is_same<typename X::key_type, pmr_string>::value ? sv.size() + 1
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class X>
|
||||||
|
typename std::enable_if<test::is_map<X>::value, std::size_t>::type
|
||||||
|
emplace_strings(X& x)
|
||||||
|
{
|
||||||
|
std::string_view key =
|
||||||
|
"this is a string that's longer than the SBO threshold";
|
||||||
|
std::string_view value =
|
||||||
|
"this is another long string that's longer than the SBO threshold";
|
||||||
|
x.emplace(key, value);
|
||||||
|
// Return how many chars were allocated using a pmr allocator
|
||||||
|
return std::is_same<typename X::key_type, pmr_string>::value
|
||||||
|
? key.size() + value.size() + 2
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void validate_resource(
|
||||||
|
pmr_string const& str, test::counted_new_delete_resource const& resource)
|
||||||
|
{
|
||||||
|
BOOST_TEST_EQ(str.get_allocator().resource(), &resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
void validate_resource(
|
||||||
|
std::string const&, test::counted_new_delete_resource const&)
|
||||||
|
{
|
||||||
|
// Pass through
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class X>
|
||||||
|
typename std::enable_if<!test::is_map<X>::value>::type validate_resource(
|
||||||
|
X& x, test::counted_new_delete_resource const& resource)
|
||||||
|
{
|
||||||
|
#if defined(BOOST_UNORDERED_CFOA_TESTS)
|
||||||
|
x.cvisit_all(
|
||||||
|
[&resource](auto& element) { validate_resource(element, resource); });
|
||||||
|
#else
|
||||||
|
for (auto& element : x) {
|
||||||
|
validate_resource(element, resource);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class X>
|
||||||
|
typename std::enable_if<test::is_map<X>::value>::type validate_resource(
|
||||||
|
X& x, test::counted_new_delete_resource const& resource)
|
||||||
|
{
|
||||||
|
#if defined(BOOST_UNORDERED_CFOA_TESTS)
|
||||||
|
x.cvisit_all([&resource](auto& element) {
|
||||||
|
validate_resource(element.first, resource);
|
||||||
|
validate_resource(element.second, resource);
|
||||||
|
});
|
||||||
|
#else
|
||||||
|
for (auto& element : x) {
|
||||||
|
validate_resource(element.first, resource);
|
||||||
|
validate_resource(element.second, resource);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class X> static void pmr_emplace_erase(X*)
|
||||||
|
{
|
||||||
|
using container = X;
|
||||||
|
using allocator_type = typename container::allocator_type;
|
||||||
|
|
||||||
|
test::counted_new_delete_resource resource;
|
||||||
|
|
||||||
|
{
|
||||||
|
allocator_type alloc(&resource);
|
||||||
|
container x(alloc);
|
||||||
|
|
||||||
|
std::size_t num_chars = emplace_strings(x);
|
||||||
|
BOOST_TEST_EQ(x.size(), 1);
|
||||||
|
std::size_t count_after_emplace = resource.count();
|
||||||
|
BOOST_TEST_GT(count_after_emplace,
|
||||||
|
num_chars + sizeof(typename container::value_type));
|
||||||
|
|
||||||
|
validate_resource(x, resource);
|
||||||
|
|
||||||
|
x.clear();
|
||||||
|
BOOST_TEST_LE(resource.count(), count_after_emplace);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST_EQ(resource.count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
|
UNORDERED_TEST(
|
||||||
|
pmr_emplace_erase,
|
||||||
|
PMR_ALLOCATOR_TESTS_ARGS
|
||||||
|
)
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
} // namespace pmr_allocator_tests
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
RUN_TESTS()
|
Reference in New Issue
Block a user