1 // Copyright 2019 The Chromium Authors 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/crc32.h" 6 7 #include <stdint.h> 8 9 #include "base/containers/span.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace base { 13 14 // Table was generated similarly to sample code for CRC-32 given on: 15 // http://www.w3.org/TR/PNG/#D-CRCAppendix. TEST(Crc32Test,TableTest)16TEST(Crc32Test, TableTest) { 17 for (int i = 0; i < 256; ++i) { 18 uint32_t checksum = i; 19 for (int j = 0; j < 8; ++j) { 20 const uint32_t kReversedPolynomial = 0xEDB88320L; 21 if (checksum & 1) 22 checksum = kReversedPolynomial ^ (checksum >> 1); 23 else 24 checksum >>= 1; 25 } 26 EXPECT_EQ(kCrcTable[i], checksum); 27 } 28 } 29 30 // A CRC of nothing should always be zero. TEST(Crc32Test,ZeroTest)31TEST(Crc32Test, ZeroTest) { 32 span<const uint8_t> empty_data; 33 EXPECT_EQ(0U, Crc32(0, empty_data)); 34 } 35 36 } // namespace base 37