Revert of xp: copy dbghelp.dll to output dir and have base.isolate use it there ...
[chromium-blink-merge.git] / cc / quads / list_container_unittest.cc
blobde88750467dd46a86c658361b7d792c4c27cf7d5
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.
5 #include "cc/quads/list_container.h"
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"
16 namespace cc {
17 namespace {
19 int kMagicNumberToUseForDrawQuadOne = 42;
20 int kMagicNumberToUseForDrawQuadTwo = 314;
22 bool isConstSharedQuadStatePointer(const SharedQuadState* ptr) {
23 return true;
26 bool isConstSharedQuadStatePointer(SharedQuadState* ptr) {
27 return false;
30 class SimpleDrawQuad : public DrawQuad {
31 public:
32 ~SimpleDrawQuad() override {}
33 void IterateResources(const ResourceIteratorCallback& callback) override {}
35 void set_value(int val) { value = val; }
36 int get_value() { return value; }
37 void ExtendValue(base::trace_event::TracedValue* value) const override {}
39 private:
40 int value;
43 class SimpleDrawQuadConstructMagicNumberOne : public SimpleDrawQuad {
44 public:
45 SimpleDrawQuadConstructMagicNumberOne() : SimpleDrawQuad() {
46 set_value(kMagicNumberToUseForDrawQuadOne);
50 class SimpleDrawQuadConstructMagicNumberTwo : public SimpleDrawQuad {
51 public:
52 SimpleDrawQuadConstructMagicNumberTwo() : SimpleDrawQuad() {
53 set_value(kMagicNumberToUseForDrawQuadTwo);
57 class MockDrawQuad : public DrawQuad {
58 public:
59 ~MockDrawQuad() override { Destruct(); }
60 void IterateResources(const ResourceIteratorCallback& callback) override {}
61 void ExtendValue(base::trace_event::TracedValue* value) const override {}
62 MOCK_METHOD0(Destruct, void());
65 TEST(ListContainerTest, ConstructorCalledInAllocateAndConstruct) {
66 ListContainer<DrawQuad> list(LargestDrawQuadSize());
68 size_t size = 2;
69 SimpleDrawQuadConstructMagicNumberOne* dq_1 =
70 list.AllocateAndConstruct<SimpleDrawQuadConstructMagicNumberOne>();
71 SimpleDrawQuadConstructMagicNumberTwo* dq_2 =
72 list.AllocateAndConstruct<SimpleDrawQuadConstructMagicNumberTwo>();
74 EXPECT_EQ(size, list.size());
75 EXPECT_EQ(dq_1, list.front());
76 EXPECT_EQ(dq_2, list.back());
78 EXPECT_EQ(kMagicNumberToUseForDrawQuadOne, dq_1->get_value());
79 EXPECT_EQ(kMagicNumberToUseForDrawQuadTwo, dq_2->get_value());
82 TEST(ListContainerTest, DestructorCalled) {
83 ListContainer<DrawQuad> list(LargestDrawQuadSize());
85 size_t size = 1;
86 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
88 EXPECT_CALL(*dq_1, Destruct());
89 EXPECT_EQ(size, list.size());
90 EXPECT_EQ(dq_1, list.front());
93 TEST(ListContainerTest, DestructorCalledOnceWhenClear) {
94 ListContainer<DrawQuad> list(LargestDrawQuadSize());
95 size_t size = 1;
96 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
98 EXPECT_EQ(size, list.size());
99 EXPECT_EQ(dq_1, list.front());
101 // Make sure destructor is called once during clear, and won't be called
102 // again.
103 testing::MockFunction<void()> separator;
105 testing::InSequence s;
106 EXPECT_CALL(*dq_1, Destruct());
107 EXPECT_CALL(separator, Call());
108 EXPECT_CALL(*dq_1, Destruct()).Times(0);
111 list.clear();
112 separator.Call();
115 TEST(ListContainerTest, DestructorCalledOnceWhenErase) {
116 ListContainer<DrawQuad> list(LargestDrawQuadSize());
117 size_t size = 1;
118 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
120 EXPECT_EQ(size, list.size());
121 EXPECT_EQ(dq_1, list.front());
123 // Make sure destructor is called once during clear, and won't be called
124 // again.
125 testing::MockFunction<void()> separator;
127 testing::InSequence s;
128 EXPECT_CALL(*dq_1, Destruct());
129 EXPECT_CALL(separator, Call());
130 EXPECT_CALL(*dq_1, Destruct()).Times(0);
133 list.EraseAndInvalidateAllPointers(list.begin());
134 separator.Call();
137 TEST(ListContainerTest, SimpleIndexAccessSharedQuadState) {
138 ListContainer<SharedQuadState> list;
140 size_t size = 3;
141 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>();
142 SharedQuadState* sqs_2 = list.AllocateAndConstruct<SharedQuadState>();
143 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>();
145 EXPECT_EQ(size, list.size());
146 EXPECT_EQ(sqs_1, list.front());
147 EXPECT_EQ(sqs_3, list.back());
148 EXPECT_EQ(list.front(), list.ElementAt(0));
149 EXPECT_EQ(sqs_2, list.ElementAt(1));
150 EXPECT_EQ(list.back(), list.ElementAt(2));
153 TEST(ListContainerTest, SimpleInsertionSharedQuadState) {
154 ListContainer<SharedQuadState> list;
156 size_t size = 3;
157 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>();
158 list.AllocateAndConstruct<SharedQuadState>();
159 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>();
161 EXPECT_EQ(size, list.size());
162 EXPECT_EQ(sqs_1, list.front());
163 EXPECT_EQ(sqs_3, list.back());
166 TEST(ListContainerTest, SimpleInsertionAndClearSharedQuadState) {
167 ListContainer<SharedQuadState> list;
168 EXPECT_TRUE(list.empty());
169 EXPECT_EQ(0u, list.size());
171 size_t size = 3;
172 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>();
173 list.AllocateAndConstruct<SharedQuadState>();
174 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>();
176 EXPECT_EQ(size, list.size());
177 EXPECT_EQ(sqs_1, list.front());
178 EXPECT_EQ(sqs_3, list.back());
179 EXPECT_FALSE(list.empty());
181 list.clear();
182 EXPECT_TRUE(list.empty());
183 EXPECT_EQ(0u, list.size());
186 TEST(ListContainerTest, SimpleInsertionClearAndInsertAgainSharedQuadState) {
187 ListContainer<SharedQuadState> list;
188 EXPECT_TRUE(list.empty());
189 EXPECT_EQ(0u, list.size());
191 size_t size = 2;
192 SharedQuadState* sqs_front = list.AllocateAndConstruct<SharedQuadState>();
193 SharedQuadState* sqs_back = list.AllocateAndConstruct<SharedQuadState>();
195 EXPECT_EQ(size, list.size());
196 EXPECT_EQ(sqs_front, list.front());
197 EXPECT_EQ(sqs_back, list.back());
198 EXPECT_FALSE(list.empty());
200 list.clear();
201 EXPECT_TRUE(list.empty());
202 EXPECT_EQ(0u, list.size());
204 size = 3;
205 sqs_front = list.AllocateAndConstruct<SharedQuadState>();
206 list.AllocateAndConstruct<SharedQuadState>();
207 sqs_back = list.AllocateAndConstruct<SharedQuadState>();
209 EXPECT_EQ(size, list.size());
210 EXPECT_EQ(sqs_front, list.front());
211 EXPECT_EQ(sqs_back, list.back());
212 EXPECT_FALSE(list.empty());
215 // This test is used to test when there is more than one allocation needed
216 // for, ListContainer can still perform like normal vector.
217 TEST(ListContainerTest,
218 SimpleInsertionTriggerMoreThanOneAllocationSharedQuadState) {
219 ListContainer<SharedQuadState> list(sizeof(SharedQuadState), 2);
220 std::vector<SharedQuadState*> sqs_list;
221 size_t size = 10;
222 for (size_t i = 0; i < size; ++i) {
223 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
225 EXPECT_EQ(size, list.size());
227 ListContainer<SharedQuadState>::Iterator iter = list.begin();
228 for (std::vector<SharedQuadState*>::const_iterator sqs_iter =
229 sqs_list.begin();
230 sqs_iter != sqs_list.end();
231 ++sqs_iter) {
232 EXPECT_EQ(*sqs_iter, *iter);
233 ++iter;
237 TEST(ListContainerTest,
238 CorrectAllocationSizeForMoreThanOneAllocationSharedQuadState) {
239 // Constructor sets the allocation size to 2. Every time ListContainer needs
240 // to allocate again, it doubles allocation size. In this test, 10 elements is
241 // needed, thus ListContainerShould allocate spaces 2, 4 and 8 elements.
242 ListContainer<SharedQuadState> list(sizeof(SharedQuadState), 2);
243 std::vector<SharedQuadState*> sqs_list;
244 size_t size = 10;
245 for (size_t i = 0; i < size; ++i) {
246 // Before asking for a new element, space available without another
247 // allocation follows.
248 switch (i) {
249 case 2:
250 case 6:
251 EXPECT_EQ(0u, list.AvailableSizeWithoutAnotherAllocationForTesting());
252 break;
253 case 1:
254 case 5:
255 EXPECT_EQ(1u, list.AvailableSizeWithoutAnotherAllocationForTesting());
256 break;
257 case 0:
258 case 4:
259 EXPECT_EQ(2u, list.AvailableSizeWithoutAnotherAllocationForTesting());
260 break;
261 case 3:
262 EXPECT_EQ(3u, list.AvailableSizeWithoutAnotherAllocationForTesting());
263 break;
264 case 9:
265 EXPECT_EQ(5u, list.AvailableSizeWithoutAnotherAllocationForTesting());
266 break;
267 case 8:
268 EXPECT_EQ(6u, list.AvailableSizeWithoutAnotherAllocationForTesting());
269 break;
270 case 7:
271 EXPECT_EQ(7u, list.AvailableSizeWithoutAnotherAllocationForTesting());
272 break;
273 default:
274 break;
276 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
277 // After asking for a new element, space available without another
278 // allocation follows.
279 switch (i) {
280 case 1:
281 case 5:
282 EXPECT_EQ(0u, list.AvailableSizeWithoutAnotherAllocationForTesting());
283 break;
284 case 0:
285 case 4:
286 EXPECT_EQ(1u, list.AvailableSizeWithoutAnotherAllocationForTesting());
287 break;
288 case 3:
289 EXPECT_EQ(2u, list.AvailableSizeWithoutAnotherAllocationForTesting());
290 break;
291 case 2:
292 EXPECT_EQ(3u, list.AvailableSizeWithoutAnotherAllocationForTesting());
293 break;
294 case 9:
295 EXPECT_EQ(4u, list.AvailableSizeWithoutAnotherAllocationForTesting());
296 break;
297 case 8:
298 EXPECT_EQ(5u, list.AvailableSizeWithoutAnotherAllocationForTesting());
299 break;
300 case 7:
301 EXPECT_EQ(6u, list.AvailableSizeWithoutAnotherAllocationForTesting());
302 break;
303 case 6:
304 EXPECT_EQ(7u, list.AvailableSizeWithoutAnotherAllocationForTesting());
305 break;
306 default:
307 break;
310 EXPECT_EQ(size, list.size());
312 ListContainer<SharedQuadState>::Iterator iter = list.begin();
313 for (std::vector<SharedQuadState*>::const_iterator sqs_iter =
314 sqs_list.begin();
315 sqs_iter != sqs_list.end();
316 ++sqs_iter) {
317 EXPECT_EQ(*sqs_iter, *iter);
318 ++iter;
322 TEST(ListContainerTest, SimpleIterationSharedQuadState) {
323 ListContainer<SharedQuadState> list;
324 std::vector<SharedQuadState*> sqs_list;
325 size_t size = 10;
326 for (size_t i = 0; i < size; ++i) {
327 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
329 EXPECT_EQ(size, list.size());
331 size_t num_iters_in_list = 0;
333 std::vector<SharedQuadState*>::const_iterator sqs_iter = sqs_list.begin();
334 for (ListContainer<SharedQuadState>::Iterator iter = list.begin();
335 iter != list.end();
336 ++iter) {
337 EXPECT_EQ(*sqs_iter, *iter);
338 ++num_iters_in_list;
339 ++sqs_iter;
343 size_t num_iters_in_vector = 0;
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();
349 ++sqs_iter) {
350 EXPECT_EQ(*sqs_iter, *iter);
351 ++num_iters_in_vector;
352 ++iter;
356 EXPECT_EQ(num_iters_in_vector, num_iters_in_list);
359 TEST(ListContainerTest, SimpleConstIteratorIterationSharedQuadState) {
360 ListContainer<SharedQuadState> list;
361 std::vector<const SharedQuadState*> sqs_list;
362 size_t size = 10;
363 for (size_t i = 0; i < size; ++i) {
364 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
366 EXPECT_EQ(size, list.size());
369 std::vector<const SharedQuadState*>::const_iterator sqs_iter =
370 sqs_list.begin();
371 for (ListContainer<SharedQuadState>::ConstIterator iter = list.begin();
372 iter != list.end();
373 ++iter) {
374 EXPECT_TRUE(isConstSharedQuadStatePointer(*iter));
375 EXPECT_EQ(*sqs_iter, *iter);
376 ++sqs_iter;
381 std::vector<const SharedQuadState*>::const_iterator sqs_iter =
382 sqs_list.begin();
383 for (ListContainer<SharedQuadState>::Iterator iter = list.begin();
384 iter != list.end();
385 ++iter) {
386 EXPECT_FALSE(isConstSharedQuadStatePointer(*iter));
387 EXPECT_EQ(*sqs_iter, *iter);
388 ++sqs_iter;
393 ListContainer<SharedQuadState>::ConstIterator iter = list.begin();
394 for (std::vector<const SharedQuadState*>::const_iterator sqs_iter =
395 sqs_list.begin();
396 sqs_iter != sqs_list.end();
397 ++sqs_iter) {
398 EXPECT_EQ(*sqs_iter, *iter);
399 ++iter;
404 TEST(ListContainerTest, SimpleReverseInsertionSharedQuadState) {
405 ListContainer<SharedQuadState> list;
406 std::vector<SharedQuadState*> sqs_list;
407 size_t size = 10;
408 for (size_t i = 0; i < size; ++i) {
409 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
411 EXPECT_EQ(size, list.size());
414 std::vector<SharedQuadState*>::const_reverse_iterator sqs_iter =
415 sqs_list.rbegin();
416 for (ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin();
417 iter != list.rend();
418 ++iter) {
419 EXPECT_EQ(*sqs_iter, *iter);
420 ++sqs_iter;
425 ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin();
426 for (std::vector<SharedQuadState*>::reverse_iterator sqs_iter =
427 sqs_list.rbegin();
428 sqs_iter != sqs_list.rend();
429 ++sqs_iter) {
430 EXPECT_EQ(*sqs_iter, *iter);
431 ++iter;
436 TEST(ListContainerTest, SimpleDeletion) {
437 ListContainer<DrawQuad> list(LargestDrawQuadSize());
438 std::vector<SimpleDrawQuad*> sdq_list;
439 size_t size = 10;
440 for (size_t i = 0; i < size; ++i) {
441 sdq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>());
442 sdq_list.back()->set_value(i);
444 EXPECT_EQ(size, list.size());
446 list.EraseAndInvalidateAllPointers(list.begin());
447 --size;
448 EXPECT_EQ(size, list.size());
449 int i = 1;
450 for (ListContainer<DrawQuad>::Iterator iter = list.begin();
451 iter != list.end();
452 ++iter) {
453 EXPECT_EQ(i, static_cast<SimpleDrawQuad*>(*iter)->get_value());
454 ++i;
458 TEST(ListContainerTest, SimpleIterationAndManipulation) {
459 ListContainer<DrawQuad> list(LargestDrawQuadSize());
460 std::vector<SimpleDrawQuad*> sdq_list;
461 size_t size = 10;
462 for (size_t i = 0; i < size; ++i) {
463 SimpleDrawQuad* simple_dq = list.AllocateAndConstruct<SimpleDrawQuad>();
464 sdq_list.push_back(simple_dq);
466 EXPECT_EQ(size, list.size());
468 ListContainer<DrawQuad>::Iterator iter = list.begin();
469 for (int i = 0; i < 10; ++i) {
470 static_cast<SimpleDrawQuad*>(*iter)->set_value(i);
471 ++iter;
474 int i = 0;
475 for (std::vector<SimpleDrawQuad*>::const_iterator sdq_iter = sdq_list.begin();
476 sdq_iter < sdq_list.end();
477 ++sdq_iter) {
478 EXPECT_EQ(i, (*sdq_iter)->get_value());
479 ++i;
483 TEST(ListContainerTest, SimpleManipulationWithIndexSimpleDrawQuad) {
484 ListContainer<DrawQuad> list(LargestDrawQuadSize());
485 std::vector<SimpleDrawQuad*> dq_list;
486 size_t size = 10;
487 for (size_t i = 0; i < size; ++i) {
488 dq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>());
490 EXPECT_EQ(size, list.size());
492 for (size_t i = 0; i < size; ++i) {
493 static_cast<SimpleDrawQuad*>(list.ElementAt(i))->set_value(i);
496 int i = 0;
497 for (std::vector<SimpleDrawQuad*>::const_iterator dq_iter = dq_list.begin();
498 dq_iter != dq_list.end();
499 ++dq_iter, ++i) {
500 EXPECT_EQ(i, (*dq_iter)->get_value());
504 TEST(ListContainerTest,
505 SimpleManipulationWithIndexMoreThanOneAllocationSimpleDrawQuad) {
506 ListContainer<DrawQuad> list(LargestDrawQuadSize(), 2);
507 std::vector<SimpleDrawQuad*> dq_list;
508 size_t size = 10;
509 for (size_t i = 0; i < size; ++i) {
510 dq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>());
512 EXPECT_EQ(size, list.size());
514 for (size_t i = 0; i < size; ++i) {
515 static_cast<SimpleDrawQuad*>(list.ElementAt(i))->set_value(i);
518 int i = 0;
519 for (std::vector<SimpleDrawQuad*>::const_iterator dq_iter = dq_list.begin();
520 dq_iter != dq_list.end();
521 ++dq_iter, ++i) {
522 EXPECT_EQ(i, (*dq_iter)->get_value());
526 TEST(ListContainerTest,
527 SimpleIterationAndReverseIterationWithIndexSharedQuadState) {
528 ListContainer<SharedQuadState> list;
529 std::vector<SharedQuadState*> sqs_list;
530 size_t size = 10;
531 for (size_t i = 0; i < size; ++i) {
532 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
534 EXPECT_EQ(size, list.size());
536 size_t i = 0;
537 for (ListContainer<SharedQuadState>::Iterator iter = list.begin();
538 iter != list.end();
539 ++iter) {
540 EXPECT_EQ(i, iter.index());
541 ++i;
544 i = 0;
545 for (ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin();
546 iter != list.rend();
547 ++iter) {
548 EXPECT_EQ(i, iter.index());
549 ++i;
553 } // namespace
554 } // namespace cc