crc32c_test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "util/crc32c.h"
  10. #include "test_util/testharness.h"
  11. #include "util/coding.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. namespace crc32c {
  14. class CRC { };
  15. // Tests for 3-way crc32c algorithm. We need these tests because it uses
  16. // different lookup tables than the original Fast_CRC32
  17. const unsigned int BUFFER_SIZE = 512 * 1024 * sizeof(uint64_t);
  18. char buffer[BUFFER_SIZE];
  19. struct ExpectedResult {
  20. size_t offset;
  21. size_t length;
  22. uint32_t crc32c;
  23. };
  24. ExpectedResult expectedResults[] = {
  25. // Zero-byte input
  26. { 0, 0, ~0U },
  27. // Small aligned inputs to test special cases in SIMD implementations
  28. { 8, 1, 1543413366 },
  29. { 8, 2, 523493126 },
  30. { 8, 3, 1560427360 },
  31. { 8, 4, 3422504776 },
  32. { 8, 5, 447841138 },
  33. { 8, 6, 3910050499 },
  34. { 8, 7, 3346241981 },
  35. // Small unaligned inputs
  36. { 9, 1, 3855826643 },
  37. { 10, 2, 560880875 },
  38. { 11, 3, 1479707779 },
  39. { 12, 4, 2237687071 },
  40. { 13, 5, 4063855784 },
  41. { 14, 6, 2553454047 },
  42. { 15, 7, 1349220140 },
  43. // Larger inputs to test leftover chunks at the end of aligned blocks
  44. { 8, 8, 627613930 },
  45. { 8, 9, 2105929409 },
  46. { 8, 10, 2447068514 },
  47. { 8, 11, 863807079 },
  48. { 8, 12, 292050879 },
  49. { 8, 13, 1411837737 },
  50. { 8, 14, 2614515001 },
  51. { 8, 15, 3579076296 },
  52. { 8, 16, 2897079161 },
  53. { 8, 17, 675168386 },
  54. // // Much larger inputs
  55. { 0, BUFFER_SIZE, 2096790750 },
  56. { 1, BUFFER_SIZE / 2, 3854797577 },
  57. };
  58. TEST(CRC, StandardResults) {
  59. // Original Fast_CRC32 tests.
  60. // From rfc3720 section B.4.
  61. char buf[32];
  62. memset(buf, 0, sizeof(buf));
  63. ASSERT_EQ(0x8a9136aaU, Value(buf, sizeof(buf)));
  64. memset(buf, 0xff, sizeof(buf));
  65. ASSERT_EQ(0x62a8ab43U, Value(buf, sizeof(buf)));
  66. for (int i = 0; i < 32; i++) {
  67. buf[i] = static_cast<char>(i);
  68. }
  69. ASSERT_EQ(0x46dd794eU, Value(buf, sizeof(buf)));
  70. for (int i = 0; i < 32; i++) {
  71. buf[i] = static_cast<char>(31 - i);
  72. }
  73. ASSERT_EQ(0x113fdb5cU, Value(buf, sizeof(buf)));
  74. unsigned char data[48] = {
  75. 0x01, 0xc0, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00,
  79. 0x14, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x04, 0x00,
  81. 0x00, 0x00, 0x00, 0x14,
  82. 0x00, 0x00, 0x00, 0x18,
  83. 0x28, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00,
  85. 0x02, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00,
  87. };
  88. ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
  89. // 3-Way Crc32c tests ported from folly.
  90. // Test 1: single computation
  91. for (auto expected : expectedResults) {
  92. uint32_t result = Value(buffer + expected.offset, expected.length);
  93. EXPECT_EQ(~expected.crc32c, result);
  94. }
  95. // Test 2: stitching two computations
  96. for (auto expected : expectedResults) {
  97. size_t partialLength = expected.length / 2;
  98. uint32_t partialChecksum = Value(buffer + expected.offset, partialLength);
  99. uint32_t result = Extend(partialChecksum,
  100. buffer + expected.offset + partialLength,
  101. expected.length - partialLength);
  102. EXPECT_EQ(~expected.crc32c, result);
  103. }
  104. }
  105. TEST(CRC, Values) {
  106. ASSERT_NE(Value("a", 1), Value("foo", 3));
  107. }
  108. TEST(CRC, Extend) {
  109. ASSERT_EQ(Value("hello world", 11),
  110. Extend(Value("hello ", 6), "world", 5));
  111. }
  112. TEST(CRC, Mask) {
  113. uint32_t crc = Value("foo", 3);
  114. ASSERT_NE(crc, Mask(crc));
  115. ASSERT_NE(crc, Mask(Mask(crc)));
  116. ASSERT_EQ(crc, Unmask(Mask(crc)));
  117. ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
  118. }
  119. } // namespace crc32c
  120. } // namespace ROCKSDB_NAMESPACE
  121. // copied from folly
  122. const uint64_t FNV_64_HASH_START = 14695981039346656037ULL;
  123. inline uint64_t fnv64_buf(const void* buf,
  124. size_t n,
  125. uint64_t hash = FNV_64_HASH_START) {
  126. // forcing signed char, since other platforms can use unsigned
  127. const signed char* char_buf = reinterpret_cast<const signed char*>(buf);
  128. for (size_t i = 0; i < n; ++i) {
  129. hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
  130. (hash << 8) + (hash << 40);
  131. hash ^= char_buf[i];
  132. }
  133. return hash;
  134. }
  135. int main(int argc, char** argv) {
  136. ::testing::InitGoogleTest(&argc, argv);
  137. // Populate a buffer with a deterministic pattern
  138. // on which to compute checksums
  139. const uint8_t* src = (uint8_t*)ROCKSDB_NAMESPACE::crc32c::buffer;
  140. uint64_t* dst = (uint64_t*)ROCKSDB_NAMESPACE::crc32c::buffer;
  141. const uint64_t* end =
  142. (const uint64_t*)(ROCKSDB_NAMESPACE::crc32c::buffer +
  143. ROCKSDB_NAMESPACE::crc32c::BUFFER_SIZE);
  144. *dst++ = 0;
  145. while (dst < end) {
  146. ROCKSDB_NAMESPACE::EncodeFixed64(
  147. reinterpret_cast<char*>(dst),
  148. fnv64_buf((const char*)src, sizeof(uint64_t)));
  149. dst++;
  150. src += sizeof(uint64_t);
  151. }
  152. return RUN_ALL_TESTS();
  153. }