| 1 | |
| 2 | // Copyright 2021-2023 Christian Mazakas. |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <boost/config.hpp> |
| 7 | #include <boost/config/pragma_message.hpp> |
| 8 | #include <boost/config/workaround.hpp> |
| 9 | |
| 10 | #if BOOST_CXX_VERSION <= 199711L || \ |
| 11 | BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800) || \ |
| 12 | BOOST_WORKAROUND(BOOST_MSVC, == 1900) |
| 13 | |
| 14 | BOOST_PRAGMA_MESSAGE( |
| 15 | "scoped allocator adaptor tests only work under C++11 and above" ) |
| 16 | int main() {} |
| 17 | |
| 18 | #else |
| 19 | |
| 20 | // This test is based on a user-submitted issue found here: |
| 21 | // https://github.com/boostorg/unordered/issues/22 |
| 22 | // |
| 23 | |
| 24 | #include "../helpers/unordered.hpp" |
| 25 | |
| 26 | #include "../helpers/test.hpp" |
| 27 | |
| 28 | #include <boost/cstdint.hpp> |
| 29 | |
| 30 | #include <boost/core/ignore_unused.hpp> |
| 31 | |
| 32 | #include <scoped_allocator> |
| 33 | #include <string> |
| 34 | #include <utility> |
| 35 | #include <vector> |
| 36 | |
| 37 | namespace test { |
| 38 | template <class T> struct allocator |
| 39 | { |
| 40 | typedef T value_type; |
| 41 | |
| 42 | allocator() = delete; |
| 43 | allocator(int) {} |
| 44 | allocator(allocator const&) = default; |
| 45 | allocator(allocator&&) = default; |
| 46 | |
| 47 | template <class U> allocator(allocator<U> const&) {} |
| 48 | |
| 49 | BOOST_ATTRIBUTE_NODISCARD T* allocate(std::size_t n) |
| 50 | { |
| 51 | return static_cast<T*>(::operator new(n * sizeof(T))); |
| 52 | } |
| 53 | |
| 54 | void deallocate(T* p, std::size_t) noexcept { ::operator delete(p); } |
| 55 | |
| 56 | bool operator==(allocator const&) const { return true; } |
| 57 | bool operator!=(allocator const&) const { return false; } |
| 58 | }; |
| 59 | } // namespace test |
| 60 | |
| 61 | typedef std::vector<boost::uint64_t, test::allocator<boost::uint64_t> > |
| 62 | vector_type; |
| 63 | |
| 64 | typedef std::pair<boost::uint64_t, vector_type> pair_type; |
| 65 | |
| 66 | typedef std::scoped_allocator_adaptor<test::allocator<pair_type>, |
| 67 | test::allocator<boost::uint64_t> > |
| 68 | allocator_type; |
| 69 | |
| 70 | template <class X> static void scoped_allocator(X*) |
| 71 | { |
| 72 | allocator_type alloc( |
| 73 | test::allocator<pair_type>(1337), test::allocator<boost::uint64_t>(7331)); |
| 74 | |
| 75 | X map(alloc); |
| 76 | |
| 77 | for (unsigned i = 0; i < 10; ++i) { |
| 78 | boost::ignore_unused(map[i]); |
| 79 | } |
| 80 | |
| 81 | BOOST_TEST(map.size() == 10); |
| 82 | } |
| 83 | |
| 84 | #ifdef BOOST_UNORDERED_FOA_TESTS |
| 85 | static boost::unordered_flat_map<const boost::uint64_t, vector_type, |
| 86 | boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>* |
| 87 | test_map; |
| 88 | |
| 89 | static boost::unordered_node_map<const boost::uint64_t, vector_type, |
| 90 | boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>* |
| 91 | test_node_map; |
| 92 | |
| 93 | UNORDERED_TEST(scoped_allocator, ((test_map)(test_node_map))) |
| 94 | #else |
| 95 | static boost::unordered_map<const boost::uint64_t, vector_type, |
| 96 | boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>* |
| 97 | test_map; |
| 98 | |
| 99 | UNORDERED_TEST(scoped_allocator, ((test_map))) |
| 100 | #endif |
| 101 | |
| 102 | RUN_TESTS() |
| 103 | |
| 104 | #endif |
| 105 | |