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

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

Powered by Google App Engine
This is Rietveld 408576698