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