OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
| 7 #include <iterator> |
| 8 |
7 #include "cc/base/contiguous_container.h" | 9 #include "cc/base/contiguous_container.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 namespace cc { | 13 namespace cc { |
12 namespace { | 14 namespace { |
13 | 15 |
14 struct Point2D { | 16 struct Point2D { |
15 Point2D() : Point2D(0, 0) {} | 17 Point2D() : Point2D(0, 0) {} |
16 Point2D(int x, int y) : x(x), y(y) {} | 18 Point2D(int x, int y) : x(x), y(y) {} |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 for (Point2D& point : list) { | 223 for (Point2D& point : list) { |
222 EXPECT_EQ((int)count, point.x); | 224 EXPECT_EQ((int)count, point.x); |
223 count++; | 225 count++; |
224 } | 226 } |
225 EXPECT_EQ(kNumElements, count); | 227 EXPECT_EQ(kNumElements, count); |
226 | 228 |
227 static_assert(std::is_same<decltype(*list.begin()), Point2D&>::value, | 229 static_assert(std::is_same<decltype(*list.begin()), Point2D&>::value, |
228 "Non-const iteration should produce non-const references."); | 230 "Non-const iteration should produce non-const references."); |
229 } | 231 } |
230 | 232 |
| 233 TEST(ContiguousContainerTest, ForwardIterationEmpty) { |
| 234 ContiguousContainer<Point2D, kPointAlignment> list(kMaxPointSize); |
| 235 |
| 236 // ContiguousContainer allocates memory for elements lazily at first append |
| 237 // operation, so at this point memory is not allocated. Check that iteration |
| 238 // doesn't crash and produces zero elements. |
| 239 EXPECT_EQ(0, std::distance(list.begin(), list.end())); |
| 240 } |
| 241 |
231 TEST(ContiguousContainerTest, ConstForwardIteration) { | 242 TEST(ContiguousContainerTest, ConstForwardIteration) { |
232 ContiguousContainer<Point2D, kPointAlignment> list(kMaxPointSize); | 243 ContiguousContainer<Point2D, kPointAlignment> list(kMaxPointSize); |
233 for (int i = 0; i < (int)kNumElements; i++) | 244 for (int i = 0; i < (int)kNumElements; i++) |
234 list.AllocateAndConstruct<Point2D>(i, i); | 245 list.AllocateAndConstruct<Point2D>(i, i); |
235 | 246 |
236 const auto& const_list = list; | 247 const auto& const_list = list; |
237 unsigned count = 0; | 248 unsigned count = 0; |
238 for (const Point2D& point : const_list) { | 249 for (const Point2D& point : const_list) { |
239 EXPECT_EQ((int)count, point.x); | 250 EXPECT_EQ((int)count, point.x); |
240 count++; | 251 count++; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 // Clearing should restore the capacity of the container to the same as a | 515 // Clearing should restore the capacity of the container to the same as a |
505 // newly allocated one (without reserved capacity requested). | 516 // newly allocated one (without reserved capacity requested). |
506 ContiguousContainer<Point2D, kPointAlignment> list(kMaxPointSize); | 517 ContiguousContainer<Point2D, kPointAlignment> list(kMaxPointSize); |
507 size_t empty_capacity = list.GetCapacityInBytes(); | 518 size_t empty_capacity = list.GetCapacityInBytes(); |
508 list.AllocateAndConstruct<Point2D>(); | 519 list.AllocateAndConstruct<Point2D>(); |
509 list.AllocateAndConstruct<Point2D>(); | 520 list.AllocateAndConstruct<Point2D>(); |
510 list.Clear(); | 521 list.Clear(); |
511 EXPECT_EQ(empty_capacity, list.GetCapacityInBytes()); | 522 EXPECT_EQ(empty_capacity, list.GetCapacityInBytes()); |
512 } | 523 } |
513 | 524 |
| 525 TEST(ContiguousContainerTest, MemoryUsageInBytes) { |
| 526 constexpr size_t initial_size1 = 10 * kMaxPointSize; |
| 527 ContiguousContainer<Point2D, kPointAlignment> list1(kMaxPointSize, |
| 528 initial_size1); |
| 529 |
| 530 constexpr size_t initial_size2 = 10000 * kMaxPointSize; |
| 531 ContiguousContainer<Point2D, kPointAlignment> list2(kMaxPointSize, |
| 532 initial_size2); |
| 533 |
| 534 // Memory is allocated lazily, so even though lists were created with |
| 535 // different initial_size values, they'll have the same memory usage here. |
| 536 size_t memory_usage1 = list1.MemoryUsageInBytes(); |
| 537 size_t memory_usage2 = list2.MemoryUsageInBytes(); |
| 538 EXPECT_EQ(memory_usage1, memory_usage2); |
| 539 |
| 540 // Trigger memory allocation. |
| 541 list1.AllocateAndConstruct<Point2D>(); |
| 542 list2.AllocateAndConstruct<Point2D>(); |
| 543 |
| 544 // Same object was created in both lists, but their memory usages grew |
| 545 // differently, based on initial_size values lists were created with. |
| 546 EXPECT_NE(list1.MemoryUsageInBytes(), list2.MemoryUsageInBytes()); |
| 547 EXPECT_GE(list1.MemoryUsageInBytes(), memory_usage1 + initial_size1); |
| 548 EXPECT_GE(list2.MemoryUsageInBytes(), memory_usage2 + initial_size2); |
| 549 } |
| 550 |
514 TEST(ContiguousContainerTest, Alignment) { | 551 TEST(ContiguousContainerTest, Alignment) { |
515 const size_t max_align = ALIGNOF(long double); | 552 const size_t max_align = ALIGNOF(long double); |
516 ContiguousContainer<Point2D, max_align> list(kMaxPointSize); | 553 ContiguousContainer<Point2D, max_align> list(kMaxPointSize); |
517 | 554 |
518 list.AllocateAndConstruct<Point2D>(); | 555 list.AllocateAndConstruct<Point2D>(); |
519 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 556 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
520 list.AllocateAndConstruct<Point2D>(); | 557 list.AllocateAndConstruct<Point2D>(); |
521 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 558 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
522 list.AllocateAndConstruct<Point3D>(); | 559 list.AllocateAndConstruct<Point3D>(); |
523 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 560 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
524 list.AllocateAndConstruct<Point3D>(); | 561 list.AllocateAndConstruct<Point3D>(); |
525 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 562 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
526 list.AllocateAndConstruct<Point2D>(); | 563 list.AllocateAndConstruct<Point2D>(); |
527 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 564 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
528 | 565 |
529 list.AppendByMoving(&list[0], sizeof(Point2D)); | 566 list.AppendByMoving(&list[0], sizeof(Point2D)); |
530 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 567 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
531 list.AppendByMoving(&list[1], sizeof(Point2D)); | 568 list.AppendByMoving(&list[1], sizeof(Point2D)); |
532 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 569 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
533 list.AppendByMoving(&list[2], sizeof(Point3D)); | 570 list.AppendByMoving(&list[2], sizeof(Point3D)); |
534 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 571 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
535 list.AppendByMoving(&list[3], sizeof(Point3D)); | 572 list.AppendByMoving(&list[3], sizeof(Point3D)); |
536 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 573 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
537 list.AppendByMoving(&list[4], sizeof(Point2D)); | 574 list.AppendByMoving(&list[4], sizeof(Point2D)); |
538 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); | 575 EXPECT_EQ(0u, reinterpret_cast<intptr_t>(&list.last()) & (max_align - 1)); |
539 } | 576 } |
540 | 577 |
541 } // namespace | 578 } // namespace |
542 } // namespace cc | 579 } // namespace cc |
OLD | NEW |