Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "base/debug/scoped_heap_usage.h" | |
| 6 | |
| 7 #include "base/allocator/features.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace base { | |
| 11 namespace debug { | |
| 12 | |
| 13 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | |
| 14 namespace { | |
| 15 | |
| 16 class TestingScopedHeapUsage : public ScopedHeapUsage { | |
| 17 public: | |
| 18 using ScopedHeapUsage::TearDownForTesting; | |
| 19 }; | |
| 20 | |
| 21 class ScopedHeapUsageTest : public testing::Test { | |
| 22 public: | |
| 23 void SetUp() override { ScopedHeapUsage::Initialize(); } | |
| 24 | |
| 25 void TearDown() override { TestingScopedHeapUsage::TearDownForTesting(); } | |
| 26 }; | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 TEST_F(ScopedHeapUsageTest, SimpleUsage) { | |
| 31 ScopedHeapUsage scoped_usage; | |
| 32 | |
| 33 ScopedHeapUsage::AllocatorUsage u1 = scoped_usage.usage_at_creation(); | |
| 34 ScopedHeapUsage::AllocatorUsage u2 = ScopedHeapUsage::Now(); | |
| 35 | |
| 36 EXPECT_EQ(u1.alloc_ops, u2.alloc_ops); | |
|
Primiano Tucci (use gerrit)
2016/08/24 14:11:46
I'd move these expect statements below., after u3
Sigurður Ásgeirsson
2016/09/01 15:18:18
Yeah, I'm going to rewrite this test to test the i
| |
| 37 EXPECT_EQ(u1.alloc_bytes, u2.alloc_bytes); | |
| 38 EXPECT_EQ(u1.alloc_overhead_bytes, u2.alloc_overhead_bytes); | |
| 39 EXPECT_EQ(u1.free_ops, u2.free_ops); | |
| 40 EXPECT_EQ(u1.free_bytes, u2.free_bytes); | |
| 41 EXPECT_EQ(0U, u2.max_allocated_bytes); | |
| 42 | |
| 43 const size_t kAllocSize = 1029U; | |
| 44 void* ptr = malloc(kAllocSize); | |
| 45 free(ptr); | |
| 46 | |
| 47 ScopedHeapUsage::AllocatorUsage u3 = ScopedHeapUsage::Now(); | |
| 48 | |
| 49 EXPECT_LT(u1.alloc_ops, u3.alloc_ops); | |
| 50 EXPECT_LE(u1.alloc_bytes + kAllocSize, u3.alloc_bytes); | |
| 51 EXPECT_LE(u1.alloc_overhead_bytes, u3.alloc_overhead_bytes); | |
| 52 EXPECT_LT(u1.free_ops, u3.free_ops); | |
| 53 EXPECT_LE(u1.free_bytes + kAllocSize, u3.free_bytes); | |
| 54 EXPECT_LE(kAllocSize, u3.max_allocated_bytes); | |
| 55 } | |
| 56 | |
| 57 TEST_F(ScopedHeapUsageTest, NestedMaxWorks) { | |
| 58 ScopedHeapUsage outer_scoped_usage; | |
| 59 | |
| 60 const size_t kOuterAllocSize = 1029U; | |
| 61 void* ptr = malloc(kOuterAllocSize); | |
| 62 free(ptr); | |
| 63 | |
| 64 EXPECT_LE(kOuterAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); | |
| 65 | |
| 66 { | |
| 67 ScopedHeapUsage inner_scoped_usage; | |
| 68 | |
| 69 const size_t kInnerAllocSize = 673U; | |
| 70 ptr = malloc(kInnerAllocSize); | |
| 71 free(ptr); | |
| 72 | |
| 73 EXPECT_LE(kInnerAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); | |
| 74 } | |
| 75 | |
| 76 // The greater, outer allocation size should have been restored. | |
| 77 EXPECT_LE(kOuterAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); | |
| 78 | |
| 79 const size_t kLargerInnerAllocSize = kOuterAllocSize + 673U; | |
| 80 { | |
| 81 ScopedHeapUsage inner_scoped_usage; | |
| 82 | |
| 83 ptr = malloc(kLargerInnerAllocSize); | |
| 84 free(ptr); | |
| 85 | |
| 86 EXPECT_LE(kLargerInnerAllocSize, | |
| 87 ScopedHeapUsage::Now().max_allocated_bytes); | |
| 88 } | |
| 89 | |
| 90 // The greater, inner allocation size should have been preserved. | |
| 91 EXPECT_LE(kLargerInnerAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); | |
| 92 } | |
| 93 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | |
| 94 | |
| 95 } // namespace debug | |
| 96 } // namespace base | |
| OLD | NEW |