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