]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/utilities/allocator.adaptor/allocator.adaptor.members/construct_pair_piecewise.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / utilities / allocator.adaptor / allocator.adaptor.members / construct_pair_piecewise.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 // UNSUPPORTED: c++98, c++03
11
12 // <scoped_allocator>
13
14 // template <class OtherAlloc, class ...InnerAlloc>
15 //   class scoped_allocator_adaptor
16
17 // template <class U1, class U2, class ...Args1, class ...Args2>
18 // void scoped_allocator_adaptor::construct(pair<U1, U2>*,
19 //     piecewise_construct_t, tuple<Args1...>, tuple<Args2...>)
20
21 #include <scoped_allocator>
22 #include <type_traits>
23 #include <utility>
24 #include <tuple>
25 #include <cassert>
26 #include <cstdlib>
27 #include "uses_alloc_types.hpp"
28 #include "controlled_allocators.hpp"
29
30
31 void test_no_inner_alloc()
32 {
33     using VoidAlloc = CountingAllocator<void>;
34     AllocController P;
35     {
36         using T = UsesAllocatorV1<VoidAlloc, 1>;
37         using U = UsesAllocatorV2<VoidAlloc, 1>;
38         using Pair = std::pair<T, U>;
39         int x = 42;
40         const int y = 101;
41         using Alloc = CountingAllocator<Pair>;
42         using SA = std::scoped_allocator_adaptor<Alloc>;
43         static_assert(std::uses_allocator<T, CountingAllocator<T> >::value, "");
44         Pair * ptr = (Pair*)std::malloc(sizeof(Pair));
45         Alloc CA(P);
46         SA A(CA);
47         A.construct(ptr, std::piecewise_construct,
48                     std::forward_as_tuple(x),
49                     std::forward_as_tuple(std::move(y)));
50         assert(checkConstruct<int&>(ptr->first, UA_AllocArg, CA));
51         assert(checkConstruct<int const&&>(ptr->second, UA_AllocLast, CA));
52         assert((P.checkConstruct<std::piecewise_construct_t const&,
53                                  std::tuple<std::allocator_arg_t, SA&, int&>&&,
54                                  std::tuple<int const&&, SA&>&&
55               >(CA, ptr)));
56         A.destroy(ptr);
57         std::free(ptr);
58
59     }
60     P.reset();
61     {
62         using T = UsesAllocatorV3<VoidAlloc, 1>;
63         using U = NotUsesAllocator<VoidAlloc, 1>;
64         using Pair = std::pair<T, U>;
65         int x = 42;
66         const int y = 101;
67         using Alloc = CountingAllocator<Pair>;
68         using SA = std::scoped_allocator_adaptor<Alloc>;
69         static_assert(std::uses_allocator<T, CountingAllocator<T> >::value, "");
70         Pair * ptr = (Pair*)std::malloc(sizeof(Pair));
71         Alloc CA(P);
72         SA A(CA);
73         A.construct(ptr, std::piecewise_construct,
74                     std::forward_as_tuple(std::move(x)),
75                     std::forward_as_tuple(y));
76         assert(checkConstruct<int&&>(ptr->first, UA_AllocArg, CA));
77         assert(checkConstruct<int const&>(ptr->second, UA_None));
78         assert((P.checkConstruct<std::piecewise_construct_t const&,
79                                  std::tuple<std::allocator_arg_t, SA&, int&&>&&,
80                                  std::tuple<int const&>&&
81                    >(CA, ptr)));
82         A.destroy(ptr);
83         std::free(ptr);
84     }
85 }
86
87 void test_with_inner_alloc()
88 {
89     using VoidAlloc1 = CountingAllocator<void, 1>;
90     using VoidAlloc2 = CountingAllocator<void, 2>;
91
92     AllocController POuter;
93     AllocController PInner;
94     {
95         using T = UsesAllocatorV1<VoidAlloc2, 1>;
96         using U = UsesAllocatorV2<VoidAlloc2, 1>;
97         using Pair = std::pair<T, U>;
98         int x = 42;
99         int y = 101;
100         using Outer = CountingAllocator<Pair, 1>;
101         using Inner = CountingAllocator<Pair, 2>;
102         using SA = std::scoped_allocator_adaptor<Outer, Inner>;
103         using SAInner = std::scoped_allocator_adaptor<Inner>;
104         static_assert(!std::uses_allocator<T, Outer>::value, "");
105         static_assert(std::uses_allocator<T, Inner>::value, "");
106         Pair * ptr = (Pair*)std::malloc(sizeof(Pair));
107         Outer O(POuter);
108         Inner I(PInner);
109         SA A(O, I);
110         A.construct(ptr, std::piecewise_construct,
111                     std::forward_as_tuple(x),
112                     std::forward_as_tuple(std::move(y)));
113         assert(checkConstruct<int&>(ptr->first, UA_AllocArg, I));
114         assert(checkConstruct<int &&>(ptr->second, UA_AllocLast));
115         assert((POuter.checkConstruct<std::piecewise_construct_t const&,
116                                  std::tuple<std::allocator_arg_t, SAInner&, int&>&&,
117                                  std::tuple<int &&, SAInner&>&&
118               >(O, ptr)));
119         A.destroy(ptr);
120         std::free(ptr);
121     }
122     PInner.reset();
123     POuter.reset();
124     {
125         using T = UsesAllocatorV3<VoidAlloc2, 1>;
126         using U = NotUsesAllocator<VoidAlloc2, 1>;
127         using Pair = std::pair<T, U>;
128         int x = 42;
129         const int y = 101;
130         using Outer = CountingAllocator<Pair, 1>;
131         using Inner = CountingAllocator<Pair, 2>;
132         using SA = std::scoped_allocator_adaptor<Outer, Inner>;
133         using SAInner = std::scoped_allocator_adaptor<Inner>;
134         static_assert(!std::uses_allocator<T, Outer>::value, "");
135         static_assert(std::uses_allocator<T, Inner>::value, "");
136         Pair * ptr = (Pair*)std::malloc(sizeof(Pair));
137         Outer O(POuter);
138         Inner I(PInner);
139         SA A(O, I);
140         A.construct(ptr, std::piecewise_construct,
141                     std::forward_as_tuple(std::move(x)),
142                     std::forward_as_tuple(std::move(y)));
143         assert(checkConstruct<int&&>(ptr->first, UA_AllocArg, I));
144         assert(checkConstruct<int const&&>(ptr->second, UA_None));
145         assert((POuter.checkConstruct<std::piecewise_construct_t const&,
146                                  std::tuple<std::allocator_arg_t, SAInner&, int&&>&&,
147                                  std::tuple<int const&&>&&
148               >(O, ptr)));
149         A.destroy(ptr);
150         std::free(ptr);
151     }
152 }
153 int main() {
154     test_no_inner_alloc();
155     test_with_inner_alloc();
156 }