crc32c_test.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "util/random.h"
  13. namespace ROCKSDB_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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
  77. 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. };
  80. ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
  81. // 3-Way Crc32c tests ported from folly.
  82. // Test 1: single computation
  83. for (auto expected : expectedResults) {
  84. uint32_t result = Value(buffer + expected.offset, expected.length);
  85. EXPECT_EQ(~expected.crc32c, result);
  86. }
  87. // Test 2: stitching two computations
  88. for (auto expected : expectedResults) {
  89. size_t partialLength = expected.length / 2;
  90. uint32_t partialChecksum = Value(buffer + expected.offset, partialLength);
  91. uint32_t result =
  92. Extend(partialChecksum, buffer + expected.offset + partialLength,
  93. expected.length - partialLength);
  94. EXPECT_EQ(~expected.crc32c, result);
  95. }
  96. }
  97. TEST(CRC, Values) { ASSERT_NE(Value("a", 1), Value("foo", 3)); }
  98. TEST(CRC, Extend) {
  99. ASSERT_EQ(Value("hello world", 11), Extend(Value("hello ", 6), "world", 5));
  100. }
  101. TEST(CRC, Mask) {
  102. uint32_t crc = Value("foo", 3);
  103. ASSERT_NE(crc, Mask(crc));
  104. ASSERT_NE(crc, Mask(Mask(crc)));
  105. ASSERT_EQ(crc, Unmask(Mask(crc)));
  106. ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
  107. }
  108. TEST(CRC, Crc32cCombineBasicTest) {
  109. uint32_t crc1 = Value("hello ", 6);
  110. uint32_t crc2 = Value("world", 5);
  111. uint32_t crc3 = Value("hello world", 11);
  112. uint32_t crc1_2_combine = Crc32cCombine(crc1, crc2, 5);
  113. ASSERT_EQ(crc3, crc1_2_combine);
  114. }
  115. TEST(CRC, Crc32cCombineOrderMattersTest) {
  116. uint32_t crc1 = Value("hello ", 6);
  117. uint32_t crc2 = Value("world", 5);
  118. uint32_t crc3 = Value("hello world", 11);
  119. uint32_t crc2_1_combine = Crc32cCombine(crc2, crc1, 6);
  120. ASSERT_NE(crc3, crc2_1_combine);
  121. }
  122. TEST(CRC, Crc32cCombineFullCoverTest) {
  123. int scale = 4 * 1024;
  124. Random rnd(test::RandomSeed());
  125. int size_1 = 1024 * 1024;
  126. std::string s1 = rnd.RandomBinaryString(size_1);
  127. uint32_t crc1 = Value(s1.data(), size_1);
  128. for (int i = 0; i < scale; i++) {
  129. int size_2 = i;
  130. std::string s2 = rnd.RandomBinaryString(size_2);
  131. uint32_t crc2 = Value(s2.data(), s2.size());
  132. uint32_t crc1_2 = Extend(crc1, s2.data(), s2.size());
  133. uint32_t crc1_2_combine = Crc32cCombine(crc1, crc2, size_2);
  134. ASSERT_EQ(crc1_2, crc1_2_combine);
  135. }
  136. }
  137. TEST(CRC, Crc32cCombineBigSizeTest) {
  138. Random rnd(test::RandomSeed());
  139. int size_1 = 1024 * 1024;
  140. std::string s1 = rnd.RandomBinaryString(size_1);
  141. uint32_t crc1 = Value(s1.data(), size_1);
  142. int size_2 = 16 * 1024 * 1024 - 1;
  143. std::string s2 = rnd.RandomBinaryString(size_2);
  144. uint32_t crc2 = Value(s2.data(), s2.size());
  145. uint32_t crc1_2 = Extend(crc1, s2.data(), s2.size());
  146. uint32_t crc1_2_combine = Crc32cCombine(crc1, crc2, size_2);
  147. ASSERT_EQ(crc1_2, crc1_2_combine);
  148. }
  149. } // namespace ROCKSDB_NAMESPACE::crc32c
  150. // copied from folly
  151. const uint64_t FNV_64_HASH_START = 14695981039346656037ULL;
  152. inline uint64_t fnv64_buf(const void* buf, size_t n,
  153. uint64_t hash = FNV_64_HASH_START) {
  154. // forcing signed char, since other platforms can use unsigned
  155. const signed char* char_buf = reinterpret_cast<const signed char*>(buf);
  156. for (size_t i = 0; i < n; ++i) {
  157. hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
  158. (hash << 8) + (hash << 40);
  159. hash ^= char_buf[i];
  160. }
  161. return hash;
  162. }
  163. int main(int argc, char** argv) {
  164. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  165. ::testing::InitGoogleTest(&argc, argv);
  166. // Populate a buffer with a deterministic pattern
  167. // on which to compute checksums
  168. const uint8_t* src = (uint8_t*)ROCKSDB_NAMESPACE::crc32c::buffer;
  169. uint64_t* dst = (uint64_t*)ROCKSDB_NAMESPACE::crc32c::buffer;
  170. const uint64_t* end =
  171. (const uint64_t*)(ROCKSDB_NAMESPACE::crc32c::buffer +
  172. ROCKSDB_NAMESPACE::crc32c::BUFFER_SIZE);
  173. *dst++ = 0;
  174. while (dst < end) {
  175. ROCKSDB_NAMESPACE::EncodeFixed64(
  176. reinterpret_cast<char*>(dst),
  177. fnv64_buf((const char*)src, sizeof(uint64_t)));
  178. dst++;
  179. src += sizeof(uint64_t);
  180. }
  181. return RUN_ALL_TESTS();
  182. }