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 3// found in the LICENSE file. 4 5#include "base/metrics/bucket_ranges.h" 6 7#include "testing/gtest/include/gtest/gtest.h" 8 9namespace base { 10namespace { 11 12TEST(BucketRangesTest, NormalSetup) { 13 BucketRanges ranges(5); 14 ASSERT_EQ(5u, ranges.size()); 15 ASSERT_EQ(4u, ranges.bucket_count()); 16 17 for (int i = 0; i < 5; ++i) { 18 EXPECT_EQ(0, ranges.range(i)); 19 } 20 EXPECT_EQ(0u, ranges.checksum()); 21 22 ranges.set_range(3, 100); 23 EXPECT_EQ(100, ranges.range(3)); 24} 25 26TEST(BucketRangesTest, Equals) { 27 // Compare empty ranges. 28 BucketRanges ranges1(3); 29 BucketRanges ranges2(3); 30 BucketRanges ranges3(5); 31 32 EXPECT_TRUE(ranges1.Equals(&ranges2)); 33 EXPECT_FALSE(ranges1.Equals(&ranges3)); 34 EXPECT_FALSE(ranges2.Equals(&ranges3)); 35 36 // Compare full filled ranges. 37 ranges1.set_range(0, 0); 38 ranges1.set_range(1, 1); 39 ranges1.set_range(2, 2); 40 ranges1.set_checksum(100); 41 ranges2.set_range(0, 0); 42 ranges2.set_range(1, 1); 43 ranges2.set_range(2, 2); 44 ranges2.set_checksum(100); 45 46 EXPECT_TRUE(ranges1.Equals(&ranges2)); 47 48 // Checksum does not match. 49 ranges1.set_checksum(99); 50 EXPECT_FALSE(ranges1.Equals(&ranges2)); 51 ranges1.set_checksum(100); 52 53 // Range does not match. 54 ranges1.set_range(1, 3); 55 EXPECT_FALSE(ranges1.Equals(&ranges2)); 56} 57 58TEST(BucketRangesTest, Checksum) { 59 BucketRanges ranges(3); 60 ranges.set_range(0, 0); 61 ranges.set_range(1, 1); 62 ranges.set_range(2, 2); 63 64 ranges.ResetChecksum(); 65 EXPECT_EQ(289217253u, ranges.checksum()); 66 67 ranges.set_range(2, 3); 68 EXPECT_FALSE(ranges.HasValidChecksum()); 69 70 ranges.ResetChecksum(); 71 EXPECT_EQ(2843835776u, ranges.checksum()); 72 EXPECT_TRUE(ranges.HasValidChecksum()); 73} 74 75// Table was generated similarly to sample code for CRC-32 given on: 76// http://www.w3.org/TR/PNG/#D-CRCAppendix. 77TEST(BucketRangesTest, Crc32TableTest) { 78 for (int i = 0; i < 256; ++i) { 79 uint32 checksum = i; 80 for (int j = 0; j < 8; ++j) { 81 const uint32 kReversedPolynomial = 0xedb88320L; 82 if (checksum & 1) 83 checksum = kReversedPolynomial ^ (checksum >> 1); 84 else 85 checksum >>= 1; 86 } 87 EXPECT_EQ(kCrcTable[i], checksum); 88 } 89} 90 91} // namespace 92} // namespace base 93