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