2021-11-15 11:22:40 -08:00
|
|
|
|
2023-02-14 10:36:57 -08:00
|
|
|
// Copyright 2021-2023 Christian Mazakas.
|
2021-11-15 11:22:40 -08:00
|
|
|
// 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)
|
|
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
#include <boost/config.hpp>
|
|
|
|
|
#include <boost/config/pragma_message.hpp>
|
2022-06-27 19:58:22 +02:00
|
|
|
#include <boost/config/workaround.hpp>
|
2022-02-08 15:10:51 -08:00
|
|
|
|
2023-05-30 12:05:10 -07:00
|
|
|
#if BOOST_CXX_VERSION <= 199711L || \
|
|
|
|
|
BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800) || \
|
|
|
|
|
BOOST_WORKAROUND(BOOST_MSVC, == 1900)
|
2022-02-08 15:10:51 -08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
//
|
|
|
|
|
|
2022-10-20 15:25:40 -07:00
|
|
|
#include "../helpers/unordered.hpp"
|
2021-11-15 11:22:40 -08:00
|
|
|
|
|
|
|
|
#include "../helpers/test.hpp"
|
|
|
|
|
|
|
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
|
|
|
|
|
|
#include <boost/core/ignore_unused.hpp>
|
|
|
|
|
|
2022-10-14 11:02:05 -07:00
|
|
|
#include <scoped_allocator>
|
2021-11-15 11:22:40 -08:00
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
namespace test {
|
|
|
|
|
template <class T> struct allocator
|
|
|
|
|
{
|
|
|
|
|
typedef T value_type;
|
2021-11-15 11:22:40 -08:00
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
allocator() = delete;
|
|
|
|
|
allocator(int) {}
|
|
|
|
|
allocator(allocator const&) = default;
|
|
|
|
|
allocator(allocator&&) = default;
|
2021-11-15 11:22:40 -08:00
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
template <class U> allocator(allocator<U> const&) {}
|
2021-11-15 11:22:40 -08:00
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
BOOST_ATTRIBUTE_NODISCARD T* allocate(std::size_t n)
|
|
|
|
|
{
|
|
|
|
|
return static_cast<T*>(::operator new(n * sizeof(T)));
|
|
|
|
|
}
|
2021-11-15 11:22:40 -08:00
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
void deallocate(T* p, std::size_t) noexcept { ::operator delete(p); }
|
2021-11-15 11:22:40 -08:00
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
bool operator==(allocator const&) const { return true; }
|
|
|
|
|
bool operator!=(allocator const&) const { return false; }
|
|
|
|
|
};
|
|
|
|
|
} // namespace test
|
2021-11-15 11:22:40 -08:00
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
typedef std::vector<boost::uint64_t, test::allocator<boost::uint64_t> >
|
2021-11-15 11:22:40 -08:00
|
|
|
vector_type;
|
|
|
|
|
|
|
|
|
|
typedef std::pair<boost::uint64_t, vector_type> pair_type;
|
|
|
|
|
|
2022-02-08 15:10:51 -08:00
|
|
|
typedef std::scoped_allocator_adaptor<test::allocator<pair_type>,
|
|
|
|
|
test::allocator<boost::uint64_t> >
|
2021-11-15 11:22:40 -08:00
|
|
|
allocator_type;
|
|
|
|
|
|
2023-02-14 10:36:57 -08:00
|
|
|
template <class X> static void scoped_allocator(X*)
|
|
|
|
|
{
|
2022-02-08 15:10:51 -08:00
|
|
|
allocator_type alloc(
|
|
|
|
|
test::allocator<pair_type>(1337), test::allocator<boost::uint64_t>(7331));
|
2021-11-15 11:22:40 -08:00
|
|
|
|
2023-02-14 10:36:57 -08:00
|
|
|
X map(alloc);
|
2021-11-15 11:22:40 -08:00
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 10; ++i) {
|
|
|
|
|
boost::ignore_unused(map[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_TEST(map.size() == 10);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 10:36:57 -08:00
|
|
|
#ifdef BOOST_UNORDERED_FOA_TESTS
|
|
|
|
|
static boost::unordered_flat_map<const boost::uint64_t, vector_type,
|
|
|
|
|
boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>*
|
|
|
|
|
test_map;
|
|
|
|
|
|
|
|
|
|
static boost::unordered_node_map<const boost::uint64_t, vector_type,
|
|
|
|
|
boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>*
|
|
|
|
|
test_node_map;
|
|
|
|
|
|
|
|
|
|
UNORDERED_TEST(scoped_allocator, ((test_map)(test_node_map)))
|
|
|
|
|
#else
|
|
|
|
|
static boost::unordered_map<const boost::uint64_t, vector_type,
|
|
|
|
|
boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>*
|
|
|
|
|
test_map;
|
|
|
|
|
|
|
|
|
|
UNORDERED_TEST(scoped_allocator, ((test_map)))
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-11-15 11:22:40 -08:00
|
|
|
RUN_TESTS()
|
|
|
|
|
|
|
|
|
|
#endif
|