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 #include "base/metrics/histogram.h" | 5 #include "base/metrics/histogram.h" |
6 | 6 |
7 #include <climits> | 7 #include <climits> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 Histogram* histogram = static_cast<Histogram*>( | 224 Histogram* histogram = static_cast<Histogram*>( |
225 CustomHistogram::FactoryGet("2BucketsCustomHistogram", custom_ranges, | 225 CustomHistogram::FactoryGet("2BucketsCustomHistogram", custom_ranges, |
226 HistogramBase::kNoFlags)); | 226 HistogramBase::kNoFlags)); |
227 const BucketRanges* ranges = histogram->bucket_ranges(); | 227 const BucketRanges* ranges = histogram->bucket_ranges(); |
228 ASSERT_EQ(3u, ranges->size()); | 228 ASSERT_EQ(3u, ranges->size()); |
229 EXPECT_EQ(0, ranges->range(0)); | 229 EXPECT_EQ(0, ranges->range(0)); |
230 EXPECT_EQ(4, ranges->range(1)); | 230 EXPECT_EQ(4, ranges->range(1)); |
231 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2)); | 231 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2)); |
232 } | 232 } |
233 | 233 |
| 234 // Test the AddCount function. |
| 235 TEST_F(HistogramTest, AddCountTest) { |
| 236 const size_t kBucketCount = 50; |
| 237 Histogram* histogram = static_cast<Histogram*>( |
| 238 Histogram::FactoryGet("AddCountHistogram", 10, 100, kBucketCount, |
| 239 HistogramBase::kNoFlags)); |
| 240 |
| 241 histogram->AddCount(20, 15); |
| 242 histogram->AddCount(30, 14); |
| 243 |
| 244 scoped_ptr<SampleVector> samples = histogram->SnapshotSampleVector(); |
| 245 EXPECT_EQ(29, samples->TotalCount()); |
| 246 EXPECT_EQ(15, samples->GetCount(20)); |
| 247 EXPECT_EQ(14, samples->GetCount(30)); |
| 248 |
| 249 histogram->AddCount(20, 25); |
| 250 histogram->AddCount(30, 24); |
| 251 |
| 252 scoped_ptr<SampleVector> samples2 = histogram->SnapshotSampleVector(); |
| 253 EXPECT_EQ(78, samples2->TotalCount()); |
| 254 EXPECT_EQ(40, samples2->GetCount(20)); |
| 255 EXPECT_EQ(38, samples2->GetCount(30)); |
| 256 } |
| 257 |
234 // Make sure histogram handles out-of-bounds data gracefully. | 258 // Make sure histogram handles out-of-bounds data gracefully. |
235 TEST_F(HistogramTest, BoundsTest) { | 259 TEST_F(HistogramTest, BoundsTest) { |
236 const size_t kBucketCount = 50; | 260 const size_t kBucketCount = 50; |
237 Histogram* histogram = static_cast<Histogram*>( | 261 Histogram* histogram = static_cast<Histogram*>( |
238 Histogram::FactoryGet("Bounded", 10, 100, kBucketCount, | 262 Histogram::FactoryGet("Bounded", 10, 100, kBucketCount, |
239 HistogramBase::kNoFlags)); | 263 HistogramBase::kNoFlags)); |
240 | 264 |
241 // Put two samples "out of bounds" above and below. | 265 // Put two samples "out of bounds" above and below. |
242 histogram->Add(5); | 266 histogram->Add(5); |
243 histogram->Add(-50); | 267 histogram->Add(-50); |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 // CustomHistogram needs at least 1 valid range. | 527 // CustomHistogram needs at least 1 valid range. |
504 custom_ranges.clear(); | 528 custom_ranges.clear(); |
505 custom_ranges.push_back(0); | 529 custom_ranges.push_back(0); |
506 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, | 530 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, |
507 HistogramBase::kNoFlags), | 531 HistogramBase::kNoFlags), |
508 ""); | 532 ""); |
509 } | 533 } |
510 #endif | 534 #endif |
511 | 535 |
512 } // namespace base | 536 } // namespace base |
OLD | NEW |