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