aboutsummaryrefslogtreecommitdiff
path: root/test/support/nasty_containers.hpp
blob: b52263a97b4e25176143b2d0f56f9537fefd79cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef NASTY_CONTAINERS_H
#define NASTY_CONTAINERS_H

#include <cassert>
#include <vector>
#include <list>

#include "test_macros.h"

template <class T>
class nasty_vector
{
public:
    typedef typename std::vector<T>                           nested_container;
    typedef typename nested_container::value_type             value_type;
    typedef typename nested_container::reference              reference;
    typedef typename nested_container::const_reference        const_reference;
    typedef typename nested_container::iterator               iterator;
    typedef typename nested_container::const_iterator         const_iterator;

    typedef typename nested_container::size_type              size_type;
    typedef typename nested_container::difference_type        difference_type;
    typedef typename nested_container::pointer                pointer;
    typedef typename nested_container::const_pointer          const_pointer;

    typedef typename nested_container::reverse_iterator       reverse_iterator;
    typedef typename nested_container::const_reverse_iterator const_reverse_iterator;

    nasty_vector() : v_() {}
    explicit nasty_vector(size_type n) : v_(n) {}
    nasty_vector(size_type n, const value_type& value) : v_(n, value) {}
    template <class InputIterator> nasty_vector(InputIterator first, InputIterator last) : v_(first, last) {}
#if TEST_STD_VER >= 11
    nasty_vector(std::initializer_list<value_type> il) : v_(il) {}
#endif
    ~nasty_vector() {}

    template <class InputIterator>
        void assign(InputIterator first, InputIterator last) { v_.assign(first, last); }
    void assign(size_type n, const value_type& u) { v_.assign(n, u); }
#if TEST_STD_VER >= 11
    void assign(std::initializer_list<value_type> il)  { v_.assign(il); }
#endif

    iterator               begin() TEST_NOEXCEPT         { return v_.begin(); }
    const_iterator         begin()   const TEST_NOEXCEPT { return v_.begin(); }
    iterator               end() TEST_NOEXCEPT           { return v_.end(); }
    const_iterator         end()     const TEST_NOEXCEPT { return v_.end(); }

    reverse_iterator       rbegin() TEST_NOEXCEPT        { return v_.rbegin(); }
    const_reverse_iterator rbegin()  const TEST_NOEXCEPT { return v_.rbegin(); }
    reverse_iterator       rend() TEST_NOEXCEPT          { return v_.rend(); }
    const_reverse_iterator rend()    const TEST_NOEXCEPT { return v_.rend(); }

    const_iterator         cbegin()  const TEST_NOEXCEPT { return v_.cbegin(); }
    const_iterator         cend()    const TEST_NOEXCEPT { return v_.cend(); }
    const_reverse_iterator crbegin() const TEST_NOEXCEPT { return v_.crbegin(); }
    const_reverse_iterator crend()   const TEST_NOEXCEPT { return v_.crend(); }

    size_type size() const TEST_NOEXCEPT      { return v_.size(); }
    size_type max_size() const TEST_NOEXCEPT  { return v_.max_size(); }
    size_type capacity() const TEST_NOEXCEPT  { return v_.capacity(); }
    bool empty() const TEST_NOEXCEPT          { return v_.empty(); }
    void reserve(size_type n)             { v_.reserve(n); };
    void shrink_to_fit() TEST_NOEXCEPT        { v_.shrink_to_fit(); }

    reference       operator[](size_type n)       { return v_[n]; }
    const_reference operator[](size_type n) const { return v_[n]; }
    reference       at(size_type n)               { return v_.at(n); }
    const_reference at(size_type n) const         { return v_.at(n); }

    reference       front()       { return v_.front(); }
    const_reference front() const { return v_.front(); }
    reference       back()        { return v_.back(); }
    const_reference back() const  { return v_.back(); }

    value_type*       data() TEST_NOEXCEPT       { return v_.data(); }
    const value_type* data() const TEST_NOEXCEPT { return v_.data(); }

    void push_back(const value_type& x)     { v_.push_back(x); }
#if TEST_STD_VER >= 11
    void push_back(value_type&& x)          { v_.push_back(std::forward<value_type&&>(x)); }
    template <class... Args>
        void emplace_back(Args&&... args)   { v_.emplace_back(std::forward<Args>(args)...); }
#endif
    void pop_back()                         { v_.pop_back(); }

#if TEST_STD_VER >= 11
    template <class... Args> iterator emplace(const_iterator pos, Args&&... args)
    { return v_.emplace(pos, std::forward<Args>(args)...); }
#endif

    iterator insert(const_iterator pos, const value_type& x) { return v_.insert(pos, x); }
#if TEST_STD_VER >= 11
    iterator insert(const_iterator pos, value_type&& x)      { return v_.insert(pos, std::forward<value_type>(x)); }
#endif
    iterator insert(const_iterator pos, size_type n, const value_type& x) { return v_.insert(pos, n, x); }
    template <class InputIterator>
        iterator insert(const_iterator pos, InputIterator first, InputIterator last)
    { return v_.insert(pos, first, last); }

#if TEST_STD_VER >= 11
    iterator insert(const_iterator pos, std::initializer_list<value_type> il) { return v_.insert(pos, il); }
#endif

    iterator erase(const_iterator pos)                        { return v_.erase(pos); }
    iterator erase(const_iterator first, const_iterator last) { return v_.erase(first, last); }

    void clear() TEST_NOEXCEPT { v_.clear(); }

    void resize(size_type sz)                      { v_.resize(sz); }
    void resize(size_type sz, const value_type& c) { v_.resize(sz, c); }

    void swap(nasty_vector &nv)
#if TEST_STD_VER > 14
    noexcept(std::is_nothrow_swappable<nested_container>::value)
#elif defined(_LIBCPP_VERSION)
    TEST_NOEXCEPT_COND(std::__is_nothrow_swappable<nested_container>::value)
#endif
    { v_.swap(nv.v_); }

    nasty_vector *operator &()             { assert(false); return nullptr; }  // nasty
    const nasty_vector *operator &() const { assert(false); return nullptr; }  // nasty

    nested_container v_;
};

template <class T>
bool operator==(const nasty_vector<T>& x, const nasty_vector<T>& y) { return x.v_ == y.v_; }

template <class T>
class nasty_list
{
public:

    typedef typename std::list<T>                             nested_container;
    typedef typename nested_container::value_type             value_type;
    typedef typename nested_container::reference              reference;
    typedef typename nested_container::const_reference        const_reference;
    typedef typename nested_container::iterator               iterator;
    typedef typename nested_container::const_iterator         const_iterator;

    typedef typename nested_container::size_type              size_type;
    typedef typename nested_container::difference_type        difference_type;
    typedef typename nested_container::pointer                pointer;
    typedef typename nested_container::const_pointer          const_pointer;

    typedef typename nested_container::reverse_iterator       reverse_iterator;
    typedef typename nested_container::const_reverse_iterator const_reverse_iterator;

    nasty_list() : l_() {}
    explicit nasty_list(size_type n)  : l_(n) {}
    nasty_list(size_type n, const value_type& value)  : l_(n,value) {}
    template <class Iter>
        nasty_list(Iter first, Iter last)  : l_(first, last) {}
#if TEST_STD_VER >= 11
    nasty_list(std::initializer_list<value_type> il) : l_(il) {}
#endif

    ~nasty_list() {}

#if TEST_STD_VER >= 11
    nasty_list& operator=(std::initializer_list<value_type> il) { l_ = il; return *this; }
#endif
    template <class Iter>
        void assign(Iter first, Iter last) { l_.assign(first, last); }
    void assign(size_type n, const value_type& t) { l_.assign(n, t); }
#if TEST_STD_VER >= 11
    void assign(std::initializer_list<value_type> il) { l_.assign(il); }
#endif


    iterator               begin() TEST_NOEXCEPT         { return l_.begin(); }
    const_iterator         begin()   const TEST_NOEXCEPT { return l_.begin(); }
    iterator               end() TEST_NOEXCEPT           { return l_.end(); }
    const_iterator         end()     const TEST_NOEXCEPT { return l_.end(); }

    reverse_iterator       rbegin() TEST_NOEXCEPT        { return l_.rbegin(); }
    const_reverse_iterator rbegin()  const TEST_NOEXCEPT { return l_.rbegin(); }
    reverse_iterator       rend() TEST_NOEXCEPT          { return l_.rend(); }
    const_reverse_iterator rend()    const TEST_NOEXCEPT { return l_.rend(); }

    const_iterator         cbegin()  const TEST_NOEXCEPT { return l_.cbegin(); }
    const_iterator         cend()    const TEST_NOEXCEPT { return l_.cend(); }
    const_reverse_iterator crbegin() const TEST_NOEXCEPT { return l_.crbegin(); }
    const_reverse_iterator crend()   const TEST_NOEXCEPT { return l_.crend(); }

    reference       front()       { return l_.front(); }
    const_reference front() const { return l_.front(); }
    reference       back()        { return l_.back(); }
    const_reference back() const  { return l_.back(); }

    size_type size() const TEST_NOEXCEPT      { return l_.size(); }
    size_type max_size() const TEST_NOEXCEPT  { return l_.max_size(); }
    bool empty() const TEST_NOEXCEPT          { return l_.empty(); }

    void push_front(const value_type& x)    { l_.push_front(x); }
    void push_back(const value_type& x)     { l_.push_back(x); }
#if TEST_STD_VER >= 11
    void push_back(value_type&& x)          { l_.push_back(std::forward<value_type&&>(x)); }
    void push_front(value_type&& x)         { l_.push_back(std::forward<value_type&&>(x)); }
    template <class... Args>
        void emplace_back(Args&&... args)   { l_.emplace_back(std::forward<Args>(args)...); }
    template <class... Args>
        void emplace_front(Args&&... args)  { l_.emplace_front(std::forward<Args>(args)...); }
#endif
    void pop_front()                        { l_.pop_front(); }
    void pop_back()                         { l_.pop_back(); }

#if TEST_STD_VER >= 11
    template <class... Args> iterator emplace(const_iterator pos, Args&&... args)
    { return l_.emplace(pos, std::forward<Args>(args)...); }
#endif

    iterator insert(const_iterator pos, const value_type& x) { return l_.insert(pos, x); }
#if TEST_STD_VER >= 11
    iterator insert(const_iterator pos, value_type&& x)      { return l_.insert(pos, std::forward<value_type>(x)); }
#endif
    iterator insert(const_iterator pos, size_type n, const value_type& x) { return l_.insert(pos, n, x); }
    template <class InputIterator>
        iterator insert(const_iterator pos, InputIterator first, InputIterator last)
    { return l_.insert(pos, first, last); }

#if TEST_STD_VER >= 11
    iterator insert(const_iterator pos, std::initializer_list<value_type> il) { return l_.insert(pos, il); }
#endif

    iterator erase(const_iterator pos)                      { return l_.erase(pos); }
    iterator erase(const_iterator pos, const_iterator last) { return l_.erase(pos, last); }

    void resize(size_type)                      { l_.resize(); }
    void resize(size_type, const value_type& c) { l_.resize(c); }

    void swap(nasty_list &nl)
#if TEST_STD_VER > 14
    noexcept(std::is_nothrow_swappable<nested_container>::value)
#elif defined(_LIBCPP_VERSION)
    TEST_NOEXCEPT_COND(std::__is_nothrow_swappable<nested_container>::value)
#endif
    { l_.swap(nl.l_); }

    void clear() TEST_NOEXCEPT { l_.clear(); }

//     void splice(const_iterator position, list& x);
//     void splice(const_iterator position, list&& x);
//     void splice(const_iterator position, list& x, const_iterator i);
//     void splice(const_iterator position, list&& x, const_iterator i);
//     void splice(const_iterator position, list& x, const_iterator first,
//                                                   const_iterator last);
//     void splice(const_iterator position, list&& x, const_iterator first,
//                                                   const_iterator last);
//
//     void remove(const value_type& value);
//     template <class Pred> void remove_if(Pred pred);
//     void unique();
//     template <class BinaryPredicate>
//         void unique(BinaryPredicate binary_pred);
//     void merge(list& x);
//     void merge(list&& x);
//     template <class Compare>
//         void merge(list& x, Compare comp);
//     template <class Compare>
//         void merge(list&& x, Compare comp);
//     void sort();
//     template <class Compare>
//         void sort(Compare comp);
//     void reverse() noexcept;

    nasty_list *operator &()             { assert(false); return nullptr; }  // nasty
    const nasty_list *operator &() const { assert(false); return nullptr; }  // nasty

    nested_container l_;
};

template <class T>
bool operator==(const nasty_list<T>& x, const nasty_list<T>& y) { return x.l_ == y.l_; }

// Not really a mutex, but can play one in tests
class nasty_mutex
{
public:
     nasty_mutex() TEST_NOEXCEPT {}
     ~nasty_mutex() {}

    nasty_mutex *operator& ()   { assert(false); return nullptr; }
    template <typename T>
    void operator, (const T &) { assert(false); }

private:
    nasty_mutex(const nasty_mutex&)            { assert(false); }
    nasty_mutex& operator=(const nasty_mutex&) { assert(false); return *this; }

public:
    void lock()               {}
    bool try_lock() TEST_NOEXCEPT { return true; }
    void unlock() TEST_NOEXCEPT   {}

    // Shared ownership
    void lock_shared()     {}
    bool try_lock_shared() { return true; }
    void unlock_shared()   {}
};

#endif