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>*, 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, TT&& t, UU&& u) {
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, std::forward<TT>(t), std::forward<UU>(u));
47 A.construct(ptr2,
48 std::piecewise_construct,
49 std::forward_as_tuple(std::forward<TT>(t)),
50 std::forward_as_tuple(std::forward<UU>(u)));
51 // ------- //
52
53 bool tres = checkConstruct<TT&&>(ptr->first, TExpect, M) && checkConstructionEquiv(ptr->first, ptr2->first);
54
55 bool ures = checkConstruct<UU&&>(ptr->second, UExpect, M) && checkConstructionEquiv(ptr->second, ptr2->second);
56
57 A.destroy(ptr);
58 A.destroy(ptr2);
59 std::free(ptr: ptr);
60 std::free(ptr: ptr2);
61 return tres && ures;
62}
63
64template <class Alloc, class TT, class UU>
65void test_pmr_uses_allocator(TT&& t, UU&& u) {
66 {
67 using T = NotUsesAllocator<Alloc, 1>;
68 using U = NotUsesAllocator<Alloc, 1>;
69 assert((doTest<T, U>(UA_None, UA_None, std::forward<TT>(t), std::forward<UU>(u))));
70 }
71 {
72 using T = UsesAllocatorV1<Alloc, 1>;
73 using U = UsesAllocatorV2<Alloc, 1>;
74 assert((doTest<T, U>(UA_AllocArg, UA_AllocLast, std::forward<TT>(t), std::forward<UU>(u))));
75 }
76 {
77 using T = UsesAllocatorV2<Alloc, 1>;
78 using U = UsesAllocatorV3<Alloc, 1>;
79 assert((doTest<T, U>(UA_AllocLast, UA_AllocArg, std::forward<TT>(t), std::forward<UU>(u))));
80 }
81 {
82 using T = UsesAllocatorV3<Alloc, 1>;
83 using U = NotUsesAllocator<Alloc, 1>;
84 assert((doTest<T, U>(UA_AllocArg, UA_None, std::forward<TT>(t), std::forward<UU>(u))));
85 }
86}
87
88template <class Alloc, class TT, class UU>
89void test_pmr_not_uses_allocator(TT&& t, UU&& u) {
90 {
91 using T = NotUsesAllocator<Alloc, 1>;
92 using U = NotUsesAllocator<Alloc, 1>;
93 assert((doTest<T, U>(UA_None, UA_None, std::forward<TT>(t), std::forward<UU>(u))));
94 }
95 {
96 using T = UsesAllocatorV1<Alloc, 1>;
97 using U = UsesAllocatorV2<Alloc, 1>;
98 assert((doTest<T, U>(UA_None, UA_None, std::forward<TT>(t), std::forward<UU>(u))));
99 }
100 {
101 using T = UsesAllocatorV2<Alloc, 1>;
102 using U = UsesAllocatorV3<Alloc, 1>;
103 assert((doTest<T, U>(UA_None, UA_None, std::forward<TT>(t), std::forward<UU>(u))));
104 }
105 {
106 using T = UsesAllocatorV3<Alloc, 1>;
107 using U = NotUsesAllocator<Alloc, 1>;
108 assert((doTest<T, U>(UA_None, UA_None, std::forward<TT>(t), std::forward<UU>(u))));
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 test_pmr_not_uses_allocator<PMR>(t&: x, u: std::move(y));
119 test_pmr_uses_allocator<PMA>(t&: x, u: std::move(y));
120 }
121 {
122 int x = 42;
123 const int y = 42;
124 test_pmr_not_uses_allocator<PMR>(t: std::move(x), u: y);
125 test_pmr_uses_allocator<PMA>(t: std::move(x), u: y);
126 }
127
128 return 0;
129}
130

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