1//===----------------------------------------------------------------------===//
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 P1, class P2, class U1, class U2>
17// void polymorphic_allocator<T>::construct(pair<P1, P2>*, U1&&, U2&&)
18
19#include <experimental/memory_resource>
20#include <type_traits>
21#include <utility>
22#include <tuple>
23#include <cassert>
24#include <cstdlib>
25#include "uses_alloc_types.hpp"
26
27namespace ex = std::experimental::pmr;
28
29
30template <class UA1, class UA2, class TT, class UU>
31bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect,
32            TT&& t, UU&& u)
33{
34    using P = std::pair<UA1, UA2>;
35    TestResource R;
36    ex::memory_resource * M = &R;
37    ex::polymorphic_allocator<P> A(M);
38    P * ptr = (P*)std::malloc(sizeof(P));
39    P * ptr2 = (P*)std::malloc(sizeof(P));
40
41    // UNDER TEST //
42    A.construct(ptr, std::forward<TT>(t), std::forward<UU>(u));
43    A.construct(ptr2, std::piecewise_construct,
44                      std::forward_as_tuple(std::forward<TT>(t)),
45                      std::forward_as_tuple(std::forward<UU>(u)));
46    // ------- //
47
48    bool tres = checkConstruct<TT&&>(ptr->first, TExpect, M) &&
49                checkConstructionEquiv(ptr->first, ptr2->first);
50
51    bool ures = checkConstruct<UU&&>(ptr->second, UExpect, M) &&
52                checkConstructionEquiv(ptr->second, ptr2->second);
53
54    A.destroy(ptr);
55    A.destroy(ptr2);
56    std::free(ptr);
57    std::free(ptr2);
58    return tres && ures;
59}
60
61template <class Alloc, class TT, class UU>
62void test_pmr_uses_allocator(TT&& t, UU&& u)
63{
64    {
65        using T = NotUsesAllocator<Alloc, 1>;
66        using U = NotUsesAllocator<Alloc, 1>;
67        assert((doTest<T, U>(UA_None, UA_None,
68                             std::forward<TT>(t), std::forward<UU>(u))));
69    }
70    {
71        using T = UsesAllocatorV1<Alloc, 1>;
72        using U = UsesAllocatorV2<Alloc, 1>;
73        assert((doTest<T, U>(UA_AllocArg, UA_AllocLast,
74                             std::forward<TT>(t), std::forward<UU>(u))));
75    }
76    {
77        using T = UsesAllocatorV2<Alloc, 1>;
78        using U = UsesAllocatorV3<Alloc, 1>;
79        assert((doTest<T, U>(UA_AllocLast, UA_AllocArg,
80                             std::forward<TT>(t), std::forward<UU>(u))));
81    }
82    {
83        using T = UsesAllocatorV3<Alloc, 1>;
84        using U = NotUsesAllocator<Alloc, 1>;
85        assert((doTest<T, U>(UA_AllocArg, UA_None,
86                             std::forward<TT>(t), std::forward<UU>(u))));
87    }
88}
89
90int main()
91{
92    using ERT = std::experimental::erased_type;
93    using PMR = ex::memory_resource*;
94    using PMA = ex::polymorphic_allocator<char>;
95    {
96        int x = 42;
97        int y = 42;
98        test_pmr_uses_allocator<ERT>(x, std::move(y));
99        test_pmr_uses_allocator<PMR>(x, std::move(y));
100        test_pmr_uses_allocator<PMA>(x, std::move(y));
101    }
102    {
103        int x = 42;
104        const int y = 42;
105        test_pmr_uses_allocator<ERT>(std::move(x), y);
106        test_pmr_uses_allocator<PMR>(std::move(x), y);
107        test_pmr_uses_allocator<PMA>(std::move(x), y);
108    }
109}
110