summaryrefslogtreecommitdiff
path: root/chromium/base/metrics/crc32_unittest.cc
blob: a5e5d29008880c2e8a62519139c501dc076fcd29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/metrics/crc32.h"

#include <stdint.h>

#include "testing/gtest/include/gtest/gtest.h"

namespace base {

// Table was generated similarly to sample code for CRC-32 given on:
// http://www.w3.org/TR/PNG/#D-CRCAppendix.
TEST(Crc32Test, TableTest) {
  for (int i = 0; i < 256; ++i) {
    uint32_t checksum = i;
    for (int j = 0; j < 8; ++j) {
      const uint32_t kReversedPolynomial = 0xEDB88320L;
      if (checksum & 1)
        checksum = kReversedPolynomial ^ (checksum >> 1);
      else
        checksum >>= 1;
    }
    EXPECT_EQ(kCrcTable[i], checksum);
  }
}

// A CRC of nothing should always be zero.
TEST(Crc32Test, ZeroTest) {
  EXPECT_EQ(0U, Crc32(0, nullptr, 0));
}

}  // namespace base