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 "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace base { |
| 10 namespace debug { |
| 11 |
| 12 namespace { |
| 13 |
| 14 class ScopedHeapUsageTest : public testing::Test { |
| 15 public: |
| 16 void SetUp() override { ScopedHeapUsage::Initialize(); } |
| 17 |
| 18 void TearDown() override { ScopedHeapUsage::TearDownForTesting(); } |
| 19 }; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 TEST_F(ScopedHeapUsageTest, SimpleUsage) { |
| 24 ScopedHeapUsage scoped_usage; |
| 25 |
| 26 ScopedHeapUsage::AllocatorUsage u1 = scoped_usage.usage_at_creation(); |
| 27 ScopedHeapUsage::AllocatorUsage u2 = ScopedHeapUsage::Now(); |
| 28 |
| 29 EXPECT_EQ(u1.alloc_ops, u2.alloc_ops); |
| 30 EXPECT_EQ(u1.alloc_bytes, u2.alloc_bytes); |
| 31 EXPECT_EQ(u1.alloc_overhead_bytes, u2.alloc_overhead_bytes); |
| 32 EXPECT_EQ(u1.free_ops, u2.free_ops); |
| 33 EXPECT_EQ(u1.free_bytes, u2.free_bytes); |
| 34 EXPECT_EQ(0U, u2.max_allocated_bytes); |
| 35 |
| 36 const size_t kAllocSize = 1029U; |
| 37 void* ptr = malloc(kAllocSize); |
| 38 free(ptr); |
| 39 |
| 40 ScopedHeapUsage::AllocatorUsage u3 = ScopedHeapUsage::Now(); |
| 41 |
| 42 EXPECT_LT(u1.alloc_ops, u3.alloc_ops); |
| 43 EXPECT_LE(u1.alloc_bytes + kAllocSize, u3.alloc_bytes); |
| 44 EXPECT_LE(u1.alloc_overhead_bytes, u3.alloc_overhead_bytes); |
| 45 EXPECT_LT(u1.free_ops, u3.free_ops); |
| 46 EXPECT_LE(u1.free_bytes + kAllocSize, u3.free_bytes); |
| 47 EXPECT_LE(kAllocSize, u3.max_allocated_bytes); |
| 48 } |
| 49 |
| 50 TEST_F(ScopedHeapUsageTest, NestedMaxWorks) { |
| 51 ScopedHeapUsage outer_scoped_usage; |
| 52 |
| 53 const size_t kOuterAllocSize = 1029U; |
| 54 void* ptr = malloc(kOuterAllocSize); |
| 55 free(ptr); |
| 56 |
| 57 EXPECT_EQ(kOuterAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); |
| 58 |
| 59 { |
| 60 ScopedHeapUsage inner_scoped_usage; |
| 61 |
| 62 const size_t kInnerAllocSize = 673U; |
| 63 ptr = malloc(kInnerAllocSize); |
| 64 free(ptr); |
| 65 |
| 66 EXPECT_EQ(kInnerAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); |
| 67 } |
| 68 |
| 69 // The greater, outer allocation size should have been restored. |
| 70 EXPECT_EQ(kOuterAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); |
| 71 |
| 72 const size_t kLargerInnerAllocSize = kOuterAllocSize + 673U; |
| 73 { |
| 74 ScopedHeapUsage inner_scoped_usage; |
| 75 |
| 76 ptr = malloc(kLargerInnerAllocSize); |
| 77 free(ptr); |
| 78 |
| 79 EXPECT_EQ(kLargerInnerAllocSize, |
| 80 ScopedHeapUsage::Now().max_allocated_bytes); |
| 81 } |
| 82 |
| 83 // The greater, inner allocation size should have been preserved. |
| 84 EXPECT_EQ(kLargerInnerAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); |
| 85 } |
| 86 |
| 87 } // namespace debug |
| 88 } // namespace base |
OLD | NEW |