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, libcpp-no-rtti
10
11 // <experimental/memory_resource>
12
13 // template <class T> class polymorphic_allocator
14
15 // template <class P1, class P2, class U1, class U2>
16 // void polymorphic_allocator<T>::construct(pair<P1, P2>*, pair<U1, U2> &&)
17
18 #include <experimental/memory_resource>
19 #include <type_traits>
20 #include <utility>
21 #include <tuple>
22 #include <cassert>
23 #include <cstdlib>
24
25 #include "test_macros.h"
26 #include "test_memory_resource.h"
27 #include "uses_alloc_types.h"
28 #include "controlled_allocators.h"
29 #include "test_allocator.h"
30
31 namespace ex = std::experimental::pmr;
32
33
34
35 template <class UA1, class UA2, class TT, class UU>
doTest(UsesAllocatorType TExpect,UsesAllocatorType UExpect,std::pair<TT,UU> && p)36 bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect,
37 std::pair<TT, UU>&& p)
38 {
39 using P = std::pair<UA1, UA2>;
40 TestResource R;
41 ex::memory_resource * M = &R;
42 ex::polymorphic_allocator<P> A(M);
43 P * ptr = A.allocate(2);
44 P * ptr2 = ptr + 1;
45
46 // UNDER TEST //
47 A.construct(ptr, std::move(p));
48
49 A.construct(ptr2, 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) &&
55 checkConstructionEquiv(ptr->first, ptr2->first);
56
57 bool ures = checkConstruct<UU&&>(ptr->second, UExpect, M) &&
58 checkConstructionEquiv(ptr->second, ptr2->second);
59
60 A.destroy(ptr);
61 A.destroy(ptr2);
62 A.deallocate(ptr, 2);
63 return tres && ures;
64 }
65
66 template <class Alloc, class TT, class UU>
test_pmr_uses_allocator(std::pair<TT,UU> && p)67 void test_pmr_uses_allocator(std::pair<TT, UU>&& p)
68 {
69 {
70 using T = NotUsesAllocator<Alloc, 1>;
71 using U = NotUsesAllocator<Alloc, 1>;
72 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
73 }
74 {
75 using T = UsesAllocatorV1<Alloc, 1>;
76 using U = UsesAllocatorV2<Alloc, 1>;
77 assert((doTest<T, U>(UA_AllocArg, UA_AllocLast, std::move(p))));
78 }
79 {
80 using T = UsesAllocatorV2<Alloc, 1>;
81 using U = UsesAllocatorV3<Alloc, 1>;
82 assert((doTest<T, U>(UA_AllocLast, UA_AllocArg, std::move(p))));
83 }
84 {
85 using T = UsesAllocatorV3<Alloc, 1>;
86 using U = NotUsesAllocator<Alloc, 1>;
87 assert((doTest<T, U>(UA_AllocArg, UA_None, std::move(p))));
88 }
89 }
90
91 template <class Alloc, class TT, class UU>
test_pmr_not_uses_allocator(std::pair<TT,UU> && p)92 void test_pmr_not_uses_allocator(std::pair<TT, UU>&& p)
93 {
94 {
95 using T = NotUsesAllocator<Alloc, 1>;
96 using U = NotUsesAllocator<Alloc, 1>;
97 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
98 }
99 {
100 using T = UsesAllocatorV1<Alloc, 1>;
101 using U = UsesAllocatorV2<Alloc, 1>;
102 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
103 }
104 {
105 using T = UsesAllocatorV2<Alloc, 1>;
106 using U = UsesAllocatorV3<Alloc, 1>;
107 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
108 }
109 {
110 using T = UsesAllocatorV3<Alloc, 1>;
111 using U = NotUsesAllocator<Alloc, 1>;
112 assert((doTest<T, U>(UA_None, UA_None, std::move(p))));
113 }
114 }
115
main(int,char **)116 int main(int, char**)
117 {
118 using ERT = std::experimental::erased_type;
119 using PMR = ex::memory_resource*;
120 using PMA = ex::polymorphic_allocator<char>;
121 {
122 int x = 42;
123 int y = 42;
124 std::pair<int&, int&&> p(x, std::move(y));
125 test_pmr_uses_allocator<ERT>(std::move(p));
126 test_pmr_not_uses_allocator<PMR>(std::move(p));
127 test_pmr_uses_allocator<PMA>(std::move(p));
128 }
129 {
130 int x = 42;
131 int y = 42;
132 std::pair<int&&, int&> p(std::move(x), y);
133 test_pmr_uses_allocator<ERT>(std::move(p));
134 test_pmr_not_uses_allocator<PMR>(std::move(p));
135 test_pmr_uses_allocator<PMA>(std::move(p));
136 }
137
138 return 0;
139 }
140