Index: base/debug/scoped_heap_usage_unittest.cc |
diff --git a/base/debug/scoped_heap_usage_unittest.cc b/base/debug/scoped_heap_usage_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bfe247c99040076942e717ffe28f1077a0efaac2 |
--- /dev/null |
+++ b/base/debug/scoped_heap_usage_unittest.cc |
@@ -0,0 +1,88 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/debug/scoped_heap_usage.h" |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace base { |
+namespace debug { |
+ |
+namespace { |
+ |
+class ScopedHeapUsageTest : public testing::Test { |
+ public: |
+ void SetUp() override { ScopedHeapUsage::Initialize(); } |
+ |
+ void TearDown() override { ScopedHeapUsage::TearDownForTesting(); } |
+}; |
+ |
+} // namespace |
+ |
+TEST_F(ScopedHeapUsageTest, SimpleUsage) { |
+ ScopedHeapUsage scoped_usage; |
+ |
+ ScopedHeapUsage::AllocatorUsage u1 = scoped_usage.usage_at_creation(); |
+ ScopedHeapUsage::AllocatorUsage u2 = ScopedHeapUsage::Now(); |
+ |
+ EXPECT_EQ(u1.alloc_ops, u2.alloc_ops); |
+ EXPECT_EQ(u1.alloc_bytes, u2.alloc_bytes); |
+ EXPECT_EQ(u1.alloc_overhead_bytes, u2.alloc_overhead_bytes); |
+ EXPECT_EQ(u1.free_ops, u2.free_ops); |
+ EXPECT_EQ(u1.free_bytes, u2.free_bytes); |
+ EXPECT_EQ(0U, u2.max_allocated_bytes); |
+ |
+ const size_t kAllocSize = 1029U; |
+ void* ptr = malloc(kAllocSize); |
+ free(ptr); |
+ |
+ ScopedHeapUsage::AllocatorUsage u3 = ScopedHeapUsage::Now(); |
+ |
+ EXPECT_LT(u1.alloc_ops, u3.alloc_ops); |
+ EXPECT_LE(u1.alloc_bytes + kAllocSize, u3.alloc_bytes); |
+ EXPECT_LE(u1.alloc_overhead_bytes, u3.alloc_overhead_bytes); |
+ EXPECT_LT(u1.free_ops, u3.free_ops); |
+ EXPECT_LE(u1.free_bytes + kAllocSize, u3.free_bytes); |
+ EXPECT_LE(kAllocSize, u3.max_allocated_bytes); |
+} |
+ |
+TEST_F(ScopedHeapUsageTest, NestedMaxWorks) { |
+ ScopedHeapUsage outer_scoped_usage; |
+ |
+ const size_t kOuterAllocSize = 1029U; |
+ void* ptr = malloc(kOuterAllocSize); |
+ free(ptr); |
+ |
+ EXPECT_EQ(kOuterAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); |
+ |
+ { |
+ ScopedHeapUsage inner_scoped_usage; |
+ |
+ const size_t kInnerAllocSize = 673U; |
+ ptr = malloc(kInnerAllocSize); |
+ free(ptr); |
+ |
+ EXPECT_EQ(kInnerAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); |
+ } |
+ |
+ // The greater, outer allocation size should have been restored. |
+ EXPECT_EQ(kOuterAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); |
+ |
+ const size_t kLargerInnerAllocSize = kOuterAllocSize + 673U; |
+ { |
+ ScopedHeapUsage inner_scoped_usage; |
+ |
+ ptr = malloc(kLargerInnerAllocSize); |
+ free(ptr); |
+ |
+ EXPECT_EQ(kLargerInnerAllocSize, |
+ ScopedHeapUsage::Now().max_allocated_bytes); |
+ } |
+ |
+ // The greater, inner allocation size should have been preserved. |
+ EXPECT_EQ(kLargerInnerAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); |
+} |
+ |
+} // namespace debug |
+} // namespace base |