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 std::deque<int>
make(int size,int start=0)18 make(int size, int start = 0 )
19 {
20 const int b = 4096 / sizeof(int);
21 int init = 0;
22 if (start > 0)
23 {
24 init = (start+1) / b + ((start+1) % b != 0);
25 init *= b;
26 --init;
27 }
28 std::deque<int> c(init, 0);
29 for (int i = 0; i < init-start; ++i)
30 c.pop_back();
31 for (int i = 0; i < size; ++i)
32 c.push_back(i);
33 for (int i = 0; i < start; ++i)
34 c.pop_front();
35 return c;
36 };
37
38 void
test(int P,std::deque<int> & c1,int x)39 test(int P, std::deque<int>& c1, int x)
40 {
41 typedef std::deque<int> C;
42 typedef C::iterator I;
43 typedef C::const_iterator CI;
44 std::size_t c1_osize = c1.size();
45 CI i = c1.insert(c1.begin() + P, x);
46 assert(i == c1.begin() + P);
47 assert(c1.size() == c1_osize + 1);
48 assert(distance(c1.begin(), c1.end()) == c1.size());
49 i = c1.begin();
50 for (int j = 0; j < P; ++j, ++i)
51 assert(*i == j);
52 assert(*i == x);
53 ++i;
54 for (int j = P; j < c1_osize; ++j, ++i)
55 assert(*i == j);
56 }
57
58 void
testN(int start,int N)59 testN(int start, int N)
60 {
61 typedef std::deque<int> C;
62 typedef C::iterator I;
63 typedef C::const_iterator CI;
64 for (int i = 0; i <= 3; ++i)
65 {
66 if (0 <= i && i <= N)
67 {
68 C c1 = make(N, start);
69 test(i, c1, -10);
70 }
71 }
72 for (int i = N/2-1; i <= N/2+1; ++i)
73 {
74 if (0 <= i && i <= N)
75 {
76 C c1 = make(N, start);
77 test(i, c1, -10);
78 }
79 }
80 for (int i = N - 3; i <= N; ++i)
81 {
82 if (0 <= i && i <= N)
83 {
84 C c1 = make(N, start);
85 test(i, c1, -10);
86 }
87 }
88 }
89
90 void
self_reference_test()91 self_reference_test()
92 {
93 typedef std::deque<int> C;
94 typedef C::const_iterator CI;
95 for (int i = 0; i < 20; ++i)
96 {
97 for (int j = 0; j < 20; ++j)
98 {
99 C c = make(20);
100 CI it = c.cbegin() + i;
101 CI jt = c.cbegin() + j;
102 c.insert(it, *jt);
103 assert(c.size() == 21);
104 assert(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 assert(*it == j);
109 ++it;
110 for (int k = i; k < 20; ++k, ++it)
111 assert(*it == k);
112 }
113 }
114 }
115
main()116 int main()
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 testN(rng[i], rng[j]);
123 self_reference_test();
124 }
125