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> &&)
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
36template <class UA1, class UA2, class TT, class UU>
37bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect, std::pair<TT, UU>&& 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 = A.allocate(2);
43 P* ptr2 = ptr + 1;
44
45 // UNDER TEST //
46 A.construct(ptr, std::move(p));
47
48 A.construct(ptr2,
49 std::piecewise_construct,
50 std::forward_as_tuple(std::forward<TT>(p.first)),
51 std::forward_as_tuple(std::forward<UU>(p.second)));
52 // ------- //
53
54 bool tres = checkConstruct<TT&&>(ptr->first, TExpect, M) && checkConstructionEquiv(ptr->first, ptr2->first);
55
56 bool ures = checkConstruct<UU&&>(ptr->second, UExpect, M) && checkConstructionEquiv(ptr->second, ptr2->second);
57
58 A.destroy(ptr);
59 A.destroy(ptr2);
60 A.deallocate(ptr, 2);
61 return tres && ures;
62}
63
64template <class Alloc, class TT, class UU>
65void test_pmr_uses_allocator(std::pair<TT, UU>&& p) {
66 {
67 using T = NotUsesAllocator<Alloc, 1>;
68 using U = NotUsesAllocator<Alloc, 1>;
69 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
70 }
71 {
72 using T = UsesAllocatorV1<Alloc, 1>;
73 using U = UsesAllocatorV2<Alloc, 1>;
74 assert((doTest<T, U>(UA_AllocArg, UA_AllocLast, std::move(p))));
75 }
76 {
77 using T = UsesAllocatorV2<Alloc, 1>;
78 using U = UsesAllocatorV3<Alloc, 1>;
79 assert((doTest<T, U>(UA_AllocLast, UA_AllocArg, std::move(p))));
80 }
81 {
82 using T = UsesAllocatorV3<Alloc, 1>;
83 using U = NotUsesAllocator<Alloc, 1>;
84 assert((doTest<T, U>(UA_AllocArg, UA_None, std::move(p))));
85 }
86}
87
88template <class Alloc, class TT, class UU>
89void test_pmr_not_uses_allocator(std::pair<TT, UU>&& p) {
90 {
91 using T = NotUsesAllocator<Alloc, 1>;
92 using U = NotUsesAllocator<Alloc, 1>;
93 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
94 }
95 {
96 using T = UsesAllocatorV1<Alloc, 1>;
97 using U = UsesAllocatorV2<Alloc, 1>;
98 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
99 }
100 {
101 using T = UsesAllocatorV2<Alloc, 1>;
102 using U = UsesAllocatorV3<Alloc, 1>;
103 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
104 }
105 {
106 using T = UsesAllocatorV3<Alloc, 1>;
107 using U = NotUsesAllocator<Alloc, 1>;
108 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
109 }
110}
111
112int main(int, char**) {
113 using PMR = std::pmr::memory_resource*;
114 using PMA = std::pmr::polymorphic_allocator<char>;
115 {
116 int x = 42;
117 int y = 42;
118 std::pair<int&, int&&> p(x, std::move(y));
119 test_pmr_not_uses_allocator<PMR>(p: std::move(p));
120 test_pmr_uses_allocator<PMA>(p: std::move(p));
121 }
122 {
123 int x = 42;
124 int y = 42;
125 std::pair<int&&, int&> p(std::move(x), y);
126 test_pmr_not_uses_allocator<PMR>(p: std::move(p));
127 test_pmr_uses_allocator<PMA>(p: std::move(p));
128 }
129
130 return 0;
131}
132

source code of libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_pair_rvalue.pass.cpp