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

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

Issue 141393002: Return a NULL histogram pointer on construction error (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add construction param to restore old CHECK for non-API calls Created 6 years, 11 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
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 <climits>
8 #include <algorithm> 8 #include <algorithm>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 28 matching lines...) Expand all
39 } 39 }
40 40
41 void UninitializeStatisticsRecorder() { 41 void UninitializeStatisticsRecorder() {
42 delete statistics_recorder_; 42 delete statistics_recorder_;
43 statistics_recorder_ = NULL; 43 statistics_recorder_ = NULL;
44 } 44 }
45 45
46 StatisticsRecorder* statistics_recorder_; 46 StatisticsRecorder* statistics_recorder_;
47 }; 47 };
48 48
49 typedef HistogramTest HistogramDeathTest;
50
49 // Check for basic syntax and use. 51 // Check for basic syntax and use.
50 TEST_F(HistogramTest, BasicTest) { 52 TEST_F(HistogramTest, BasicTest) {
51 // Try basic construction 53 // Try basic construction
52 HistogramBase* histogram = Histogram::FactoryGet( 54 HistogramBase* histogram = Histogram::FactoryGet(
53 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 55 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
54 EXPECT_TRUE(histogram); 56 EXPECT_TRUE(histogram);
55 57
56 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( 58 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
57 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 59 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
58 EXPECT_TRUE(linear_histogram); 60 EXPECT_TRUE(linear_histogram);
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 int range; 438 int range;
437 EXPECT_TRUE(iter.ReadInt(&range)); 439 EXPECT_TRUE(iter.ReadInt(&range));
438 EXPECT_EQ(10, range); 440 EXPECT_EQ(10, range);
439 EXPECT_TRUE(iter.ReadInt(&range)); 441 EXPECT_TRUE(iter.ReadInt(&range));
440 EXPECT_EQ(100, range); 442 EXPECT_EQ(100, range);
441 443
442 // No more data in the pickle. 444 // No more data in the pickle.
443 EXPECT_FALSE(iter.SkipBytes(1)); 445 EXPECT_FALSE(iter.SkipBytes(1));
444 } 446 }
445 447
448 TEST_F(HistogramTest, BadConstruction) {
449 HistogramBase* histogram = Histogram::FactoryGet(
450 "BadConstruction", 0, 100, 8,
451 HistogramBase::kNoFlags);
452 EXPECT_TRUE(
453 histogram->HasConstructionArguments(
454 1, 100, 8));
455 histogram->Add(10);
456
457 // Try to get the same histogram name with different arguments.
458 HistogramBase* dummy_histogram = Histogram::FactoryGet(
459 "BadConstruction", 0, 100, 7,
460 HistogramBase::kNoFlags, HistogramBase::kAllowBadConstruction);
461 dummy_histogram->Add(80);
462 dummy_histogram = Histogram::FactoryGet(
463 "BadConstruction", 0, 99, 8,
464 HistogramBase::kNoFlags, HistogramBase::kAllowBadConstruction);
465 dummy_histogram->Add(80);
466
467 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
468 EXPECT_EQ(1, samples->TotalCount());
469 EXPECT_EQ(1, samples->GetCount(10));
470 EXPECT_EQ(0, samples->GetCount(80));
471
472 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
473 "BadConstructionLinear", 0, 100, 8,
474 HistogramBase::kNoFlags);
475 EXPECT_TRUE(
476 linear_histogram->HasConstructionArguments(
477 1, 100, 8));
478 linear_histogram->Add(10);
479
480 // Try to get the same histogram name with different arguments.
481 dummy_histogram = LinearHistogram::FactoryGet(
482 "BadConstructionLinear", 0, 100, 7,
483 HistogramBase::kNoFlags, HistogramBase::kAllowBadConstruction);
484 dummy_histogram->Add(80);
485 dummy_histogram = LinearHistogram::FactoryGet(
486 "BadConstructionLinear", 10, 100, 8,
487 HistogramBase::kNoFlags, HistogramBase::kAllowBadConstruction);
488 dummy_histogram->Add(80);
489
490 scoped_ptr<HistogramSamples> l_samples = linear_histogram->SnapshotSamples();
491 EXPECT_EQ(1, l_samples->TotalCount());
492 EXPECT_EQ(1, l_samples->GetCount(10));
493 EXPECT_EQ(0, l_samples->GetCount(80));
494 }
495
446 #if GTEST_HAS_DEATH_TEST 496 #if GTEST_HAS_DEATH_TEST
497 TEST_F(HistogramDeathTest, BadConstructionFatal) {
498 HistogramBase* histogram = Histogram::FactoryGet(
499 "BadConstruction", 0, 100, 8,
500 HistogramBase::kNoFlags);
501 EXPECT_TRUE(
502 histogram->HasConstructionArguments(
503 1, 100, 8));
504 EXPECT_DEATH(Histogram::FactoryGet("BadConstruction", 0, 100, 7,
505 HistogramBase::kNoFlags),
506 "");
507 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
508 "BadConstructionLinear", 0, 100, 8,
509 HistogramBase::kNoFlags);
510 EXPECT_TRUE(
511 linear_histogram->HasConstructionArguments(
512 1, 100, 8));
513 EXPECT_DEATH(LinearHistogram::FactoryGet("BadConstructionLinear", 10, 100, 8,
514 HistogramBase::kNoFlags),
515 "");
516 }
517
447 // For Histogram, LinearHistogram and CustomHistogram, the minimum for a 518 // For Histogram, LinearHistogram and CustomHistogram, the minimum for a
448 // declared range is 1, while the maximum is (HistogramBase::kSampleType_MAX - 519 // declared range is 1, while the maximum is (HistogramBase::kSampleType_MAX -
449 // 1). But we accept ranges exceeding those limits, and silently clamped to 520 // 1). But we accept ranges exceeding those limits, and silently clamped to
450 // those limits. This is for backwards compatibility. 521 // those limits. This is for backwards compatibility.
451 TEST(HistogramDeathTest, BadRangesTest) { 522 TEST_F(HistogramDeathTest, BadRangesTest) {
452 HistogramBase* histogram = Histogram::FactoryGet( 523 HistogramBase* histogram = Histogram::FactoryGet(
453 "BadRanges", 0, HistogramBase::kSampleType_MAX, 8, 524 "BadRanges", 0, HistogramBase::kSampleType_MAX, 8,
454 HistogramBase::kNoFlags); 525 HistogramBase::kNoFlags);
455 EXPECT_TRUE( 526 EXPECT_TRUE(
456 histogram->HasConstructionArguments( 527 histogram->HasConstructionArguments(
457 1, HistogramBase::kSampleType_MAX - 1, 8)); 528 1, HistogramBase::kSampleType_MAX - 1, 8));
458 529
459 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( 530 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
460 "BadRangesLinear", 0, HistogramBase::kSampleType_MAX, 8, 531 "BadRangesLinear", 0, HistogramBase::kSampleType_MAX, 8,
461 HistogramBase::kNoFlags); 532 HistogramBase::kNoFlags);
(...skipping 22 matching lines...) Expand all
484 // CustomHistogram needs at least 1 valid range. 555 // CustomHistogram needs at least 1 valid range.
485 custom_ranges.clear(); 556 custom_ranges.clear();
486 custom_ranges.push_back(0); 557 custom_ranges.push_back(0);
487 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, 558 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges,
488 HistogramBase::kNoFlags), 559 HistogramBase::kNoFlags),
489 ""); 560 "");
490 } 561 }
491 #endif 562 #endif
492 563
493 } // namespace base 564 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698