| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 | 380 |
| 381 TEST(HistogramTest, CorruptBucketBounds) { | 381 TEST(HistogramTest, CorruptBucketBounds) { |
| 382 Histogram* histogram(Histogram::FactoryGet( | 382 Histogram* histogram(Histogram::FactoryGet( |
| 383 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. | 383 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. |
| 384 | 384 |
| 385 Histogram::SampleSet snapshot; | 385 Histogram::SampleSet snapshot; |
| 386 histogram->SnapshotSample(&snapshot); | 386 histogram->SnapshotSample(&snapshot); |
| 387 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); | 387 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); |
| 388 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. | 388 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. |
| 389 | 389 |
| 390 std::swap(histogram->ranges_[1], histogram->ranges_[2]); | 390 CachedRanges* cached_ranges = histogram->cached_ranges(); |
| 391 std::swap(cached_ranges->ranges_[1], cached_ranges->ranges_[2]); |
| 391 EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR, | 392 EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR, |
| 392 histogram->FindCorruption(snapshot)); | 393 histogram->FindCorruption(snapshot)); |
| 393 | 394 |
| 394 std::swap(histogram->ranges_[1], histogram->ranges_[2]); | 395 std::swap(cached_ranges->ranges_[1], cached_ranges->ranges_[2]); |
| 395 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); | 396 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); |
| 396 | 397 |
| 397 ++histogram->ranges_[3]; | 398 ++cached_ranges->ranges_[3]; |
| 398 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, | 399 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, |
| 399 histogram->FindCorruption(snapshot)); | 400 histogram->FindCorruption(snapshot)); |
| 400 | 401 |
| 401 // Show that two simple changes don't offset each other | 402 // Show that two simple changes don't offset each other |
| 402 --histogram->ranges_[4]; | 403 --cached_ranges->ranges_[4]; |
| 403 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, | 404 EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, |
| 404 histogram->FindCorruption(snapshot)); | 405 histogram->FindCorruption(snapshot)); |
| 405 | 406 |
| 406 // Repair histogram so that destructor won't DCHECK(). | 407 // Repair histogram so that destructor won't DCHECK(). |
| 407 --histogram->ranges_[3]; | 408 --cached_ranges->ranges_[3]; |
| 408 ++histogram->ranges_[4]; | 409 ++cached_ranges->ranges_[4]; |
| 409 } | 410 } |
| 410 | 411 |
| 411 // Table was generated similarly to sample code for CRC-32 given on: | 412 // Table was generated similarly to sample code for CRC-32 given on: |
| 412 // http://www.w3.org/TR/PNG/#D-CRCAppendix. | 413 // http://www.w3.org/TR/PNG/#D-CRCAppendix. |
| 413 TEST(HistogramTest, Crc32TableTest) { | 414 TEST(HistogramTest, Crc32TableTest) { |
| 414 for (int i = 0; i < 256; ++i) { | 415 for (int i = 0; i < 256; ++i) { |
| 415 uint32 checksum = i; | 416 uint32 checksum = i; |
| 416 for (int j = 0; j < 8; ++j) { | 417 for (int j = 0; j < 8; ++j) { |
| 417 const uint32 kReversedPolynomial = 0xedb88320L; | 418 const uint32 kReversedPolynomial = 0xedb88320L; |
| 418 if (checksum & 1) | 419 if (checksum & 1) |
| 419 checksum = kReversedPolynomial ^ (checksum >> 1); | 420 checksum = kReversedPolynomial ^ (checksum >> 1); |
| 420 else | 421 else |
| 421 checksum >>= 1; | 422 checksum >>= 1; |
| 422 } | 423 } |
| 423 EXPECT_EQ(Histogram::kCrcTable[i], checksum); | 424 EXPECT_EQ(Histogram::kCrcTable[i], checksum); |
| 424 } | 425 } |
| 425 } | 426 } |
| 426 | 427 |
| 428 // RangeTest, CustomRangeTest and CorruptBucketBounds test CachedRanges class. |
| 429 // The following tests sharing of CachedRanges object. |
| 430 TEST(HistogramTest, CachedRangesTest) { |
| 431 StatisticsRecorder recorder; |
| 432 StatisticsRecorder::Histograms histograms; |
| 433 |
| 434 recorder.GetHistograms(&histograms); |
| 435 EXPECT_EQ(0U, histograms.size()); |
| 436 |
| 437 Histogram* histogram1(Histogram::FactoryGet( |
| 438 "Histogram", 1, 64, 8, Histogram::kNoFlags)); |
| 439 |
| 440 Histogram* histogram2(Histogram::FactoryGet( |
| 441 "Histogram2", 1, 64, 8, Histogram::kNoFlags)); |
| 442 |
| 443 Histogram* histogram3(Histogram::FactoryGet( |
| 444 "Histogram3", 1, 64, 16, Histogram::kNoFlags)); |
| 445 |
| 446 CachedRanges* cached_ranges1 = histogram1->cached_ranges(); |
| 447 CachedRanges* cached_ranges2 = histogram2->cached_ranges(); |
| 448 CachedRanges* cached_ranges3 = histogram3->cached_ranges(); |
| 449 EXPECT_TRUE(cached_ranges1->Equals(cached_ranges2)); |
| 450 EXPECT_FALSE(cached_ranges1->Equals(cached_ranges3)); |
| 451 } |
| 452 |
| 427 } // namespace base | 453 } // namespace base |
| OLD | NEW |