OLD | NEW |
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 // Test of Histogram class | 5 // Test of Histogram class |
6 | 6 |
| 7 #include <climits> |
7 #include <algorithm> | 8 #include <algorithm> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/metrics/bucket_ranges.h" | 13 #include "base/metrics/bucket_ranges.h" |
13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
14 #include "base/metrics/statistics_recorder.h" | 15 #include "base/metrics/statistics_recorder.h" |
15 #include "base/time.h" | 16 #include "base/time.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 using std::vector; | 19 using std::vector; |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 | 22 |
| 23 TEST(BucketHistogramSamplesTest, AccumulateTest) { |
| 24 // Custom buckets: [1, 5) [5, 10) |
| 25 BucketRanges ranges(3); |
| 26 ranges.set_range(0, 1); |
| 27 ranges.set_range(1, 5); |
| 28 ranges.set_range(2, 10); |
| 29 BucketHistogramSamples samples(&ranges); |
| 30 |
| 31 samples.Accumulate(1, 200); |
| 32 samples.Accumulate(2, -300); |
| 33 EXPECT_EQ(-100, samples.GetCountFromBucketIndex(0)); |
| 34 |
| 35 samples.Accumulate(5, 200); |
| 36 EXPECT_EQ(200, samples.GetCountFromBucketIndex(1)); |
| 37 |
| 38 EXPECT_EQ(600, samples.sum()); |
| 39 EXPECT_EQ(100, samples.redundant_count()); |
| 40 EXPECT_EQ(samples.TotalCount(), samples.redundant_count()); |
| 41 |
| 42 samples.Accumulate(5, -100); |
| 43 EXPECT_EQ(100, samples.GetCountFromBucketIndex(1)); |
| 44 |
| 45 EXPECT_EQ(100, samples.sum()); |
| 46 EXPECT_EQ(0, samples.redundant_count()); |
| 47 EXPECT_EQ(samples.TotalCount(), samples.redundant_count()); |
| 48 } |
| 49 |
| 50 TEST(BucketHistogramSamplesDeathTest, BucketIndexTest) { |
| 51 // 8 buckets with exponential layout: |
| 52 // [0, 1) [1, 2) [2, 4) [4, 8) [8, 16) [16, 32) [32, 64) [64, INT_MAX) |
| 53 BucketRanges ranges(9); |
| 54 Histogram::InitializeBucketRanges(1, 64, 8, &ranges); |
| 55 BucketHistogramSamples samples(&ranges); |
| 56 |
| 57 // Normal case |
| 58 samples.Accumulate(0, 1); |
| 59 samples.Accumulate(3, 2); |
| 60 samples.Accumulate(64, 3); |
| 61 EXPECT_EQ(1, samples.GetCount(0)); |
| 62 EXPECT_EQ(2, samples.GetCount(2)); |
| 63 EXPECT_EQ(3, samples.GetCount(65)); |
| 64 |
| 65 // Extreme case. |
| 66 EXPECT_DEATH(samples.Accumulate(INT_MIN, 100), ""); |
| 67 EXPECT_DEATH(samples.Accumulate(-1, 100), ""); |
| 68 EXPECT_DEATH(samples.Accumulate(INT_MAX, 100), ""); |
| 69 |
| 70 // Custom buckets: [1, 5) [5, 10) |
| 71 // Note, this is not a valid BucketRanges for Histogram because it does not |
| 72 // have overflow buckets. |
| 73 BucketRanges ranges2(3); |
| 74 ranges2.set_range(0, 1); |
| 75 ranges2.set_range(1, 5); |
| 76 ranges2.set_range(2, 10); |
| 77 BucketHistogramSamples samples2(&ranges2); |
| 78 |
| 79 // Normal case. |
| 80 samples2.Accumulate(1, 1); |
| 81 samples2.Accumulate(4, 1); |
| 82 samples2.Accumulate(5, 2); |
| 83 samples2.Accumulate(9, 2); |
| 84 EXPECT_EQ(2, samples2.GetCount(1)); |
| 85 EXPECT_EQ(4, samples2.GetCount(5)); |
| 86 |
| 87 // Extreme case. |
| 88 EXPECT_DEATH(samples2.Accumulate(0, 100), ""); |
| 89 EXPECT_DEATH(samples2.Accumulate(10, 100), ""); |
| 90 } |
| 91 |
| 92 TEST(BucketHistogramSamplesTest, AddSubtractTest) { |
| 93 // Custom buckets: [0, 1) [1, 2) [2, 3) [3, INT_MAX) |
| 94 BucketRanges ranges(5); |
| 95 ranges.set_range(0, 0); |
| 96 ranges.set_range(1, 1); |
| 97 ranges.set_range(2, 2); |
| 98 ranges.set_range(3, 3); |
| 99 ranges.set_range(4, INT_MAX); |
| 100 |
| 101 BucketHistogramSamples samples1(&ranges); |
| 102 samples1.Accumulate(0, 100); |
| 103 samples1.Accumulate(2, 100); |
| 104 samples1.Accumulate(4, 100); |
| 105 EXPECT_EQ(600, samples1.sum()); |
| 106 EXPECT_EQ(300, samples1.TotalCount()); |
| 107 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 108 |
| 109 BucketHistogramSamples samples2(&ranges); |
| 110 samples2.Accumulate(1, 200); |
| 111 samples2.Accumulate(2, 200); |
| 112 samples2.Accumulate(4, 200); |
| 113 EXPECT_EQ(1400, samples2.sum()); |
| 114 EXPECT_EQ(600, samples2.TotalCount()); |
| 115 EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount()); |
| 116 |
| 117 samples1.Add(samples2); |
| 118 EXPECT_EQ(100, samples1.GetCountFromBucketIndex(0)); |
| 119 EXPECT_EQ(200, samples1.GetCountFromBucketIndex(1)); |
| 120 EXPECT_EQ(300, samples1.GetCountFromBucketIndex(2)); |
| 121 EXPECT_EQ(300, samples1.GetCountFromBucketIndex(3)); |
| 122 EXPECT_EQ(2000, samples1.sum()); |
| 123 EXPECT_EQ(900, samples1.TotalCount()); |
| 124 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 125 |
| 126 samples1.Subtract(samples2); |
| 127 EXPECT_EQ(100, samples1.GetCountFromBucketIndex(0)); |
| 128 EXPECT_EQ(0, samples1.GetCountFromBucketIndex(1)); |
| 129 EXPECT_EQ(100, samples1.GetCountFromBucketIndex(2)); |
| 130 EXPECT_EQ(100, samples1.GetCountFromBucketIndex(3)); |
| 131 EXPECT_EQ(600, samples1.sum()); |
| 132 EXPECT_EQ(300, samples1.TotalCount()); |
| 133 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 134 } |
| 135 |
| 136 TEST(BucketHistogramSamplesDeathTest, AddSubtractBucketNotMatchTest) { |
| 137 // Custom buckets 1: [1, 3) [3, 5) |
| 138 BucketRanges ranges1(3); |
| 139 ranges1.set_range(0, 1); |
| 140 ranges1.set_range(1, 3); |
| 141 ranges1.set_range(2, 5); |
| 142 BucketHistogramSamples samples1(&ranges1); |
| 143 |
| 144 // Custom buckets 2: [0, 1) [1, 3) [3, 6) [6, 7) |
| 145 BucketRanges ranges2(5); |
| 146 ranges2.set_range(0, 0); |
| 147 ranges2.set_range(1, 1); |
| 148 ranges2.set_range(2, 3); |
| 149 ranges2.set_range(3, 6); |
| 150 ranges2.set_range(4, 7); |
| 151 BucketHistogramSamples samples2(&ranges2); |
| 152 |
| 153 samples2.Accumulate(1, 100); |
| 154 samples1.Add(samples2); |
| 155 EXPECT_EQ(100, samples1.GetCountFromBucketIndex(0)); |
| 156 |
| 157 // Extra bucket in the beginning. |
| 158 samples2.Accumulate(0, 100); |
| 159 EXPECT_DEATH(samples1.Add(samples2), ""); |
| 160 EXPECT_DEATH(samples1.Subtract(samples2), ""); |
| 161 |
| 162 // Extra bucket in the end. |
| 163 samples2.Accumulate(0, -100); |
| 164 samples2.Accumulate(6, 100); |
| 165 EXPECT_DEATH(samples1.Add(samples2), ""); |
| 166 EXPECT_DEATH(samples1.Subtract(samples2), ""); |
| 167 |
| 168 // Bucket not match: [3, 5) VS [3, 6) |
| 169 samples2.Accumulate(6, -100); |
| 170 samples2.Accumulate(3, 100); |
| 171 EXPECT_DEATH(samples1.Add(samples2), ""); |
| 172 EXPECT_DEATH(samples1.Subtract(samples2), ""); |
| 173 } |
| 174 |
| 175 TEST(BucketHistogramSamplesTest, SerializationTest) { |
| 176 } |
| 177 |
| 178 TEST(BucketHistogramSamplesIteratorTest, IterateTest) { |
| 179 BucketRanges ranges(5); |
| 180 ranges.set_range(0, 0); |
| 181 ranges.set_range(1, 1); |
| 182 ranges.set_range(2, 2); |
| 183 ranges.set_range(3, 3); |
| 184 ranges.set_range(4, 4); |
| 185 |
| 186 vector<HistogramBase::Count> counts(3); |
| 187 counts[0] = 1; |
| 188 counts[1] = 0; // Iterator will bypass this empty bucket. |
| 189 counts[2] = 2; |
| 190 |
| 191 // BucketRanges can have larger size than counts. |
| 192 BucketHistogramSamplesIterator it(&counts, &ranges); |
| 193 |
| 194 HistogramBase::Sample min; |
| 195 HistogramBase::Sample max; |
| 196 HistogramBase::Count count; |
| 197 it.Get(&min, &max, &count); |
| 198 EXPECT_EQ(0, min); |
| 199 EXPECT_EQ(1, max); |
| 200 EXPECT_EQ(1, count); |
| 201 |
| 202 it.Next(); |
| 203 it.Get(&min, &max, &count); |
| 204 EXPECT_EQ(2, min); |
| 205 EXPECT_EQ(3, max); |
| 206 EXPECT_EQ(2, count); |
| 207 |
| 208 it.Next(); |
| 209 EXPECT_TRUE(it.Done()); |
| 210 |
| 211 // Create iterator from BucketHistogramSamples. |
| 212 BucketHistogramSamples samples(&ranges); |
| 213 samples.Accumulate(0, 0); |
| 214 samples.Accumulate(1, 1); |
| 215 samples.Accumulate(2, 2); |
| 216 samples.Accumulate(3, 3); |
| 217 scoped_ptr<SampleCountIterator> it2 = samples.Iterator(); |
| 218 |
| 219 int i; |
| 220 for (i = 1; !it2->Done(); i++, it2->Next()) { |
| 221 it2->Get(&min, &max, &count); |
| 222 EXPECT_EQ(i, min); |
| 223 EXPECT_EQ(i + 1, max); |
| 224 EXPECT_EQ(i, count); |
| 225 } |
| 226 EXPECT_EQ(4, i); |
| 227 } |
| 228 |
| 229 TEST(BucketHistogramSamplesIteratorDeathTest, IterateDoneTest) { |
| 230 BucketRanges ranges(5); |
| 231 ranges.set_range(0, 0); |
| 232 ranges.set_range(1, 1); |
| 233 ranges.set_range(2, 2); |
| 234 ranges.set_range(3, 3); |
| 235 ranges.set_range(4, INT_MAX); |
| 236 BucketHistogramSamples samples(&ranges); |
| 237 |
| 238 scoped_ptr<SampleCountIterator> it = samples.Iterator(); |
| 239 |
| 240 EXPECT_TRUE(it->Done()); |
| 241 |
| 242 HistogramBase::Sample min; |
| 243 HistogramBase::Sample max; |
| 244 HistogramBase::Count count; |
| 245 EXPECT_DEATH(it->Get(&min, &max, &count), ""); |
| 246 |
| 247 EXPECT_DEATH(it->Next(), ""); |
| 248 |
| 249 samples.Accumulate(2, 100); |
| 250 it = samples.Iterator(); |
| 251 EXPECT_FALSE(it->Done()); |
| 252 } |
| 253 |
22 // Check for basic syntax and use. | 254 // Check for basic syntax and use. |
23 TEST(HistogramTest, BasicTest) { | 255 TEST(HistogramTest, BasicTest) { |
24 // Try basic construction | 256 // Try basic construction |
25 Histogram* histogram(Histogram::FactoryGet( | 257 Histogram* histogram(Histogram::FactoryGet( |
26 "TestHistogram", 1, 1000, 10, Histogram::kNoFlags)); | 258 "TestHistogram", 1, 1000, 10, Histogram::kNoFlags)); |
27 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), histogram); | 259 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), histogram); |
28 | 260 |
29 Histogram* linear_histogram(LinearHistogram::FactoryGet( | 261 Histogram* linear_histogram(LinearHistogram::FactoryGet( |
30 "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags)); | 262 "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags)); |
31 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), linear_histogram); | 263 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), linear_histogram); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 "Bounded", 10, 100, kBucketCount, Histogram::kNoFlags)); | 431 "Bounded", 10, 100, kBucketCount, Histogram::kNoFlags)); |
200 | 432 |
201 // Put two samples "out of bounds" above and below. | 433 // Put two samples "out of bounds" above and below. |
202 histogram->Add(5); | 434 histogram->Add(5); |
203 histogram->Add(-50); | 435 histogram->Add(-50); |
204 | 436 |
205 histogram->Add(100); | 437 histogram->Add(100); |
206 histogram->Add(10000); | 438 histogram->Add(10000); |
207 | 439 |
208 // Verify they landed in the underflow, and overflow buckets. | 440 // Verify they landed in the underflow, and overflow buckets. |
209 Histogram::SampleSet sample; | 441 scoped_ptr<BucketHistogramSamples> samples = histogram->SnapshotSamples(); |
210 histogram->SnapshotSample(&sample); | 442 EXPECT_EQ(2, samples->GetCountFromBucketIndex(0)); |
211 EXPECT_EQ(2, sample.counts(0)); | 443 EXPECT_EQ(0, samples->GetCountFromBucketIndex(1)); |
212 EXPECT_EQ(0, sample.counts(1)); | |
213 size_t array_size = histogram->bucket_count(); | 444 size_t array_size = histogram->bucket_count(); |
214 EXPECT_EQ(kBucketCount, array_size); | 445 EXPECT_EQ(kBucketCount, array_size); |
215 EXPECT_EQ(0, sample.counts(array_size - 2)); | 446 EXPECT_EQ(0, samples->GetCountFromBucketIndex(array_size - 2)); |
216 EXPECT_EQ(2, sample.counts(array_size - 1)); | 447 EXPECT_EQ(2, samples->GetCountFromBucketIndex(array_size - 1)); |
217 | 448 |
218 vector<int> custom_ranges; | 449 vector<int> custom_ranges; |
219 custom_ranges.push_back(10); | 450 custom_ranges.push_back(10); |
220 custom_ranges.push_back(50); | 451 custom_ranges.push_back(50); |
221 custom_ranges.push_back(100); | 452 custom_ranges.push_back(100); |
222 Histogram* test_custom_histogram(CustomHistogram::FactoryGet( | 453 Histogram* test_custom_histogram(CustomHistogram::FactoryGet( |
223 "TestCustomRangeBoundedHistogram", custom_ranges, Histogram::kNoFlags)); | 454 "TestCustomRangeBoundedHistogram", custom_ranges, Histogram::kNoFlags)); |
224 | 455 |
225 // Put two samples "out of bounds" above and below. | 456 // Put two samples "out of bounds" above and below. |
226 test_custom_histogram->Add(5); | 457 test_custom_histogram->Add(5); |
227 test_custom_histogram->Add(-50); | 458 test_custom_histogram->Add(-50); |
228 test_custom_histogram->Add(100); | 459 test_custom_histogram->Add(100); |
229 test_custom_histogram->Add(1000); | 460 test_custom_histogram->Add(1000); |
| 461 test_custom_histogram->Add(INT_MAX); |
230 | 462 |
231 // Verify they landed in the underflow, and overflow buckets. | 463 // Verify they landed in the underflow, and overflow buckets. |
232 Histogram::SampleSet custom_sample; | 464 scoped_ptr<BucketHistogramSamples> custom_samples = |
233 test_custom_histogram->SnapshotSample(&custom_sample); | 465 test_custom_histogram->SnapshotSamples(); |
234 EXPECT_EQ(2, custom_sample.counts(0)); | 466 EXPECT_EQ(2, custom_samples->GetCountFromBucketIndex(0)); |
235 EXPECT_EQ(0, custom_sample.counts(1)); | 467 EXPECT_EQ(0, custom_samples->GetCountFromBucketIndex(1)); |
236 size_t custom_array_size = test_custom_histogram->bucket_count(); | 468 size_t bucket_count = test_custom_histogram->bucket_count(); |
237 EXPECT_EQ(0, custom_sample.counts(custom_array_size - 2)); | 469 EXPECT_EQ(0, custom_samples->GetCountFromBucketIndex(bucket_count - 2)); |
238 EXPECT_EQ(2, custom_sample.counts(custom_array_size - 1)); | 470 EXPECT_EQ(3, custom_samples->GetCountFromBucketIndex(bucket_count - 1)); |
239 } | 471 } |
240 | 472 |
241 // Check to be sure samples land as expected is "correct" buckets. | 473 // Check to be sure samples land as expected is "correct" buckets. |
242 TEST(HistogramTest, BucketPlacementTest) { | 474 TEST(HistogramTest, BucketPlacementTest) { |
243 Histogram* histogram(Histogram::FactoryGet( | 475 Histogram* histogram(Histogram::FactoryGet( |
244 "Histogram", 1, 64, 8, Histogram::kNoFlags)); | 476 "Histogram", 1, 64, 8, Histogram::kNoFlags)); |
245 | 477 |
246 // Add i+1 samples to the i'th bucket. | 478 // Add i+1 samples to the i'th bucket. |
247 histogram->Add(0); | 479 histogram->Add(0); |
248 int power_of_2 = 1; | 480 int power_of_2 = 1; |
249 for (int i = 1; i < 8; i++) { | 481 for (int i = 1; i < 8; i++) { |
250 for (int j = 0; j <= i; j++) | 482 for (int j = 0; j <= i; j++) |
251 histogram->Add(power_of_2); | 483 histogram->Add(power_of_2); |
252 power_of_2 *= 2; | 484 power_of_2 *= 2; |
253 } | 485 } |
254 | 486 |
255 // Check to see that the bucket counts reflect our additions. | 487 // Check to see that the bucket counts reflect our additions. |
256 Histogram::SampleSet sample; | 488 scoped_ptr<BucketHistogramSamples> samples = histogram->SnapshotSamples(); |
257 histogram->SnapshotSample(&sample); | |
258 for (int i = 0; i < 8; i++) | 489 for (int i = 0; i < 8; i++) |
259 EXPECT_EQ(i + 1, sample.counts(i)); | 490 EXPECT_EQ(i + 1, samples->GetCountFromBucketIndex(i)); |
260 } | 491 } |
261 | 492 |
262 TEST(HistogramTest, CorruptSampleCounts) { | 493 TEST(HistogramTest, CorruptSampleCounts) { |
263 Histogram* histogram(Histogram::FactoryGet( | 494 Histogram* histogram(Histogram::FactoryGet( |
264 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. | 495 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. |
265 | 496 |
266 EXPECT_EQ(0, histogram->sample_.redundant_count()); | 497 // Add some samples. |
267 histogram->Add(20); // Add some samples. | 498 histogram->Add(20); |
268 histogram->Add(40); | 499 histogram->Add(40); |
269 EXPECT_EQ(2, histogram->sample_.redundant_count()); | |
270 | 500 |
271 Histogram::SampleSet snapshot; | 501 scoped_ptr<BucketHistogramSamples> snapshot = histogram->SnapshotSamples(); |
272 histogram->SnapshotSample(&snapshot); | 502 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, |
273 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); | 503 histogram->FindCorruption(*snapshot)); |
274 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. | 504 EXPECT_EQ(2, snapshot->redundant_count()); |
275 EXPECT_EQ(2, snapshot.redundant_count()); | 505 EXPECT_EQ(2, snapshot->TotalCount()); |
276 | 506 |
277 snapshot.counts_[3] += 100; // Sample count won't match redundant count. | 507 snapshot->counts_[3] += 100; // Sample count won't match redundant count. |
278 EXPECT_EQ(Histogram::COUNT_LOW_ERROR, histogram->FindCorruption(snapshot)); | 508 EXPECT_EQ(Histogram::COUNT_LOW_ERROR, |
279 snapshot.counts_[2] -= 200; | 509 histogram->FindCorruption(*snapshot)); |
280 EXPECT_EQ(Histogram::COUNT_HIGH_ERROR, histogram->FindCorruption(snapshot)); | 510 snapshot->counts_[2] -= 200; |
| 511 EXPECT_EQ(Histogram::COUNT_HIGH_ERROR, |
| 512 histogram->FindCorruption(*snapshot)); |
281 | 513 |
282 // But we can't spot a corruption if it is compensated for. | 514 // But we can't spot a corruption if it is compensated for. |
283 snapshot.counts_[1] += 100; | 515 snapshot->counts_[1] += 100; |
284 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); | 516 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, |
| 517 histogram->FindCorruption(*snapshot)); |
285 } | 518 } |
286 | 519 |
287 TEST(HistogramTest, CorruptBucketBounds) { | 520 TEST(HistogramTest, CorruptBucketBounds) { |
288 Histogram* histogram(Histogram::FactoryGet( | 521 Histogram* histogram(Histogram::FactoryGet( |
289 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. | 522 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. |
290 | 523 |
291 Histogram::SampleSet snapshot; | 524 scoped_ptr<BucketHistogramSamples> snapshot = histogram->SnapshotSamples(); |
292 histogram->SnapshotSample(&snapshot); | 525 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, |
293 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); | 526 histogram->FindCorruption(*snapshot)); |
294 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. | |
295 | 527 |
296 BucketRanges* bucket_ranges = | 528 BucketRanges* bucket_ranges = |
297 const_cast<BucketRanges*>(histogram->bucket_ranges()); | 529 const_cast<BucketRanges*>(histogram->bucket_ranges()); |
298 HistogramBase::Sample tmp = bucket_ranges->range(1); | 530 HistogramBase::Sample tmp = bucket_ranges->range(1); |
299 bucket_ranges->set_range(1, bucket_ranges->range(2)); | 531 bucket_ranges->set_range(1, bucket_ranges->range(2)); |
300 bucket_ranges->set_range(2, tmp); | 532 bucket_ranges->set_range(2, tmp); |
301 EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR, | 533 EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR, |
302 histogram->FindCorruption(snapshot)); | 534 histogram->FindCorruption(*snapshot)); |
303 | 535 |
304 bucket_ranges->set_range(2, bucket_ranges->range(1)); | 536 bucket_ranges->set_range(2, bucket_ranges->range(1)); |
305 bucket_ranges->set_range(1, tmp); | 537 bucket_ranges->set_range(1, tmp); |
306 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); | 538 EXPECT_EQ(0, histogram->FindCorruption(*snapshot)); |
307 | 539 |
| 540 // Show that two simple changes don't offset each other |
308 bucket_ranges->set_range(3, bucket_ranges->range(3) + 1); | 541 bucket_ranges->set_range(3, bucket_ranges->range(3) + 1); |
309 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, | 542 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, |
310 histogram->FindCorruption(snapshot)); | 543 histogram->FindCorruption(*snapshot)); |
311 | 544 |
312 // Show that two simple changes don't offset each other | |
313 bucket_ranges->set_range(4, bucket_ranges->range(4) - 1); | 545 bucket_ranges->set_range(4, bucket_ranges->range(4) - 1); |
314 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, | 546 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, |
315 histogram->FindCorruption(snapshot)); | 547 histogram->FindCorruption(*snapshot)); |
316 | 548 |
317 // Repair histogram so that destructor won't DCHECK(). | 549 // Repair histogram so that destructor won't DCHECK(). |
318 bucket_ranges->set_range(3, bucket_ranges->range(3) - 1); | 550 bucket_ranges->set_range(3, bucket_ranges->range(3) - 1); |
319 bucket_ranges->set_range(4, bucket_ranges->range(4) + 1); | 551 bucket_ranges->set_range(4, bucket_ranges->range(4) + 1); |
320 } | 552 } |
321 | 553 |
322 #if GTEST_HAS_DEATH_TEST | 554 #if GTEST_HAS_DEATH_TEST |
323 // For Histogram, LinearHistogram and CustomHistogram, the minimum for a | 555 // For Histogram, LinearHistogram and CustomHistogram, the minimum for a |
324 // declared range is 1, while the maximum is (HistogramBase::kSampleType_MAX - | 556 // declared range is 1, while the maximum is (HistogramBase::kSampleType_MAX - |
325 // 1). But we accept ranges exceeding those limits, and silently clamped to | 557 // 1). But we accept ranges exceeding those limits, and silently clamped to |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 // CustomHistogram needs at least 1 valid range. | 589 // CustomHistogram needs at least 1 valid range. |
358 custom_ranges.clear(); | 590 custom_ranges.clear(); |
359 custom_ranges.push_back(0); | 591 custom_ranges.push_back(0); |
360 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, | 592 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, |
361 Histogram::kNoFlags), | 593 Histogram::kNoFlags), |
362 ""); | 594 ""); |
363 } | 595 } |
364 #endif | 596 #endif |
365 | 597 |
366 } // namespace base | 598 } // namespace base |
OLD | NEW |