]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_const_lvalue_pair.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / experimental / memory / memory.polymorphic.allocator.class / memory.polymorphic.allocator.mem / construct_pair_const_lvalue_pair.pass.cpp
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 // REQUIRES: c++experimental
11 // UNSUPPORTED: c++98, c++03
12
13 // <experimental/memory_resource>
14
15 // template <class T> class polymorphic_allocator
16
17 // template <class P1, class P2, class U1, class U2>
18 // void polymorphic_allocator<T>::construct(pair<P1, P2>*, pair<U1, U2> const&)
19
20 #include <experimental/memory_resource>
21 #include <type_traits>
22 #include <utility>
23 #include <tuple>
24 #include <cassert>
25 #include <cstdlib>
26
27 #include "test_macros.h"
28 #include "test_memory_resource.hpp"
29 #include "uses_alloc_types.hpp"
30 #include "controlled_allocators.hpp"
31 #include "test_allocator.h"
32
33 namespace ex = std::experimental::pmr;
34
35
36 template <class UA1, class UA2, class TT, class UU>
37 bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect,
38             std::pair<TT, UU> const& p)
39 {
40     using P = std::pair<UA1, UA2>;
41     TestResource R;
42     ex::memory_resource * M = &R;
43     ex::polymorphic_allocator<P> A(M);
44     P * ptr = (P*)std::malloc(sizeof(P));
45     P * ptr2 =  (P*)std::malloc(sizeof(P));
46
47     // UNDER TEST //
48     A.construct(ptr, p);
49
50     A.construct(ptr2, std::piecewise_construct,
51                 std::forward_as_tuple(p.first),
52                 std::forward_as_tuple(p.second));
53     // ------- //
54
55     bool tres = checkConstruct<decltype((p.first))>(ptr->first, TExpect, M) &&
56                 checkConstructionEquiv(ptr->first, ptr2->first);
57
58     bool ures = checkConstruct<decltype((p.second))>(ptr->second, UExpect, M) &&
59                 checkConstructionEquiv(ptr->second, ptr2->second);
60
61     A.destroy(ptr);
62     std::free(ptr);
63     A.destroy(ptr2);
64     std::free(ptr2);
65     return tres && ures;
66
67 }
68
69 template <class Alloc, class TT, class UU>
70 void test_pmr_uses_allocator(std::pair<TT, UU> const& p)
71 {
72     {
73         using T = NotUsesAllocator<Alloc, 1>;
74         using U = NotUsesAllocator<Alloc, 1>;
75         assert((doTest<T, U>(UA_None, UA_None, p)));
76     }
77     {
78         using T = UsesAllocatorV1<Alloc, 1>;
79         using U = UsesAllocatorV2<Alloc, 1>;
80         assert((doTest<T, U>(UA_AllocArg, UA_AllocLast, p)));
81     }
82     {
83         using T = UsesAllocatorV2<Alloc, 1>;
84         using U = UsesAllocatorV3<Alloc, 1>;
85         assert((doTest<T, U>(UA_AllocLast, UA_AllocArg, p)));
86     }
87     {
88         using T = UsesAllocatorV3<Alloc, 1>;
89         using U = NotUsesAllocator<Alloc, 1>;
90         assert((doTest<T, U>(UA_AllocArg, UA_None, p)));
91     }
92 }
93 template <class Tp>
94 struct Print;
95
96 int main()
97 {
98     using ERT = std::experimental::erased_type;
99     using PMR = ex::memory_resource*;
100     using PMA = ex::polymorphic_allocator<char>;
101     {
102         int x = 42;
103         int y = 42;
104         const std::pair<int, int&> p(x, y);
105         test_pmr_uses_allocator<ERT>(p);
106         test_pmr_uses_allocator<PMR>(p);
107         test_pmr_uses_allocator<PMA>(p);
108     }
109     {
110         int x = 42;
111         int y = 42;
112         const std::pair<int&, int&&> p(x, std::move(y));
113         test_pmr_uses_allocator<ERT>(p);
114         test_pmr_uses_allocator<PMR>(p);
115         test_pmr_uses_allocator<PMA>(p);
116     }
117 }