1 // Copyright 2015 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/base/random_access_list_container.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
19 int get_value() const { return value_
; }
22 explicit Base(int value
) : value_(value
) {}
27 const int kMagicNumberOne
= 1;
28 const int kMagicNumberTwo
= 2;
29 const int kMagicNumberThree
= 3;
31 class Derived1
: public Base
{
33 Derived1() : Base(kMagicNumberOne
) {}
36 class Derived2
: public Base
{
38 Derived2() : Base(kMagicNumberTwo
) {}
41 class Derived3
: public Base
{
43 Derived3() : Base(kMagicNumberThree
) {}
46 size_t LargestDerivedElementSize() {
47 static_assert(sizeof(Derived1
) >= sizeof(Derived2
),
48 "Derived2 is larger than Derived1");
49 static_assert(sizeof(Derived1
) >= sizeof(Derived3
),
50 "Derived3 is larger than Derived1");
51 return sizeof(Derived1
);
54 TEST(RandomAccessListContainerTest
, RandomAccess
) {
55 RandomAccessListContainer
<Base
> list(LargestDerivedElementSize(), 1);
57 list
.AllocateAndConstruct
<Derived1
>();
58 list
.AllocateAndConstruct
<Derived2
>();
59 list
.AllocateAndConstruct
<Derived3
>();
60 list
.AllocateAndConstruct
<Derived1
>();
61 list
.AllocateAndConstruct
<Derived2
>();
62 list
.AllocateAndConstruct
<Derived3
>();
64 EXPECT_EQ(kMagicNumberOne
, list
[0]->get_value());
65 EXPECT_EQ(kMagicNumberTwo
, list
[1]->get_value());
66 EXPECT_EQ(kMagicNumberThree
, list
[2]->get_value());
67 EXPECT_EQ(kMagicNumberOne
, list
[3]->get_value());
68 EXPECT_EQ(kMagicNumberTwo
, list
[4]->get_value());
69 EXPECT_EQ(kMagicNumberThree
, list
[5]->get_value());
75 EXPECT_EQ(kMagicNumberOne
, list
[0]->get_value());
76 EXPECT_EQ(kMagicNumberTwo
, list
[1]->get_value());
77 EXPECT_EQ(kMagicNumberThree
, list
[2]->get_value());
79 list
.AllocateAndConstruct
<Derived3
>();
80 list
.AllocateAndConstruct
<Derived2
>();
81 list
.AllocateAndConstruct
<Derived1
>();
83 EXPECT_EQ(kMagicNumberOne
, list
[0]->get_value());
84 EXPECT_EQ(kMagicNumberTwo
, list
[1]->get_value());
85 EXPECT_EQ(kMagicNumberThree
, list
[2]->get_value());
86 EXPECT_EQ(kMagicNumberThree
, list
[3]->get_value());
87 EXPECT_EQ(kMagicNumberTwo
, list
[4]->get_value());
88 EXPECT_EQ(kMagicNumberOne
, list
[5]->get_value());
91 TEST(RandomAccessListContainerTest
, Clear
) {
92 RandomAccessListContainer
<Base
> list(LargestDerivedElementSize(), 1);
94 list
.AllocateAndConstruct
<Derived1
>();
95 list
.AllocateAndConstruct
<Derived2
>();
97 EXPECT_EQ(kMagicNumberOne
, list
[0]->get_value());
98 EXPECT_EQ(kMagicNumberTwo
, list
[1]->get_value());
101 list
.AllocateAndConstruct
<Derived3
>();
103 EXPECT_EQ(kMagicNumberThree
, list
[0]->get_value());