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