OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "base/metrics/persistent_histogram_allocator.h" | 5 #include "base/metrics/persistent_histogram_allocator.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/metrics/bucket_ranges.h" | 9 #include "base/metrics/bucket_ranges.h" |
9 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
10 #include "base/metrics/persistent_memory_allocator.h" | 11 #include "base/metrics/persistent_memory_allocator.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 | 15 |
15 class PersistentHistogramAllocatorTest : public testing::Test { | 16 class PersistentHistogramAllocatorTest : public testing::Test { |
16 protected: | 17 protected: |
17 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB | 18 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB |
(...skipping 14 matching lines...) Expand all Loading... |
32 "PersistentHistogramAllocatorTest"); | 33 "PersistentHistogramAllocatorTest"); |
33 allocator_ = | 34 allocator_ = |
34 PersistentHistogramAllocator::GetGlobalAllocator()->memory_allocator(); | 35 PersistentHistogramAllocator::GetGlobalAllocator()->memory_allocator(); |
35 } | 36 } |
36 | 37 |
37 void DestroyPersistentHistogramAllocator() { | 38 void DestroyPersistentHistogramAllocator() { |
38 allocator_ = nullptr; | 39 allocator_ = nullptr; |
39 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); | 40 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
40 } | 41 } |
41 | 42 |
42 scoped_ptr<char[]> allocator_memory_; | 43 std::unique_ptr<char[]> allocator_memory_; |
43 PersistentMemoryAllocator* allocator_ = nullptr; | 44 PersistentMemoryAllocator* allocator_ = nullptr; |
44 | 45 |
45 private: | 46 private: |
46 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); | 47 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); |
47 }; | 48 }; |
48 | 49 |
49 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { | 50 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { |
50 PersistentMemoryAllocator::MemoryInfo meminfo0; | 51 PersistentMemoryAllocator::MemoryInfo meminfo0; |
51 allocator_->GetMemoryInfo(&meminfo0); | 52 allocator_->GetMemoryInfo(&meminfo0); |
52 | 53 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 PersistentMemoryAllocator::Iterator iter; | 90 PersistentMemoryAllocator::Iterator iter; |
90 uint32_t type; | 91 uint32_t type; |
91 allocator_->CreateIterator(&iter); | 92 allocator_->CreateIterator(&iter); |
92 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // Histogram | 93 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // Histogram |
93 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // LinearHistogram | 94 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // LinearHistogram |
94 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // BooleanHistogram | 95 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // BooleanHistogram |
95 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // CustomHistogram | 96 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // CustomHistogram |
96 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | 97 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
97 | 98 |
98 // Create a second allocator and have it access the memory of the first. | 99 // Create a second allocator and have it access the memory of the first. |
99 scoped_ptr<HistogramBase> recovered; | 100 std::unique_ptr<HistogramBase> recovered; |
100 PersistentHistogramAllocator recovery( | 101 PersistentHistogramAllocator recovery( |
101 make_scoped_ptr(new PersistentMemoryAllocator( | 102 WrapUnique(new PersistentMemoryAllocator( |
102 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, "", false))); | 103 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, "", false))); |
103 PersistentHistogramAllocator::Iterator histogram_iter; | 104 PersistentHistogramAllocator::Iterator histogram_iter; |
104 recovery.CreateIterator(&histogram_iter); | 105 recovery.CreateIterator(&histogram_iter); |
105 | 106 |
106 recovered = recovery.GetNextHistogram(&histogram_iter); | 107 recovered = recovery.GetNextHistogram(&histogram_iter); |
107 ASSERT_TRUE(recovered); | 108 ASSERT_TRUE(recovered); |
108 recovered->CheckName("TestHistogram"); | 109 recovered->CheckName("TestHistogram"); |
109 | 110 |
110 recovered = recovery.GetNextHistogram(&histogram_iter); | 111 recovered = recovery.GetNextHistogram(&histogram_iter); |
111 ASSERT_TRUE(recovered); | 112 ASSERT_TRUE(recovered); |
112 recovered->CheckName("TestLinearHistogram"); | 113 recovered->CheckName("TestLinearHistogram"); |
113 | 114 |
114 recovered = recovery.GetNextHistogram(&histogram_iter); | 115 recovered = recovery.GetNextHistogram(&histogram_iter); |
115 ASSERT_TRUE(recovered); | 116 ASSERT_TRUE(recovered); |
116 recovered->CheckName("TestBooleanHistogram"); | 117 recovered->CheckName("TestBooleanHistogram"); |
117 | 118 |
118 recovered = recovery.GetNextHistogram(&histogram_iter); | 119 recovered = recovery.GetNextHistogram(&histogram_iter); |
119 ASSERT_TRUE(recovered); | 120 ASSERT_TRUE(recovered); |
120 recovered->CheckName("TestCustomHistogram"); | 121 recovered->CheckName("TestCustomHistogram"); |
121 | 122 |
122 recovered = recovery.GetNextHistogram(&histogram_iter); | 123 recovered = recovery.GetNextHistogram(&histogram_iter); |
123 EXPECT_FALSE(recovered); | 124 EXPECT_FALSE(recovered); |
124 } | 125 } |
125 | 126 |
126 } // namespace base | 127 } // namespace base |
OLD | NEW |