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());
16 for (int i
= 0; i
< 5; i
++) {
17 EXPECT_EQ(0, ranges
.range(i
));
19 EXPECT_EQ(0u, ranges
.checksum());
21 ranges
.set_range(3, 100);
22 EXPECT_EQ(100, ranges
.range(3));
25 TEST(BucketRangesTest
, Equals
) {
26 // Compare empty ranges.
27 BucketRanges
ranges1(3);
28 BucketRanges
ranges2(3);
29 BucketRanges
ranges3(5);
31 EXPECT_TRUE(ranges1
.Equals(&ranges2
));
32 EXPECT_FALSE(ranges1
.Equals(&ranges3
));
33 EXPECT_FALSE(ranges2
.Equals(&ranges3
));
35 // Compare full filled ranges.
36 ranges1
.set_range(0, 0);
37 ranges1
.set_range(1, 1);
38 ranges1
.set_range(2, 2);
39 ranges1
.set_checksum(100);
40 ranges2
.set_range(0, 0);
41 ranges2
.set_range(1, 1);
42 ranges2
.set_range(2, 2);
43 ranges2
.set_checksum(100);
45 EXPECT_TRUE(ranges1
.Equals(&ranges2
));
47 // Checksum does not match.
48 ranges1
.set_checksum(99);
49 EXPECT_FALSE(ranges1
.Equals(&ranges2
));
50 ranges1
.set_checksum(100);
52 // Range does not match.
53 ranges1
.set_range(1, 3);
54 EXPECT_FALSE(ranges1
.Equals(&ranges2
));
57 TEST(BucketRangesTest
, Checksum
) {
58 BucketRanges
ranges(3);
59 ranges
.set_range(0, 0);
60 ranges
.set_range(1, 1);
61 ranges
.set_range(2, 2);
63 ranges
.ResetChecksum();
64 EXPECT_EQ(289217253u, ranges
.checksum());
66 ranges
.set_range(2, 3);
67 EXPECT_FALSE(ranges
.HasValidChecksum());
69 ranges
.ResetChecksum();
70 EXPECT_EQ(2843835776u, ranges
.checksum());
71 EXPECT_TRUE(ranges
.HasValidChecksum());
74 // Table was generated similarly to sample code for CRC-32 given on:
75 // http://www.w3.org/TR/PNG/#D-CRCAppendix.
76 TEST(HistogramTest
, Crc32TableTest
) {
77 for (int i
= 0; i
< 256; ++i
) {
79 for (int j
= 0; j
< 8; ++j
) {
80 const uint32 kReversedPolynomial
= 0xedb88320L
;
82 checksum
= kReversedPolynomial
^ (checksum
>> 1);
86 EXPECT_EQ(kCrcTable
[i
], checksum
);