crc32_unittest.cc 882 B

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