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
10
11// <deque>
12
13// iterator erase(const_iterator f, const_iterator l)
14
15#include "asan_testing.h"
16#include <deque>
17#include <algorithm>
18#include <iterator>
19#include <cassert>
20#include <cstddef>
21
22#include "min_allocator.h"
23#include "test_macros.h"
24
25#ifndef TEST_HAS_NO_EXCEPTIONS
26struct 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_;
34
35 static bool sThrows;
36 };
37
38bool Throws::sThrows = false;
39#endif
40
41
42template <class C>
43C
44make(int size, int start = 0 )
45{
46 const int b = 4096 / sizeof(int);
47 int init = 0;
48 if (start > 0)
49 {
50 init = (start+1) / b + ((start+1) % b != 0);
51 init *= b;
52 --init;
53 }
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;
62}
63
64template <class C>
65void
66test(int P, C& c1, int size)
67{
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);
82}
83
84template <class C>
85void
86testN(int start, int N)
87{
88 int pstep = std::max(a: N / std::max(a: std::min(a: N, b: 10), b: 1), b: 1);
89 for (int p = 0; p <= N; p += pstep)
90 {
91 int sstep = std::max(a: (N - p) / std::max(a: std::min(a: N - p, b: 10), b: 1), b: 1);
92 for (int s = 0; s <= N - p; s += sstep)
93 {
94 C c1 = make<C>(N, start);
95 test(p, c1, s);
96 }
97 }
98}
99
100int main(int, char**)
101{
102 {
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> >(start: rng[i], N: rng[j]);
108 }
109#if TEST_STD_VER >= 11
110 {
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]);
116 }
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)
122 {
123 Throws arr[] = {1, 2, 3};
124 std::deque<Throws> v(arr, arr+3);
125 Throws::sThrows = true;
126 v.erase(first: v.begin(), last: --v.end());
127 assert(v.size() == 1);
128 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(v));
129 v.erase(first: v.begin(), last: v.end());
130 assert(v.size() == 0);
131 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(v));
132 }
133#endif
134
135 return 0;
136}
137

source code of libcxx/test/std/containers/sequences/deque/deque.modifiers/erase_iter_iter.pass.cpp