72 line
1.7 KiB

  1. // Copyright (c) 2011 The LevelDB 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. See the AUTHORS file for names of contributors.
  4. #include "util/crc32c.h"
  5. #include "util/testharness.h"
  6. namespace leveldb {
  7. namespace crc32c {
  8. class CRC { };
  9. TEST(CRC, StandardResults) {
  10. // From rfc3720 section B.4.
  11. char buf[32];
  12. memset(buf, 0, sizeof(buf));
  13. ASSERT_EQ(0x8a9136aa, Value(buf, sizeof(buf)));
  14. memset(buf, 0xff, sizeof(buf));
  15. ASSERT_EQ(0x62a8ab43, Value(buf, sizeof(buf)));
  16. for (int i = 0; i < 32; i++) {
  17. buf[i] = i;
  18. }
  19. ASSERT_EQ(0x46dd794e, Value(buf, sizeof(buf)));
  20. for (int i = 0; i < 32; i++) {
  21. buf[i] = 31 - i;
  22. }
  23. ASSERT_EQ(0x113fdb5c, Value(buf, sizeof(buf)));
  24. unsigned char data[48] = {
  25. 0x01, 0xc0, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00,
  29. 0x14, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x04, 0x00,
  31. 0x00, 0x00, 0x00, 0x14,
  32. 0x00, 0x00, 0x00, 0x18,
  33. 0x28, 0x00, 0x00, 0x00,
  34. 0x00, 0x00, 0x00, 0x00,
  35. 0x02, 0x00, 0x00, 0x00,
  36. 0x00, 0x00, 0x00, 0x00,
  37. };
  38. ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
  39. }
  40. TEST(CRC, Values) {
  41. ASSERT_NE(Value("a", 1), Value("foo", 3));
  42. }
  43. TEST(CRC, Extend) {
  44. ASSERT_EQ(Value("hello world", 11),
  45. Extend(Value("hello ", 6), "world", 5));
  46. }
  47. TEST(CRC, Mask) {
  48. uint32_t crc = Value("foo", 3);
  49. ASSERT_NE(crc, Mask(crc));
  50. ASSERT_NE(crc, Mask(Mask(crc)));
  51. ASSERT_EQ(crc, Unmask(Mask(crc)));
  52. ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
  53. }
  54. } // namespace crc32c
  55. } // namespace leveldb
  56. int main(int argc, char** argv) {
  57. return leveldb::test::RunAllTests();
  58. }