Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: third_party/WebKit/Source/wtf/VectorTest.cpp

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/Vector.h ('k') | third_party/WebKit/Source/wtf/VectorTraits.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 17 matching lines...) Expand all
28 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "wtf/HashSet.h" 29 #include "wtf/HashSet.h"
30 #include "wtf/OwnPtr.h" 30 #include "wtf/OwnPtr.h"
31 #include "wtf/PassOwnPtr.h" 31 #include "wtf/PassOwnPtr.h"
32 #include "wtf/text/WTFString.h" 32 #include "wtf/text/WTFString.h"
33 33
34 namespace WTF { 34 namespace WTF {
35 35
36 namespace { 36 namespace {
37 37
38 TEST(VectorTest, Basic) 38 TEST(VectorTest, Basic) {
39 { 39 Vector<int> intVector;
40 Vector<int> intVector; 40 EXPECT_TRUE(intVector.isEmpty());
41 EXPECT_TRUE(intVector.isEmpty()); 41 EXPECT_EQ(0ul, intVector.size());
42 EXPECT_EQ(0ul, intVector.size()); 42 EXPECT_EQ(0ul, intVector.capacity());
43 EXPECT_EQ(0ul, intVector.capacity()); 43 }
44 } 44
45 45 TEST(VectorTest, Reverse) {
46 TEST(VectorTest, Reverse) 46 Vector<int> intVector;
47 { 47 intVector.append(10);
48 Vector<int> intVector; 48 intVector.append(11);
49 intVector.append(10); 49 intVector.append(12);
50 intVector.append(11); 50 intVector.append(13);
51 intVector.append(12); 51 intVector.reverse();
52 intVector.append(13); 52
53 intVector.reverse(); 53 EXPECT_EQ(13, intVector[0]);
54 54 EXPECT_EQ(12, intVector[1]);
55 EXPECT_EQ(13, intVector[0]); 55 EXPECT_EQ(11, intVector[2]);
56 EXPECT_EQ(12, intVector[1]); 56 EXPECT_EQ(10, intVector[3]);
57 EXPECT_EQ(11, intVector[2]); 57
58 EXPECT_EQ(10, intVector[3]); 58 intVector.append(9);
59 59 intVector.reverse();
60 intVector.append(9); 60
61 intVector.reverse(); 61 EXPECT_EQ(9, intVector[0]);
62 62 EXPECT_EQ(10, intVector[1]);
63 EXPECT_EQ(9, intVector[0]); 63 EXPECT_EQ(11, intVector[2]);
64 EXPECT_EQ(10, intVector[1]); 64 EXPECT_EQ(12, intVector[3]);
65 EXPECT_EQ(11, intVector[2]); 65 EXPECT_EQ(13, intVector[4]);
66 EXPECT_EQ(12, intVector[3]); 66 }
67 EXPECT_EQ(13, intVector[4]); 67
68 } 68 TEST(VectorTest, Remove) {
69 69 Vector<int> intVector;
70 TEST(VectorTest, Remove) 70 intVector.append(0);
71 { 71 intVector.append(1);
72 Vector<int> intVector; 72 intVector.append(2);
73 intVector.append(0); 73 intVector.append(3);
74 intVector.append(1); 74
75 intVector.append(2); 75 EXPECT_EQ(4u, intVector.size());
76 intVector.append(3); 76 EXPECT_EQ(0, intVector[0]);
77 77 EXPECT_EQ(1, intVector[1]);
78 EXPECT_EQ(4u, intVector.size()); 78 EXPECT_EQ(2, intVector[2]);
79 EXPECT_EQ(0, intVector[0]); 79 EXPECT_EQ(3, intVector[3]);
80 EXPECT_EQ(1, intVector[1]); 80
81 EXPECT_EQ(2, intVector[2]); 81 intVector.remove(2, 0);
82 EXPECT_EQ(3, intVector[3]); 82 EXPECT_EQ(4u, intVector.size());
83 83 EXPECT_EQ(2, intVector[2]);
84 intVector.remove(2, 0); 84
85 EXPECT_EQ(4u, intVector.size()); 85 intVector.remove(2, 1);
86 EXPECT_EQ(2, intVector[2]); 86 EXPECT_EQ(3u, intVector.size());
87 87 EXPECT_EQ(3, intVector[2]);
88 intVector.remove(2, 1); 88
89 EXPECT_EQ(3u, intVector.size()); 89 intVector.remove(0, 0);
90 EXPECT_EQ(3, intVector[2]); 90 EXPECT_EQ(3u, intVector.size());
91 91 EXPECT_EQ(0, intVector[0]);
92 intVector.remove(0, 0); 92
93 EXPECT_EQ(3u, intVector.size()); 93 intVector.remove(0);
94 EXPECT_EQ(0, intVector[0]); 94 EXPECT_EQ(2u, intVector.size());
95 95 EXPECT_EQ(1, intVector[0]);
96 intVector.remove(0); 96 }
97 EXPECT_EQ(2u, intVector.size()); 97
98 EXPECT_EQ(1, intVector[0]); 98 TEST(VectorTest, Iterator) {
99 } 99 Vector<int> intVector;
100 100 intVector.append(10);
101 TEST(VectorTest, Iterator) 101 intVector.append(11);
102 { 102 intVector.append(12);
103 Vector<int> intVector; 103 intVector.append(13);
104 intVector.append(10); 104
105 intVector.append(11); 105 Vector<int>::iterator it = intVector.begin();
106 intVector.append(12); 106 Vector<int>::iterator end = intVector.end();
107 intVector.append(13); 107 EXPECT_TRUE(end != it);
108 108
109 Vector<int>::iterator it = intVector.begin(); 109 EXPECT_EQ(10, *it);
110 Vector<int>::iterator end = intVector.end(); 110 ++it;
111 EXPECT_TRUE(end != it); 111 EXPECT_EQ(11, *it);
112 112 ++it;
113 EXPECT_EQ(10, *it); 113 EXPECT_EQ(12, *it);
114 ++it; 114 ++it;
115 EXPECT_EQ(11, *it); 115 EXPECT_EQ(13, *it);
116 ++it; 116 ++it;
117 EXPECT_EQ(12, *it); 117
118 ++it; 118 EXPECT_TRUE(end == it);
119 EXPECT_EQ(13, *it); 119 }
120 ++it; 120
121 121 TEST(VectorTest, ReverseIterator) {
122 EXPECT_TRUE(end == it); 122 Vector<int> intVector;
123 } 123 intVector.append(10);
124 124 intVector.append(11);
125 TEST(VectorTest, ReverseIterator) 125 intVector.append(12);
126 { 126 intVector.append(13);
127 Vector<int> intVector; 127
128 intVector.append(10); 128 Vector<int>::reverse_iterator it = intVector.rbegin();
129 intVector.append(11); 129 Vector<int>::reverse_iterator end = intVector.rend();
130 intVector.append(12); 130 EXPECT_TRUE(end != it);
131 intVector.append(13); 131
132 132 EXPECT_EQ(13, *it);
133 Vector<int>::reverse_iterator it = intVector.rbegin(); 133 ++it;
134 Vector<int>::reverse_iterator end = intVector.rend(); 134 EXPECT_EQ(12, *it);
135 EXPECT_TRUE(end != it); 135 ++it;
136 136 EXPECT_EQ(11, *it);
137 EXPECT_EQ(13, *it); 137 ++it;
138 ++it; 138 EXPECT_EQ(10, *it);
139 EXPECT_EQ(12, *it); 139 ++it;
140 ++it; 140
141 EXPECT_EQ(11, *it); 141 EXPECT_TRUE(end == it);
142 ++it;
143 EXPECT_EQ(10, *it);
144 ++it;
145
146 EXPECT_TRUE(end == it);
147 } 142 }
148 143
149 class DestructCounter { 144 class DestructCounter {
150 public: 145 public:
151 explicit DestructCounter(int i, int* destructNumber) 146 explicit DestructCounter(int i, int* destructNumber)
152 : m_i(i) 147 : m_i(i), m_destructNumber(destructNumber) {}
153 , m_destructNumber(destructNumber) 148
154 { } 149 ~DestructCounter() { ++(*m_destructNumber); }
155 150 int get() const { return m_i; }
156 ~DestructCounter() { ++(*m_destructNumber); } 151
157 int get() const { return m_i; } 152 private:
158 153 int m_i;
159 private: 154 int* m_destructNumber;
160 int m_i;
161 int* m_destructNumber;
162 }; 155 };
163 156
164 typedef WTF::Vector<OwnPtr<DestructCounter>> OwnPtrVector; 157 typedef WTF::Vector<OwnPtr<DestructCounter>> OwnPtrVector;
165 158
166 TEST(VectorTest, OwnPtr) 159 TEST(VectorTest, OwnPtr) {
167 { 160 int destructNumber = 0;
168 int destructNumber = 0; 161 OwnPtrVector vector;
169 OwnPtrVector vector; 162 vector.append(adoptPtr(new DestructCounter(0, &destructNumber)));
170 vector.append(adoptPtr(new DestructCounter(0, &destructNumber))); 163 vector.append(adoptPtr(new DestructCounter(1, &destructNumber)));
171 vector.append(adoptPtr(new DestructCounter(1, &destructNumber))); 164 EXPECT_EQ(2u, vector.size());
172 EXPECT_EQ(2u, vector.size()); 165
173 166 OwnPtr<DestructCounter>& counter0 = vector.first();
174 OwnPtr<DestructCounter>& counter0 = vector.first(); 167 ASSERT_EQ(0, counter0->get());
175 ASSERT_EQ(0, counter0->get()); 168 int counter1 = vector.last()->get();
176 int counter1 = vector.last()->get(); 169 ASSERT_EQ(1, counter1);
177 ASSERT_EQ(1, counter1); 170 ASSERT_EQ(0, destructNumber);
178 ASSERT_EQ(0, destructNumber); 171
179 172 size_t index = 0;
180 size_t index = 0; 173 for (OwnPtrVector::iterator iter = vector.begin(); iter != vector.end();
181 for (OwnPtrVector::iterator iter = vector.begin(); iter != vector.end(); ++i ter) { 174 ++iter) {
182 OwnPtr<DestructCounter>* refCounter = iter; 175 OwnPtr<DestructCounter>* refCounter = iter;
183 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()->get())); 176 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()->get()));
184 EXPECT_EQ(index, static_cast<size_t>((*refCounter)->get())); 177 EXPECT_EQ(index, static_cast<size_t>((*refCounter)->get()));
185 index++; 178 index++;
179 }
180 EXPECT_EQ(0, destructNumber);
181
182 for (index = 0; index < vector.size(); index++) {
183 OwnPtr<DestructCounter>& refCounter = vector[index];
184 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()));
185 }
186 EXPECT_EQ(0, destructNumber);
187
188 EXPECT_EQ(0, vector[0]->get());
189 EXPECT_EQ(1, vector[1]->get());
190 vector.remove(0);
191 EXPECT_EQ(1, vector[0]->get());
192 EXPECT_EQ(1u, vector.size());
193 EXPECT_EQ(1, destructNumber);
194
195 OwnPtr<DestructCounter> ownCounter1 = vector[0].release();
196 vector.remove(0);
197 ASSERT_EQ(counter1, ownCounter1->get());
198 ASSERT_EQ(0u, vector.size());
199 ASSERT_EQ(1, destructNumber);
200
201 ownCounter1.clear();
202 EXPECT_EQ(2, destructNumber);
203
204 size_t count = 1025;
205 destructNumber = 0;
206 for (size_t i = 0; i < count; i++)
207 vector.prepend(adoptPtr(new DestructCounter(i, &destructNumber)));
208
209 // Vector relocation must not destruct OwnPtr element.
210 EXPECT_EQ(0, destructNumber);
211 EXPECT_EQ(count, vector.size());
212
213 OwnPtrVector copyVector;
214 vector.swap(copyVector);
215 EXPECT_EQ(0, destructNumber);
216 EXPECT_EQ(count, copyVector.size());
217 EXPECT_EQ(0u, vector.size());
218
219 copyVector.clear();
220 EXPECT_EQ(count, static_cast<size_t>(destructNumber));
221 }
222
223 class MoveOnly {
224 public:
225 explicit MoveOnly(int i = 0) : m_i(i) {}
226
227 MoveOnly(MoveOnly&& other) : m_i(other.m_i) { other.m_i = 0; }
228
229 MoveOnly& operator=(MoveOnly&& other) {
230 if (this != &other) {
231 m_i = other.m_i;
232 other.m_i = 0;
186 } 233 }
187 EXPECT_EQ(0, destructNumber); 234 return *this;
188 235 }
189 for (index = 0; index < vector.size(); index++) { 236
190 OwnPtr<DestructCounter>& refCounter = vector[index]; 237 int value() const { return m_i; }
191 EXPECT_EQ(index, static_cast<size_t>(refCounter->get())); 238
192 } 239 private:
193 EXPECT_EQ(0, destructNumber); 240 WTF_MAKE_NONCOPYABLE(MoveOnly);
194 241 int m_i;
195 EXPECT_EQ(0, vector[0]->get());
196 EXPECT_EQ(1, vector[1]->get());
197 vector.remove(0);
198 EXPECT_EQ(1, vector[0]->get());
199 EXPECT_EQ(1u, vector.size());
200 EXPECT_EQ(1, destructNumber);
201
202 OwnPtr<DestructCounter> ownCounter1 = vector[0].release();
203 vector.remove(0);
204 ASSERT_EQ(counter1, ownCounter1->get());
205 ASSERT_EQ(0u, vector.size());
206 ASSERT_EQ(1, destructNumber);
207
208 ownCounter1.clear();
209 EXPECT_EQ(2, destructNumber);
210
211 size_t count = 1025;
212 destructNumber = 0;
213 for (size_t i = 0; i < count; i++)
214 vector.prepend(adoptPtr(new DestructCounter(i, &destructNumber)));
215
216 // Vector relocation must not destruct OwnPtr element.
217 EXPECT_EQ(0, destructNumber);
218 EXPECT_EQ(count, vector.size());
219
220 OwnPtrVector copyVector;
221 vector.swap(copyVector);
222 EXPECT_EQ(0, destructNumber);
223 EXPECT_EQ(count, copyVector.size());
224 EXPECT_EQ(0u, vector.size());
225
226 copyVector.clear();
227 EXPECT_EQ(count, static_cast<size_t>(destructNumber));
228 }
229
230 class MoveOnly {
231 public:
232 explicit MoveOnly(int i = 0)
233 : m_i(i)
234 { }
235
236 MoveOnly(MoveOnly&& other)
237 : m_i(other.m_i)
238 {
239 other.m_i = 0;
240 }
241
242 MoveOnly& operator=(MoveOnly&& other)
243 {
244 if (this != &other) {
245 m_i = other.m_i;
246 other.m_i = 0;
247 }
248 return *this;
249 }
250
251 int value() const { return m_i; }
252
253 private:
254 WTF_MAKE_NONCOPYABLE(MoveOnly);
255 int m_i;
256 }; 242 };
257 243
258 TEST(VectorTest, MoveOnlyType) 244 TEST(VectorTest, MoveOnlyType) {
259 { 245 WTF::Vector<MoveOnly> vector;
260 WTF::Vector<MoveOnly> vector; 246 vector.append(MoveOnly(1));
261 vector.append(MoveOnly(1)); 247 vector.append(MoveOnly(2));
262 vector.append(MoveOnly(2)); 248 EXPECT_EQ(2u, vector.size());
263 EXPECT_EQ(2u, vector.size()); 249
264 250 ASSERT_EQ(1, vector.first().value());
265 ASSERT_EQ(1, vector.first().value()); 251 ASSERT_EQ(2, vector.last().value());
266 ASSERT_EQ(2, vector.last().value()); 252
267 253 vector.remove(0);
268 vector.remove(0); 254 EXPECT_EQ(2, vector[0].value());
269 EXPECT_EQ(2, vector[0].value()); 255 EXPECT_EQ(1u, vector.size());
270 EXPECT_EQ(1u, vector.size()); 256
271 257 MoveOnly moveOnly(std::move(vector[0]));
272 MoveOnly moveOnly(std::move(vector[0])); 258 vector.remove(0);
273 vector.remove(0); 259 ASSERT_EQ(2, moveOnly.value());
274 ASSERT_EQ(2, moveOnly.value()); 260 ASSERT_EQ(0u, vector.size());
275 ASSERT_EQ(0u, vector.size()); 261
276 262 size_t count = vector.capacity() + 1;
277 size_t count = vector.capacity() + 1; 263 for (size_t i = 0; i < count; i++)
278 for (size_t i = 0; i < count; i++) 264 vector.append(
279 vector.append(MoveOnly(i + 1)); // +1 to distinguish from default-constr ucted. 265 MoveOnly(i + 1)); // +1 to distinguish from default-constructed.
280 266
281 // Reallocation did not affect the vector's content. 267 // Reallocation did not affect the vector's content.
282 EXPECT_EQ(count, vector.size()); 268 EXPECT_EQ(count, vector.size());
283 for (size_t i = 0; i < vector.size(); i++) 269 for (size_t i = 0; i < vector.size(); i++)
284 EXPECT_EQ(static_cast<int>(i + 1), vector[i].value()); 270 EXPECT_EQ(static_cast<int>(i + 1), vector[i].value());
285 271
286 WTF::Vector<MoveOnly> otherVector; 272 WTF::Vector<MoveOnly> otherVector;
287 vector.swap(otherVector); 273 vector.swap(otherVector);
288 EXPECT_EQ(count, otherVector.size()); 274 EXPECT_EQ(count, otherVector.size());
289 EXPECT_EQ(0u, vector.size()); 275 EXPECT_EQ(0u, vector.size());
290 276
291 vector = std::move(otherVector); 277 vector = std::move(otherVector);
292 EXPECT_EQ(count, vector.size()); 278 EXPECT_EQ(count, vector.size());
293 } 279 }
294 280
295 // WrappedInt class will fail if it was memmoved or memcpyed. 281 // WrappedInt class will fail if it was memmoved or memcpyed.
296 static HashSet<void*> constructedWrappedInts; 282 static HashSet<void*> constructedWrappedInts;
297 class WrappedInt { 283 class WrappedInt {
298 public: 284 public:
299 WrappedInt(int i = 0) 285 WrappedInt(int i = 0) : m_originalThisPtr(this), m_i(i) {
300 : m_originalThisPtr(this) 286 constructedWrappedInts.add(this);
301 , m_i(i) 287 }
302 { 288
303 constructedWrappedInts.add(this); 289 WrappedInt(const WrappedInt& other)
304 } 290 : m_originalThisPtr(this), m_i(other.m_i) {
305 291 constructedWrappedInts.add(this);
306 WrappedInt(const WrappedInt& other) 292 }
307 : m_originalThisPtr(this) 293
308 , m_i(other.m_i) 294 WrappedInt& operator=(const WrappedInt& other) {
309 { 295 m_i = other.m_i;
310 constructedWrappedInts.add(this); 296 return *this;
311 } 297 }
312 298
313 WrappedInt& operator=(const WrappedInt& other) 299 ~WrappedInt() {
314 { 300 EXPECT_EQ(m_originalThisPtr, this);
315 m_i = other.m_i; 301 EXPECT_TRUE(constructedWrappedInts.contains(this));
316 return *this; 302 constructedWrappedInts.remove(this);
317 } 303 }
318 304
319 ~WrappedInt() 305 int get() const { return m_i; }
320 { 306
321 EXPECT_EQ(m_originalThisPtr, this); 307 private:
322 EXPECT_TRUE(constructedWrappedInts.contains(this)); 308 void* m_originalThisPtr;
323 constructedWrappedInts.remove(this); 309 int m_i;
324 }
325
326 int get() const { return m_i; }
327
328 private:
329 void* m_originalThisPtr;
330 int m_i;
331 }; 310 };
332 311
333 TEST(VectorTest, SwapWithInlineCapacity) 312 TEST(VectorTest, SwapWithInlineCapacity) {
334 { 313 const size_t inlineCapacity = 2;
335 const size_t inlineCapacity = 2; 314 Vector<WrappedInt, inlineCapacity> vectorA;
336 Vector<WrappedInt, inlineCapacity> vectorA; 315 vectorA.append(WrappedInt(1));
337 vectorA.append(WrappedInt(1)); 316 Vector<WrappedInt, inlineCapacity> vectorB;
338 Vector<WrappedInt, inlineCapacity> vectorB; 317 vectorB.append(WrappedInt(2));
339 vectorB.append(WrappedInt(2)); 318
340 319 EXPECT_EQ(vectorA.size(), vectorB.size());
341 EXPECT_EQ(vectorA.size(), vectorB.size()); 320 vectorA.swap(vectorB);
342 vectorA.swap(vectorB); 321
343 322 EXPECT_EQ(1u, vectorA.size());
344 EXPECT_EQ(1u, vectorA.size()); 323 EXPECT_EQ(2, vectorA.at(0).get());
345 EXPECT_EQ(2, vectorA.at(0).get()); 324 EXPECT_EQ(1u, vectorB.size());
346 EXPECT_EQ(1u, vectorB.size()); 325 EXPECT_EQ(1, vectorB.at(0).get());
347 EXPECT_EQ(1, vectorB.at(0).get()); 326
348 327 vectorA.append(WrappedInt(3));
349 vectorA.append(WrappedInt(3)); 328
350 329 EXPECT_GT(vectorA.size(), vectorB.size());
351 EXPECT_GT(vectorA.size(), vectorB.size()); 330 vectorA.swap(vectorB);
352 vectorA.swap(vectorB); 331
353 332 EXPECT_EQ(1u, vectorA.size());
354 EXPECT_EQ(1u, vectorA.size()); 333 EXPECT_EQ(1, vectorA.at(0).get());
355 EXPECT_EQ(1, vectorA.at(0).get()); 334 EXPECT_EQ(2u, vectorB.size());
356 EXPECT_EQ(2u, vectorB.size()); 335 EXPECT_EQ(2, vectorB.at(0).get());
357 EXPECT_EQ(2, vectorB.at(0).get()); 336 EXPECT_EQ(3, vectorB.at(1).get());
358 EXPECT_EQ(3, vectorB.at(1).get()); 337
359 338 EXPECT_LT(vectorA.size(), vectorB.size());
360 EXPECT_LT(vectorA.size(), vectorB.size()); 339 vectorA.swap(vectorB);
361 vectorA.swap(vectorB); 340
362 341 EXPECT_EQ(2u, vectorA.size());
363 EXPECT_EQ(2u, vectorA.size()); 342 EXPECT_EQ(2, vectorA.at(0).get());
364 EXPECT_EQ(2, vectorA.at(0).get()); 343 EXPECT_EQ(3, vectorA.at(1).get());
365 EXPECT_EQ(3, vectorA.at(1).get()); 344 EXPECT_EQ(1u, vectorB.size());
366 EXPECT_EQ(1u, vectorB.size()); 345 EXPECT_EQ(1, vectorB.at(0).get());
367 EXPECT_EQ(1, vectorB.at(0).get()); 346
368 347 vectorA.append(WrappedInt(4));
369 vectorA.append(WrappedInt(4)); 348 EXPECT_GT(vectorA.size(), inlineCapacity);
370 EXPECT_GT(vectorA.size(), inlineCapacity); 349 vectorA.swap(vectorB);
371 vectorA.swap(vectorB); 350
372 351 EXPECT_EQ(1u, vectorA.size());
373 EXPECT_EQ(1u, vectorA.size()); 352 EXPECT_EQ(1, vectorA.at(0).get());
374 EXPECT_EQ(1, vectorA.at(0).get()); 353 EXPECT_EQ(3u, vectorB.size());
375 EXPECT_EQ(3u, vectorB.size()); 354 EXPECT_EQ(2, vectorB.at(0).get());
376 EXPECT_EQ(2, vectorB.at(0).get()); 355 EXPECT_EQ(3, vectorB.at(1).get());
377 EXPECT_EQ(3, vectorB.at(1).get()); 356 EXPECT_EQ(4, vectorB.at(2).get());
378 EXPECT_EQ(4, vectorB.at(2).get()); 357
379 358 vectorB.swap(vectorA);
380 vectorB.swap(vectorA);
381 } 359 }
382 360
383 #if defined(ANNOTATE_CONTIGUOUS_CONTAINER) 361 #if defined(ANNOTATE_CONTIGUOUS_CONTAINER)
384 TEST(VectorTest, ContainerAnnotations) 362 TEST(VectorTest, ContainerAnnotations) {
385 { 363 Vector<int> vectorA;
386 Vector<int> vectorA; 364 vectorA.append(10);
387 vectorA.append(10); 365 vectorA.reserveCapacity(32);
388 vectorA.reserveCapacity(32); 366
389 367 volatile int* intPointerA = vectorA.data();
390 volatile int* intPointerA = vectorA.data(); 368 EXPECT_DEATH(intPointerA[1] = 11, "container-overflow");
391 EXPECT_DEATH(intPointerA[1] = 11, "container-overflow"); 369 vectorA.append(11);
392 vectorA.append(11); 370 intPointerA[1] = 11;
393 intPointerA[1] = 11; 371 EXPECT_DEATH(intPointerA[2] = 12, "container-overflow");
394 EXPECT_DEATH(intPointerA[2] = 12, "container-overflow"); 372 EXPECT_DEATH((void)intPointerA[2], "container-overflow");
395 EXPECT_DEATH((void)intPointerA[2], "container-overflow"); 373 vectorA.shrinkToFit();
396 vectorA.shrinkToFit(); 374 vectorA.reserveCapacity(16);
397 vectorA.reserveCapacity(16); 375 intPointerA = vectorA.data();
398 intPointerA = vectorA.data(); 376 EXPECT_DEATH((void)intPointerA[2], "container-overflow");
399 EXPECT_DEATH((void)intPointerA[2], "container-overflow"); 377
400 378 Vector<int> vectorB(vectorA);
401 Vector<int> vectorB(vectorA); 379 vectorB.reserveCapacity(16);
402 vectorB.reserveCapacity(16); 380 volatile int* intPointerB = vectorB.data();
403 volatile int* intPointerB = vectorB.data(); 381 EXPECT_DEATH((void)intPointerB[2], "container-overflow");
404 EXPECT_DEATH((void)intPointerB[2], "container-overflow"); 382
405 383 Vector<int> vectorC((Vector<int>(vectorA)));
406 Vector<int> vectorC((Vector<int>(vectorA))); 384 volatile int* intPointerC = vectorC.data();
407 volatile int* intPointerC = vectorC.data(); 385 EXPECT_DEATH((void)intPointerC[2], "container-overflow");
408 EXPECT_DEATH((void)intPointerC[2], "container-overflow"); 386 vectorC.append(13);
409 vectorC.append(13); 387 vectorC.swap(vectorB);
410 vectorC.swap(vectorB); 388
411 389 volatile int* intPointerB2 = vectorB.data();
412 volatile int* intPointerB2 = vectorB.data(); 390 volatile int* intPointerC2 = vectorC.data();
413 volatile int* intPointerC2 = vectorC.data(); 391 intPointerB2[2] = 13;
414 intPointerB2[2] = 13; 392 EXPECT_DEATH((void)intPointerB2[3], "container-overflow");
415 EXPECT_DEATH((void)intPointerB2[3], "container-overflow"); 393 EXPECT_DEATH((void)intPointerC2[2], "container-overflow");
416 EXPECT_DEATH((void)intPointerC2[2], "container-overflow"); 394
417 395 vectorB = vectorC;
418 vectorB = vectorC; 396 volatile int* intPointerB3 = vectorB.data();
419 volatile int* intPointerB3 = vectorB.data(); 397 EXPECT_DEATH((void)intPointerB3[2], "container-overflow");
420 EXPECT_DEATH((void)intPointerB3[2], "container-overflow"); 398 }
421 } 399 #endif // defined(ANNOTATE_CONTIGUOUS_CONTAINER)
422 #endif // defined(ANNOTATE_CONTIGUOUS_CONTAINER) 400
423 401 class Comparable {};
424 class Comparable { 402 bool operator==(const Comparable& a, const Comparable& b) {
425 }; 403 return true;
426 bool operator==(const Comparable& a, const Comparable& b) { return true; } 404 }
427 405
428 template<typename T> void compare() 406 template <typename T>
429 { 407 void compare() {
430 EXPECT_TRUE(Vector<T>() == Vector<T>()); 408 EXPECT_TRUE(Vector<T>() == Vector<T>());
431 EXPECT_FALSE(Vector<T>(1) == Vector<T>(0)); 409 EXPECT_FALSE(Vector<T>(1) == Vector<T>(0));
432 EXPECT_FALSE(Vector<T>() == Vector<T>(1)); 410 EXPECT_FALSE(Vector<T>() == Vector<T>(1));
433 EXPECT_TRUE(Vector<T>(1) == Vector<T>(1)); 411 EXPECT_TRUE(Vector<T>(1) == Vector<T>(1));
434 412
435 Vector<T, 1> vectorWithInlineCapacity; 413 Vector<T, 1> vectorWithInlineCapacity;
436 EXPECT_TRUE(vectorWithInlineCapacity == Vector<T>()); 414 EXPECT_TRUE(vectorWithInlineCapacity == Vector<T>());
437 EXPECT_FALSE(vectorWithInlineCapacity == Vector<T>(1)); 415 EXPECT_FALSE(vectorWithInlineCapacity == Vector<T>(1));
438 } 416 }
439 417
440 TEST(VectorTest, Compare) 418 TEST(VectorTest, Compare) {
441 { 419 compare<int>();
442 compare<int>(); 420 compare<Comparable>();
443 compare<Comparable>(); 421 compare<WTF::String>();
444 compare<WTF::String>(); 422 }
445 } 423
446 424 TEST(VectorTest, AppendFirst) {
447 TEST(VectorTest, AppendFirst) 425 Vector<WTF::String> vector;
448 { 426 vector.append("string");
449 Vector<WTF::String> vector; 427 // Test passes if it does not crash (reallocation did not make
450 vector.append("string"); 428 // the input reference stale).
451 // Test passes if it does not crash (reallocation did not make 429 size_t limit = vector.capacity() + 1;
452 // the input reference stale). 430 for (size_t i = 0; i < limit; i++)
453 size_t limit = vector.capacity() + 1; 431 vector.append(vector.first());
454 for (size_t i = 0; i < limit; i++) 432
455 vector.append(vector.first()); 433 limit = vector.capacity() + 1;
456 434 for (size_t i = 0; i < limit; i++)
457 limit = vector.capacity() + 1; 435 vector.append(const_cast<const WTF::String&>(vector.first()));
458 for (size_t i = 0; i < limit; i++) 436 }
459 vector.append(const_cast<const WTF::String&>(vector.first())); 437
460 } 438 } // anonymous namespace
461 439
462 } // anonymous namespace 440 } // namespace WTF
463
464 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/Vector.h ('k') | third_party/WebKit/Source/wtf/VectorTraits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698