[lld][WebAssembly] Introduce Ctx::arg
[llvm-project.git] / libcxx / test / std / containers / sequences / deque / deque.modifiers / erase_iter_iter.pass.cpp
blob51e774cb5919480deef5c5159c99de1ffcd77763
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 //===----------------------------------------------------------------------===//
8 //
9 // REQUIRES: long_tests
11 // <deque>
13 // iterator erase(const_iterator f, const_iterator l)
15 #include "asan_testing.h"
16 #include <deque>
17 #include <algorithm>
18 #include <iterator>
19 #include <cassert>
20 #include <cstddef>
22 #include "min_allocator.h"
23 #include "test_macros.h"
25 #ifndef TEST_HAS_NO_EXCEPTIONS
26 struct Throws {
27 Throws() : v_(0) {}
28 Throws(int v) : v_(v) {}
29 Throws(const Throws &rhs) : v_(rhs.v_) { if (sThrows) throw 1; }
30 Throws( Throws &&rhs) : v_(rhs.v_) { if (sThrows) throw 1; }
31 Throws& operator=(const Throws &rhs) { v_ = rhs.v_; return *this; }
32 Throws& operator=( Throws &&rhs) { v_ = rhs.v_; return *this; }
33 int v_;
35 static bool sThrows;
38 bool Throws::sThrows = false;
39 #endif
42 template <class C>
44 make(int size, int start = 0 )
46 const int b = 4096 / sizeof(int);
47 int init = 0;
48 if (start > 0)
50 init = (start+1) / b + ((start+1) % b != 0);
51 init *= b;
52 --init;
54 C c(init, 0);
55 for (int i = 0; i < init-start; ++i)
56 c.pop_back();
57 for (int i = 0; i < size; ++i)
58 c.push_back(i);
59 for (int i = 0; i < start; ++i)
60 c.pop_front();
61 return c;
64 template <class C>
65 void
66 test(int P, C& c1, int size)
68 typedef typename C::iterator I;
69 assert(static_cast<std::size_t>(P + size) <= c1.size());
70 std::size_t c1_osize = c1.size();
71 I i = c1.erase(c1.cbegin() + P, c1.cbegin() + (P + size));
72 assert(i == c1.begin() + P);
73 assert(c1.size() == c1_osize - size);
74 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
75 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
76 i = c1.begin();
77 int j = 0;
78 for (; j < P; ++j, ++i)
79 assert(*i == j);
80 for (j += size; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
81 assert(*i == j);
84 template <class C>
85 void
86 testN(int start, int N)
88 int pstep = std::max(N / std::max(std::min(N, 10), 1), 1);
89 for (int p = 0; p <= N; p += pstep)
91 int sstep = std::max((N - p) / std::max(std::min(N - p, 10), 1), 1);
92 for (int s = 0; s <= N - p; s += sstep)
94 C c1 = make<C>(N, start);
95 test(p, c1, s);
100 int main(int, char**)
103 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
104 const int N = sizeof(rng)/sizeof(rng[0]);
105 for (int i = 0; i < N; ++i)
106 for (int j = 0; j < N; ++j)
107 testN<std::deque<int> >(rng[i], rng[j]);
109 #if TEST_STD_VER >= 11
111 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
112 const int N = sizeof(rng)/sizeof(rng[0]);
113 for (int i = 0; i < N; ++i)
114 for (int j = 0; j < N; ++j)
115 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
117 #endif
118 #ifndef TEST_HAS_NO_EXCEPTIONS
119 // Test for LWG2953:
120 // Throws: Nothing unless an exception is thrown by the assignment operator of T.
121 // (which includes move assignment)
123 Throws arr[] = {1, 2, 3};
124 std::deque<Throws> v(arr, arr+3);
125 Throws::sThrows = true;
126 v.erase(v.begin(), --v.end());
127 assert(v.size() == 1);
128 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(v));
129 v.erase(v.begin(), v.end());
130 assert(v.size() == 0);
131 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(v));
133 #endif
135 return 0;