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

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

Powered by Google App Engine
This is Rietveld 408576698