1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef NASTY_CONTAINERS_H
10 #define NASTY_CONTAINERS_H
16 #include <type_traits>
18 #include "test_macros.h"
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
) {}
48 template <class InputIterator
>
49 void assign(InputIterator first
, InputIterator last
) { v_
.assign(first
, last
); }
50 void assign(size_type n
, const value_type
& u
) { v_
.assign(n
, u
); }
51 #if TEST_STD_VER >= 11
52 void assign(std::initializer_list
<value_type
> il
) { v_
.assign(il
); }
55 iterator
begin() TEST_NOEXCEPT
{ return v_
.begin(); }
56 const_iterator
begin() const TEST_NOEXCEPT
{ return v_
.begin(); }
57 iterator
end() TEST_NOEXCEPT
{ return v_
.end(); }
58 const_iterator
end() const TEST_NOEXCEPT
{ return v_
.end(); }
60 reverse_iterator
rbegin() TEST_NOEXCEPT
{ return v_
.rbegin(); }
61 const_reverse_iterator
rbegin() const TEST_NOEXCEPT
{ return v_
.rbegin(); }
62 reverse_iterator
rend() TEST_NOEXCEPT
{ return v_
.rend(); }
63 const_reverse_iterator
rend() const TEST_NOEXCEPT
{ return v_
.rend(); }
65 const_iterator
cbegin() const TEST_NOEXCEPT
{ return v_
.cbegin(); }
66 const_iterator
cend() const TEST_NOEXCEPT
{ return v_
.cend(); }
67 const_reverse_iterator
crbegin() const TEST_NOEXCEPT
{ return v_
.crbegin(); }
68 const_reverse_iterator
crend() const TEST_NOEXCEPT
{ return v_
.crend(); }
70 size_type
size() const TEST_NOEXCEPT
{ return v_
.size(); }
71 size_type
max_size() const TEST_NOEXCEPT
{ return v_
.max_size(); }
72 size_type
capacity() const TEST_NOEXCEPT
{ return v_
.capacity(); }
73 bool empty() const TEST_NOEXCEPT
{ return v_
.empty(); }
74 void reserve(size_type n
) { v_
.reserve(n
); };
75 void shrink_to_fit() TEST_NOEXCEPT
{ v_
.shrink_to_fit(); }
77 reference
operator[](size_type n
) { return v_
[n
]; }
78 const_reference
operator[](size_type n
) const { return v_
[n
]; }
79 reference
at(size_type n
) { return v_
.at(n
); }
80 const_reference
at(size_type n
) const { return v_
.at(n
); }
82 reference
front() { return v_
.front(); }
83 const_reference
front() const { return v_
.front(); }
84 reference
back() { return v_
.back(); }
85 const_reference
back() const { return v_
.back(); }
87 value_type
* data() TEST_NOEXCEPT
{ return v_
.data(); }
88 const value_type
* data() const TEST_NOEXCEPT
{ return v_
.data(); }
90 void push_back(const value_type
& x
) { v_
.push_back(x
); }
91 #if TEST_STD_VER >= 11
92 void push_back(value_type
&& x
) { v_
.push_back(std::forward
<value_type
&&>(x
)); }
93 template <class... Args
>
94 void emplace_back(Args
&&... args
) { v_
.emplace_back(std::forward
<Args
>(args
)...); }
96 void pop_back() { v_
.pop_back(); }
98 #if TEST_STD_VER >= 11
99 template <class... Args
> iterator
emplace(const_iterator pos
, Args
&&... args
)
100 { return v_
.emplace(pos
, std::forward
<Args
>(args
)...); }
103 iterator
insert(const_iterator pos
, const value_type
& x
) { return v_
.insert(pos
, x
); }
104 #if TEST_STD_VER >= 11
105 iterator
insert(const_iterator pos
, value_type
&& x
) { return v_
.insert(pos
, std::forward
<value_type
>(x
)); }
107 iterator
insert(const_iterator pos
, size_type n
, const value_type
& x
) { return v_
.insert(pos
, n
, x
); }
108 template <class InputIterator
>
109 iterator
insert(const_iterator pos
, InputIterator first
, InputIterator last
)
110 { return v_
.insert(pos
, first
, last
); }
112 #if TEST_STD_VER >= 11
113 iterator
insert(const_iterator pos
, std::initializer_list
<value_type
> il
) { return v_
.insert(pos
, il
); }
116 iterator
erase(const_iterator pos
) { return v_
.erase(pos
); }
117 iterator
erase(const_iterator first
, const_iterator last
) { return v_
.erase(first
, last
); }
119 void clear() TEST_NOEXCEPT
{ v_
.clear(); }
121 void resize(size_type sz
) { v_
.resize(sz
); }
122 void resize(size_type sz
, const value_type
& c
) { v_
.resize(sz
, c
); }
124 void swap(nasty_vector
&nv
)
125 #if TEST_STD_VER > 14
126 noexcept(std::is_nothrow_swappable
<nested_container
>::value
)
127 #elif defined(_LIBCPP_VERSION)
128 TEST_NOEXCEPT_COND(std::__is_nothrow_swappable
<nested_container
>::value
)
132 nasty_vector
*operator &() { assert(false); return nullptr; } // nasty
133 const nasty_vector
*operator &() const { assert(false); return nullptr; } // nasty
139 bool operator==(const nasty_vector
<T
>& x
, const nasty_vector
<T
>& y
) { return x
.v_
== y
.v_
; }
142 #if TEST_STD_VER >= 20
145 auto operator<=>(const nasty_vector
<T
>& x
, const nasty_vector
<T
>& y
) { return x
.v_
<=> y
.v_
; }
154 typedef typename
std::list
<T
> nested_container
;
155 typedef typename
nested_container::value_type value_type
;
156 typedef typename
nested_container::reference reference
;
157 typedef typename
nested_container::const_reference const_reference
;
158 typedef typename
nested_container::iterator iterator
;
159 typedef typename
nested_container::const_iterator const_iterator
;
161 typedef typename
nested_container::size_type size_type
;
162 typedef typename
nested_container::difference_type difference_type
;
163 typedef typename
nested_container::pointer pointer
;
164 typedef typename
nested_container::const_pointer const_pointer
;
166 typedef typename
nested_container::reverse_iterator reverse_iterator
;
167 typedef typename
nested_container::const_reverse_iterator const_reverse_iterator
;
169 nasty_list() : l_() {}
170 explicit nasty_list(size_type n
) : l_(n
) {}
171 nasty_list(size_type n
, const value_type
& value
) : l_(n
,value
) {}
172 template <class Iter
>
173 nasty_list(Iter first
, Iter last
) : l_(first
, last
) {}
174 #if TEST_STD_VER >= 11
175 nasty_list(std::initializer_list
<value_type
> il
) : l_(il
) {}
180 #if TEST_STD_VER >= 11
181 nasty_list
& operator=(std::initializer_list
<value_type
> il
) { l_
= il
; return *this; }
183 template <class Iter
>
184 void assign(Iter first
, Iter last
) { l_
.assign(first
, last
); }
185 void assign(size_type n
, const value_type
& t
) { l_
.assign(n
, t
); }
186 #if TEST_STD_VER >= 11
187 void assign(std::initializer_list
<value_type
> il
) { l_
.assign(il
); }
191 iterator
begin() TEST_NOEXCEPT
{ return l_
.begin(); }
192 const_iterator
begin() const TEST_NOEXCEPT
{ return l_
.begin(); }
193 iterator
end() TEST_NOEXCEPT
{ return l_
.end(); }
194 const_iterator
end() const TEST_NOEXCEPT
{ return l_
.end(); }
196 reverse_iterator
rbegin() TEST_NOEXCEPT
{ return l_
.rbegin(); }
197 const_reverse_iterator
rbegin() const TEST_NOEXCEPT
{ return l_
.rbegin(); }
198 reverse_iterator
rend() TEST_NOEXCEPT
{ return l_
.rend(); }
199 const_reverse_iterator
rend() const TEST_NOEXCEPT
{ return l_
.rend(); }
201 const_iterator
cbegin() const TEST_NOEXCEPT
{ return l_
.cbegin(); }
202 const_iterator
cend() const TEST_NOEXCEPT
{ return l_
.cend(); }
203 const_reverse_iterator
crbegin() const TEST_NOEXCEPT
{ return l_
.crbegin(); }
204 const_reverse_iterator
crend() const TEST_NOEXCEPT
{ return l_
.crend(); }
206 reference
front() { return l_
.front(); }
207 const_reference
front() const { return l_
.front(); }
208 reference
back() { return l_
.back(); }
209 const_reference
back() const { return l_
.back(); }
211 size_type
size() const TEST_NOEXCEPT
{ return l_
.size(); }
212 size_type
max_size() const TEST_NOEXCEPT
{ return l_
.max_size(); }
213 bool empty() const TEST_NOEXCEPT
{ return l_
.empty(); }
215 void push_front(const value_type
& x
) { l_
.push_front(x
); }
216 void push_back(const value_type
& x
) { l_
.push_back(x
); }
217 #if TEST_STD_VER >= 11
218 void push_back(value_type
&& x
) { l_
.push_back(std::forward
<value_type
&&>(x
)); }
219 void push_front(value_type
&& x
) { l_
.push_front(std::forward
<value_type
&&>(x
)); }
220 template <class... Args
>
221 void emplace_back(Args
&&... args
) { l_
.emplace_back(std::forward
<Args
>(args
)...); }
222 template <class... Args
>
223 void emplace_front(Args
&&... args
) { l_
.emplace_front(std::forward
<Args
>(args
)...); }
225 void pop_front() { l_
.pop_front(); }
226 void pop_back() { l_
.pop_back(); }
228 #if TEST_STD_VER >= 11
229 template <class... Args
> iterator
emplace(const_iterator pos
, Args
&&... args
)
230 { return l_
.emplace(pos
, std::forward
<Args
>(args
)...); }
233 iterator
insert(const_iterator pos
, const value_type
& x
) { return l_
.insert(pos
, x
); }
234 #if TEST_STD_VER >= 11
235 iterator
insert(const_iterator pos
, value_type
&& x
) { return l_
.insert(pos
, std::forward
<value_type
>(x
)); }
237 iterator
insert(const_iterator pos
, size_type n
, const value_type
& x
) { return l_
.insert(pos
, n
, x
); }
238 template <class InputIterator
>
239 iterator
insert(const_iterator pos
, InputIterator first
, InputIterator last
)
240 { return l_
.insert(pos
, first
, last
); }
242 #if TEST_STD_VER >= 11
243 iterator
insert(const_iterator pos
, std::initializer_list
<value_type
> il
) { return l_
.insert(pos
, il
); }
246 iterator
erase(const_iterator pos
) { return l_
.erase(pos
); }
247 iterator
erase(const_iterator first
, const_iterator last
) { return l_
.erase(first
, last
); }
249 void resize(size_type n
) { l_
.resize(n
); }
250 void resize(size_type n
, const value_type
& c
) { l_
.resize(n
, c
); }
252 void swap(nasty_list
&nl
)
253 #if TEST_STD_VER > 14
254 noexcept(std::is_nothrow_swappable
<nested_container
>::value
)
255 #elif defined(_LIBCPP_VERSION)
256 TEST_NOEXCEPT_COND(std::__is_nothrow_swappable
<nested_container
>::value
)
260 void clear() TEST_NOEXCEPT
{ l_
.clear(); }
262 // void splice(const_iterator position, list& x);
263 // void splice(const_iterator position, list&& x);
264 // void splice(const_iterator position, list& x, const_iterator i);
265 // void splice(const_iterator position, list&& x, const_iterator i);
266 // void splice(const_iterator position, list& x, const_iterator first,
267 // const_iterator last);
268 // void splice(const_iterator position, list&& x, const_iterator first,
269 // const_iterator last);
271 // void remove(const value_type& value);
272 // template <class Pred> void remove_if(Pred pred);
274 // template <class BinaryPredicate>
275 // void unique(BinaryPredicate binary_pred);
276 // void merge(list& x);
277 // void merge(list&& x);
278 // template <class Compare>
279 // void merge(list& x, Compare comp);
280 // template <class Compare>
281 // void merge(list&& x, Compare comp);
283 // template <class Compare>
284 // void sort(Compare comp);
285 // void reverse() noexcept;
287 nasty_list
*operator &() { assert(false); return nullptr; } // nasty
288 const nasty_list
*operator &() const { assert(false); return nullptr; } // nasty
294 bool operator==(const nasty_list
<T
>& x
, const nasty_list
<T
>& y
) { return x
.l_
== y
.l_
; }
296 #if TEST_STD_VER >= 20
299 auto operator<=>(const nasty_list
<T
>& x
, const nasty_list
<T
>& y
) { return x
.l_
<=> y
.l_
; }
303 // Not really a mutex, but can play one in tests
307 nasty_mutex() TEST_NOEXCEPT
{}
310 nasty_mutex
*operator& () { assert(false); return nullptr; }
311 template <typename T
>
312 void operator, (const T
&) { assert(false); }
315 nasty_mutex(const nasty_mutex
&) { assert(false); }
316 nasty_mutex
& operator=(const nasty_mutex
&) { assert(false); return *this; }
320 bool try_lock() TEST_NOEXCEPT
{ return true; }
321 void unlock() TEST_NOEXCEPT
{}
324 void lock_shared() {}
325 bool try_lock_shared() { return true; }
326 void unlock_shared() {}