[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / std / containers / sequences / deque / deque.modifiers / erase_iter.pass.cpp
blobd8db68368b00af2894b96ca6b1344d37401539b4
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 // <deque>
11 // iterator erase(const_iterator p)
13 #include <deque>
14 #include <algorithm>
15 #include <iterator>
16 #include <cassert>
17 #include <cstddef>
19 #include "min_allocator.h"
20 #include "test_macros.h"
22 #ifndef TEST_HAS_NO_EXCEPTIONS
23 struct Throws {
24 Throws() : v_(0) {}
25 Throws(int v) : v_(v) {}
26 Throws(const Throws &rhs) : v_(rhs.v_) { if (sThrows) throw 1; }
27 Throws( Throws &&rhs) : v_(rhs.v_) { if (sThrows) throw 1; }
28 Throws& operator=(const Throws &rhs) { v_ = rhs.v_; return *this; }
29 Throws& operator=( Throws &&rhs) { v_ = rhs.v_; return *this; }
30 int v_;
32 static bool sThrows;
35 bool Throws::sThrows = false;
36 #endif
38 template <class C>
40 make(int size, int start = 0 )
42 const int b = 4096 / sizeof(int);
43 int init = 0;
44 if (start > 0)
46 init = (start+1) / b + ((start+1) % b != 0);
47 init *= b;
48 --init;
50 C c(init, 0);
51 for (int i = 0; i < init-start; ++i)
52 c.pop_back();
53 for (int i = 0; i < size; ++i)
54 c.push_back(i);
55 for (int i = 0; i < start; ++i)
56 c.pop_front();
57 return c;
60 template <class C>
61 void
62 test(int P, C& c1)
64 typedef typename C::iterator I;
65 assert(static_cast<std::size_t>(P) < c1.size());
66 std::size_t c1_osize = c1.size();
67 I i = c1.erase(c1.cbegin() + P);
68 assert(i == c1.begin() + P);
69 assert(c1.size() == c1_osize - 1);
70 assert(static_cast<std::size_t>(distance(c1.begin(), c1.end())) == c1.size());
71 i = c1.begin();
72 int j = 0;
73 for (; j < P; ++j, ++i)
74 assert(*i == j);
75 for (++j; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
76 assert(*i == j);
79 template <class C>
80 void
81 testN(int start, int N)
83 int pstep = std::max(N / std::max(std::min(N, 10), 1), 1);
84 for (int p = 0; p < N; p += pstep)
86 C c1 = make<C>(N, start);
87 test(p, c1);
91 int main(int, char**)
94 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
95 const int N = sizeof(rng)/sizeof(rng[0]);
96 for (int i = 0; i < N; ++i)
97 for (int j = 0; j < N; ++j)
98 testN<std::deque<int> >(rng[i], rng[j]);
100 #if TEST_STD_VER >= 11
102 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
103 const int N = sizeof(rng)/sizeof(rng[0]);
104 for (int i = 0; i < N; ++i)
105 for (int j = 0; j < N; ++j)
106 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
108 #endif
110 #ifndef TEST_HAS_NO_EXCEPTIONS
111 // Test for LWG2953:
112 // Throws: Nothing unless an exception is thrown by the assignment operator of T.
113 // (which includes move assignment)
115 Throws arr[] = {1, 2, 3};
116 std::deque<Throws> v(arr, arr+3);
117 Throws::sThrows = true;
118 v.erase(v.begin());
119 v.erase(--v.end());
120 v.erase(v.begin());
121 assert(v.size() == 0);
123 #endif
125 return 0;