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.
5 #include "base/metrics/bucket_ranges.h"
7 #include "testing/gtest/include/gtest/gtest.h"
12 TEST(BucketRangesTest
, NormalSetup
) {
13 BucketRanges
ranges(5);
14 ASSERT_EQ(5u, ranges
.size());
15 ASSERT_EQ(4u, ranges
.bucket_count());
17 for (int i
= 0; i
< 5; ++i
) {
18 EXPECT_EQ(0, ranges
.range(i
));
20 EXPECT_EQ(0u, ranges
.checksum());
22 ranges
.set_range(3, 100);
23 EXPECT_EQ(100, ranges
.range(3));
26 TEST(BucketRangesTest
, Equals
) {
27 // Compare empty ranges.
28 BucketRanges
ranges1(3);
29 BucketRanges
ranges2(3);
30 BucketRanges
ranges3(5);
32 EXPECT_TRUE(ranges1
.Equals(&ranges2
));
33 EXPECT_FALSE(ranges1
.Equals(&ranges3
));
34 EXPECT_FALSE(ranges2
.Equals(&ranges3
));
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);
46 EXPECT_TRUE(ranges1
.Equals(&ranges2
));
48 // Checksum does not match.
49 ranges1
.set_checksum(99);
50 EXPECT_FALSE(ranges1
.Equals(&ranges2
));
51 ranges1
.set_checksum(100);
53 // Range does not match.
54 ranges1
.set_range(1, 3);
55 EXPECT_FALSE(ranges1
.Equals(&ranges2
));
58 TEST(BucketRangesTest
, Checksum
) {
59 BucketRanges
ranges(3);
60 ranges
.set_range(0, 0);
61 ranges
.set_range(1, 1);
62 ranges
.set_range(2, 2);
64 ranges
.ResetChecksum();
65 EXPECT_EQ(289217253u, ranges
.checksum());
67 ranges
.set_range(2, 3);
68 EXPECT_FALSE(ranges
.HasValidChecksum());
70 ranges
.ResetChecksum();
71 EXPECT_EQ(2843835776u, ranges
.checksum());
72 EXPECT_TRUE(ranges
.HasValidChecksum());
75 // Table was generated similarly to sample code for CRC-32 given on:
76 // http://www.w3.org/TR/PNG/#D-CRCAppendix.
77 TEST(BucketRangesTest
, Crc32TableTest
) {
78 for (int i
= 0; i
< 256; ++i
) {
80 for (int j
= 0; j
< 8; ++j
) {
81 const uint32 kReversedPolynomial
= 0xedb88320L
;
83 checksum
= kReversedPolynomial
^ (checksum
>> 1);
87 EXPECT_EQ(kCrcTable
[i
], checksum
);