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