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 //===----------------------------------------------------------------------===//
11 // iterator erase(const_iterator position);
17 #include "test_macros.h"
18 #include "min_allocator.h"
19 #include "asan_testing.h"
21 #ifndef TEST_HAS_NO_EXCEPTIONS
24 Throws(int v
) : v_(v
) {}
25 Throws(const Throws
&rhs
) : v_(rhs
.v_
) { if (sThrows
) throw 1; }
26 Throws( Throws
&&rhs
) : v_(rhs
.v_
) { if (sThrows
) throw 1; }
27 Throws
& operator=(const Throws
&rhs
) { v_
= rhs
.v_
; return *this; }
28 Throws
& operator=( Throws
&&rhs
) { v_
= rhs
.v_
; return *this; }
33 bool Throws::sThrows
= false;
39 int a1
[] = {1, 2, 3, 4, 5};
40 std::vector
<int> l1(a1
, a1
+5);
42 assert(is_contiguous_container_asan_correct(l1
));
43 assert(l1
== std::vector
<int>(a1
+1, a1
+5));
46 int a1
[] = {1, 2, 3, 4, 5};
47 int e1
[] = {1, 3, 4, 5};
48 std::vector
<int> l1(a1
, a1
+5);
49 l1
.erase(l1
.begin() + 1);
50 assert(is_contiguous_container_asan_correct(l1
));
51 assert(l1
== std::vector
<int>(e1
, e1
+4));
55 std::vector
<int> l1(a1
, a1
+3);
56 std::vector
<int>::const_iterator i
= l1
.begin();
57 assert(is_contiguous_container_asan_correct(l1
));
59 std::vector
<int>::iterator j
= l1
.erase(i
);
60 assert(l1
.size() == 2);
61 assert(distance(l1
.begin(), l1
.end()) == 2);
63 assert(*l1
.begin() == 1);
64 assert(*next(l1
.begin()) == 3);
65 assert(is_contiguous_container_asan_correct(l1
));
67 assert(j
== l1
.end());
68 assert(l1
.size() == 1);
69 assert(distance(l1
.begin(), l1
.end()) == 1);
70 assert(*l1
.begin() == 1);
71 assert(is_contiguous_container_asan_correct(l1
));
72 j
= l1
.erase(l1
.begin());
73 assert(j
== l1
.end());
74 assert(l1
.size() == 0);
75 assert(distance(l1
.begin(), l1
.end()) == 0);
76 assert(is_contiguous_container_asan_correct(l1
));
78 #if TEST_STD_VER >= 11
81 std::vector
<int, min_allocator
<int>> l1(a1
, a1
+3);
82 std::vector
<int, min_allocator
<int>>::const_iterator i
= l1
.begin();
83 assert(is_contiguous_container_asan_correct(l1
));
85 std::vector
<int, min_allocator
<int>>::iterator j
= l1
.erase(i
);
86 assert(l1
.size() == 2);
87 assert(distance(l1
.begin(), l1
.end()) == 2);
89 assert(*l1
.begin() == 1);
90 assert(*next(l1
.begin()) == 3);
91 assert(is_contiguous_container_asan_correct(l1
));
93 assert(j
== l1
.end());
94 assert(l1
.size() == 1);
95 assert(distance(l1
.begin(), l1
.end()) == 1);
96 assert(*l1
.begin() == 1);
97 assert(is_contiguous_container_asan_correct(l1
));
98 j
= l1
.erase(l1
.begin());
99 assert(j
== l1
.end());
100 assert(l1
.size() == 0);
101 assert(distance(l1
.begin(), l1
.end()) == 0);
102 assert(is_contiguous_container_asan_correct(l1
));
105 #ifndef TEST_HAS_NO_EXCEPTIONS
107 // Throws: Nothing unless an exception is thrown by the assignment operator or move assignment operator of T.
109 Throws arr
[] = {1, 2, 3};
110 std::vector
<Throws
> v(arr
, arr
+3);
111 Throws::sThrows
= true;
115 assert(v
.size() == 0);