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