1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // REQUIRES: c++experimental
11 // UNSUPPORTED: c++98, c++03
13 // <experimental/memory_resource>
15 // template <class T> class polymorphic_allocator
17 // template <class U1, class U2, class ...Args1, class ...Args2>
18 // void polymorphic_allocator<T>::construct(pair<U1, U2>*, piecewise_construct_t
19 // tuple<Args1...>, tuple<Args2...>)
21 #include <experimental/memory_resource>
22 #include <type_traits>
28 #include "test_macros.h"
29 #include "test_memory_resource.hpp"
30 #include "uses_alloc_types.hpp"
31 #include "controlled_allocators.hpp"
32 #include "test_allocator.h"
34 namespace ex = std::experimental::pmr;
36 template <class T, class U, class ...TTuple, class ...UTuple>
37 bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect,
38 std::tuple<TTuple...> ttuple, std::tuple<UTuple...> utuple)
40 using P = std::pair<T, U>;
42 ex::memory_resource * M = &R;
43 ex::polymorphic_allocator<P> A(M);
44 P * ptr = A.allocate(1);
47 A.construct(ptr, std::piecewise_construct, std::move(ttuple), std::move(utuple));
49 bool tres = checkConstruct<TTuple&&...>(ptr->first, TExpect, M);
50 bool ures = checkConstruct<UTuple&&...>(ptr->second, UExpect, M);
57 template <class Alloc, class ...TTypes, class ...UTypes>
58 void test_pmr_uses_allocator(std::tuple<TTypes...> ttuple, std::tuple<UTypes...> utuple)
61 using T = NotUsesAllocator<Alloc, sizeof...(TTypes)>;
62 using U = NotUsesAllocator<Alloc, sizeof...(UTypes)>;
63 assert((doTest<T, U>(UA_None, UA_None,
64 std::move(ttuple), std::move(utuple))));
67 using T = UsesAllocatorV1<Alloc, sizeof...(TTypes)>;
68 using U = UsesAllocatorV2<Alloc, sizeof...(UTypes)>;
69 assert((doTest<T, U>(UA_AllocArg, UA_AllocLast,
70 std::move(ttuple), std::move(utuple))));
73 using T = UsesAllocatorV2<Alloc, sizeof...(TTypes)>;
74 using U = UsesAllocatorV3<Alloc, sizeof...(UTypes)>;
75 assert((doTest<T, U>(UA_AllocLast, UA_AllocArg,
76 std::move(ttuple), std::move(utuple))));
79 using T = UsesAllocatorV3<Alloc, sizeof...(TTypes)>;
80 using U = NotUsesAllocator<Alloc, sizeof...(UTypes)>;
81 assert((doTest<T, U>(UA_AllocArg, UA_None,
82 std::move(ttuple), std::move(utuple))));
88 using ERT = std::experimental::erased_type;
89 using PMR = ex::memory_resource*;
90 using PMA = ex::polymorphic_allocator<char>;
93 test_pmr_uses_allocator<ERT>(t1, t1);
94 test_pmr_uses_allocator<PMR>(t1, t1);
95 test_pmr_uses_allocator<PMA>(t1, t1);
98 std::tuple<int> t1(42);
100 test_pmr_uses_allocator<ERT>(t1, t2);
101 test_pmr_uses_allocator<ERT>(t2, t1);
102 test_pmr_uses_allocator<PMR>(t1, t2);
103 test_pmr_uses_allocator<PMR>(t2, t1);
104 test_pmr_uses_allocator<PMA>(t1, t2);
105 test_pmr_uses_allocator<PMA>(t2, t1);
108 std::tuple<int> t1(42);
111 std::tuple<int&, double&&> t2(x, std::move(dx));
112 test_pmr_uses_allocator<ERT>( t1, std::move(t2));
113 test_pmr_uses_allocator<ERT>(std::move(t2), t1);
114 test_pmr_uses_allocator<PMR>( t1, std::move(t2));
115 test_pmr_uses_allocator<PMR>(std::move(t2), t1);
116 test_pmr_uses_allocator<PMA>( t1, std::move(t2));
117 test_pmr_uses_allocator<PMA>(std::move(t2), t1);
120 void* xptr = nullptr;
122 std::tuple<int, long const&, void*&> t1(42, y, xptr);
125 const char* s = "hello World";
126 std::tuple<int&, double&&, const char*> t2(x, std::move(dx), s);
127 test_pmr_uses_allocator<ERT>( t1, std::move(t2));
128 test_pmr_uses_allocator<ERT>(std::move(t2), t1);
129 test_pmr_uses_allocator<PMR>( t1, std::move(t2));
130 test_pmr_uses_allocator<PMR>(std::move(t2), t1);
131 test_pmr_uses_allocator<PMA>( t1, std::move(t2));
132 test_pmr_uses_allocator<PMA>(std::move(t2), t1);