1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03
14 // template <class OtherAlloc, class ...InnerAlloc>
15 // class scoped_allocator_adaptor
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...>)
21 #include <scoped_allocator>
22 #include <type_traits>
27 #include "uses_alloc_types.hpp"
28 #include "controlled_allocators.hpp"
31 void test_no_inner_alloc()
33 using VoidAlloc = CountingAllocator<void>;
36 using T = UsesAllocatorV1<VoidAlloc, 1>;
37 using U = UsesAllocatorV2<VoidAlloc, 1>;
38 using Pair = std::pair<T, U>;
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));
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&>&&
62 using T = UsesAllocatorV3<VoidAlloc, 1>;
63 using U = NotUsesAllocator<VoidAlloc, 1>;
64 using Pair = std::pair<T, U>;
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));
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&>&&
87 void test_with_inner_alloc()
89 using VoidAlloc1 = CountingAllocator<void, 1>;
90 using VoidAlloc2 = CountingAllocator<void, 2>;
92 AllocController POuter;
93 AllocController PInner;
95 using T = UsesAllocatorV1<VoidAlloc2, 1>;
96 using U = UsesAllocatorV2<VoidAlloc2, 1>;
97 using Pair = std::pair<T, U>;
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));
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&>&&
125 using T = UsesAllocatorV3<VoidAlloc2, 1>;
126 using U = NotUsesAllocator<VoidAlloc2, 1>;
127 using Pair = std::pair<T, U>;
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));
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&&>&&
154 test_no_inner_alloc();
155 test_with_inner_alloc();