Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / containers / container.adaptors / stack / stack.cons / deduct.pass.cpp
blobbc0c594a3c6416da078852a2a7e1474f24d8eeaf
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // <stack>
10 // UNSUPPORTED: c++03, c++11, c++14
12 // template<class Container>
13 // stack(Container) -> stack<typename Container::value_type, Container>;
15 // template<class Container, class Allocator>
16 // stack(Container, Allocator) -> stack<typename Container::value_type, Container>;
18 // template<ranges::input_range R>
19 // stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>; // since C++23
21 // template<ranges::input_range R, class Allocator>
22 // stack(from_range_t, R&&, Allocator)
23 // -> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23
25 #include <array>
26 #include <stack>
27 #include <deque>
28 #include <vector>
29 #include <list>
30 #include <iterator>
31 #include <cassert>
32 #include <cstddef>
33 #include <climits> // INT_MAX
35 #include "deduction_guides_sfinae_checks.h"
36 #include "test_macros.h"
37 #include "test_iterators.h"
38 #include "test_allocator.h"
40 struct A {};
42 int main(int, char**)
45 // Test the explicit deduction guides
47 std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
48 std::stack stk(v);
50 static_assert(std::is_same_v<decltype(stk), std::stack<int, std::vector<int>>>, "");
51 assert(stk.size() == v.size());
52 assert(stk.top() == v.back());
56 std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
57 std::stack stk(l, test_allocator<long>(0,2)); // different allocator
58 static_assert(std::is_same_v<decltype(stk)::container_type, std::list<long, test_allocator<long>>>, "");
59 static_assert(std::is_same_v<decltype(stk)::value_type, long>, "");
60 assert(stk.size() == 10);
61 assert(stk.top() == 19);
62 // I'd like to assert that we've gotten the right allocator in the stack, but
63 // I don't know how to get at the underlying container.
66 // Test the implicit deduction guides
69 // We don't expect this one to work - no way to implicitly get value_type
70 // std::stack stk(std::allocator<int>()); // stack (allocator &)
74 std::stack<A> source;
75 std::stack stk(source); // stack(stack &)
76 static_assert(std::is_same_v<decltype(stk)::value_type, A>, "");
77 static_assert(std::is_same_v<decltype(stk)::container_type, std::deque<A>>, "");
78 assert(stk.size() == 0);
82 typedef short T;
83 typedef test_allocator<T> Alloc;
84 typedef std::list<T, Alloc> Cont;
85 typedef test_allocator<int> ConvertibleToAlloc;
86 static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
87 !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
90 Cont cont;
91 std::stack stk(cont, Alloc(2));
92 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
96 Cont cont;
97 std::stack stk(cont, ConvertibleToAlloc(2));
98 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
102 Cont cont;
103 std::stack stk(std::move(cont), Alloc(2));
104 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
108 Cont cont;
109 std::stack stk(std::move(cont), ConvertibleToAlloc(2));
110 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
115 typedef short T;
116 typedef test_allocator<T> Alloc;
117 typedef std::list<T, Alloc> Cont;
118 typedef test_allocator<int> ConvertibleToAlloc;
119 static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
120 !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
123 std::stack<T, Cont> source;
124 std::stack stk(source, Alloc(2));
125 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
129 std::stack<T, Cont> source;
130 std::stack stk(source, ConvertibleToAlloc(2));
131 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
135 std::stack<T, Cont> source;
136 std::stack stk(std::move(source), Alloc(2));
137 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
141 std::stack<T, Cont> source;
142 std::stack stk(std::move(source), ConvertibleToAlloc(2));
143 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
147 #if TEST_STD_VER >= 23
149 typedef short T;
150 typedef test_allocator<T> Alloc;
151 std::list<T> a;
153 std::stack s(a.begin(), a.end());
154 static_assert(std::is_same_v<decltype(s), std::stack<T>>);
157 std::stack s(a.begin(), a.end(), Alloc());
158 static_assert(std::is_same_v<decltype(s), std::stack<T, std::deque<T, Alloc>>>);
164 std::stack c(std::from_range, std::array<int, 0>());
165 static_assert(std::is_same_v<decltype(c), std::stack<int>>);
169 using Alloc = test_allocator<int>;
170 std::stack c(std::from_range, std::array<int, 0>(), Alloc());
171 static_assert(std::is_same_v<decltype(c), std::stack<int, std::deque<int, Alloc>>>);
174 #endif
176 ContainerAdaptorDeductionGuidesSfinaeAway<std::stack, std::stack<int>>();
178 return 0;