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 f)
12 
13 //  Erasing items from the beginning or the end of a deque shall not invalidate iterators
14 //  to items that were not erased.
15 
16 #include <deque>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 template <typename C>
del_at_start(C c)22 void del_at_start(C c)
23 {
24     typename C::iterator first = c.begin();
25     typename C::iterator it1 = first + 1;
26     typename C::iterator it2 = c.end() - 1;
27 
28     c.erase (first);
29 
30     typename C::iterator it3 = c.begin();
31     typename C::iterator it4 = c.end() - 1;
32     assert(  it1 ==   it3);
33     assert( *it1 ==  *it3);
34     assert(&*it1 == &*it3);
35     assert(  it2 ==   it4);
36     assert( *it2 ==  *it4);
37     assert(&*it2 == &*it4);
38 }
39 
40 template <typename C>
del_at_end(C c)41 void del_at_end(C c)
42 {
43     typename C::iterator first = c.end() - 1;
44     typename C::iterator it1 = c.begin();
45     typename C::iterator it2 = first - 1;
46 
47     c.erase (first);
48 
49     typename C::iterator it3 = c.begin();
50     typename C::iterator it4 = c.end() - 1;
51     assert(  it1 ==   it3);
52     assert( *it1 ==  *it3);
53     assert(&*it1 == &*it3);
54     assert(  it2 ==   it4);
55     assert( *it2 ==  *it4);
56     assert(&*it2 == &*it4);
57 }
58 
main(int,char **)59 int main(int, char**)
60 {
61     std::deque<int> queue;
62     for (int i = 0; i < 20; ++i)
63         queue.push_back(i);
64 
65     while (queue.size() > 1)
66     {
67         del_at_start(queue);
68         del_at_end(queue);
69         queue.pop_back();
70     }
71 
72   return 0;
73 }
74