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, const_iterator l)
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
17#include "asan_testing.h"
18#include <deque>
19#include <cstdint>
20#include <cassert>
21
22#include "test_macros.h"
23
24template <typename C>
25void del_at_start(C c, std::size_t num)
26{
27 typename C::iterator first = c.begin();
28 typename C::iterator last = first + num;
29 typename C::iterator it1 = last;
30 typename C::iterator it2 = c.end() - 1;
31
32 c.erase (first, last);
33
34 typename C::iterator it3 = c.begin();
35 typename C::iterator it4 = c.end() - 1;
36 assert( it1 == it3);
37 assert( *it1 == *it3);
38 assert(&*it1 == &*it3);
39 assert( it2 == it4);
40 assert( *it2 == *it4);
41 assert(&*it2 == &*it4);
42 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));
43}
44
45template <typename C>
46void del_at_end(C c, std::size_t num)
47{
48 typename C::iterator last = c.end();
49 typename C::iterator first = last - num;
50 typename C::iterator it1 = c.begin();
51 typename C::iterator it2 = first - 1;
52
53 c.erase (first, last);
54
55 typename C::iterator it3 = c.begin();
56 typename C::iterator it4 = c.end() - 1;
57 assert( it1 == it3);
58 assert( *it1 == *it3);
59 assert(&*it1 == &*it3);
60 assert( it2 == it4);
61 assert( *it2 == *it4);
62 assert(&*it2 == &*it4);
63 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));
64}
65
66
67int main(int, char**)
68{
69 std::deque<int> queue;
70 for (int i = 0; i < 20; ++i)
71 queue.push_back(i);
72
73 while (queue.size() > 1)
74 {
75 for (std::size_t i = 1; i < queue.size(); ++i)
76 {
77 del_at_start(queue, i);
78 del_at_end (queue, i);
79 }
80 queue.pop_back();
81 }
82
83 return 0;
84}
85

source code of libcxx/test/std/containers/sequences/deque/deque.modifiers/erase_iter_iter.invalidation.pass.cpp