| 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 insert (const_iterator p, const value_type& v); |
| 12 | |
| 13 | #include "asan_testing.h" |
| 14 | #include <deque> |
| 15 | #include <cassert> |
| 16 | #include <cstddef> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "min_allocator.h" |
| 20 | |
| 21 | template <class C> |
| 22 | C make(int size, int start = 0) { |
| 23 | const int b = 4096 / sizeof(int); |
| 24 | int init = 0; |
| 25 | if (start > 0) { |
| 26 | init = (start + 1) / b + ((start + 1) % b != 0); |
| 27 | init *= b; |
| 28 | --init; |
| 29 | } |
| 30 | C c(init, 0); |
| 31 | for (int i = 0; i < init - start; ++i) |
| 32 | c.pop_back(); |
| 33 | for (int i = 0; i < size; ++i) |
| 34 | c.push_back(i); |
| 35 | for (int i = 0; i < start; ++i) |
| 36 | c.pop_front(); |
| 37 | return c; |
| 38 | } |
| 39 | |
| 40 | template <class C> |
| 41 | void test(int P, C& c1, int x) { |
| 42 | typedef typename C::const_iterator CI; |
| 43 | std::size_t c1_osize = c1.size(); |
| 44 | CI i = c1.insert(c1.begin() + P, x); |
| 45 | assert(i == c1.begin() + P); |
| 46 | assert(c1.size() == c1_osize + 1); |
| 47 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 48 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1)); |
| 49 | i = c1.begin(); |
| 50 | for (int j = 0; j < P; ++j, ++i) |
| 51 | assert(*i == j); |
| 52 | assert(*i == x); |
| 53 | ++i; |
| 54 | for (int j = P; static_cast<std::size_t>(j) < c1_osize; ++j, ++i) |
| 55 | assert(*i == j); |
| 56 | } |
| 57 | |
| 58 | template <class C> |
| 59 | void testN(int start, int N) { |
| 60 | for (int i = 0; i <= 3; ++i) { |
| 61 | if (0 <= i && i <= N) { |
| 62 | C c1 = make<C>(N, start); |
| 63 | test(i, c1, -10); |
| 64 | } |
| 65 | } |
| 66 | for (int i = N / 2 - 1; i <= N / 2 + 1; ++i) { |
| 67 | if (0 <= i && i <= N) { |
| 68 | C c1 = make<C>(N, start); |
| 69 | test(i, c1, -10); |
| 70 | } |
| 71 | } |
| 72 | for (int i = N - 3; i <= N; ++i) { |
| 73 | if (0 <= i && i <= N) { |
| 74 | C c1 = make<C>(N, start); |
| 75 | test(i, c1, -10); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | template <class C> |
| 81 | void self_reference_test() { |
| 82 | typedef typename C::const_iterator CI; |
| 83 | for (int i = 0; i < 20; ++i) { |
| 84 | for (int j = 0; j < 20; ++j) { |
| 85 | C c = make<C>(20); |
| 86 | CI it = c.cbegin() + i; |
| 87 | CI jt = c.cbegin() + j; |
| 88 | c.insert(it, *jt); |
| 89 | assert(c.size() == 21); |
| 90 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 91 | it = c.cbegin(); |
| 92 | for (int k = 0; k < i; ++k, ++it) |
| 93 | assert(*it == k); |
| 94 | assert(*it == j); |
| 95 | ++it; |
| 96 | for (int k = i; k < 20; ++k, ++it) |
| 97 | assert(*it == k); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | int 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> >(start: rng[i], N: rng[j]); |
| 109 | self_reference_test<std::deque<int> >(); |
| 110 | } |
| 111 | #if TEST_STD_VER >= 11 |
| 112 | { |
| 113 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 114 | const int N = sizeof(rng) / sizeof(rng[0]); |
| 115 | for (int i = 0; i < N; ++i) |
| 116 | for (int j = 0; j < N; ++j) |
| 117 | testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]); |
| 118 | self_reference_test<std::deque<int, min_allocator<int>> >(); |
| 119 | } |
| 120 | { |
| 121 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 122 | const int N = sizeof(rng) / sizeof(rng[0]); |
| 123 | for (int i = 0; i < N; ++i) |
| 124 | for (int j = 0; j < N; ++j) |
| 125 | testN<std::deque<int, safe_allocator<int>> >(rng[i], rng[j]); |
| 126 | self_reference_test<std::deque<int, safe_allocator<int>> >(); |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |