blob: 206bfb017ed422d42636bb9139078a9b3432ed4d [file] [log] [blame]
Eric Fiselier257fd692016-05-07 01:04:55 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// UNSUPPORTED: c++98, c++03
11
12// <experimental/memory_resource>
13
14// template <class T> class polymorphic_allocator
15
16// template <class U1, class U2, class ...Args1, class ...Args2>
17// void polymorphic_allocator<T>::construct(pair<U1, U2>*, piecewise_construct_t
18// tuple<Args1...>, tuple<Args2...>)
19
20#include <experimental/memory_resource>
21#include <type_traits>
22#include <utility>
23#include <tuple>
24#include <cassert>
25#include <cstdlib>
26#include "uses_alloc_types.hpp"
27#include "test_allocator.h"
28
29namespace ex = std::experimental::pmr;
30
31template <class T, class U, class ...TTuple, class ...UTuple>
32bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect,
33 std::tuple<TTuple...> ttuple, std::tuple<UTuple...> utuple)
34{
35 using P = std::pair<T, U>;
36 TestResource R;
37 ex::memory_resource * M = &R;
38 ex::polymorphic_allocator<P> A(M);
39 P * ptr = A.allocate(1);
40
41 // UNDER TEST //
42 A.construct(ptr, std::piecewise_construct, std::move(ttuple), std::move(utuple));
43 // ------- //
44 bool tres = checkConstruct<TTuple&&...>(ptr->first, TExpect, M);
45 bool ures = checkConstruct<UTuple&&...>(ptr->second, UExpect, M);
46
47 A.destroy(ptr);
48 A.deallocate(ptr, 1);
49 return tres && ures;
50}
51
52template <class Alloc, class ...TTypes, class ...UTypes>
53void test_pmr_uses_allocator(std::tuple<TTypes...> ttuple, std::tuple<UTypes...> utuple)
54{
55 {
56 using T = NotUsesAllocator<Alloc, sizeof...(TTypes)>;
57 using U = NotUsesAllocator<Alloc, sizeof...(UTypes)>;
58 assert((doTest<T, U>(UA_None, UA_None,
59 std::move(ttuple), std::move(utuple))));
60 }
61 {
62 using T = UsesAllocatorV1<Alloc, sizeof...(TTypes)>;
63 using U = UsesAllocatorV2<Alloc, sizeof...(UTypes)>;
64 assert((doTest<T, U>(UA_AllocArg, UA_AllocLast,
65 std::move(ttuple), std::move(utuple))));
66 }
67 {
68 using T = UsesAllocatorV2<Alloc, sizeof...(TTypes)>;
69 using U = UsesAllocatorV3<Alloc, sizeof...(UTypes)>;
70 assert((doTest<T, U>(UA_AllocLast, UA_AllocArg,
71 std::move(ttuple), std::move(utuple))));
72 }
73 {
74 using T = UsesAllocatorV3<Alloc, sizeof...(TTypes)>;
75 using U = NotUsesAllocator<Alloc, sizeof...(UTypes)>;
76 assert((doTest<T, U>(UA_AllocArg, UA_None,
77 std::move(ttuple), std::move(utuple))));
78 }
79}
80
81int main()
82{
83 using ERT = std::experimental::erased_type;
84 using PMR = ex::memory_resource*;
85 using PMA = ex::polymorphic_allocator<char>;
86 {
87 std::tuple<> t1;
88 test_pmr_uses_allocator<ERT>(t1, t1);
89 test_pmr_uses_allocator<PMR>(t1, t1);
90 test_pmr_uses_allocator<PMA>(t1, t1);
91 }
92 {
93 std::tuple<int> t1(42);
94 std::tuple<> t2;
95 test_pmr_uses_allocator<ERT>(t1, t2);
96 test_pmr_uses_allocator<ERT>(t2, t1);
97 test_pmr_uses_allocator<PMR>(t1, t2);
98 test_pmr_uses_allocator<PMR>(t2, t1);
99 test_pmr_uses_allocator<PMA>(t1, t2);
100 test_pmr_uses_allocator<PMA>(t2, t1);
101 }
102 {
103 std::tuple<int> t1(42);
104 int x = 55;
105 double dx = 42.42;
106 std::tuple<int&, double&&> t2(x, std::move(dx));
107 test_pmr_uses_allocator<ERT>( t1, std::move(t2));
108 test_pmr_uses_allocator<ERT>(std::move(t2), t1);
109 test_pmr_uses_allocator<PMR>( t1, std::move(t2));
110 test_pmr_uses_allocator<PMR>(std::move(t2), t1);
111 test_pmr_uses_allocator<PMA>( t1, std::move(t2));
112 test_pmr_uses_allocator<PMA>(std::move(t2), t1);
113 }
114 {
115 void* xptr = nullptr;
116 long y = 4242;
117 std::tuple<int, long const&, void*&> t1(42, y, xptr);
118 int x = 55;
119 double dx = 42.42;
120 const char* s = "hello World";
121 std::tuple<int&, double&&, const char*> t2(x, std::move(dx), s);
122 test_pmr_uses_allocator<ERT>( t1, std::move(t2));
123 test_pmr_uses_allocator<ERT>(std::move(t2), t1);
124 test_pmr_uses_allocator<PMR>( t1, std::move(t2));
125 test_pmr_uses_allocator<PMR>(std::move(t2), t1);
126 test_pmr_uses_allocator<PMA>( t1, std::move(t2));
127 test_pmr_uses_allocator<PMA>(std::move(t2), t1);
128 }
129}