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

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

Issue 10809076: Move CachedRanges out and add support for checksum. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « base/metrics/histogram.cc ('k') | base/metrics/statistics_recorder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/bucket_ranges.h"
12 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
13 #include "base/metrics/statistics_recorder.h" 14 #include "base/metrics/statistics_recorder.h"
14 #include "base/time.h" 15 #include "base/time.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace base { 18 namespace base {
18 19
19 class HistogramTest : public testing::Test { 20 class HistogramTest : public testing::Test {
20 }; 21 };
21 22
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 376
376 TEST(HistogramTest, CorruptBucketBounds) { 377 TEST(HistogramTest, CorruptBucketBounds) {
377 Histogram* histogram(Histogram::FactoryGet( 378 Histogram* histogram(Histogram::FactoryGet(
378 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. 379 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file.
379 380
380 Histogram::SampleSet snapshot; 381 Histogram::SampleSet snapshot;
381 histogram->SnapshotSample(&snapshot); 382 histogram->SnapshotSample(&snapshot);
382 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); 383 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0);
383 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. 384 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption.
384 385
385 CachedRanges* cached_ranges = histogram->cached_ranges(); 386 BucketRanges* bucket_ranges = histogram->bucket_ranges();
386 std::swap(cached_ranges->ranges_[1], cached_ranges->ranges_[2]); 387 HistogramBase::Sample tmp = bucket_ranges->range(1);
388 bucket_ranges->set_range(1, bucket_ranges->range(2));
389 bucket_ranges->set_range(2, tmp);
387 EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR, 390 EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR,
388 histogram->FindCorruption(snapshot)); 391 histogram->FindCorruption(snapshot));
389 392
390 std::swap(cached_ranges->ranges_[1], cached_ranges->ranges_[2]); 393 bucket_ranges->set_range(2, bucket_ranges->range(1));
394 bucket_ranges->set_range(1, tmp);
391 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); 395 EXPECT_EQ(0, histogram->FindCorruption(snapshot));
392 396
393 ++cached_ranges->ranges_[3]; 397 bucket_ranges->set_range(3, bucket_ranges->range(3) + 1);
394 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, 398 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR,
395 histogram->FindCorruption(snapshot)); 399 histogram->FindCorruption(snapshot));
396 400
397 // Show that two simple changes don't offset each other 401 // Show that two simple changes don't offset each other
398 --cached_ranges->ranges_[4]; 402 bucket_ranges->set_range(4, bucket_ranges->range(4) - 1);
399 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, 403 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR,
400 histogram->FindCorruption(snapshot)); 404 histogram->FindCorruption(snapshot));
401 405
402 // Repair histogram so that destructor won't DCHECK(). 406 // Repair histogram so that destructor won't DCHECK().
403 --cached_ranges->ranges_[3]; 407 bucket_ranges->set_range(3, bucket_ranges->range(3) - 1);
404 ++cached_ranges->ranges_[4]; 408 bucket_ranges->set_range(4, bucket_ranges->range(4) + 1);
405 } 409 }
406 410
407 // Table was generated similarly to sample code for CRC-32 given on: 411 // Table was generated similarly to sample code for CRC-32 given on:
408 // http://www.w3.org/TR/PNG/#D-CRCAppendix. 412 // http://www.w3.org/TR/PNG/#D-CRCAppendix.
409 TEST(HistogramTest, Crc32TableTest) { 413 TEST(HistogramTest, Crc32TableTest) {
410 for (int i = 0; i < 256; ++i) { 414 for (int i = 0; i < 256; ++i) {
411 uint32 checksum = i; 415 uint32 checksum = i;
412 for (int j = 0; j < 8; ++j) { 416 for (int j = 0; j < 8; ++j) {
413 const uint32 kReversedPolynomial = 0xedb88320L; 417 const uint32 kReversedPolynomial = 0xedb88320L;
414 if (checksum & 1) 418 if (checksum & 1)
415 checksum = kReversedPolynomial ^ (checksum >> 1); 419 checksum = kReversedPolynomial ^ (checksum >> 1);
416 else 420 else
417 checksum >>= 1; 421 checksum >>= 1;
418 } 422 }
419 EXPECT_EQ(Histogram::kCrcTable[i], checksum); 423 EXPECT_EQ(Histogram::kCrcTable[i], checksum);
420 } 424 }
421 } 425 }
422 426
423 // RangeTest, CustomRangeTest and CorruptBucketBounds test CachedRanges class. 427 // RangeTest, CustomRangeTest and CorruptBucketBounds test BucketRanges class.
424 // The following tests sharing of CachedRanges object. 428 // The following tests sharing of BucketRanges object.
425 TEST(HistogramTest, CachedRangesTest) { 429 TEST(HistogramTest, BucketRangesTest) {
426 StatisticsRecorder recorder; 430 StatisticsRecorder recorder;
427 StatisticsRecorder::Histograms histograms; 431 StatisticsRecorder::Histograms histograms;
428 432
429 recorder.GetHistograms(&histograms); 433 recorder.GetHistograms(&histograms);
430 EXPECT_EQ(0U, histograms.size()); 434 EXPECT_EQ(0U, histograms.size());
431 435
432 Histogram* histogram1(Histogram::FactoryGet( 436 Histogram* histogram1(Histogram::FactoryGet(
433 "Histogram", 1, 64, 8, Histogram::kNoFlags)); 437 "Histogram", 1, 64, 8, Histogram::kNoFlags));
434 438
435 Histogram* histogram2(Histogram::FactoryGet( 439 Histogram* histogram2(Histogram::FactoryGet(
436 "Histogram2", 1, 64, 8, Histogram::kNoFlags)); 440 "Histogram2", 1, 64, 8, Histogram::kNoFlags));
437 441
438 Histogram* histogram3(Histogram::FactoryGet( 442 Histogram* histogram3(Histogram::FactoryGet(
439 "Histogram3", 1, 64, 16, Histogram::kNoFlags)); 443 "Histogram3", 1, 64, 16, Histogram::kNoFlags));
440 444
441 CachedRanges* cached_ranges1 = histogram1->cached_ranges(); 445 BucketRanges* bucket_ranges1 = histogram1->bucket_ranges();
442 CachedRanges* cached_ranges2 = histogram2->cached_ranges(); 446 BucketRanges* bucket_ranges2 = histogram2->bucket_ranges();
443 CachedRanges* cached_ranges3 = histogram3->cached_ranges(); 447 BucketRanges* bucket_ranges3 = histogram3->bucket_ranges();
444 EXPECT_TRUE(cached_ranges1->Equals(cached_ranges2)); 448 EXPECT_TRUE(bucket_ranges1->Equals(bucket_ranges2));
445 EXPECT_FALSE(cached_ranges1->Equals(cached_ranges3)); 449 EXPECT_FALSE(bucket_ranges1->Equals(bucket_ranges3));
446 } 450 }
447 451
448 } // namespace base 452 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/histogram.cc ('k') | base/metrics/statistics_recorder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698