Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / support / nasty_containers.h
blob91e3af2e805b449856f67390636fb866aedbdab6
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 #ifndef NASTY_CONTAINERS_H
10 #define NASTY_CONTAINERS_H
12 #include <cassert>
13 #include <cstddef>
14 #include <vector>
15 #include <list>
16 #include <type_traits>
18 #include "test_macros.h"
20 template <class T>
21 class nasty_vector
23 public:
24 typedef typename std::vector<T> nested_container;
25 typedef typename nested_container::value_type value_type;
26 typedef typename nested_container::reference reference;
27 typedef typename nested_container::const_reference const_reference;
28 typedef typename nested_container::iterator iterator;
29 typedef typename nested_container::const_iterator const_iterator;
31 typedef typename nested_container::size_type size_type;
32 typedef typename nested_container::difference_type difference_type;
33 typedef typename nested_container::pointer pointer;
34 typedef typename nested_container::const_pointer const_pointer;
36 typedef typename nested_container::reverse_iterator reverse_iterator;
37 typedef typename nested_container::const_reverse_iterator const_reverse_iterator;
39 nasty_vector() : v_() {}
40 explicit nasty_vector(size_type n) : v_(n) {}
41 nasty_vector(size_type n, const value_type& value) : v_(n, value) {}
42 template <class InputIterator> nasty_vector(InputIterator first, InputIterator last) : v_(first, last) {}
43 #if TEST_STD_VER >= 11
44 nasty_vector(std::initializer_list<value_type> il) : v_(il) {}
45 #endif
46 nasty_vector(const nasty_vector&) = default;
47 nasty_vector& operator=(const nasty_vector&) = default;
48 ~nasty_vector() {}
50 template <class InputIterator>
51 void assign(InputIterator first, InputIterator last) { v_.assign(first, last); }
52 void assign(size_type n, const value_type& u) { v_.assign(n, u); }
53 #if TEST_STD_VER >= 11
54 void assign(std::initializer_list<value_type> il) { v_.assign(il); }
55 #endif
57 iterator begin() TEST_NOEXCEPT { return v_.begin(); }
58 const_iterator begin() const TEST_NOEXCEPT { return v_.begin(); }
59 iterator end() TEST_NOEXCEPT { return v_.end(); }
60 const_iterator end() const TEST_NOEXCEPT { return v_.end(); }
62 reverse_iterator rbegin() TEST_NOEXCEPT { return v_.rbegin(); }
63 const_reverse_iterator rbegin() const TEST_NOEXCEPT { return v_.rbegin(); }
64 reverse_iterator rend() TEST_NOEXCEPT { return v_.rend(); }
65 const_reverse_iterator rend() const TEST_NOEXCEPT { return v_.rend(); }
67 const_iterator cbegin() const TEST_NOEXCEPT { return v_.cbegin(); }
68 const_iterator cend() const TEST_NOEXCEPT { return v_.cend(); }
69 const_reverse_iterator crbegin() const TEST_NOEXCEPT { return v_.crbegin(); }
70 const_reverse_iterator crend() const TEST_NOEXCEPT { return v_.crend(); }
72 size_type size() const TEST_NOEXCEPT { return v_.size(); }
73 size_type max_size() const TEST_NOEXCEPT { return v_.max_size(); }
74 size_type capacity() const TEST_NOEXCEPT { return v_.capacity(); }
75 bool empty() const TEST_NOEXCEPT { return v_.empty(); }
76 void reserve(size_type n) { v_.reserve(n); };
77 void shrink_to_fit() TEST_NOEXCEPT { v_.shrink_to_fit(); }
79 reference operator[](size_type n) { return v_[n]; }
80 const_reference operator[](size_type n) const { return v_[n]; }
81 reference at(size_type n) { return v_.at(n); }
82 const_reference at(size_type n) const { return v_.at(n); }
84 reference front() { return v_.front(); }
85 const_reference front() const { return v_.front(); }
86 reference back() { return v_.back(); }
87 const_reference back() const { return v_.back(); }
89 value_type* data() TEST_NOEXCEPT { return v_.data(); }
90 const value_type* data() const TEST_NOEXCEPT { return v_.data(); }
92 void push_back(const value_type& x) { v_.push_back(x); }
93 #if TEST_STD_VER >= 11
94 void push_back(value_type&& x) { v_.push_back(std::forward<value_type&&>(x)); }
95 template <class... Args>
96 void emplace_back(Args&&... args) { v_.emplace_back(std::forward<Args>(args)...); }
97 #endif
98 void pop_back() { v_.pop_back(); }
100 #if TEST_STD_VER >= 11
101 template <class... Args> iterator emplace(const_iterator pos, Args&&... args)
102 { return v_.emplace(pos, std::forward<Args>(args)...); }
103 #endif
105 iterator insert(const_iterator pos, const value_type& x) { return v_.insert(pos, x); }
106 #if TEST_STD_VER >= 11
107 iterator insert(const_iterator pos, value_type&& x) { return v_.insert(pos, std::forward<value_type>(x)); }
108 #endif
109 iterator insert(const_iterator pos, size_type n, const value_type& x) { return v_.insert(pos, n, x); }
110 template <class InputIterator>
111 iterator insert(const_iterator pos, InputIterator first, InputIterator last)
112 { return v_.insert(pos, first, last); }
114 #if TEST_STD_VER >= 11
115 iterator insert(const_iterator pos, std::initializer_list<value_type> il) { return v_.insert(pos, il); }
116 #endif
118 iterator erase(const_iterator pos) { return v_.erase(pos); }
119 iterator erase(const_iterator first, const_iterator last) { return v_.erase(first, last); }
121 void clear() TEST_NOEXCEPT { v_.clear(); }
123 void resize(size_type sz) { v_.resize(sz); }
124 void resize(size_type sz, const value_type& c) { v_.resize(sz, c); }
126 void swap(nasty_vector &nv)
127 #if TEST_STD_VER > 14
128 noexcept(std::is_nothrow_swappable<nested_container>::value)
129 #elif defined(_LIBCPP_VERSION)
130 TEST_NOEXCEPT_COND(std::__is_nothrow_swappable<nested_container>::value)
131 #endif
132 { v_.swap(nv.v_); }
134 nasty_vector *operator &() { assert(false); return nullptr; } // nasty
135 const nasty_vector *operator &() const { assert(false); return nullptr; } // nasty
137 nested_container v_;
140 template <class T>
141 bool operator==(const nasty_vector<T>& x, const nasty_vector<T>& y) { return x.v_ == y.v_; }
144 #if TEST_STD_VER >= 20
146 template <class T>
147 auto operator<=>(const nasty_vector<T>& x, const nasty_vector<T>& y) { return x.v_ <=> y.v_; }
149 #endif
151 template <class T>
152 class nasty_list
154 public:
156 typedef typename std::list<T> nested_container;
157 typedef typename nested_container::value_type value_type;
158 typedef typename nested_container::reference reference;
159 typedef typename nested_container::const_reference const_reference;
160 typedef typename nested_container::iterator iterator;
161 typedef typename nested_container::const_iterator const_iterator;
163 typedef typename nested_container::size_type size_type;
164 typedef typename nested_container::difference_type difference_type;
165 typedef typename nested_container::pointer pointer;
166 typedef typename nested_container::const_pointer const_pointer;
168 typedef typename nested_container::reverse_iterator reverse_iterator;
169 typedef typename nested_container::const_reverse_iterator const_reverse_iterator;
171 nasty_list() : l_() {}
172 explicit nasty_list(size_type n) : l_(n) {}
173 nasty_list(size_type n, const value_type& value) : l_(n,value) {}
174 template <class Iter>
175 nasty_list(Iter first, Iter last) : l_(first, last) {}
176 #if TEST_STD_VER >= 11
177 nasty_list(std::initializer_list<value_type> il) : l_(il) {}
178 #endif
179 nasty_list(const nasty_list&) = default;
180 nasty_list& operator=(const nasty_list&) = default;
181 ~nasty_list() {}
183 #if TEST_STD_VER >= 11
184 nasty_list& operator=(std::initializer_list<value_type> il) { l_ = il; return *this; }
185 #endif
186 template <class Iter>
187 void assign(Iter first, Iter last) { l_.assign(first, last); }
188 void assign(size_type n, const value_type& t) { l_.assign(n, t); }
189 #if TEST_STD_VER >= 11
190 void assign(std::initializer_list<value_type> il) { l_.assign(il); }
191 #endif
194 iterator begin() TEST_NOEXCEPT { return l_.begin(); }
195 const_iterator begin() const TEST_NOEXCEPT { return l_.begin(); }
196 iterator end() TEST_NOEXCEPT { return l_.end(); }
197 const_iterator end() const TEST_NOEXCEPT { return l_.end(); }
199 reverse_iterator rbegin() TEST_NOEXCEPT { return l_.rbegin(); }
200 const_reverse_iterator rbegin() const TEST_NOEXCEPT { return l_.rbegin(); }
201 reverse_iterator rend() TEST_NOEXCEPT { return l_.rend(); }
202 const_reverse_iterator rend() const TEST_NOEXCEPT { return l_.rend(); }
204 const_iterator cbegin() const TEST_NOEXCEPT { return l_.cbegin(); }
205 const_iterator cend() const TEST_NOEXCEPT { return l_.cend(); }
206 const_reverse_iterator crbegin() const TEST_NOEXCEPT { return l_.crbegin(); }
207 const_reverse_iterator crend() const TEST_NOEXCEPT { return l_.crend(); }
209 reference front() { return l_.front(); }
210 const_reference front() const { return l_.front(); }
211 reference back() { return l_.back(); }
212 const_reference back() const { return l_.back(); }
214 size_type size() const TEST_NOEXCEPT { return l_.size(); }
215 size_type max_size() const TEST_NOEXCEPT { return l_.max_size(); }
216 bool empty() const TEST_NOEXCEPT { return l_.empty(); }
218 void push_front(const value_type& x) { l_.push_front(x); }
219 void push_back(const value_type& x) { l_.push_back(x); }
220 #if TEST_STD_VER >= 11
221 void push_back(value_type&& x) { l_.push_back(std::forward<value_type&&>(x)); }
222 void push_front(value_type&& x) { l_.push_front(std::forward<value_type&&>(x)); }
223 template <class... Args>
224 void emplace_back(Args&&... args) { l_.emplace_back(std::forward<Args>(args)...); }
225 template <class... Args>
226 void emplace_front(Args&&... args) { l_.emplace_front(std::forward<Args>(args)...); }
227 #endif
228 void pop_front() { l_.pop_front(); }
229 void pop_back() { l_.pop_back(); }
231 #if TEST_STD_VER >= 11
232 template <class... Args> iterator emplace(const_iterator pos, Args&&... args)
233 { return l_.emplace(pos, std::forward<Args>(args)...); }
234 #endif
236 iterator insert(const_iterator pos, const value_type& x) { return l_.insert(pos, x); }
237 #if TEST_STD_VER >= 11
238 iterator insert(const_iterator pos, value_type&& x) { return l_.insert(pos, std::forward<value_type>(x)); }
239 #endif
240 iterator insert(const_iterator pos, size_type n, const value_type& x) { return l_.insert(pos, n, x); }
241 template <class InputIterator>
242 iterator insert(const_iterator pos, InputIterator first, InputIterator last)
243 { return l_.insert(pos, first, last); }
245 #if TEST_STD_VER >= 11
246 iterator insert(const_iterator pos, std::initializer_list<value_type> il) { return l_.insert(pos, il); }
247 #endif
249 iterator erase(const_iterator pos) { return l_.erase(pos); }
250 iterator erase(const_iterator first, const_iterator last) { return l_.erase(first, last); }
252 void resize(size_type n) { l_.resize(n); }
253 void resize(size_type n, const value_type& c) { l_.resize(n, c); }
255 void swap(nasty_list &nl)
256 #if TEST_STD_VER > 14
257 noexcept(std::is_nothrow_swappable<nested_container>::value)
258 #elif defined(_LIBCPP_VERSION)
259 TEST_NOEXCEPT_COND(std::__is_nothrow_swappable<nested_container>::value)
260 #endif
261 { l_.swap(nl.l_); }
263 void clear() TEST_NOEXCEPT { l_.clear(); }
265 // void splice(const_iterator position, list& x);
266 // void splice(const_iterator position, list&& x);
267 // void splice(const_iterator position, list& x, const_iterator i);
268 // void splice(const_iterator position, list&& x, const_iterator i);
269 // void splice(const_iterator position, list& x, const_iterator first,
270 // const_iterator last);
271 // void splice(const_iterator position, list&& x, const_iterator first,
272 // const_iterator last);
274 // void remove(const value_type& value);
275 // template <class Pred> void remove_if(Pred pred);
276 // void unique();
277 // template <class BinaryPredicate>
278 // void unique(BinaryPredicate binary_pred);
279 // void merge(list& x);
280 // void merge(list&& x);
281 // template <class Compare>
282 // void merge(list& x, Compare comp);
283 // template <class Compare>
284 // void merge(list&& x, Compare comp);
285 // void sort();
286 // template <class Compare>
287 // void sort(Compare comp);
288 // void reverse() noexcept;
290 nasty_list *operator &() { assert(false); return nullptr; } // nasty
291 const nasty_list *operator &() const { assert(false); return nullptr; } // nasty
293 nested_container l_;
296 template <class T>
297 bool operator==(const nasty_list<T>& x, const nasty_list<T>& y) { return x.l_ == y.l_; }
299 #if TEST_STD_VER >= 20
301 template <class T>
302 auto operator<=>(const nasty_list<T>& x, const nasty_list<T>& y) { return x.l_ <=> y.l_; }
304 #endif
306 // Not really a mutex, but can play one in tests
307 class nasty_mutex
309 public:
310 nasty_mutex() TEST_NOEXCEPT {}
311 ~nasty_mutex() {}
313 nasty_mutex *operator& () { assert(false); return nullptr; }
314 template <typename T>
315 void operator, (const T &) { assert(false); }
317 private:
318 nasty_mutex(const nasty_mutex&) { assert(false); }
319 nasty_mutex& operator=(const nasty_mutex&) { assert(false); return *this; }
321 public:
322 void lock() {}
323 bool try_lock() TEST_NOEXCEPT { return true; }
324 void unlock() TEST_NOEXCEPT {}
326 // Shared ownership
327 void lock_shared() {}
328 bool try_lock_shared() { return true; }
329 void unlock_shared() {}
332 #endif