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