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