OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/base/list_container.h" | |
6 | |
7 #include <vector> | |
8 #include "cc/quads/draw_quad.h" | |
9 #include "cc/quads/largest_draw_quad.h" | |
10 #include "cc/quads/render_pass_draw_quad.h" | |
11 #include "cc/quads/shared_quad_state.h" | |
12 #include "cc/quads/stream_video_draw_quad.h" | |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 // TODO(jbroman): This unit test should be made generic and moved to cc/base/. | |
17 | |
18 namespace cc { | |
19 namespace { | |
20 | |
21 int kMagicNumberToUseForDrawQuadOne = 42; | |
22 int kMagicNumberToUseForDrawQuadTwo = 314; | |
23 | |
24 bool isConstSharedQuadStatePointer(const SharedQuadState* ptr) { | |
25 return true; | |
26 } | |
27 | |
28 bool isConstSharedQuadStatePointer(SharedQuadState* ptr) { | |
29 return false; | |
30 } | |
31 | |
32 class SimpleDrawQuad : public DrawQuad { | |
33 public: | |
34 ~SimpleDrawQuad() override {} | |
35 void IterateResources(const ResourceIteratorCallback& callback) override {} | |
36 | |
37 void set_value(int val) { value = val; } | |
38 int get_value() { return value; } | |
39 void ExtendValue(base::trace_event::TracedValue* value) const override {} | |
40 | |
41 private: | |
42 int value; | |
43 }; | |
44 | |
45 class SimpleDrawQuadConstructMagicNumberOne : public SimpleDrawQuad { | |
46 public: | |
47 SimpleDrawQuadConstructMagicNumberOne() : SimpleDrawQuad() { | |
48 set_value(kMagicNumberToUseForDrawQuadOne); | |
49 } | |
50 }; | |
51 | |
52 class SimpleDrawQuadConstructMagicNumberTwo : public SimpleDrawQuad { | |
53 public: | |
54 SimpleDrawQuadConstructMagicNumberTwo() : SimpleDrawQuad() { | |
55 set_value(kMagicNumberToUseForDrawQuadTwo); | |
56 } | |
57 }; | |
58 | |
59 class MockDrawQuad : public SimpleDrawQuadConstructMagicNumberOne { | |
60 public: | |
61 ~MockDrawQuad() override { Destruct(); } | |
62 MOCK_METHOD0(Destruct, void()); | |
63 }; | |
64 | |
65 class MockDrawQuadSubclass : public MockDrawQuad { | |
66 public: | |
67 MockDrawQuadSubclass() { set_value(kMagicNumberToUseForDrawQuadTwo); } | |
68 }; | |
69 | |
70 const size_t kLargestQuadSize = | |
71 std::max(LargestDrawQuadSize(), sizeof(MockDrawQuadSubclass)); | |
72 | |
73 TEST(ListContainerTest, ConstructorCalledInAllocateAndConstruct) { | |
74 ListContainer<DrawQuad> list(kLargestQuadSize); | |
75 | |
76 size_t size = 2; | |
77 SimpleDrawQuadConstructMagicNumberOne* dq_1 = | |
78 list.AllocateAndConstruct<SimpleDrawQuadConstructMagicNumberOne>(); | |
79 SimpleDrawQuadConstructMagicNumberTwo* dq_2 = | |
80 list.AllocateAndConstruct<SimpleDrawQuadConstructMagicNumberTwo>(); | |
81 | |
82 EXPECT_EQ(size, list.size()); | |
83 EXPECT_EQ(dq_1, list.front()); | |
84 EXPECT_EQ(dq_2, list.back()); | |
85 | |
86 EXPECT_EQ(kMagicNumberToUseForDrawQuadOne, dq_1->get_value()); | |
87 EXPECT_EQ(kMagicNumberToUseForDrawQuadTwo, dq_2->get_value()); | |
88 } | |
89 | |
90 TEST(ListContainerTest, DestructorCalled) { | |
91 ListContainer<DrawQuad> list(kLargestQuadSize); | |
92 | |
93 size_t size = 1; | |
94 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>(); | |
95 | |
96 EXPECT_CALL(*dq_1, Destruct()); | |
97 EXPECT_EQ(size, list.size()); | |
98 EXPECT_EQ(dq_1, list.front()); | |
99 } | |
100 | |
101 TEST(ListContainerTest, DestructorCalledOnceWhenClear) { | |
102 ListContainer<DrawQuad> list(kLargestQuadSize); | |
103 size_t size = 1; | |
104 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>(); | |
105 | |
106 EXPECT_EQ(size, list.size()); | |
107 EXPECT_EQ(dq_1, list.front()); | |
108 | |
109 // Make sure destructor is called once during clear, and won't be called | |
110 // again. | |
111 testing::MockFunction<void()> separator; | |
112 { | |
113 testing::InSequence s; | |
114 EXPECT_CALL(*dq_1, Destruct()); | |
115 EXPECT_CALL(separator, Call()); | |
116 EXPECT_CALL(*dq_1, Destruct()).Times(0); | |
117 } | |
118 | |
119 list.clear(); | |
120 separator.Call(); | |
121 } | |
122 | |
123 TEST(ListContainerTest, ReplaceExistingElement) { | |
124 ListContainer<DrawQuad> list(kLargestQuadSize); | |
125 size_t size = 1; | |
126 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>(); | |
127 | |
128 EXPECT_EQ(size, list.size()); | |
129 EXPECT_EQ(dq_1, list.front()); | |
130 | |
131 // Make sure destructor is called once during clear, and won't be called | |
132 // again. | |
133 testing::MockFunction<void()> separator; | |
134 { | |
135 testing::InSequence s; | |
136 EXPECT_CALL(*dq_1, Destruct()); | |
137 EXPECT_CALL(separator, Call()); | |
138 EXPECT_CALL(*dq_1, Destruct()).Times(0); | |
139 } | |
140 | |
141 list.ReplaceExistingElement<MockDrawQuadSubclass>(list.begin()); | |
142 EXPECT_EQ(kMagicNumberToUseForDrawQuadTwo, dq_1->get_value()); | |
143 separator.Call(); | |
144 | |
145 EXPECT_CALL(*dq_1, Destruct()); | |
146 list.clear(); | |
147 } | |
148 | |
149 TEST(ListContainerTest, DestructorCalledOnceWhenErase) { | |
150 ListContainer<DrawQuad> list(kLargestQuadSize); | |
151 size_t size = 1; | |
152 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>(); | |
153 | |
154 EXPECT_EQ(size, list.size()); | |
155 EXPECT_EQ(dq_1, list.front()); | |
156 | |
157 // Make sure destructor is called once during clear, and won't be called | |
158 // again. | |
159 testing::MockFunction<void()> separator; | |
160 { | |
161 testing::InSequence s; | |
162 EXPECT_CALL(*dq_1, Destruct()); | |
163 EXPECT_CALL(separator, Call()); | |
164 EXPECT_CALL(*dq_1, Destruct()).Times(0); | |
165 } | |
166 | |
167 list.EraseAndInvalidateAllPointers(list.begin()); | |
168 separator.Call(); | |
169 } | |
170 | |
171 TEST(ListContainerTest, SimpleIndexAccessSharedQuadState) { | |
172 ListContainer<SharedQuadState> list; | |
173 | |
174 size_t size = 3; | |
175 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>(); | |
176 SharedQuadState* sqs_2 = list.AllocateAndConstruct<SharedQuadState>(); | |
177 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>(); | |
178 | |
179 EXPECT_EQ(size, list.size()); | |
180 EXPECT_EQ(sqs_1, list.front()); | |
181 EXPECT_EQ(sqs_3, list.back()); | |
182 EXPECT_EQ(list.front(), list.ElementAt(0)); | |
183 EXPECT_EQ(sqs_2, list.ElementAt(1)); | |
184 EXPECT_EQ(list.back(), list.ElementAt(2)); | |
185 } | |
186 | |
187 TEST(ListContainerTest, SimpleInsertionSharedQuadState) { | |
188 ListContainer<SharedQuadState> list; | |
189 | |
190 size_t size = 3; | |
191 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>(); | |
192 list.AllocateAndConstruct<SharedQuadState>(); | |
193 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>(); | |
194 | |
195 EXPECT_EQ(size, list.size()); | |
196 EXPECT_EQ(sqs_1, list.front()); | |
197 EXPECT_EQ(sqs_3, list.back()); | |
198 } | |
199 | |
200 TEST(ListContainerTest, SimpleInsertionAndClearSharedQuadState) { | |
201 ListContainer<SharedQuadState> list; | |
202 EXPECT_TRUE(list.empty()); | |
203 EXPECT_EQ(0u, list.size()); | |
204 | |
205 size_t size = 3; | |
206 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>(); | |
207 list.AllocateAndConstruct<SharedQuadState>(); | |
208 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>(); | |
209 | |
210 EXPECT_EQ(size, list.size()); | |
211 EXPECT_EQ(sqs_1, list.front()); | |
212 EXPECT_EQ(sqs_3, list.back()); | |
213 EXPECT_FALSE(list.empty()); | |
214 | |
215 list.clear(); | |
216 EXPECT_TRUE(list.empty()); | |
217 EXPECT_EQ(0u, list.size()); | |
218 } | |
219 | |
220 TEST(ListContainerTest, SimpleInsertionClearAndInsertAgainSharedQuadState) { | |
221 ListContainer<SharedQuadState> list; | |
222 EXPECT_TRUE(list.empty()); | |
223 EXPECT_EQ(0u, list.size()); | |
224 | |
225 size_t size = 2; | |
226 SharedQuadState* sqs_front = list.AllocateAndConstruct<SharedQuadState>(); | |
227 SharedQuadState* sqs_back = list.AllocateAndConstruct<SharedQuadState>(); | |
228 | |
229 EXPECT_EQ(size, list.size()); | |
230 EXPECT_EQ(sqs_front, list.front()); | |
231 EXPECT_EQ(sqs_back, list.back()); | |
232 EXPECT_FALSE(list.empty()); | |
233 | |
234 list.clear(); | |
235 EXPECT_TRUE(list.empty()); | |
236 EXPECT_EQ(0u, list.size()); | |
237 | |
238 size = 3; | |
239 sqs_front = list.AllocateAndConstruct<SharedQuadState>(); | |
240 list.AllocateAndConstruct<SharedQuadState>(); | |
241 sqs_back = list.AllocateAndConstruct<SharedQuadState>(); | |
242 | |
243 EXPECT_EQ(size, list.size()); | |
244 EXPECT_EQ(sqs_front, list.front()); | |
245 EXPECT_EQ(sqs_back, list.back()); | |
246 EXPECT_FALSE(list.empty()); | |
247 } | |
248 | |
249 // This test is used to test when there is more than one allocation needed | |
250 // for, ListContainer can still perform like normal vector. | |
251 TEST(ListContainerTest, | |
252 SimpleInsertionTriggerMoreThanOneAllocationSharedQuadState) { | |
253 ListContainer<SharedQuadState> list(sizeof(SharedQuadState), 2); | |
254 std::vector<SharedQuadState*> sqs_list; | |
255 size_t size = 10; | |
256 for (size_t i = 0; i < size; ++i) { | |
257 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>()); | |
258 } | |
259 EXPECT_EQ(size, list.size()); | |
260 | |
261 ListContainer<SharedQuadState>::Iterator iter = list.begin(); | |
262 for (std::vector<SharedQuadState*>::const_iterator sqs_iter = | |
263 sqs_list.begin(); | |
264 sqs_iter != sqs_list.end(); ++sqs_iter) { | |
265 EXPECT_EQ(*sqs_iter, *iter); | |
266 ++iter; | |
267 } | |
268 } | |
269 | |
270 TEST(ListContainerTest, | |
271 CorrectAllocationSizeForMoreThanOneAllocationSharedQuadState) { | |
272 // Constructor sets the allocation size to 2. Every time ListContainer needs | |
273 // to allocate again, it doubles allocation size. In this test, 10 elements is | |
274 // needed, thus ListContainerShould allocate spaces 2, 4 and 8 elements. | |
275 ListContainer<SharedQuadState> list(sizeof(SharedQuadState), 2); | |
276 std::vector<SharedQuadState*> sqs_list; | |
277 size_t size = 10; | |
278 for (size_t i = 0; i < size; ++i) { | |
279 // Before asking for a new element, space available without another | |
280 // allocation follows. | |
281 switch (i) { | |
282 case 2: | |
283 case 6: | |
284 EXPECT_EQ(0u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
285 break; | |
286 case 1: | |
287 case 5: | |
288 EXPECT_EQ(1u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
289 break; | |
290 case 0: | |
291 case 4: | |
292 EXPECT_EQ(2u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
293 break; | |
294 case 3: | |
295 EXPECT_EQ(3u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
296 break; | |
297 case 9: | |
298 EXPECT_EQ(5u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
299 break; | |
300 case 8: | |
301 EXPECT_EQ(6u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
302 break; | |
303 case 7: | |
304 EXPECT_EQ(7u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
305 break; | |
306 default: | |
307 break; | |
308 } | |
309 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>()); | |
310 // After asking for a new element, space available without another | |
311 // allocation follows. | |
312 switch (i) { | |
313 case 1: | |
314 case 5: | |
315 EXPECT_EQ(0u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
316 break; | |
317 case 0: | |
318 case 4: | |
319 EXPECT_EQ(1u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
320 break; | |
321 case 3: | |
322 EXPECT_EQ(2u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
323 break; | |
324 case 2: | |
325 EXPECT_EQ(3u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
326 break; | |
327 case 9: | |
328 EXPECT_EQ(4u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
329 break; | |
330 case 8: | |
331 EXPECT_EQ(5u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
332 break; | |
333 case 7: | |
334 EXPECT_EQ(6u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
335 break; | |
336 case 6: | |
337 EXPECT_EQ(7u, list.AvailableSizeWithoutAnotherAllocationForTesting()); | |
338 break; | |
339 default: | |
340 break; | |
341 } | |
342 } | |
343 EXPECT_EQ(size, list.size()); | |
344 | |
345 ListContainer<SharedQuadState>::Iterator iter = list.begin(); | |
346 for (std::vector<SharedQuadState*>::const_iterator sqs_iter = | |
347 sqs_list.begin(); | |
348 sqs_iter != sqs_list.end(); ++sqs_iter) { | |
349 EXPECT_EQ(*sqs_iter, *iter); | |
350 ++iter; | |
351 } | |
352 } | |
353 | |
354 TEST(ListContainerTest, SimpleIterationSharedQuadState) { | |
355 ListContainer<SharedQuadState> list; | |
356 std::vector<SharedQuadState*> sqs_list; | |
357 size_t size = 10; | |
358 for (size_t i = 0; i < size; ++i) { | |
359 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>()); | |
360 } | |
361 EXPECT_EQ(size, list.size()); | |
362 | |
363 size_t num_iters_in_list = 0; | |
364 { | |
365 std::vector<SharedQuadState*>::const_iterator sqs_iter = sqs_list.begin(); | |
366 for (ListContainer<SharedQuadState>::Iterator iter = list.begin(); | |
367 iter != list.end(); ++iter) { | |
368 EXPECT_EQ(*sqs_iter, *iter); | |
369 ++num_iters_in_list; | |
370 ++sqs_iter; | |
371 } | |
372 } | |
373 | |
374 size_t num_iters_in_vector = 0; | |
375 { | |
376 ListContainer<SharedQuadState>::Iterator iter = list.begin(); | |
377 for (std::vector<SharedQuadState*>::const_iterator sqs_iter = | |
378 sqs_list.begin(); | |
379 sqs_iter != sqs_list.end(); ++sqs_iter) { | |
380 EXPECT_EQ(*sqs_iter, *iter); | |
381 ++num_iters_in_vector; | |
382 ++iter; | |
383 } | |
384 } | |
385 | |
386 EXPECT_EQ(num_iters_in_vector, num_iters_in_list); | |
387 } | |
388 | |
389 TEST(ListContainerTest, SimpleConstIteratorIterationSharedQuadState) { | |
390 ListContainer<SharedQuadState> list; | |
391 std::vector<const SharedQuadState*> sqs_list; | |
392 size_t size = 10; | |
393 for (size_t i = 0; i < size; ++i) { | |
394 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>()); | |
395 } | |
396 EXPECT_EQ(size, list.size()); | |
397 | |
398 { | |
399 std::vector<const SharedQuadState*>::const_iterator sqs_iter = | |
400 sqs_list.begin(); | |
401 for (ListContainer<SharedQuadState>::ConstIterator iter = list.begin(); | |
402 iter != list.end(); ++iter) { | |
403 EXPECT_TRUE(isConstSharedQuadStatePointer(*iter)); | |
404 EXPECT_EQ(*sqs_iter, *iter); | |
405 ++sqs_iter; | |
406 } | |
407 } | |
408 | |
409 { | |
410 std::vector<const SharedQuadState*>::const_iterator sqs_iter = | |
411 sqs_list.begin(); | |
412 for (ListContainer<SharedQuadState>::Iterator iter = list.begin(); | |
413 iter != list.end(); ++iter) { | |
414 EXPECT_FALSE(isConstSharedQuadStatePointer(*iter)); | |
415 EXPECT_EQ(*sqs_iter, *iter); | |
416 ++sqs_iter; | |
417 } | |
418 } | |
419 | |
420 { | |
421 ListContainer<SharedQuadState>::ConstIterator iter = list.begin(); | |
422 for (std::vector<const SharedQuadState*>::const_iterator sqs_iter = | |
423 sqs_list.begin(); | |
424 sqs_iter != sqs_list.end(); ++sqs_iter) { | |
425 EXPECT_EQ(*sqs_iter, *iter); | |
426 ++iter; | |
427 } | |
428 } | |
429 } | |
430 | |
431 TEST(ListContainerTest, SimpleReverseInsertionSharedQuadState) { | |
432 ListContainer<SharedQuadState> list; | |
433 std::vector<SharedQuadState*> sqs_list; | |
434 size_t size = 10; | |
435 for (size_t i = 0; i < size; ++i) { | |
436 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>()); | |
437 } | |
438 EXPECT_EQ(size, list.size()); | |
439 | |
440 { | |
441 std::vector<SharedQuadState*>::const_reverse_iterator sqs_iter = | |
442 sqs_list.rbegin(); | |
443 for (ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin(); | |
444 iter != list.rend(); ++iter) { | |
445 EXPECT_EQ(*sqs_iter, *iter); | |
446 ++sqs_iter; | |
447 } | |
448 } | |
449 | |
450 { | |
451 ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin(); | |
452 for (std::vector<SharedQuadState*>::reverse_iterator sqs_iter = | |
453 sqs_list.rbegin(); | |
454 sqs_iter != sqs_list.rend(); ++sqs_iter) { | |
455 EXPECT_EQ(*sqs_iter, *iter); | |
456 ++iter; | |
457 } | |
458 } | |
459 } | |
460 | |
461 TEST(ListContainerTest, SimpleDeletion) { | |
462 ListContainer<DrawQuad> list(kLargestQuadSize); | |
463 std::vector<SimpleDrawQuad*> sdq_list; | |
464 size_t size = 10; | |
465 for (size_t i = 0; i < size; ++i) { | |
466 sdq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>()); | |
467 sdq_list.back()->set_value(i); | |
468 } | |
469 EXPECT_EQ(size, list.size()); | |
470 | |
471 list.EraseAndInvalidateAllPointers(list.begin()); | |
472 --size; | |
473 EXPECT_EQ(size, list.size()); | |
474 int i = 1; | |
475 for (ListContainer<DrawQuad>::Iterator iter = list.begin(); | |
476 iter != list.end(); ++iter) { | |
477 EXPECT_EQ(i, static_cast<SimpleDrawQuad*>(*iter)->get_value()); | |
478 ++i; | |
479 } | |
480 } | |
481 | |
482 TEST(ListContainerTest, DeletionAllInAllocation) { | |
483 const size_t kReserve = 10; | |
484 ListContainer<DrawQuad> list(kLargestQuadSize, kReserve); | |
485 std::vector<SimpleDrawQuad*> sdq_list; | |
486 // Add enough quads to cause another allocation. | |
487 for (size_t i = 0; i < kReserve + 1; ++i) { | |
488 sdq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>()); | |
489 sdq_list.back()->set_value(static_cast<int>(i)); | |
490 } | |
491 EXPECT_EQ(kReserve + 1, list.size()); | |
492 | |
493 // Remove everything in the first allocation. | |
494 for (size_t i = 0; i < kReserve; ++i) | |
495 list.EraseAndInvalidateAllPointers(list.begin()); | |
496 EXPECT_EQ(1u, list.size()); | |
497 | |
498 // The last quad is left. | |
499 SimpleDrawQuad* quad = static_cast<SimpleDrawQuad*>(*list.begin()); | |
500 EXPECT_EQ(static_cast<int>(kReserve), quad->get_value()); | |
501 | |
502 // Remove the quad from the 2nd allocation. | |
503 list.EraseAndInvalidateAllPointers(list.begin()); | |
504 EXPECT_EQ(0u, list.size()); | |
505 } | |
506 | |
507 TEST(ListContainerTest, DeletionAllInAllocationReversed) { | |
508 const size_t kReserve = 10; | |
509 ListContainer<DrawQuad> list(kLargestQuadSize, kReserve); | |
510 std::vector<SimpleDrawQuad*> sdq_list; | |
511 // Add enough quads to cause another allocation. | |
512 for (size_t i = 0; i < kReserve + 1; ++i) { | |
513 sdq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>()); | |
514 sdq_list.back()->set_value(static_cast<int>(i)); | |
515 } | |
516 EXPECT_EQ(kReserve + 1, list.size()); | |
517 | |
518 // Remove everything in the 2nd allocation. | |
519 auto it = list.begin(); | |
520 for (size_t i = 0; i < kReserve; ++i) | |
521 ++it; | |
522 list.EraseAndInvalidateAllPointers(it); | |
523 | |
524 // The 2nd-last quad is next, and the rest of the quads exist. | |
525 size_t i = kReserve - 1; | |
526 for (auto it = list.rbegin(); it != list.rend(); ++it) { | |
527 SimpleDrawQuad* quad = static_cast<SimpleDrawQuad*>(*it); | |
528 EXPECT_EQ(static_cast<int>(i), quad->get_value()); | |
529 --i; | |
530 } | |
531 | |
532 // Can forward iterate too. | |
533 i = 0; | |
534 for (auto it = list.begin(); it != list.end(); ++it) { | |
535 SimpleDrawQuad* quad = static_cast<SimpleDrawQuad*>(*it); | |
536 EXPECT_EQ(static_cast<int>(i), quad->get_value()); | |
537 ++i; | |
538 } | |
539 | |
540 // Remove the last thing from the 1st allocation. | |
541 it = list.begin(); | |
542 for (size_t i = 0; i < kReserve - 1; ++i) | |
543 ++it; | |
544 list.EraseAndInvalidateAllPointers(it); | |
545 | |
546 // The 2nd-last quad is next, and the rest of the quads exist. | |
547 i = kReserve - 2; | |
548 for (auto it = list.rbegin(); it != list.rend(); ++it) { | |
549 SimpleDrawQuad* quad = static_cast<SimpleDrawQuad*>(*it); | |
550 EXPECT_EQ(static_cast<int>(i), quad->get_value()); | |
551 --i; | |
552 } | |
553 | |
554 // Can forward iterate too. | |
555 i = 0; | |
556 for (auto it = list.begin(); it != list.end(); ++it) { | |
557 SimpleDrawQuad* quad = static_cast<SimpleDrawQuad*>(*it); | |
558 EXPECT_EQ(static_cast<int>(i), quad->get_value()); | |
559 ++i; | |
560 } | |
561 } | |
562 | |
563 TEST(ListContainerTest, SimpleIterationAndManipulation) { | |
564 ListContainer<DrawQuad> list(kLargestQuadSize); | |
565 std::vector<SimpleDrawQuad*> sdq_list; | |
566 size_t size = 10; | |
567 for (size_t i = 0; i < size; ++i) { | |
568 SimpleDrawQuad* simple_dq = list.AllocateAndConstruct<SimpleDrawQuad>(); | |
569 sdq_list.push_back(simple_dq); | |
570 } | |
571 EXPECT_EQ(size, list.size()); | |
572 | |
573 ListContainer<DrawQuad>::Iterator iter = list.begin(); | |
574 for (int i = 0; i < 10; ++i) { | |
575 static_cast<SimpleDrawQuad*>(*iter)->set_value(i); | |
576 ++iter; | |
577 } | |
578 | |
579 int i = 0; | |
580 for (std::vector<SimpleDrawQuad*>::const_iterator sdq_iter = sdq_list.begin(); | |
581 sdq_iter < sdq_list.end(); ++sdq_iter) { | |
582 EXPECT_EQ(i, (*sdq_iter)->get_value()); | |
583 ++i; | |
584 } | |
585 } | |
586 | |
587 TEST(ListContainerTest, SimpleManipulationWithIndexSimpleDrawQuad) { | |
588 ListContainer<DrawQuad> list(kLargestQuadSize); | |
589 std::vector<SimpleDrawQuad*> dq_list; | |
590 size_t size = 10; | |
591 for (size_t i = 0; i < size; ++i) { | |
592 dq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>()); | |
593 } | |
594 EXPECT_EQ(size, list.size()); | |
595 | |
596 for (size_t i = 0; i < size; ++i) { | |
597 static_cast<SimpleDrawQuad*>(list.ElementAt(i))->set_value(i); | |
598 } | |
599 | |
600 int i = 0; | |
601 for (std::vector<SimpleDrawQuad*>::const_iterator dq_iter = dq_list.begin(); | |
602 dq_iter != dq_list.end(); ++dq_iter, ++i) { | |
603 EXPECT_EQ(i, (*dq_iter)->get_value()); | |
604 } | |
605 } | |
606 | |
607 TEST(ListContainerTest, | |
608 SimpleManipulationWithIndexMoreThanOneAllocationSimpleDrawQuad) { | |
609 ListContainer<DrawQuad> list(LargestDrawQuadSize(), 2); | |
610 std::vector<SimpleDrawQuad*> dq_list; | |
611 size_t size = 10; | |
612 for (size_t i = 0; i < size; ++i) { | |
613 dq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>()); | |
614 } | |
615 EXPECT_EQ(size, list.size()); | |
616 | |
617 for (size_t i = 0; i < size; ++i) { | |
618 static_cast<SimpleDrawQuad*>(list.ElementAt(i))->set_value(i); | |
619 } | |
620 | |
621 int i = 0; | |
622 for (std::vector<SimpleDrawQuad*>::const_iterator dq_iter = dq_list.begin(); | |
623 dq_iter != dq_list.end(); ++dq_iter, ++i) { | |
624 EXPECT_EQ(i, (*dq_iter)->get_value()); | |
625 } | |
626 } | |
627 | |
628 TEST(ListContainerTest, | |
629 SimpleIterationAndReverseIterationWithIndexSharedQuadState) { | |
630 ListContainer<SharedQuadState> list; | |
631 std::vector<SharedQuadState*> sqs_list; | |
632 size_t size = 10; | |
633 for (size_t i = 0; i < size; ++i) { | |
634 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>()); | |
635 } | |
636 EXPECT_EQ(size, list.size()); | |
637 | |
638 size_t i = 0; | |
639 for (ListContainer<SharedQuadState>::Iterator iter = list.begin(); | |
640 iter != list.end(); ++iter) { | |
641 EXPECT_EQ(i, iter.index()); | |
642 ++i; | |
643 } | |
644 | |
645 i = 0; | |
646 for (ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin(); | |
647 iter != list.rend(); ++iter) { | |
648 EXPECT_EQ(i, iter.index()); | |
649 ++i; | |
650 } | |
651 } | |
652 | |
653 } // namespace | |
654 } // namespace cc | |
OLD | NEW |