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 
21 #include "min_allocator.h"
22 
23 template <class C>
24 C
25 make(int size, int start = 0 )
26 {
27     const int b = 4096 / sizeof(int);
28     int init = 0;
29     if (start > 0)
30     {
31         init = (start+1) / b + ((start+1) % b != 0);
32         init *= b;
33         --init;
34     }
35     C c(init, 0);
36     for (int i = 0; i < init-start; ++i)
37         c.pop_back();
38     for (int i = 0; i < size; ++i)
39         c.push_back(i);
40     for (int i = 0; i < start; ++i)
41         c.pop_front();
42     return c;
43 }
44 
45 template <class C>
46 void
47 test(int P, C& c1, int size)
48 {
49     typedef typename C::iterator I;
50     assert(P + size <= c1.size());
51     std::size_t c1_osize = c1.size();
52     I i = c1.erase(c1.cbegin() + P, c1.cbegin() + (P + size));
53     assert(i == c1.begin() + P);
54     assert(c1.size() == c1_osize - size);
55     assert(distance(c1.begin(), c1.end()) == c1.size());
56     i = c1.begin();
57     int j = 0;
58     for (; j < P; ++j, ++i)
59         assert(*i == j);
60     for (j += size; j < c1_osize; ++j, ++i)
61         assert(*i == j);
62 }
63 
64 template <class C>
65 void
66 testN(int start, int N)
67 {
68     int pstep = std::max(N / std::max(std::min(N, 10), 1), 1);
69     for (int p = 0; p <= N; p += pstep)
70     {
71         int sstep = std::max((N - p) / std::max(std::min(N - p, 10), 1), 1);
72         for (int s = 0; s <= N - p; s += sstep)
73         {
74             C c1 = make<C>(N, start);
75             test(p, c1, s);
76         }
77     }
78 }
79 
80 int main()
81 {
82     {
83     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
84     const int N = sizeof(rng)/sizeof(rng[0]);
85     for (int i = 0; i < N; ++i)
86         for (int j = 0; j < N; ++j)
87             testN<std::deque<int> >(rng[i], rng[j]);
88     }
89 #if TEST_STD_VER >= 11
90     {
91     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
92     const int N = sizeof(rng)/sizeof(rng[0]);
93     for (int i = 0; i < N; ++i)
94         for (int j = 0; j < N; ++j)
95             testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
96     }
97 #endif
98 }
99