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