1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <deque>
11
12 // iterator insert (const_iterator p, const value_type& v);
13
14 #include <deque>
15 #include <cassert>
16
17 #include "min_allocator.h"
18
19 template <class C>
20 C
make(int size,int start=0)21 make(int size, int start = 0 )
22 {
23 const int b = 4096 / sizeof(int);
24 int init = 0;
25 if (start > 0)
26 {
27 init = (start+1) / b + ((start+1) % b != 0);
28 init *= b;
29 --init;
30 }
31 C c(init, 0);
32 for (int i = 0; i < init-start; ++i)
33 c.pop_back();
34 for (int i = 0; i < size; ++i)
35 c.push_back(i);
36 for (int i = 0; i < start; ++i)
37 c.pop_front();
38 return c;
39 };
40
41 template <class C>
42 void
test(int P,C & c1,int x)43 test(int P, C& c1, int x)
44 {
45 typedef typename C::iterator I;
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(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; 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 typedef typename C::iterator I;
66 typedef typename C::const_iterator CI;
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, -10);
73 }
74 }
75 for (int i = N/2-1; i <= N/2+1; ++i)
76 {
77 if (0 <= i && i <= N)
78 {
79 C c1 = make<C>(N, start);
80 test(i, c1, -10);
81 }
82 }
83 for (int i = N - 3; i <= N; ++i)
84 {
85 if (0 <= i && i <= N)
86 {
87 C c1 = make<C>(N, start);
88 test(i, c1, -10);
89 }
90 }
91 }
92
93 template <class C>
94 void
self_reference_test()95 self_reference_test()
96 {
97 typedef typename C::const_iterator CI;
98 for (int i = 0; i < 20; ++i)
99 {
100 for (int j = 0; j < 20; ++j)
101 {
102 C c = make<C>(20);
103 CI it = c.cbegin() + i;
104 CI jt = c.cbegin() + j;
105 c.insert(it, *jt);
106 assert(c.size() == 21);
107 assert(distance(c.begin(), c.end()) == c.size());
108 it = c.cbegin();
109 for (int k = 0; k < i; ++k, ++it)
110 assert(*it == k);
111 assert(*it == j);
112 ++it;
113 for (int k = i; k < 20; ++k, ++it)
114 assert(*it == k);
115 }
116 }
117 }
118
main()119 int main()
120 {
121 {
122 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
123 const int N = sizeof(rng)/sizeof(rng[0]);
124 for (int i = 0; i < N; ++i)
125 for (int j = 0; j < N; ++j)
126 testN<std::deque<int> >(rng[i], rng[j]);
127 self_reference_test<std::deque<int> >();
128 }
129 #if __cplusplus >= 201103L
130 {
131 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
132 const int N = sizeof(rng)/sizeof(rng[0]);
133 for (int i = 0; i < N; ++i)
134 for (int j = 0; j < N; ++j)
135 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
136 self_reference_test<std::deque<int, min_allocator<int>> >();
137 }
138 #endif
139 }
140