erase_iter.pass.cpp (3182B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <vector> 11 12 // iterator erase(const_iterator position); 13 14 #include <vector> 15 #include <iterator> 16 #include <cassert> 17 18 #include "min_allocator.h" 19 #include "asan_testing.h" 20 21 #ifndef TEST_HAS_NO_EXCEPTIONS 22 struct Throws { 23 Throws() : v_(0) {} 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; } 29 int v_; 30 static bool sThrows; 31 }; 32 33 bool Throws::sThrows = false; 34 #endif 35 36 int main() 37 { 38 { 39 int a1[] = {1, 2, 3}; 40 std::vector<int> l1(a1, a1+3); 41 std::vector<int>::const_iterator i = l1.begin(); 42 assert(is_contiguous_container_asan_correct(l1)); 43 ++i; 44 std::vector<int>::iterator j = l1.erase(i); 45 assert(l1.size() == 2); 46 assert(distance(l1.begin(), l1.end()) == 2); 47 assert(*j == 3); 48 assert(*l1.begin() == 1); 49 assert(*next(l1.begin()) == 3); 50 assert(is_contiguous_container_asan_correct(l1)); 51 j = l1.erase(j); 52 assert(j == l1.end()); 53 assert(l1.size() == 1); 54 assert(distance(l1.begin(), l1.end()) == 1); 55 assert(*l1.begin() == 1); 56 assert(is_contiguous_container_asan_correct(l1)); 57 j = l1.erase(l1.begin()); 58 assert(j == l1.end()); 59 assert(l1.size() == 0); 60 assert(distance(l1.begin(), l1.end()) == 0); 61 assert(is_contiguous_container_asan_correct(l1)); 62 } 63 #if TEST_STD_VER >= 11 64 { 65 int a1[] = {1, 2, 3}; 66 std::vector<int, min_allocator<int>> l1(a1, a1+3); 67 std::vector<int, min_allocator<int>>::const_iterator i = l1.begin(); 68 assert(is_contiguous_container_asan_correct(l1)); 69 ++i; 70 std::vector<int, min_allocator<int>>::iterator j = l1.erase(i); 71 assert(l1.size() == 2); 72 assert(distance(l1.begin(), l1.end()) == 2); 73 assert(*j == 3); 74 assert(*l1.begin() == 1); 75 assert(*next(l1.begin()) == 3); 76 assert(is_contiguous_container_asan_correct(l1)); 77 j = l1.erase(j); 78 assert(j == l1.end()); 79 assert(l1.size() == 1); 80 assert(distance(l1.begin(), l1.end()) == 1); 81 assert(*l1.begin() == 1); 82 assert(is_contiguous_container_asan_correct(l1)); 83 j = l1.erase(l1.begin()); 84 assert(j == l1.end()); 85 assert(l1.size() == 0); 86 assert(distance(l1.begin(), l1.end()) == 0); 87 assert(is_contiguous_container_asan_correct(l1)); 88 } 89 #endif 90 #ifndef TEST_HAS_NO_EXCEPTIONS 91 // Test for LWG2853: 92 // Throws: Nothing unless an exception is thrown by the assignment operator or move assignment operator of T. 93 { 94 Throws arr[] = {1, 2, 3}; 95 std::vector<Throws> v(arr, arr+3); 96 Throws::sThrows = true; 97 v.erase(v.begin()); 98 v.erase(--v.end()); 99 v.erase(v.begin()); 100 assert(v.size() == 0); 101 } 102 #endif 103 }