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