Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: components/metrics/leak_detector/ranked_list_unittest.cc

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Mac build fixes: const arg in comparator, rm const in func return type Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/metrics/leak_detector/ranked_list.h"
6
7 #include "base/macros.h"
8 #include "components/metrics/leak_detector/custom_allocator.h"
9 #include "components/metrics/leak_detector/leak_detector_value_type.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace metrics {
13 namespace leak_detector {
14
15 namespace {
16
17 // Makes it easier to instantiate LeakDetectorValueTypes.
18 LeakDetectorValueType Value(uint32_t value) {
19 return LeakDetectorValueType(value);
20 }
21
22 } // namespace
23
24 class RankedListTest : public ::testing::Test {
25 public:
26 RankedListTest() {}
27
28 void SetUp() override { CustomAllocator::Initialize(); }
29 void TearDown() override { EXPECT_TRUE(CustomAllocator::Shutdown()); }
30
31 private:
32 DISALLOW_COPY_AND_ASSIGN(RankedListTest);
33 };
34
35 TEST_F(RankedListTest, Iterators) {
36 RankedList list(10);
37 EXPECT_TRUE(list.begin() == list.end());
38
39 list.Add(Value(0x1234), 100);
40 EXPECT_FALSE(list.begin() == list.end());
41 }
42
43 TEST_F(RankedListTest, SingleInsertion) {
44 RankedList list(10);
45 EXPECT_EQ(0U, list.size());
46
47 list.Add(Value(0x1234), 100);
48 EXPECT_EQ(1U, list.size());
49
50 auto iter = list.begin();
51 EXPECT_EQ(0x1234U, iter->value.size());
52 EXPECT_EQ(100, iter->count);
53 }
54
55 TEST_F(RankedListTest, InOrderInsertion) {
56 RankedList list(10);
57 EXPECT_EQ(0U, list.size());
58
59 list.Add(Value(0x1234), 100);
60 EXPECT_EQ(1U, list.size());
61 list.Add(Value(0x2345), 95);
62 EXPECT_EQ(2U, list.size());
63 list.Add(Value(0x3456), 90);
64 EXPECT_EQ(3U, list.size());
65 list.Add(Value(0x4567), 85);
66 EXPECT_EQ(4U, list.size());
67 list.Add(Value(0x5678), 80);
68 EXPECT_EQ(5U, list.size());
69
70 // Iterate through the contents to make sure they match what went in.
71 const RankedList::Entry kExpectedValues[] = {
72 {Value(0x1234), 100}, {Value(0x2345), 95}, {Value(0x3456), 90},
73 {Value(0x4567), 85}, {Value(0x5678), 80},
74 };
75
76 size_t index = 0;
77 for (const auto& entry : list) {
78 EXPECT_LT(index, arraysize(kExpectedValues));
79 EXPECT_EQ(kExpectedValues[index].value.size(), entry.value.size());
80 EXPECT_EQ(kExpectedValues[index].count, entry.count);
81 ++index;
82 }
83 }
84
85 TEST_F(RankedListTest, ReverseOrderInsertion) {
86 RankedList list(10);
87 EXPECT_EQ(0U, list.size());
88
89 list.Add(Value(0x1234), 0);
90 EXPECT_EQ(1U, list.size());
91 list.Add(Value(0x2345), 5);
92 EXPECT_EQ(2U, list.size());
93 list.Add(Value(0x3456), 10);
94 EXPECT_EQ(3U, list.size());
95 list.Add(Value(0x4567), 15);
96 EXPECT_EQ(4U, list.size());
97 list.Add(Value(0x5678), 20);
98 EXPECT_EQ(5U, list.size());
99
100 // Iterate through the contents to make sure they match what went in.
101 const RankedList::Entry kExpectedValues[] = {
102 {Value(0x5678), 20}, {Value(0x4567), 15}, {Value(0x3456), 10},
103 {Value(0x2345), 5}, {Value(0x1234), 0},
104 };
105
106 size_t index = 0;
107 for (const auto& entry : list) {
108 EXPECT_LT(index, arraysize(kExpectedValues));
109 EXPECT_EQ(kExpectedValues[index].value.size(), entry.value.size());
110 EXPECT_EQ(kExpectedValues[index].count, entry.count);
111 ++index;
112 }
113 }
114
115 TEST_F(RankedListTest, UnorderedInsertion) {
116 RankedList list(10);
117 EXPECT_EQ(0U, list.size());
118
119 list.Add(Value(0x1234), 15);
120 list.Add(Value(0x2345), 20);
121 list.Add(Value(0x3456), 10);
122 list.Add(Value(0x4567), 30);
123 list.Add(Value(0x5678), 25);
124 EXPECT_EQ(5U, list.size());
125
126 // Iterate through the contents to make sure they match what went in.
127 const RankedList::Entry kExpectedValues1[] = {
128 {Value(0x4567), 30}, {Value(0x5678), 25}, {Value(0x2345), 20},
129 {Value(0x1234), 15}, {Value(0x3456), 10},
130 };
131
132 size_t index = 0;
133 for (const auto& entry : list) {
134 EXPECT_LT(index, arraysize(kExpectedValues1));
135 EXPECT_EQ(kExpectedValues1[index].value.size(), entry.value.size());
136 EXPECT_EQ(kExpectedValues1[index].count, entry.count);
137 ++index;
138 }
139
140 // Add more items.
141 list.Add(Value(0x6789), 35);
142 list.Add(Value(0x789a), 40);
143 list.Add(Value(0x89ab), 50);
144 list.Add(Value(0x9abc), 5);
145 list.Add(Value(0xabcd), 0);
146 EXPECT_EQ(10U, list.size());
147
148 // Iterate through the contents to make sure they match what went in.
149 const RankedList::Entry kExpectedValues2[] = {
150 {Value(0x89ab), 50}, {Value(0x789a), 40}, {Value(0x6789), 35},
151 {Value(0x4567), 30}, {Value(0x5678), 25}, {Value(0x2345), 20},
152 {Value(0x1234), 15}, {Value(0x3456), 10}, {Value(0x9abc), 5},
153 {Value(0xabcd), 0},
154 };
155
156 index = 0;
157 for (const auto& entry : list) {
158 EXPECT_LT(index, arraysize(kExpectedValues2));
159 EXPECT_EQ(kExpectedValues2[index].value.size(), entry.value.size());
160 EXPECT_EQ(kExpectedValues2[index].count, entry.count);
161 ++index;
162 }
163 }
164
165 TEST_F(RankedListTest, InsertionWithOverflow) {
166 RankedList list(5);
167 EXPECT_EQ(0U, list.size());
168
169 list.Add(Value(0x1234), 15);
170 list.Add(Value(0x2345), 20);
171 list.Add(Value(0x3456), 10);
172 list.Add(Value(0x4567), 30);
173 list.Add(Value(0x5678), 25);
174 EXPECT_EQ(5U, list.size());
175
176 // These values will not make it into the list, which is now full.
177 list.Add(Value(0x6789), 0);
178 EXPECT_EQ(5U, list.size());
179 list.Add(Value(0x789a), 5);
180 EXPECT_EQ(5U, list.size());
181
182 // Iterate through the contents to make sure they match what went in.
183 const RankedList::Entry kExpectedValues1[] = {
184 {Value(0x4567), 30}, {Value(0x5678), 25}, {Value(0x2345), 20},
185 {Value(0x1234), 15}, {Value(0x3456), 10},
186 };
187
188 size_t index = 0;
189 for (const auto& entry : list) {
190 EXPECT_LT(index, arraysize(kExpectedValues1));
191 EXPECT_EQ(kExpectedValues1[index].value.size(), entry.value.size());
192 EXPECT_EQ(kExpectedValues1[index].count, entry.count);
193 ++index;
194 }
195
196 // Insert some more values that go in the middle of the list.
197 list.Add(Value(0x89ab), 27);
198 EXPECT_EQ(5U, list.size());
199 list.Add(Value(0x9abc), 22);
200 EXPECT_EQ(5U, list.size());
201
202 // Iterate through the contents to make sure they match what went in.
203 const RankedList::Entry kExpectedValues2[] = {
204 {Value(0x4567), 30}, {Value(0x89ab), 27}, {Value(0x5678), 25},
205 {Value(0x9abc), 22}, {Value(0x2345), 20},
206 };
207
208 index = 0;
209 for (const auto& entry : list) {
210 EXPECT_LT(index, arraysize(kExpectedValues2));
211 EXPECT_EQ(kExpectedValues2[index].value.size(), entry.value.size());
212 EXPECT_EQ(kExpectedValues2[index].count, entry.count);
213 ++index;
214 }
215
216 // Insert some more values at the front of the list.
217 list.Add(Value(0xabcd), 40);
218 EXPECT_EQ(5U, list.size());
219 list.Add(Value(0xbcde), 35);
220 EXPECT_EQ(5U, list.size());
221
222 // Iterate through the contents to make sure they match what went in.
223 const RankedList::Entry kExpectedValues3[] = {
224 {Value(0xabcd), 40}, {Value(0xbcde), 35}, {Value(0x4567), 30},
225 {Value(0x89ab), 27}, {Value(0x5678), 25},
226 };
227
228 index = 0;
229 for (const auto& entry : list) {
230 EXPECT_LT(index, arraysize(kExpectedValues3));
231 EXPECT_EQ(kExpectedValues3[index].value.size(), entry.value.size());
232 EXPECT_EQ(kExpectedValues3[index].count, entry.count);
233 ++index;
234 }
235 }
236
237 TEST_F(RankedListTest, MoveOperation) {
238 const RankedList::Entry kExpectedValues[] = {
239 {Value(0x89ab), 50}, {Value(0x789a), 40}, {Value(0x6789), 35},
240 {Value(0x4567), 30}, {Value(0x5678), 25}, {Value(0x2345), 20},
241 {Value(0x1234), 15}, {Value(0x3456), 10}, {Value(0x9abc), 5},
242 {Value(0xabcd), 0},
243 };
244
245 RankedList source_list(10);
246 for (const RankedList::Entry& entry : kExpectedValues) {
247 source_list.Add(entry.value, entry.count);
248 }
249 EXPECT_EQ(10U, source_list.size());
250
251 RankedList dest_list(25); // This should be changed by the move.
252 dest_list = source_list.Pass();
253 EXPECT_EQ(10U, dest_list.size());
254 EXPECT_EQ(10U, dest_list.max_size());
255
256 size_t index = 0;
257 for (const auto& entry : dest_list) {
258 EXPECT_LT(index, arraysize(kExpectedValues));
259 EXPECT_EQ(kExpectedValues[index].value.size(), entry.value.size());
260 EXPECT_EQ(kExpectedValues[index].count, entry.count);
261 ++index;
262 }
263 }
264
265 } // namespace leak_detector
266 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/ranked_list.cc ('k') | components/metrics/leak_detector/stl_allocator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698