| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14 |
| 10 | // TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed |
| 11 | // UNSUPPORTED: availability-pmr-missing |
| 12 | |
| 13 | // test_memory_resource requires RTTI for dynamic_cast |
| 14 | // UNSUPPORTED: no-rtti |
| 15 | |
| 16 | // <memory_resource> |
| 17 | |
| 18 | // template <class T> class polymorphic_allocator |
| 19 | |
| 20 | // template <class P1, class P2, class U1, class U2> |
| 21 | // void polymorphic_allocator<T>::construct(pair<P1, P2>*, pair<U1, U2> const&) |
| 22 | |
| 23 | #include <memory_resource> |
| 24 | #include <type_traits> |
| 25 | #include <utility> |
| 26 | #include <tuple> |
| 27 | #include <cassert> |
| 28 | #include <cstdlib> |
| 29 | |
| 30 | #include "test_macros.h" |
| 31 | #include "test_std_memory_resource.h" |
| 32 | #include "uses_alloc_types.h" |
| 33 | #include "controlled_allocators.h" |
| 34 | #include "test_allocator.h" |
| 35 | |
| 36 | template <class UA1, class UA2, class TT, class UU> |
| 37 | bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect, std::pair<TT, UU> const& p) { |
| 38 | using P = std::pair<UA1, UA2>; |
| 39 | TestResource R; |
| 40 | std::pmr::memory_resource* M = &R; |
| 41 | std::pmr::polymorphic_allocator<P> A(M); |
| 42 | P* ptr = (P*)std::malloc(size: sizeof(P)); |
| 43 | P* ptr2 = (P*)std::malloc(size: sizeof(P)); |
| 44 | |
| 45 | // UNDER TEST // |
| 46 | A.construct(ptr, p); |
| 47 | |
| 48 | A.construct(ptr2, std::piecewise_construct, std::forward_as_tuple(p.first), std::forward_as_tuple(p.second)); |
| 49 | // ------- // |
| 50 | |
| 51 | bool tres = |
| 52 | checkConstruct<decltype((p.first))>(ptr->first, TExpect, M) && checkConstructionEquiv(ptr->first, ptr2->first); |
| 53 | |
| 54 | bool ures = checkConstruct<decltype((p.second))>(ptr->second, UExpect, M) && |
| 55 | checkConstructionEquiv(ptr->second, ptr2->second); |
| 56 | |
| 57 | A.destroy(ptr); |
| 58 | std::free(ptr: ptr); |
| 59 | A.destroy(ptr2); |
| 60 | std::free(ptr: ptr2); |
| 61 | return tres && ures; |
| 62 | } |
| 63 | |
| 64 | template <class Alloc, class TT, class UU> |
| 65 | void test_pmr_uses_allocator(std::pair<TT, UU> const& p) { |
| 66 | { |
| 67 | using T = NotUsesAllocator<Alloc, 1>; |
| 68 | using U = NotUsesAllocator<Alloc, 1>; |
| 69 | assert((doTest<T, U>(UA_None, UA_None, p))); |
| 70 | } |
| 71 | { |
| 72 | using T = UsesAllocatorV1<Alloc, 1>; |
| 73 | using U = UsesAllocatorV2<Alloc, 1>; |
| 74 | assert((doTest<T, U>(UA_AllocArg, UA_AllocLast, p))); |
| 75 | } |
| 76 | { |
| 77 | using T = UsesAllocatorV2<Alloc, 1>; |
| 78 | using U = UsesAllocatorV3<Alloc, 1>; |
| 79 | assert((doTest<T, U>(UA_AllocLast, UA_AllocArg, p))); |
| 80 | } |
| 81 | { |
| 82 | using T = UsesAllocatorV3<Alloc, 1>; |
| 83 | using U = NotUsesAllocator<Alloc, 1>; |
| 84 | assert((doTest<T, U>(UA_AllocArg, UA_None, p))); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | template <class Alloc, class TT, class UU> |
| 89 | void test_pmr_not_uses_allocator(std::pair<TT, UU> const& p) { |
| 90 | { |
| 91 | using T = NotUsesAllocator<Alloc, 1>; |
| 92 | using U = NotUsesAllocator<Alloc, 1>; |
| 93 | assert((doTest<T, U>(UA_None, UA_None, p))); |
| 94 | } |
| 95 | { |
| 96 | using T = UsesAllocatorV1<Alloc, 1>; |
| 97 | using U = UsesAllocatorV2<Alloc, 1>; |
| 98 | assert((doTest<T, U>(UA_None, UA_None, p))); |
| 99 | } |
| 100 | { |
| 101 | using T = UsesAllocatorV2<Alloc, 1>; |
| 102 | using U = UsesAllocatorV3<Alloc, 1>; |
| 103 | assert((doTest<T, U>(UA_None, UA_None, p))); |
| 104 | } |
| 105 | { |
| 106 | using T = UsesAllocatorV3<Alloc, 1>; |
| 107 | using U = NotUsesAllocator<Alloc, 1>; |
| 108 | assert((doTest<T, U>(UA_None, UA_None, p))); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | template <class Tp> |
| 113 | struct Print; |
| 114 | |
| 115 | int main(int, char**) { |
| 116 | using PMR = std::pmr::memory_resource*; |
| 117 | using PMA = std::pmr::polymorphic_allocator<char>; |
| 118 | { |
| 119 | int x = 42; |
| 120 | int y = 42; |
| 121 | const std::pair<int, int&> p(x, y); |
| 122 | test_pmr_not_uses_allocator<PMR>(p); |
| 123 | test_pmr_uses_allocator<PMA>(p); |
| 124 | } |
| 125 | { |
| 126 | int x = 42; |
| 127 | int y = 42; |
| 128 | const std::pair<int&, int&&> p(x, std::move(y)); |
| 129 | test_pmr_not_uses_allocator<PMR>(p); |
| 130 | test_pmr_uses_allocator<PMA>(p); |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |