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 // <deque> 10 11 // iterator erase(const_iterator p) 12 13 #include <deque> 14 #include <algorithm> 15 #include <iterator> 16 #include <cassert> 17 #include <cstddef> 18 19 #include "min_allocator.h" 20 #include "test_macros.h" 21 22 #ifndef TEST_HAS_NO_EXCEPTIONS 23 struct Throws { 24 Throws() : v_(0) {} 25 Throws(int v) : v_(v) {} 26 Throws(const Throws &rhs) : v_(rhs.v_) { if (sThrows) throw 1; } 27 Throws( Throws &&rhs) : v_(rhs.v_) { if (sThrows) throw 1; } 28 Throws& operator=(const Throws &rhs) { v_ = rhs.v_; return *this; } 29 Throws& operator=( Throws &&rhs) { v_ = rhs.v_; return *this; } 30 int v_; 31 32 static bool sThrows; 33 }; 34 35 bool Throws::sThrows = false; 36 #endif 37 38 template <class C> 39 C 40 make(int size, int start = 0 ) 41 { 42 const int b = 4096 / sizeof(int); 43 int init = 0; 44 if (start > 0) 45 { 46 init = (start+1) / b + ((start+1) % b != 0); 47 init *= b; 48 --init; 49 } 50 C c(init, 0); 51 for (int i = 0; i < init-start; ++i) 52 c.pop_back(); 53 for (int i = 0; i < size; ++i) 54 c.push_back(i); 55 for (int i = 0; i < start; ++i) 56 c.pop_front(); 57 return c; 58 } 59 60 template <class C> 61 void 62 test(int P, C& c1) 63 { 64 typedef typename C::iterator I; 65 assert(static_cast<std::size_t>(P) < c1.size()); 66 std::size_t c1_osize = c1.size(); 67 I i = c1.erase(c1.cbegin() + P); 68 assert(i == c1.begin() + P); 69 assert(c1.size() == c1_osize - 1); 70 assert(static_cast<std::size_t>(distance(c1.begin(), c1.end())) == c1.size()); 71 i = c1.begin(); 72 int j = 0; 73 for (; j < P; ++j, ++i) 74 assert(*i == j); 75 for (++j; static_cast<std::size_t>(j) < c1_osize; ++j, ++i) 76 assert(*i == j); 77 } 78 79 template <class C> 80 void 81 testN(int start, int N) 82 { 83 int pstep = std::max(N / std::max(std::min(N, 10), 1), 1); 84 for (int p = 0; p < N; p += pstep) 85 { 86 C c1 = make<C>(N, start); 87 test(p, c1); 88 } 89 } 90 91 int main(int, char**) 92 { 93 { 94 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; 95 const int N = sizeof(rng)/sizeof(rng[0]); 96 for (int i = 0; i < N; ++i) 97 for (int j = 0; j < N; ++j) 98 testN<std::deque<int> >(rng[i], rng[j]); 99 } 100 #if TEST_STD_VER >= 11 101 { 102 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; 103 const int N = sizeof(rng)/sizeof(rng[0]); 104 for (int i = 0; i < N; ++i) 105 for (int j = 0; j < N; ++j) 106 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]); 107 } 108 #endif 109 110 #ifndef TEST_HAS_NO_EXCEPTIONS 111 // Test for LWG2953: 112 // Throws: Nothing unless an exception is thrown by the assignment operator of T. 113 // (which includes move assignment) 114 { 115 Throws arr[] = {1, 2, 3}; 116 std::deque<Throws> v(arr, arr+3); 117 Throws::sThrows = true; 118 v.erase(v.begin()); 119 v.erase(--v.end()); 120 v.erase(v.begin()); 121 assert(v.size() == 0); 122 } 123 #endif 124 125 return 0; 126 } 127