OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/base/list_container.h" | 5 #include "cc/base/list_container.h" |
6 | 6 |
| 7 #include <algorithm> |
7 #include <vector> | 8 #include <vector> |
8 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 namespace cc { | 12 namespace cc { |
12 namespace { | 13 namespace { |
13 | 14 |
14 // Element class having derived classes. | 15 // Element class having derived classes. |
15 class DerivedElement { | 16 class DerivedElement { |
16 public: | 17 public: |
(...skipping 1019 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1036 EXPECT_EQ(2u, list_1.size()); | 1037 EXPECT_EQ(2u, list_1.size()); |
1037 | 1038 |
1038 EXPECT_EQ(kMagicNumberToUseForSimpleDerivedElementOne, | 1039 EXPECT_EQ(kMagicNumberToUseForSimpleDerivedElementOne, |
1039 list_2.front()->get_value()); | 1040 list_2.front()->get_value()); |
1040 EXPECT_EQ(1u, list_2.size()); | 1041 EXPECT_EQ(1u, list_2.size()); |
1041 | 1042 |
1042 // Ensure pointers are still valid after swapping. | 1043 // Ensure pointers are still valid after swapping. |
1043 EXPECT_EQ(pre_swap_list_1_front, list_2.front()); | 1044 EXPECT_EQ(pre_swap_list_1_front, list_2.front()); |
1044 } | 1045 } |
1045 | 1046 |
| 1047 TEST(ListContainerTest, GetCapacityInBytes) { |
| 1048 const int iterations = 500; |
| 1049 const size_t initial_capacity = 10; |
| 1050 const size_t upper_bound_on_min_capacity = initial_capacity; |
| 1051 |
| 1052 // At time of writing, removing elements from the end can cause up to 7x the |
| 1053 // memory required to be consumed, in the worst case, since we can have up to |
| 1054 // two trailing inner lists that are empty (for 2*size + 4*size in unused |
| 1055 // memory, due to the exponential growth strategy). |
| 1056 const size_t max_waste_factor = 8; |
| 1057 |
| 1058 ListContainer<DerivedElement> list(LargestDerivedElementSize(), |
| 1059 initial_capacity); |
| 1060 |
| 1061 // The capacity should grow with the list. |
| 1062 for (int i = 0; i < iterations; i++) { |
| 1063 size_t capacity = list.GetCapacityInBytes(); |
| 1064 ASSERT_GE(capacity, list.size() * LargestDerivedElementSize()); |
| 1065 ASSERT_LE(capacity, std::max(list.size(), upper_bound_on_min_capacity) * |
| 1066 max_waste_factor * LargestDerivedElementSize()); |
| 1067 list.AllocateAndConstruct<DerivedElement1>(); |
| 1068 } |
| 1069 |
| 1070 // The capacity should shrink with the list. |
| 1071 for (int i = 0; i < iterations; i++) { |
| 1072 size_t capacity = list.GetCapacityInBytes(); |
| 1073 ASSERT_GE(capacity, list.size() * LargestDerivedElementSize()); |
| 1074 ASSERT_LE(capacity, std::max(list.size(), upper_bound_on_min_capacity) * |
| 1075 max_waste_factor * LargestDerivedElementSize()); |
| 1076 list.RemoveLast(); |
| 1077 } |
| 1078 } |
| 1079 |
1046 } // namespace | 1080 } // namespace |
1047 } // namespace cc | 1081 } // namespace cc |
OLD | NEW |