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