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

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

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