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

Side by Side Diff: base/metrics/sample_vector_unittest.cc

Issue 2288473002: Implement Dump-on-DCHECK (via a new LogSeverity). (Closed)
Patch Set: Migrate some tests to EXPECT_DCHECK_DEATH Created 4 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/metrics/sample_vector.h" 5 #include "base/metrics/sample_vector.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 samples1.Subtract(samples2); 109 samples1.Subtract(samples2);
110 EXPECT_EQ(100, samples1.GetCountAtIndex(0)); 110 EXPECT_EQ(100, samples1.GetCountAtIndex(0));
111 EXPECT_EQ(0, samples1.GetCountAtIndex(1)); 111 EXPECT_EQ(0, samples1.GetCountAtIndex(1));
112 EXPECT_EQ(100, samples1.GetCountAtIndex(2)); 112 EXPECT_EQ(100, samples1.GetCountAtIndex(2));
113 EXPECT_EQ(100, samples1.GetCountAtIndex(3)); 113 EXPECT_EQ(100, samples1.GetCountAtIndex(3));
114 EXPECT_EQ(600, samples1.sum()); 114 EXPECT_EQ(600, samples1.sum());
115 EXPECT_EQ(300, samples1.TotalCount()); 115 EXPECT_EQ(300, samples1.TotalCount());
116 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); 116 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
117 } 117 }
118 118
119 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
120 TEST(SampleVectorDeathTest, BucketIndexTest) { 119 TEST(SampleVectorDeathTest, BucketIndexTest) {
121 // 8 buckets with exponential layout: 120 // 8 buckets with exponential layout:
122 // [0, 1) [1, 2) [2, 4) [4, 8) [8, 16) [16, 32) [32, 64) [64, INT_MAX) 121 // [0, 1) [1, 2) [2, 4) [4, 8) [8, 16) [16, 32) [32, 64) [64, INT_MAX)
123 BucketRanges ranges(9); 122 BucketRanges ranges(9);
124 Histogram::InitializeBucketRanges(1, 64, &ranges); 123 Histogram::InitializeBucketRanges(1, 64, &ranges);
125 SampleVector samples(1, &ranges); 124 SampleVector samples(1, &ranges);
126 125
127 // Normal case 126 // Normal case
128 samples.Accumulate(0, 1); 127 samples.Accumulate(0, 1);
129 samples.Accumulate(3, 2); 128 samples.Accumulate(3, 2);
130 samples.Accumulate(64, 3); 129 samples.Accumulate(64, 3);
131 EXPECT_EQ(1, samples.GetCount(0)); 130 EXPECT_EQ(1, samples.GetCount(0));
132 EXPECT_EQ(2, samples.GetCount(2)); 131 EXPECT_EQ(2, samples.GetCount(2));
133 EXPECT_EQ(3, samples.GetCount(65)); 132 EXPECT_EQ(3, samples.GetCount(65));
134 133
135 // Extreme case. 134 // Extreme case.
136 EXPECT_DEATH(samples.Accumulate(INT_MIN, 100), ""); 135 EXPECT_DCHECK_DEATH(samples.Accumulate(INT_MIN, 100));
137 EXPECT_DEATH(samples.Accumulate(-1, 100), ""); 136 EXPECT_DCHECK_DEATH(samples.Accumulate(-1, 100));
138 EXPECT_DEATH(samples.Accumulate(INT_MAX, 100), ""); 137 EXPECT_DCHECK_DEATH(samples.Accumulate(INT_MAX, 100));
Wez 2016/12/19 23:57:46 Q: Having DCHECK death-tests run but trivially pas
139 138
140 // Custom buckets: [1, 5) [5, 10) 139 // Custom buckets: [1, 5) [5, 10)
141 // Note, this is not a valid BucketRanges for Histogram because it does not 140 // Note, this is not a valid BucketRanges for Histogram because it does not
142 // have overflow buckets. 141 // have overflow buckets.
143 BucketRanges ranges2(3); 142 BucketRanges ranges2(3);
144 ranges2.set_range(0, 1); 143 ranges2.set_range(0, 1);
145 ranges2.set_range(1, 5); 144 ranges2.set_range(1, 5);
146 ranges2.set_range(2, 10); 145 ranges2.set_range(2, 10);
147 SampleVector samples2(2, &ranges2); 146 SampleVector samples2(2, &ranges2);
148 147
149 // Normal case. 148 // Normal case.
150 samples2.Accumulate(1, 1); 149 samples2.Accumulate(1, 1);
151 samples2.Accumulate(4, 1); 150 samples2.Accumulate(4, 1);
152 samples2.Accumulate(5, 2); 151 samples2.Accumulate(5, 2);
153 samples2.Accumulate(9, 2); 152 samples2.Accumulate(9, 2);
154 EXPECT_EQ(2, samples2.GetCount(1)); 153 EXPECT_EQ(2, samples2.GetCount(1));
155 EXPECT_EQ(4, samples2.GetCount(5)); 154 EXPECT_EQ(4, samples2.GetCount(5));
156 155
157 // Extreme case. 156 // Extreme case.
158 EXPECT_DEATH(samples2.Accumulate(0, 100), ""); 157 EXPECT_DCHECK_DEATH(samples2.Accumulate(0, 100));
159 EXPECT_DEATH(samples2.Accumulate(10, 100), ""); 158 EXPECT_DCHECK_DEATH(samples2.Accumulate(10, 100));
160 } 159 }
161 160
162 TEST(SampleVectorDeathTest, AddSubtractBucketNotMatchTest) { 161 TEST(SampleVectorDeathTest, AddSubtractBucketNotMatchTest) {
163 // Custom buckets 1: [1, 3) [3, 5) 162 // Custom buckets 1: [1, 3) [3, 5)
164 BucketRanges ranges1(3); 163 BucketRanges ranges1(3);
165 ranges1.set_range(0, 1); 164 ranges1.set_range(0, 1);
166 ranges1.set_range(1, 3); 165 ranges1.set_range(1, 3);
167 ranges1.set_range(2, 5); 166 ranges1.set_range(2, 5);
168 SampleVector samples1(1, &ranges1); 167 SampleVector samples1(1, &ranges1);
169 168
170 // Custom buckets 2: [0, 1) [1, 3) [3, 6) [6, 7) 169 // Custom buckets 2: [0, 1) [1, 3) [3, 6) [6, 7)
171 BucketRanges ranges2(5); 170 BucketRanges ranges2(5);
172 ranges2.set_range(0, 0); 171 ranges2.set_range(0, 0);
173 ranges2.set_range(1, 1); 172 ranges2.set_range(1, 1);
174 ranges2.set_range(2, 3); 173 ranges2.set_range(2, 3);
175 ranges2.set_range(3, 6); 174 ranges2.set_range(3, 6);
176 ranges2.set_range(4, 7); 175 ranges2.set_range(4, 7);
177 SampleVector samples2(2, &ranges2); 176 SampleVector samples2(2, &ranges2);
178 177
179 samples2.Accumulate(1, 100); 178 samples2.Accumulate(1, 100);
180 samples1.Add(samples2); 179 samples1.Add(samples2);
181 EXPECT_EQ(100, samples1.GetCountAtIndex(0)); 180 EXPECT_EQ(100, samples1.GetCountAtIndex(0));
182 181
183 // Extra bucket in the beginning. 182 // Extra bucket in the beginning.
184 samples2.Accumulate(0, 100); 183 samples2.Accumulate(0, 100);
185 EXPECT_DEATH(samples1.Add(samples2), ""); 184 EXPECT_DCHECK_DEATH(samples1.Add(samples2));
186 EXPECT_DEATH(samples1.Subtract(samples2), ""); 185 EXPECT_DCHECK_DEATH(samples1.Subtract(samples2));
187 186
188 // Extra bucket in the end. 187 // Extra bucket in the end.
189 samples2.Accumulate(0, -100); 188 samples2.Accumulate(0, -100);
190 samples2.Accumulate(6, 100); 189 samples2.Accumulate(6, 100);
191 EXPECT_DEATH(samples1.Add(samples2), ""); 190 EXPECT_DCHECK_DEATH(samples1.Add(samples2));
192 EXPECT_DEATH(samples1.Subtract(samples2), ""); 191 EXPECT_DCHECK_DEATH(samples1.Subtract(samples2));
193 192
194 // Bucket not match: [3, 5) VS [3, 6) 193 // Bucket not match: [3, 5) VS [3, 6)
195 samples2.Accumulate(6, -100); 194 samples2.Accumulate(6, -100);
196 samples2.Accumulate(3, 100); 195 samples2.Accumulate(3, 100);
197 EXPECT_DEATH(samples1.Add(samples2), ""); 196 EXPECT_DCHECK_DEATH(samples1.Add(samples2));
198 EXPECT_DEATH(samples1.Subtract(samples2), ""); 197 EXPECT_DCHECK_DEATH(samples1.Subtract(samples2));
199 } 198 }
200 199
201 #endif
202 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
203
204 TEST(SampleVectorIteratorTest, IterateTest) { 200 TEST(SampleVectorIteratorTest, IterateTest) {
205 BucketRanges ranges(5); 201 BucketRanges ranges(5);
206 ranges.set_range(0, 0); 202 ranges.set_range(0, 0);
207 ranges.set_range(1, 1); 203 ranges.set_range(1, 1);
208 ranges.set_range(2, 2); 204 ranges.set_range(2, 2);
209 ranges.set_range(3, 3); 205 ranges.set_range(3, 3);
210 ranges.set_range(4, 4); 206 ranges.set_range(4, 4);
211 207
212 std::vector<HistogramBase::Count> counts(3); 208 std::vector<HistogramBase::Count> counts(3);
213 counts[0] = 1; 209 counts[0] = 1;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 277
282 EXPECT_DCHECK_DEATH(it->Next()); 278 EXPECT_DCHECK_DEATH(it->Next());
283 279
284 samples.Accumulate(2, 100); 280 samples.Accumulate(2, 100);
285 it = samples.Iterator(); 281 it = samples.Iterator();
286 EXPECT_FALSE(it->Done()); 282 EXPECT_FALSE(it->Done());
287 } 283 }
288 284
289 } // namespace 285 } // namespace
290 } // namespace base 286 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698