aboutsummaryrefslogtreecommitdiff
path: root/test/libcxx/debug/containers/db_sequence_container_iterators.pass.cpp
blob: 57d16c2723f75b875a17332ab4c5ddd9e83f7eec (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//===----------------------------------------------------------------------===//
//
//                     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.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11, c++14
// UNSUPPORTED: libcpp-no-exceptions, libcpp-no-if-constexpr
// MODULES_DEFINES: _LIBCPP_DEBUG=1
// MODULES_DEFINES: _LIBCPP_DEBUG_USE_EXCEPTIONS

// Can't test the system lib because this test enables debug mode
// UNSUPPORTED: with_system_cxx_lib

// test container debugging

#define _LIBCPP_DEBUG 1
#define _LIBCPP_DEBUG_USE_EXCEPTIONS
#include <forward_list>
#include <list>
#include <vector>
#include <deque>
#include "debug_mode_helper.h"

using namespace IteratorDebugChecks;

template <class Container, ContainerType CT>
struct SequenceContainerChecks : BasicContainerChecks<Container, CT> {
  using Base = BasicContainerChecks<Container, CT>;
  using value_type = typename Container::value_type;
  using allocator_type = typename Container::allocator_type;
  using iterator = typename Container::iterator;
  using const_iterator = typename Container::const_iterator;

  using Base::makeContainer;
  using Base::makeValueType;
public:
  static void run() {
    Base::run();
    try {
      FrontOnEmptyContainer();

      if constexpr (CT != CT_ForwardList) {
        AssignInvalidates();
        BackOnEmptyContainer();
        InsertIterValue();
        InsertIterSizeValue();
        InsertIterIterIter();
        EmplaceIterValue();
        EraseIterIter();
      } else {
        SpliceFirstElemAfter();
      }
      if constexpr (CT == CT_Vector || CT == CT_Deque || CT == CT_List) {
        PopBack();
      }
      if constexpr (CT == CT_List || CT == CT_Deque) {
        PopFront(); // FIXME: Run with forward list as well
      }
      if constexpr (CT == CT_List || CT == CT_ForwardList) {
        RemoveFirstElem();
      }
      if constexpr (CT == CT_List) {
        SpliceFirstElem();
      }
    } catch (...) {
      assert(false && "uncaught debug exception");
    }
  }

private:
  static void RemoveFirstElem() {
    // See llvm.org/PR35564
    CHECKPOINT("remove(<first-elem>)");
    {
      Container C = makeContainer(1);
      auto FirstVal = *(C.begin());
      C.remove(FirstVal);
      assert(C.empty());
    }
    {
      Container C = {1, 1, 1, 1};
      auto FirstVal = *(C.begin());
      C.remove(FirstVal);
      assert(C.empty());
    }
  }

  static void SpliceFirstElem() {
    // See llvm.org/PR35564
    CHECKPOINT("splice(<first-elem>)");
    {
      Container C = makeContainer(1);
      Container C2;
      C2.splice(C2.end(), C, C.begin(), ++C.begin());
    }
    {
      Container C = makeContainer(1);
      Container C2;
      C2.splice(C2.end(), C, C.begin());
    }
  }


  static void SpliceFirstElemAfter() {
    // See llvm.org/PR35564
    CHECKPOINT("splice(<first-elem>)");
    {
      Container C = makeContainer(1);
      Container C2;
      C2.splice_after(C2.begin(), C, C.begin(), ++C.begin());
    }
    {
      Container C = makeContainer(1);
      Container C2;
      C2.splice_after(C2.begin(), C, C.begin());
    }
  }

  static void AssignInvalidates() {
    CHECKPOINT("assign(Size, Value)");
    Container C(allocator_type{});
    iterator it1, it2, it3;
    auto reset = [&]() {
      C = makeContainer(3);
      it1 = C.begin();
      it2 = ++C.begin();
      it3 = C.end();
    };
    auto check = [&]() {
      CHECK_DEBUG_THROWS( C.erase(it1) );
      CHECK_DEBUG_THROWS( C.erase(it2) );
      CHECK_DEBUG_THROWS( C.erase(it3, C.end()) );
    };
    reset();
    C.assign(2, makeValueType(4));
    check();
    reset();
    CHECKPOINT("assign(Iter, Iter)");
    std::vector<value_type> V = {
        makeValueType(1),
        makeValueType(2),
        makeValueType(3)
    };
    C.assign(V.begin(), V.end());
    check();
    reset();
    CHECKPOINT("assign(initializer_list)");
    C.assign({makeValueType(1), makeValueType(2), makeValueType(3)});
    check();
  }

  static void BackOnEmptyContainer() {
    CHECKPOINT("testing back on empty");
    Container C = makeContainer(1);
    Container const& CC = C;
    (void)C.back();
    (void)CC.back();
    C.clear();
    CHECK_DEBUG_THROWS( C.back() );
    CHECK_DEBUG_THROWS( CC.back() );
  }

  static void FrontOnEmptyContainer() {
    CHECKPOINT("testing front on empty");
    Container C = makeContainer(1);
    Container const& CC = C;
    (void)C.front();
    (void)CC.front();
    C.clear();
    CHECK_DEBUG_THROWS( C.front() );
    CHECK_DEBUG_THROWS( CC.front() );
  }

  static void EraseIterIter() {
    CHECKPOINT("testing erase iter iter invalidation");
    Container C1 = makeContainer(3);
    iterator it1 = C1.begin();
    iterator it1_next = ++C1.begin();
    iterator it1_after_next = ++C1.begin();
    ++it1_after_next;
    iterator it1_back = --C1.end();
    assert(it1_next != it1_back);
    if (CT == CT_Vector) {
      CHECK_DEBUG_THROWS( C1.erase(it1_next, it1) ); // bad range
    }
    C1.erase(it1, it1_after_next);
    CHECK_DEBUG_THROWS( C1.erase(it1) );
    CHECK_DEBUG_THROWS( C1.erase(it1_next) );
    if (CT == CT_List) {
      C1.erase(it1_back);
    } else {
      CHECK_DEBUG_THROWS( C1.erase(it1_back) );
    }
  }

  static void PopBack() {
    CHECKPOINT("testing  pop_back() invalidation");
    Container C1 = makeContainer(2);
    iterator it1 = C1.end();
    --it1;
    C1.pop_back();
    CHECK_DEBUG_THROWS( C1.erase(it1) );
    C1.erase(C1.begin());
    assert(C1.size() == 0);
    CHECK_DEBUG_THROWS( C1.pop_back() );
  }

  static void PopFront() {
    CHECKPOINT("testing pop_front() invalidation");
    Container C1 = makeContainer(2);
    iterator it1 = C1.begin();
    C1.pop_front();
    CHECK_DEBUG_THROWS( C1.erase(it1) );
    C1.erase(C1.begin());
    assert(C1.size() == 0);
    CHECK_DEBUG_THROWS( C1.pop_front() );
  }

  static void InsertIterValue() {
    CHECKPOINT("testing insert(iter, value)");
    Container C1 = makeContainer(2);
    iterator it1 = C1.begin();
    iterator it1_next = it1;
    ++it1_next;
    Container C2 = C1;
    const value_type value = makeValueType(3);
    value_type rvalue = makeValueType(3);
    CHECK_DEBUG_THROWS( C2.insert(it1, value) ); // wrong container
    CHECK_DEBUG_THROWS( C2.insert(it1, std::move(rvalue)) ); // wrong container
    C1.insert(it1_next, value);
    if  (CT == CT_List) {
      C1.insert(it1_next, value);
      C1.insert(it1, value);
      C1.insert(it1_next, std::move(rvalue));
      C1.insert(it1, std::move(rvalue));
    } else {
      CHECK_DEBUG_THROWS( C1.insert(it1_next, value) ); // invalidated iterator
      CHECK_DEBUG_THROWS( C1.insert(it1, value) ); // invalidated iterator
      CHECK_DEBUG_THROWS( C1.insert(it1_next, std::move(rvalue)) ); // invalidated iterator
      CHECK_DEBUG_THROWS( C1.insert(it1, std::move(rvalue)) ); // invalidated iterator
    }
  }

  static void EmplaceIterValue() {
    CHECKPOINT("testing emplace(iter, value)");
    Container C1 = makeContainer(2);
    iterator it1 = C1.begin();
    iterator it1_next = it1;
    ++it1_next;
    Container C2 = C1;
    const value_type value = makeValueType(3);
    CHECK_DEBUG_THROWS( C2.emplace(it1, value) ); // wrong container
    CHECK_DEBUG_THROWS( C2.emplace(it1, makeValueType(4)) ); // wrong container
    C1.emplace(it1_next, value);
    if  (CT == CT_List) {
      C1.emplace(it1_next, value);
      C1.emplace(it1, value);
    } else {
      CHECK_DEBUG_THROWS( C1.emplace(it1_next, value) ); // invalidated iterator
      CHECK_DEBUG_THROWS( C1.emplace(it1, value) ); // invalidated iterator
    }
  }

  static void InsertIterSizeValue() {
    CHECKPOINT("testing insert(iter, size, value)");
    Container C1 = makeContainer(2);
    iterator it1 = C1.begin();
    iterator it1_next = it1;
    ++it1_next;
    Container C2 = C1;
    const value_type value = makeValueType(3);
    CHECK_DEBUG_THROWS( C2.insert(it1, 1, value) ); // wrong container
    C1.insert(it1_next, 2, value);
    if  (CT == CT_List) {
      C1.insert(it1_next, 3, value);
      C1.insert(it1, 1, value);
    } else {
      CHECK_DEBUG_THROWS( C1.insert(it1_next, 1, value) ); // invalidated iterator
      CHECK_DEBUG_THROWS( C1.insert(it1, 1, value) ); // invalidated iterator
    }
  }

  static void InsertIterIterIter() {
    CHECKPOINT("testing insert(iter, iter, iter)");
    Container C1 = makeContainer(2);
    iterator it1 = C1.begin();
    iterator it1_next = it1;
    ++it1_next;
    Container C2 = C1;
    std::vector<value_type> V = {
        makeValueType(1),
        makeValueType(2),
        makeValueType(3)
    };
    CHECK_DEBUG_THROWS( C2.insert(it1, V.begin(), V.end()) ); // wrong container
    C1.insert(it1_next, V.begin(), V.end());
    if  (CT == CT_List) {
      C1.insert(it1_next, V.begin(), V.end());
      C1.insert(it1, V.begin(), V.end());
    } else {
      CHECK_DEBUG_THROWS( C1.insert(it1_next, V.begin(), V.end()) ); // invalidated iterator
      CHECK_DEBUG_THROWS( C1.insert(it1, V.begin(), V.end()) ); // invalidated iterator
    }
  }
};

int main()
{
  using Alloc = test_allocator<int>;
  {
    SequenceContainerChecks<std::list<int, Alloc>, CT_List>::run();
    SequenceContainerChecks<std::vector<int, Alloc>, CT_Vector>::run();
  }
  // FIXME these containers don't support iterator debugging
  if ((false)) {
    SequenceContainerChecks<
        std::vector<bool, test_allocator<bool>>, CT_VectorBool>::run();
    SequenceContainerChecks<
        std::forward_list<int, Alloc>, CT_ForwardList>::run();
    SequenceContainerChecks<
        std::deque<int, Alloc>, CT_Deque>::run();
  }
}