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