File: erase_iter.pass.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (137 lines) | stat: -rw-r--r-- 3,837 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <vector>

// iterator erase(const_iterator position);

#include <vector>
#include <cassert>
#include <memory>

#include "asan_testing.h"
#include "common.h"
#include "min_allocator.h"
#include "MoveOnly.h"
#include "test_macros.h"

template <template <class> class Allocator, class T>
TEST_CONSTEXPR_CXX20 void tests() {
  {
    T arr[]             = {T(1), T(2), T(3), T(4), T(5)};
    using Vector        = std::vector<T, Allocator<T> >;
    using Iterator      = typename Vector::iterator;

    {
      Vector v(arr, arr + 5);
      Iterator it = v.erase(v.cbegin());
      assert(v == Vector(arr + 1, arr + 5));
      assert(it == v.begin());
      assert(is_contiguous_container_asan_correct(v));
    }
    {
      T expected[] = {T(1), T(3), T(4), T(5)};
      Vector v(arr, arr + 5);
      Iterator it = v.erase(v.cbegin() + 1);
      assert(v == Vector(expected, expected + 4));
      assert(it == v.begin() + 1);
      assert(is_contiguous_container_asan_correct(v));
    }
    {
      T expected[] = {T(1), T(2), T(3), T(4)};
      Vector v(arr, arr + 5);
      Iterator it = v.erase(v.cbegin() + 4);
      assert(v == Vector(expected, expected + 4));
      assert(it == v.end());
      assert(is_contiguous_container_asan_correct(v));
    }
  }

  // Make sure vector::erase works with move-only types
  {
    // When non-trivial
    {
      std::vector<MoveOnly, Allocator<MoveOnly> > v;
      v.emplace_back(1);
      v.emplace_back(2);
      v.emplace_back(3);
      v.erase(v.begin());
      assert(v.size() == 2);
      assert(v[0] == MoveOnly(2));
      assert(v[1] == MoveOnly(3));
    }
    // When trivial
    {
      std::vector<TrivialMoveOnly, Allocator<TrivialMoveOnly> > v;
      v.emplace_back(1);
      v.emplace_back(2);
      v.emplace_back(3);
      v.erase(v.begin());
      assert(v.size() == 2);
      assert(v[0] == TrivialMoveOnly(2));
      assert(v[1] == TrivialMoveOnly(3));
    }
  }
}

TEST_CONSTEXPR_CXX20 bool tests() {
  tests<std::allocator, int>();
  tests<std::allocator, NonTriviallyRelocatable>();
  tests<min_allocator, int>();
  tests<min_allocator, NonTriviallyRelocatable>();
  return true;
}

int main(int, char**) {
  tests();
#if TEST_STD_VER > 17
  static_assert(tests());
#endif

#ifndef TEST_HAS_NO_EXCEPTIONS
  // Test for LWG2853:
  // Throws: Nothing unless an exception is thrown by the assignment operator or move assignment operator of T.
  {
    Throws arr[] = {1, 2, 3};
    std::vector<Throws> v(arr, arr + 3);
    Throws::sThrows = true;
    v.erase(v.begin());
    v.erase(--v.end());
    v.erase(v.begin());
    assert(v.size() == 0);
  }
#endif

  // Make sure we satisfy the complexity requirement in terms of the number of times the assignment
  // operator is called.
  //
  // There is currently ambiguity as to whether this is truly mandated by the Standard, so we only
  // test it for libc++.
#ifdef _LIBCPP_VERSION
  {
    Tracker tracker;
    std::vector<TrackedAssignment> v;

    // Set up the vector with 5 elements.
    for (int i = 0; i != 5; ++i) {
      v.emplace_back(&tracker);
    }
    assert(tracker.copy_assignments == 0);
    assert(tracker.move_assignments == 0);

    // Erase element [1] from it. Elements [2] [3] [4] should be shifted, so we should
    // see 3 move assignments (and nothing else).
    v.erase(v.begin() + 1);
    assert(v.size() == 4);
    assert(tracker.copy_assignments == 0);
    assert(tracker.move_assignments == 3);
  }
#endif

  return 0;
}