1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // REQUIRES: long_tests
13 // iterator erase(const_iterator f, const_iterator l)
15 #include "asan_testing.h"
22 #include "min_allocator.h"
23 #include "test_macros.h"
25 #ifndef TEST_HAS_NO_EXCEPTIONS
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; }
38 bool Throws::sThrows
= false;
44 make(int size
, int start
= 0 )
46 const int b
= 4096 / sizeof(int);
50 init
= (start
+1) / b
+ ((start
+1) % b
!= 0);
55 for (int i
= 0; i
< init
-start
; ++i
)
57 for (int i
= 0; i
< size
; ++i
)
59 for (int i
= 0; i
< start
; ++i
)
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
));
78 for (; j
< P
; ++j
, ++i
)
80 for (j
+= size
; static_cast<std::size_t>(j
) < c1_osize
; ++j
, ++i
)
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
);
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
]);
118 #ifndef TEST_HAS_NO_EXCEPTIONS
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
));