Add WTF::move()
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WTF / Vector.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "MoveOnly.h"
29 #include <wtf/Vector.h>
30
31 namespace TestWebKitAPI {
32
33 TEST(WTF_Vector, Basic)
34 {
35     Vector<int> intVector;
36     EXPECT_TRUE(intVector.isEmpty());
37     EXPECT_EQ(0U, intVector.size());
38     EXPECT_EQ(0U, intVector.capacity());
39 }
40
41 TEST(WTF_Vector, Iterator)
42 {
43     Vector<int> intVector;
44     intVector.append(10);
45     intVector.append(11);
46     intVector.append(12);
47     intVector.append(13);
48
49     Vector<int>::iterator it = intVector.begin();
50     Vector<int>::iterator end = intVector.end();
51     EXPECT_TRUE(end != it);
52
53     EXPECT_EQ(10, *it);
54     ++it;
55     EXPECT_EQ(11, *it);
56     ++it;
57     EXPECT_EQ(12, *it);
58     ++it;
59     EXPECT_EQ(13, *it);
60     ++it;
61
62     EXPECT_TRUE(end == it);
63 }
64
65 TEST(WTF_Vector, OverloadedOperatorAmpersand)
66 {
67     struct Test {
68     private:
69         Test* operator&();
70     };
71
72     Vector<Test> vector;
73     vector.append(Test());
74 }
75
76 TEST(WTF_Vector, AppendLast)
77 {
78     Vector<unsigned> vector;
79     vector.append(0);
80
81     // FIXME: This test needs to be run with GuardMalloc to show the bug.
82     for (size_t i = 0; i < 100; ++i)
83         vector.append(const_cast<const unsigned&>(vector.last()));
84 }
85
86 TEST(WTF_Vector, InitializerList)
87 {
88     Vector<int> vector = { 1, 2, 3, 4 };
89     EXPECT_EQ(4U, vector.size());
90
91     EXPECT_EQ(1, vector[0]);
92     EXPECT_EQ(2, vector[1]);
93     EXPECT_EQ(3, vector[2]);
94     EXPECT_EQ(4, vector[3]);
95 }
96
97 TEST(WTF_Vector, Reverse)
98 {
99     Vector<int> intVector;
100     intVector.append(10);
101     intVector.append(11);
102     intVector.append(12);
103     intVector.append(13);
104     intVector.reverse();
105
106     EXPECT_EQ(13, intVector[0]);
107     EXPECT_EQ(12, intVector[1]);
108     EXPECT_EQ(11, intVector[2]);
109     EXPECT_EQ(10, intVector[3]);
110
111     intVector.append(9);
112     intVector.reverse();
113
114     EXPECT_EQ(9, intVector[0]);
115     EXPECT_EQ(10, intVector[1]);
116     EXPECT_EQ(11, intVector[2]);
117     EXPECT_EQ(12, intVector[3]);
118     EXPECT_EQ(13, intVector[4]);
119 }
120
121 TEST(WTF_Vector, ReverseIterator)
122 {
123     Vector<int> intVector;
124     intVector.append(10);
125     intVector.append(11);
126     intVector.append(12);
127     intVector.append(13);
128
129     Vector<int>::reverse_iterator it = intVector.rbegin();
130     Vector<int>::reverse_iterator end = intVector.rend();
131     EXPECT_TRUE(end != it);
132
133     EXPECT_EQ(13, *it);
134     ++it;
135     EXPECT_EQ(12, *it);
136     ++it;
137     EXPECT_EQ(11, *it);
138     ++it;
139     EXPECT_EQ(10, *it);
140     ++it;
141
142     EXPECT_TRUE(end == it);
143 }
144
145 TEST(WTF_Vector, MoveOnly_UncheckedAppend)
146 {
147     Vector<MoveOnly> vector;
148
149     vector.reserveInitialCapacity(100);
150     for (size_t i = 0; i < 100; ++i) {
151         MoveOnly moveOnly(i);
152         vector.uncheckedAppend(WTF::move(moveOnly));
153         EXPECT_EQ(0U, moveOnly.value());
154     }
155
156     for (size_t i = 0; i < 100; ++i)
157         EXPECT_EQ(i, vector[i].value());
158 }
159
160 TEST(WTF_Vector, MoveOnly_Append)
161 {
162     Vector<MoveOnly> vector;
163
164     for (size_t i = 0; i < 100; ++i) {
165         MoveOnly moveOnly(i);
166         vector.append(WTF::move(moveOnly));
167         EXPECT_EQ(0U, moveOnly.value());
168     }
169
170     for (size_t i = 0; i < 100; ++i)
171         EXPECT_EQ(i, vector[i].value());
172
173     for (size_t i = 0; i < 16; ++i) {
174         Vector<MoveOnly> vector;
175
176         vector.append(i);
177
178         for (size_t j = 0; j < i; ++j)
179             vector.append(j);
180         vector.append(WTF::move(vector[0]));
181
182         EXPECT_EQ(0U, vector[0].value());
183
184         for (size_t j = 0; j < i; ++j)
185             EXPECT_EQ(j, vector[j + 1].value());
186         EXPECT_EQ(i, vector.last().value());
187     }
188 }
189
190 TEST(WTF_Vector, MoveOnly_Insert)
191 {
192     Vector<MoveOnly> vector;
193
194     for (size_t i = 0; i < 100; ++i) {
195         MoveOnly moveOnly(i);
196         vector.insert(0, WTF::move(moveOnly));
197         EXPECT_EQ(0U, moveOnly.value());
198     }
199
200     EXPECT_EQ(vector.size(), 100U);
201     for (size_t i = 0; i < 100; ++i)
202         EXPECT_EQ(99 - i, vector[i].value());
203
204     for (size_t i = 0; i < 200; i += 2) {
205         MoveOnly moveOnly(1000 + i);
206         vector.insert(i, WTF::move(moveOnly));
207         EXPECT_EQ(0U, moveOnly.value());
208     }
209
210     EXPECT_EQ(200U, vector.size());
211     for (size_t i = 0; i < 200; ++i) {
212         if (i % 2)
213             EXPECT_EQ(99 - i / 2, vector[i].value());
214         else
215             EXPECT_EQ(1000 + i, vector[i].value());
216     }
217 }
218
219 TEST(WTF_Vector, MoveOnly_TakeLast)
220 {
221     Vector<MoveOnly> vector;
222
223     for (size_t i = 0; i < 100; ++i) {
224         MoveOnly moveOnly(i);
225         vector.append(WTF::move(moveOnly));
226         EXPECT_EQ(0U, moveOnly.value());
227     }
228
229     EXPECT_EQ(100U, vector.size());
230     for (size_t i = 0; i < 100; ++i)
231         EXPECT_EQ(99 - i, vector.takeLast().value());
232
233     EXPECT_EQ(0U, vector.size());
234 }
235
236 TEST(WTF_Vector, VectorOfVectorsOfVectorsInlineCapacitySwap)
237 {
238     Vector<Vector<Vector<int, 1>, 1>, 1> a;
239     Vector<Vector<Vector<int, 1>, 1>, 1> b;
240     Vector<Vector<Vector<int, 1>, 1>, 1> c;
241
242     EXPECT_EQ(0U, a.size());
243     EXPECT_EQ(0U, b.size());
244     EXPECT_EQ(0U, c.size());
245
246     Vector<int, 1> x;
247     x.append(42);
248
249     EXPECT_EQ(1U, x.size());
250     EXPECT_EQ(42, x[0]);
251     
252     Vector<Vector<int, 1>, 1> y;
253     y.append(x);
254     
255     EXPECT_EQ(1U, x.size());
256     EXPECT_EQ(42, x[0]);
257     EXPECT_EQ(1U, y.size());
258     EXPECT_EQ(1U, y[0].size());
259     EXPECT_EQ(42, y[0][0]);
260     
261     a.append(y);
262
263     EXPECT_EQ(1U, x.size());
264     EXPECT_EQ(42, x[0]);
265     EXPECT_EQ(1U, y.size());
266     EXPECT_EQ(1U, y[0].size());
267     EXPECT_EQ(42, y[0][0]);
268     EXPECT_EQ(1U, a.size());
269     EXPECT_EQ(1U, a[0].size());
270     EXPECT_EQ(1U, a[0][0].size());
271     EXPECT_EQ(42, a[0][0][0]);
272     
273     a.swap(b);
274
275     EXPECT_EQ(0U, a.size());
276     EXPECT_EQ(1U, x.size());
277     EXPECT_EQ(42, x[0]);
278     EXPECT_EQ(1U, y.size());
279     EXPECT_EQ(1U, y[0].size());
280     EXPECT_EQ(42, y[0][0]);
281     EXPECT_EQ(1U, b.size());
282     EXPECT_EQ(1U, b[0].size());
283     EXPECT_EQ(1U, b[0][0].size());
284     EXPECT_EQ(42, b[0][0][0]);
285     
286     b.swap(c);
287
288     EXPECT_EQ(0U, a.size());
289     EXPECT_EQ(0U, b.size());
290     EXPECT_EQ(1U, x.size());
291     EXPECT_EQ(42, x[0]);
292     EXPECT_EQ(1U, y.size());
293     EXPECT_EQ(1U, y[0].size());
294     EXPECT_EQ(42, y[0][0]);
295     EXPECT_EQ(1U, c.size());
296     EXPECT_EQ(1U, c[0].size());
297     EXPECT_EQ(1U, c[0][0].size());
298     EXPECT_EQ(42, c[0][0][0]);
299     
300     y[0][0] = 24;
301
302     EXPECT_EQ(1U, x.size());
303     EXPECT_EQ(42, x[0]);
304     EXPECT_EQ(1U, y.size());
305     EXPECT_EQ(1U, y[0].size());
306     EXPECT_EQ(24, y[0][0]);
307     
308     a.append(y);
309
310     EXPECT_EQ(1U, x.size());
311     EXPECT_EQ(42, x[0]);
312     EXPECT_EQ(1U, y.size());
313     EXPECT_EQ(1U, y[0].size());
314     EXPECT_EQ(24, y[0][0]);
315     EXPECT_EQ(1U, a.size());
316     EXPECT_EQ(1U, a[0].size());
317     EXPECT_EQ(1U, a[0][0].size());
318     EXPECT_EQ(24, a[0][0][0]);
319     EXPECT_EQ(1U, c.size());
320     EXPECT_EQ(1U, c[0].size());
321     EXPECT_EQ(1U, c[0][0].size());
322     EXPECT_EQ(42, c[0][0][0]);
323     EXPECT_EQ(0U, b.size());
324 }
325
326 } // namespace TestWebKitAPI