Class for allocating a chunk of memory for RenderPass
[chromium-blink-merge.git] / cc / quads / list_container_unittest.cc
blobaebd10f34b6090279fbbcfbf145bd91f5e863b08
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 virtual ~SimpleDrawQuad() {}
33 virtual void IterateResources(
34 const ResourceIteratorCallback& callback) OVERRIDE {}
36 void set_value(int val) { value = val; }
37 int get_value() { return value; }
38 virtual void ExtendValue(base::debug::TracedValue* value) const OVERRIDE {}
40 private:
41 int value;
44 class SimpleDrawQuadConstructMagicNumberOne : public SimpleDrawQuad {
45 public:
46 SimpleDrawQuadConstructMagicNumberOne() : SimpleDrawQuad() {
47 set_value(kMagicNumberToUseForDrawQuadOne);
51 class SimpleDrawQuadConstructMagicNumberTwo : public SimpleDrawQuad {
52 public:
53 SimpleDrawQuadConstructMagicNumberTwo() : SimpleDrawQuad() {
54 set_value(kMagicNumberToUseForDrawQuadTwo);
58 class MockDrawQuad : public DrawQuad {
59 public:
60 virtual ~MockDrawQuad() { Destruct(); }
61 virtual void IterateResources(
62 const ResourceIteratorCallback& callback) OVERRIDE {}
63 virtual void ExtendValue(base::debug::TracedValue* value) const OVERRIDE {}
64 MOCK_METHOD0(Destruct, void());
67 TEST(ListContainerTest, ConstructorCalledInAllocateAndConstruct) {
68 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
70 size_t size = 2;
71 SimpleDrawQuadConstructMagicNumberOne* dq_1 =
72 list.AllocateAndConstruct<SimpleDrawQuadConstructMagicNumberOne>();
73 SimpleDrawQuadConstructMagicNumberTwo* dq_2 =
74 list.AllocateAndConstruct<SimpleDrawQuadConstructMagicNumberTwo>();
76 EXPECT_EQ(size, list.size());
77 EXPECT_EQ(dq_1, list.front());
78 EXPECT_EQ(dq_2, list.back());
80 EXPECT_EQ(kMagicNumberToUseForDrawQuadOne, dq_1->get_value());
81 EXPECT_EQ(kMagicNumberToUseForDrawQuadTwo, dq_2->get_value());
84 TEST(ListContainerTest, DestructorCalled) {
85 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
87 size_t size = 1;
88 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
90 EXPECT_CALL(*dq_1, Destruct());
91 EXPECT_EQ(size, list.size());
92 EXPECT_EQ(dq_1, list.front());
95 TEST(ListContainerTest, DestructorCalledOnceWhenClear) {
96 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
97 size_t size = 1;
98 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
100 EXPECT_EQ(size, list.size());
101 EXPECT_EQ(dq_1, list.front());
103 // Make sure destructor is called once during clear, and won't be called
104 // again.
105 testing::MockFunction<void()> separator;
107 testing::InSequence s;
108 EXPECT_CALL(*dq_1, Destruct());
109 EXPECT_CALL(separator, Call());
110 EXPECT_CALL(*dq_1, Destruct()).Times(0);
113 list.clear();
114 separator.Call();
117 TEST(ListContainerTest, DestructorCalledOnceWhenErase) {
118 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
119 size_t size = 1;
120 MockDrawQuad* dq_1 = list.AllocateAndConstruct<MockDrawQuad>();
122 EXPECT_EQ(size, list.size());
123 EXPECT_EQ(dq_1, list.front());
125 // Make sure destructor is called once during clear, and won't be called
126 // again.
127 testing::MockFunction<void()> separator;
129 testing::InSequence s;
130 EXPECT_CALL(*dq_1, Destruct());
131 EXPECT_CALL(separator, Call());
132 EXPECT_CALL(*dq_1, Destruct()).Times(0);
135 list.EraseAndInvalidateAllPointers(list.begin());
136 separator.Call();
139 TEST(ListContainerTest, SimpleInsertionSharedQuadState) {
140 ListContainer<SharedQuadState> list;
142 size_t size = 3;
143 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>();
144 list.AllocateAndConstruct<SharedQuadState>();
145 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>();
147 EXPECT_EQ(size, list.size());
148 EXPECT_EQ(sqs_1, list.front());
149 EXPECT_EQ(sqs_3, list.back());
152 TEST(ListContainerTest, SimpleInsertionAndClearSharedQuadState) {
153 ListContainer<SharedQuadState> list;
154 EXPECT_TRUE(list.empty());
155 EXPECT_EQ(0u, list.size());
157 size_t size = 3;
158 SharedQuadState* sqs_1 = list.AllocateAndConstruct<SharedQuadState>();
159 list.AllocateAndConstruct<SharedQuadState>();
160 SharedQuadState* sqs_3 = list.AllocateAndConstruct<SharedQuadState>();
162 EXPECT_EQ(size, list.size());
163 EXPECT_EQ(sqs_1, list.front());
164 EXPECT_EQ(sqs_3, list.back());
165 EXPECT_FALSE(list.empty());
167 list.clear();
168 EXPECT_TRUE(list.empty());
169 EXPECT_EQ(0u, list.size());
172 TEST(ListContainerTest, SimpleInsertionClearAndInsertAgainSharedQuadState) {
173 ListContainer<SharedQuadState> list;
174 EXPECT_TRUE(list.empty());
175 EXPECT_EQ(0u, list.size());
177 size_t size = 2;
178 SharedQuadState* sqs_front = list.AllocateAndConstruct<SharedQuadState>();
179 SharedQuadState* sqs_back = list.AllocateAndConstruct<SharedQuadState>();
181 EXPECT_EQ(size, list.size());
182 EXPECT_EQ(sqs_front, list.front());
183 EXPECT_EQ(sqs_back, list.back());
184 EXPECT_FALSE(list.empty());
186 list.clear();
187 EXPECT_TRUE(list.empty());
188 EXPECT_EQ(0u, list.size());
190 size = 3;
191 sqs_front = list.AllocateAndConstruct<SharedQuadState>();
192 list.AllocateAndConstruct<SharedQuadState>();
193 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());
201 // This test is used to test when there is more than one allocation needed
202 // for, ListContainer can still perform like normal vector.
203 TEST(ListContainerTest,
204 SimpleInsertionTriggerMoreThanOneAllocationSharedQuadState) {
205 ListContainer<SharedQuadState> list(sizeof(SharedQuadState), 2);
206 std::vector<SharedQuadState*> sqs_list;
207 size_t size = 10;
208 for (size_t i = 0; i < size; ++i) {
209 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
211 EXPECT_EQ(size, list.size());
213 ListContainer<SharedQuadState>::Iterator iter = list.begin();
214 for (std::vector<SharedQuadState*>::const_iterator sqs_iter =
215 sqs_list.begin();
216 sqs_iter != sqs_list.end();
217 ++sqs_iter) {
218 EXPECT_EQ(*sqs_iter, &*iter);
219 ++iter;
223 TEST(ListContainerTest,
224 CorrectAllocationSizeForMoreThanOneAllocationSharedQuadState) {
225 // Constructor sets the allocation size to 2. Every time ListContainer needs
226 // to allocate again, it doubles allocation size. In this test, 10 elements is
227 // needed, thus ListContainerShould allocate spaces 2, 4 and 8 elements.
228 ListContainer<SharedQuadState> list(sizeof(SharedQuadState), 2);
229 std::vector<SharedQuadState*> sqs_list;
230 size_t size = 10;
231 for (size_t i = 0; i < size; ++i) {
232 // Before asking for a new element, space available without another
233 // allocation follows.
234 switch (i) {
235 case 2:
236 case 6:
237 EXPECT_EQ(0u, list.AvailableSizeWithoutAnotherAllocationForTesting());
238 break;
239 case 1:
240 case 5:
241 EXPECT_EQ(1u, list.AvailableSizeWithoutAnotherAllocationForTesting());
242 break;
243 case 0:
244 case 4:
245 EXPECT_EQ(2u, list.AvailableSizeWithoutAnotherAllocationForTesting());
246 break;
247 case 3:
248 EXPECT_EQ(3u, list.AvailableSizeWithoutAnotherAllocationForTesting());
249 break;
250 case 9:
251 EXPECT_EQ(5u, list.AvailableSizeWithoutAnotherAllocationForTesting());
252 break;
253 case 8:
254 EXPECT_EQ(6u, list.AvailableSizeWithoutAnotherAllocationForTesting());
255 break;
256 case 7:
257 EXPECT_EQ(7u, list.AvailableSizeWithoutAnotherAllocationForTesting());
258 break;
259 default:
260 break;
262 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
263 // After asking for a new element, space available without another
264 // allocation follows.
265 switch (i) {
266 case 1:
267 case 5:
268 EXPECT_EQ(0u, list.AvailableSizeWithoutAnotherAllocationForTesting());
269 break;
270 case 0:
271 case 4:
272 EXPECT_EQ(1u, list.AvailableSizeWithoutAnotherAllocationForTesting());
273 break;
274 case 3:
275 EXPECT_EQ(2u, list.AvailableSizeWithoutAnotherAllocationForTesting());
276 break;
277 case 2:
278 EXPECT_EQ(3u, list.AvailableSizeWithoutAnotherAllocationForTesting());
279 break;
280 case 9:
281 EXPECT_EQ(4u, list.AvailableSizeWithoutAnotherAllocationForTesting());
282 break;
283 case 8:
284 EXPECT_EQ(5u, list.AvailableSizeWithoutAnotherAllocationForTesting());
285 break;
286 case 7:
287 EXPECT_EQ(6u, list.AvailableSizeWithoutAnotherAllocationForTesting());
288 break;
289 case 6:
290 EXPECT_EQ(7u, list.AvailableSizeWithoutAnotherAllocationForTesting());
291 break;
292 default:
293 break;
296 EXPECT_EQ(size, list.size());
298 ListContainer<SharedQuadState>::Iterator iter = list.begin();
299 for (std::vector<SharedQuadState*>::const_iterator sqs_iter =
300 sqs_list.begin();
301 sqs_iter != sqs_list.end();
302 ++sqs_iter) {
303 EXPECT_EQ(*sqs_iter, &*iter);
304 ++iter;
308 TEST(ListContainerTest, SimpleIterationSharedQuadState) {
309 ListContainer<SharedQuadState> list;
310 std::vector<SharedQuadState*> sqs_list;
311 size_t size = 10;
312 for (size_t i = 0; i < size; ++i) {
313 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
315 EXPECT_EQ(size, list.size());
317 size_t num_iters_in_list = 0;
319 std::vector<SharedQuadState*>::const_iterator sqs_iter = sqs_list.begin();
320 for (ListContainer<SharedQuadState>::Iterator iter = list.begin();
321 iter != list.end();
322 ++iter) {
323 EXPECT_EQ(*sqs_iter, &*iter);
324 ++num_iters_in_list;
325 ++sqs_iter;
329 size_t num_iters_in_vector = 0;
331 ListContainer<SharedQuadState>::Iterator iter = list.begin();
332 for (std::vector<SharedQuadState*>::const_iterator sqs_iter =
333 sqs_list.begin();
334 sqs_iter != sqs_list.end();
335 ++sqs_iter) {
336 EXPECT_EQ(*sqs_iter, &*iter);
337 ++num_iters_in_vector;
338 ++iter;
342 EXPECT_EQ(num_iters_in_vector, num_iters_in_list);
345 TEST(ListContainerTest, SimpleConstIteratorIterationSharedQuadState) {
346 ListContainer<SharedQuadState> list;
347 std::vector<const SharedQuadState*> sqs_list;
348 size_t size = 10;
349 for (size_t i = 0; i < size; ++i) {
350 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
352 EXPECT_EQ(size, list.size());
355 std::vector<const SharedQuadState*>::const_iterator sqs_iter =
356 sqs_list.begin();
357 for (ListContainer<SharedQuadState>::ConstIterator iter = list.begin();
358 iter != list.end();
359 ++iter) {
360 EXPECT_TRUE(isConstSharedQuadStatePointer(&*iter));
361 EXPECT_EQ(*sqs_iter, &*iter);
362 ++sqs_iter;
367 std::vector<const SharedQuadState*>::const_iterator sqs_iter =
368 sqs_list.begin();
369 for (ListContainer<SharedQuadState>::Iterator iter = list.begin();
370 iter != list.end();
371 ++iter) {
372 EXPECT_FALSE(isConstSharedQuadStatePointer(&*iter));
373 EXPECT_EQ(*sqs_iter, &*iter);
374 ++sqs_iter;
379 ListContainer<SharedQuadState>::ConstIterator iter = list.begin();
380 for (std::vector<const SharedQuadState*>::const_iterator sqs_iter =
381 sqs_list.begin();
382 sqs_iter != sqs_list.end();
383 ++sqs_iter) {
384 EXPECT_EQ(*sqs_iter, &*iter);
385 ++iter;
390 TEST(ListContainerTest, SimpleReverseInsertionSharedQuadState) {
391 ListContainer<SharedQuadState> list;
392 std::vector<SharedQuadState*> sqs_list;
393 size_t size = 10;
394 for (size_t i = 0; i < size; ++i) {
395 sqs_list.push_back(list.AllocateAndConstruct<SharedQuadState>());
397 EXPECT_EQ(size, list.size());
400 std::vector<SharedQuadState*>::const_reverse_iterator sqs_iter =
401 sqs_list.rbegin();
402 for (ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin();
403 iter != list.rend();
404 ++iter) {
405 EXPECT_EQ(*sqs_iter, &(*iter));
406 ++sqs_iter;
411 ListContainer<SharedQuadState>::ReverseIterator iter = list.rbegin();
412 for (std::vector<SharedQuadState*>::reverse_iterator sqs_iter =
413 sqs_list.rbegin();
414 sqs_iter != sqs_list.rend();
415 ++sqs_iter) {
416 EXPECT_EQ(*sqs_iter, &(*iter));
417 ++iter;
422 TEST(ListContainerTest, SimpleDeletion) {
423 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
424 std::vector<SimpleDrawQuad*> sdq_list;
425 size_t size = 10;
426 for (size_t i = 0; i < size; ++i) {
427 sdq_list.push_back(list.AllocateAndConstruct<SimpleDrawQuad>());
428 sdq_list.back()->set_value(i);
430 EXPECT_EQ(size, list.size());
432 list.EraseAndInvalidateAllPointers(list.begin());
433 --size;
434 EXPECT_EQ(size, list.size());
435 int i = 1;
436 for (ListContainer<DrawQuad>::Iterator iter = list.begin();
437 iter != list.end();
438 ++iter) {
439 EXPECT_EQ(i, static_cast<SimpleDrawQuad*>(&*iter)->get_value());
440 ++i;
444 TEST(ListContainerTest, SimpleIterationAndManipulation) {
445 ListContainer<DrawQuad> list(sizeof(kLargestDrawQuad));
446 std::vector<SimpleDrawQuad*> sdq_list;
447 size_t size = 10;
448 for (size_t i = 0; i < size; ++i) {
449 SimpleDrawQuad* simple_dq = list.AllocateAndConstruct<SimpleDrawQuad>();
450 sdq_list.push_back(simple_dq);
452 EXPECT_EQ(size, list.size());
454 ListContainer<DrawQuad>::Iterator iter = list.begin();
455 for (int i = 0; i < 10; ++i) {
456 static_cast<SimpleDrawQuad*>(&*iter)->set_value(i);
457 ++iter;
460 int i = 0;
461 for (std::vector<SimpleDrawQuad*>::const_iterator sdq_iter = sdq_list.begin();
462 sdq_iter < sdq_list.end();
463 ++sdq_iter) {
464 EXPECT_EQ(i, (*sdq_iter)->get_value());
465 ++i;
469 } // namespace
470 } // namespace cc