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

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: Addressed Alexei's comments Created 5 years, 1 month 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 {
29 CustomAllocator::Initialize();
30 }
31 void TearDown() override {
32 EXPECT_TRUE(CustomAllocator::Shutdown());
33 }
34
35 private:
36 DISALLOW_COPY_AND_ASSIGN(RankedListTest);
37 };
38
39 TEST_F(RankedListTest, Iterators) {
40 RankedList list(10);
41 EXPECT_TRUE(list.begin() == list.end());
42
43 list.Add(Value(0x1234), 100);
44 EXPECT_FALSE(list.begin() == list.end());
45 }
46
47 TEST_F(RankedListTest, SingleInsertion) {
48 RankedList list(10);
49 EXPECT_EQ(0U, list.size());
50
51 list.Add(Value(0x1234), 100);
52 EXPECT_EQ(1U, list.size());
53
54 auto iter = list.begin();
55 EXPECT_EQ(0x1234U, iter->value.size());
56 EXPECT_EQ(100, iter->count);
57 }
58
59 TEST_F(RankedListTest, InOrderInsertion) {
60 RankedList list(10);
61 EXPECT_EQ(0U, list.size());
62
63 list.Add(Value(0x1234), 100);
64 EXPECT_EQ(1U, list.size());
65 list.Add(Value(0x2345), 95);
66 EXPECT_EQ(2U, list.size());
67 list.Add(Value(0x3456), 90);
68 EXPECT_EQ(3U, list.size());
69 list.Add(Value(0x4567), 85);
70 EXPECT_EQ(4U, list.size());
71 list.Add(Value(0x5678), 80);
72 EXPECT_EQ(5U, list.size());
73
74 // Iterate through the contents to make sure they match what went in.
75 const RankedList::Entry kExpectedValues[] = {
76 { Value(0x1234), 100 },
77 { Value(0x2345), 95 },
78 { Value(0x3456), 90 },
79 { Value(0x4567), 85 },
80 { Value(0x5678), 80 },
81 };
82
83 size_t index = 0;
84 for (const auto& entry : list) {
85 EXPECT_LT(index, arraysize(kExpectedValues));
86 EXPECT_EQ(kExpectedValues[index].value.size(), entry.value.size());
87 EXPECT_EQ(kExpectedValues[index].count, entry.count);
88 ++index;
89 }
90 }
91
92 TEST_F(RankedListTest, ReverseOrderInsertion) {
93 RankedList list(10);
94 EXPECT_EQ(0U, list.size());
95
96 list.Add(Value(0x1234), 0);
97 EXPECT_EQ(1U, list.size());
98 list.Add(Value(0x2345), 5);
99 EXPECT_EQ(2U, list.size());
100 list.Add(Value(0x3456), 10);
101 EXPECT_EQ(3U, list.size());
102 list.Add(Value(0x4567), 15);
103 EXPECT_EQ(4U, list.size());
104 list.Add(Value(0x5678), 20);
105 EXPECT_EQ(5U, list.size());
106
107 // Iterate through the contents to make sure they match what went in.
108 const RankedList::Entry kExpectedValues[] = {
109 { Value(0x5678), 20 },
110 { Value(0x4567), 15 },
111 { Value(0x3456), 10 },
112 { Value(0x2345), 5 },
113 { Value(0x1234), 0 },
114 };
115
116 size_t index = 0;
117 for (const auto& entry : list) {
118 EXPECT_LT(index, arraysize(kExpectedValues));
119 EXPECT_EQ(kExpectedValues[index].value.size(), entry.value.size());
120 EXPECT_EQ(kExpectedValues[index].count, entry.count);
121 ++index;
122 }
123 }
124
125 TEST_F(RankedListTest, UnorderedInsertion) {
126 RankedList list(10);
127 EXPECT_EQ(0U, list.size());
128
129 list.Add(Value(0x1234), 15);
130 list.Add(Value(0x2345), 20);
131 list.Add(Value(0x3456), 10);
132 list.Add(Value(0x4567), 30);
133 list.Add(Value(0x5678), 25);
134 EXPECT_EQ(5U, list.size());
135
136 // Iterate through the contents to make sure they match what went in.
137 const RankedList::Entry kExpectedValues1[] = {
138 { Value(0x4567), 30 },
139 { Value(0x5678), 25 },
140 { Value(0x2345), 20 },
141 { Value(0x1234), 15 },
142 { Value(0x3456), 10 },
143 };
144
145 size_t index = 0;
146 for (const auto& entry : list) {
147 EXPECT_LT(index, arraysize(kExpectedValues1));
148 EXPECT_EQ(kExpectedValues1[index].value.size(), entry.value.size());
149 EXPECT_EQ(kExpectedValues1[index].count, entry.count);
150 ++index;
151 }
152
153 // Add more items.
154 list.Add(Value(0x6789), 35);
155 list.Add(Value(0x789a), 40);
156 list.Add(Value(0x89ab), 50);
157 list.Add(Value(0x9abc), 5);
158 list.Add(Value(0xabcd), 0);
159 EXPECT_EQ(10U, list.size());
160
161 // Iterate through the contents to make sure they match what went in.
162 const RankedList::Entry kExpectedValues2[] = {
163 { Value(0x89ab), 50 },
164 { Value(0x789a), 40 },
165 { Value(0x6789), 35 },
166 { Value(0x4567), 30 },
167 { Value(0x5678), 25 },
168 { Value(0x2345), 20 },
169 { Value(0x1234), 15 },
170 { Value(0x3456), 10 },
171 { Value(0x9abc), 5 },
172 { Value(0xabcd), 0 },
173 };
174
175 index = 0;
176 for (const auto& entry : list) {
177 EXPECT_LT(index, arraysize(kExpectedValues2));
178 EXPECT_EQ(kExpectedValues2[index].value.size(), entry.value.size());
179 EXPECT_EQ(kExpectedValues2[index].count, entry.count);
180 ++index;
181 }
182 }
183
184 TEST_F(RankedListTest, InsertionWithOverflow) {
185 RankedList list(5);
186 EXPECT_EQ(0U, list.size());
187
188 list.Add(Value(0x1234), 15);
189 list.Add(Value(0x2345), 20);
190 list.Add(Value(0x3456), 10);
191 list.Add(Value(0x4567), 30);
192 list.Add(Value(0x5678), 25);
193 EXPECT_EQ(5U, list.size());
194
195 // These values will not make it into the list, which is now full.
196 list.Add(Value(0x6789), 0);
197 EXPECT_EQ(5U, list.size());
198 list.Add(Value(0x789a), 5);
199 EXPECT_EQ(5U, list.size());
200
201 // Iterate through the contents to make sure they match what went in.
202 const RankedList::Entry kExpectedValues1[] = {
203 { Value(0x4567), 30 },
204 { Value(0x5678), 25 },
205 { Value(0x2345), 20 },
206 { Value(0x1234), 15 },
207 { Value(0x3456), 10 },
208 };
209
210 size_t index = 0;
211 for (const auto& entry : list) {
212 EXPECT_LT(index, arraysize(kExpectedValues1));
213 EXPECT_EQ(kExpectedValues1[index].value.size(), entry.value.size());
214 EXPECT_EQ(kExpectedValues1[index].count, entry.count);
215 ++index;
216 }
217
218 // Insert some more values that go in the middle of the list.
219 list.Add(Value(0x89ab), 27);
220 EXPECT_EQ(5U, list.size());
221 list.Add(Value(0x9abc), 22);
222 EXPECT_EQ(5U, list.size());
223
224 // Iterate through the contents to make sure they match what went in.
225 const RankedList::Entry kExpectedValues2[] = {
226 { Value(0x4567), 30 },
227 { Value(0x89ab), 27 },
228 { Value(0x5678), 25 },
229 { Value(0x9abc), 22 },
230 { Value(0x2345), 20 },
231 };
232
233 index = 0;
234 for (const auto& entry : list) {
235 EXPECT_LT(index, arraysize(kExpectedValues2));
236 EXPECT_EQ(kExpectedValues2[index].value.size(), entry.value.size());
237 EXPECT_EQ(kExpectedValues2[index].count, entry.count);
238 ++index;
239 }
240
241 // Insert some more values at the front of the list.
242 list.Add(Value(0xabcd), 40);
243 EXPECT_EQ(5U, list.size());
244 list.Add(Value(0xbcde), 35);
245 EXPECT_EQ(5U, list.size());
246
247 // Iterate through the contents to make sure they match what went in.
248 const RankedList::Entry kExpectedValues3[] = {
249 { Value(0xabcd), 40 },
250 { Value(0xbcde), 35 },
251 { Value(0x4567), 30 },
252 { Value(0x89ab), 27 },
253 { Value(0x5678), 25 },
254 };
255
256 index = 0;
257 for (const auto& entry : list) {
258 EXPECT_LT(index, arraysize(kExpectedValues3));
259 EXPECT_EQ(kExpectedValues3[index].value.size(), entry.value.size());
260 EXPECT_EQ(kExpectedValues3[index].count, entry.count);
261 ++index;
262 }
263 }
264
265 TEST_F(RankedListTest, MoveOperation) {
266 const RankedList::Entry kExpectedValues[] = {
267 { Value(0x89ab), 50 },
268 { Value(0x789a), 40 },
269 { Value(0x6789), 35 },
270 { Value(0x4567), 30 },
271 { Value(0x5678), 25 },
272 { Value(0x2345), 20 },
273 { Value(0x1234), 15 },
274 { Value(0x3456), 10 },
275 { Value(0x9abc), 5 },
276 { Value(0xabcd), 0 },
277 };
278
279 RankedList source_list(10);
280 for (const RankedList::Entry& entry : kExpectedValues) {
281 source_list.Add(entry.value, entry.count);
282 }
283 EXPECT_EQ(10U, source_list.size());
284
285 RankedList dest_list(25); // This should be changed by the move.
286 dest_list = source_list.Pass();
287 EXPECT_EQ(10U, dest_list.size());
288 EXPECT_EQ(10U, dest_list.max_size());
289
290 size_t index = 0;
291 for (const auto& entry : dest_list) {
292 EXPECT_LT(index, arraysize(kExpectedValues));
293 EXPECT_EQ(kExpectedValues[index].value.size(), entry.value.size());
294 EXPECT_EQ(kExpectedValues[index].count, entry.count);
295 ++index;
296 }
297 }
298
299 } // namespace leak_detector
300 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698