crc32c_test.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "gtest/gtest.h"
  6. namespace leveldb {
  7. namespace crc32c {
  8. TEST(CRC, StandardResults) {
  9. // From rfc3720 section B.4.
  10. char buf[32];
  11. memset(buf, 0, sizeof(buf));
  12. ASSERT_EQ(0x8a9136aa, Value(buf, sizeof(buf)));
  13. memset(buf, 0xff, sizeof(buf));
  14. ASSERT_EQ(0x62a8ab43, Value(buf, sizeof(buf)));
  15. for (int i = 0; i < 32; i++) {
  16. buf[i] = i;
  17. }
  18. ASSERT_EQ(0x46dd794e, Value(buf, sizeof(buf)));
  19. for (int i = 0; i < 32; i++) {
  20. buf[i] = 31 - i;
  21. }
  22. ASSERT_EQ(0x113fdb5c, Value(buf, sizeof(buf)));
  23. uint8_t data[48] = {
  24. 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
  26. 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. };
  29. ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
  30. }
  31. TEST(CRC, Values) { ASSERT_NE(Value("a", 1), Value("foo", 3)); }
  32. TEST(CRC, Extend) {
  33. ASSERT_EQ(Value("hello world", 11), Extend(Value("hello ", 6), "world", 5));
  34. }
  35. TEST(CRC, Mask) {
  36. uint32_t crc = Value("foo", 3);
  37. ASSERT_NE(crc, Mask(crc));
  38. ASSERT_NE(crc, Mask(Mask(crc)));
  39. ASSERT_EQ(crc, Unmask(Mask(crc)));
  40. ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
  41. }
  42. } // namespace crc32c
  43. } // namespace leveldb