Index: cc/base/contiguous_container_unittest.cc |
diff --git a/cc/base/contiguous_container_unittest.cc b/cc/base/contiguous_container_unittest.cc |
index 158b787d28dc4a584748e32b8861dc5e3decbc85..d617d4af55b16826638f55883fc9088de7ccd4d0 100644 |
--- a/cc/base/contiguous_container_unittest.cc |
+++ b/cc/base/contiguous_container_unittest.cc |
@@ -4,6 +4,8 @@ |
#include <stddef.h> |
+#include <iterator> |
+ |
#include "cc/base/contiguous_container.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -228,6 +230,15 @@ TEST(ContiguousContainerTest, ForwardIteration) { |
"Non-const iteration should produce non-const references."); |
} |
+TEST(ContiguousContainerTest, ForwardIterationEmpty) { |
+ ContiguousContainer<Point2D, kPointAlignment> list(kMaxPointSize); |
+ |
+ // ContiguousContainer allocates memory for elements lazily at first append |
+ // operation, so at this point memory is not allocated. Check that iteration |
+ // doesn't crash and produces zero elements. |
+ EXPECT_EQ(0, std::distance(list.begin(), list.end())); |
+} |
+ |
TEST(ContiguousContainerTest, ConstForwardIteration) { |
ContiguousContainer<Point2D, kPointAlignment> list(kMaxPointSize); |
for (int i = 0; i < (int)kNumElements; i++) |
@@ -511,6 +522,32 @@ TEST(ContiguousContainerTest, CapacityInBytesAfterClear) { |
EXPECT_EQ(empty_capacity, list.GetCapacityInBytes()); |
} |
+TEST(ContiguousContainerTest, MemoryUsageInBytes) { |
+ constexpr size_t initial_size1 = 10 * kMaxPointSize; |
+ ContiguousContainer<Point2D, kPointAlignment> list1(kMaxPointSize, |
+ initial_size1); |
+ |
+ constexpr size_t initial_size2 = 10000 * kMaxPointSize; |
+ ContiguousContainer<Point2D, kPointAlignment> list2(kMaxPointSize, |
+ initial_size2); |
+ |
+ // Memory is allocated lazily, so even though lists were created with |
+ // different initial_size values, they'll have the same memory usage here. |
+ size_t memory_usage1 = list1.MemoryUsageInBytes(); |
+ size_t memory_usage2 = list2.MemoryUsageInBytes(); |
+ EXPECT_EQ(memory_usage1, memory_usage2); |
+ |
+ // Trigger memory allocation. |
+ list1.AllocateAndConstruct<Point2D>(); |
+ list2.AllocateAndConstruct<Point2D>(); |
+ |
+ // Same object was created in both lists, but their memory usages grew |
+ // differently, based on initial_size values lists were created with. |
+ EXPECT_NE(list1.MemoryUsageInBytes(), list2.MemoryUsageInBytes()); |
+ EXPECT_GE(list1.MemoryUsageInBytes(), memory_usage1 + initial_size1); |
+ EXPECT_GE(list2.MemoryUsageInBytes(), memory_usage2 + initial_size2); |
+} |
+ |
TEST(ContiguousContainerTest, Alignment) { |
const size_t max_align = ALIGNOF(long double); |
ContiguousContainer<Point2D, max_align> list(kMaxPointSize); |