OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "cc/base/random_access_list_container.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <vector> |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace cc { |
| 13 namespace { |
| 14 |
| 15 class Base { |
| 16 public: |
| 17 virtual ~Base() {} |
| 18 int get_value() const { return value_; } |
| 19 |
| 20 protected: |
| 21 explicit Base(int value) : value_(value) {} |
| 22 |
| 23 int value_; |
| 24 }; |
| 25 |
| 26 const int kMagicNumberOne = 1; |
| 27 const int kMagicNumberTwo = 2; |
| 28 const int kMagicNumberThree = 3; |
| 29 |
| 30 class Derived1 : public Base { |
| 31 public: |
| 32 Derived1() : Base(kMagicNumberOne) {} |
| 33 }; |
| 34 |
| 35 class Derived2 : public Base { |
| 36 public: |
| 37 Derived2() : Base(kMagicNumberTwo) {} |
| 38 }; |
| 39 |
| 40 class Derived3 : public Base { |
| 41 public: |
| 42 Derived3() : Base(kMagicNumberThree) {} |
| 43 }; |
| 44 |
| 45 size_t LargestDerivedElementSize() { |
| 46 static_assert(sizeof(Derived1) >= sizeof(Derived2), |
| 47 "Derived2 is larger than Derived1"); |
| 48 static_assert(sizeof(Derived1) >= sizeof(Derived3), |
| 49 "Derived3 is larger than Derived1"); |
| 50 return sizeof(Derived1); |
| 51 } |
| 52 |
| 53 TEST(RandomAccessListContainerTest, RandomAccess) { |
| 54 RandomAccessListContainer<Base> list(LargestDerivedElementSize(), 1); |
| 55 |
| 56 list.AllocateAndConstruct<Derived1>(); |
| 57 list.AllocateAndConstruct<Derived2>(); |
| 58 list.AllocateAndConstruct<Derived3>(); |
| 59 list.AllocateAndConstruct<Derived1>(); |
| 60 list.AllocateAndConstruct<Derived2>(); |
| 61 list.AllocateAndConstruct<Derived3>(); |
| 62 |
| 63 EXPECT_EQ(kMagicNumberOne, list[0]->get_value()); |
| 64 EXPECT_EQ(kMagicNumberTwo, list[1]->get_value()); |
| 65 EXPECT_EQ(kMagicNumberThree, list[2]->get_value()); |
| 66 EXPECT_EQ(kMagicNumberOne, list[3]->get_value()); |
| 67 EXPECT_EQ(kMagicNumberTwo, list[4]->get_value()); |
| 68 EXPECT_EQ(kMagicNumberThree, list[5]->get_value()); |
| 69 |
| 70 list.RemoveLast(); |
| 71 list.RemoveLast(); |
| 72 list.RemoveLast(); |
| 73 |
| 74 EXPECT_EQ(kMagicNumberOne, list[0]->get_value()); |
| 75 EXPECT_EQ(kMagicNumberTwo, list[1]->get_value()); |
| 76 EXPECT_EQ(kMagicNumberThree, list[2]->get_value()); |
| 77 } |
| 78 |
| 79 } // namespace |
| 80 } // namespace cc |
OLD | NEW |