作者: 韩晨旭 10225101440 李畅 10225102463
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

59 rindas
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. uint8_t data[48] = {
  25. 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
  27. 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29. };
  30. ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
  31. }
  32. TEST(CRC, Values) { ASSERT_NE(Value("a", 1), Value("foo", 3)); }
  33. TEST(CRC, Extend) {
  34. ASSERT_EQ(Value("hello world", 11), Extend(Value("hello ", 6), "world", 5));
  35. }
  36. TEST(CRC, Mask) {
  37. uint32_t crc = Value("foo", 3);
  38. ASSERT_NE(crc, Mask(crc));
  39. ASSERT_NE(crc, Mask(Mask(crc)));
  40. ASSERT_EQ(crc, Unmask(Mask(crc)));
  41. ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
  42. }
  43. } // namespace crc32c
  44. } // namespace leveldb
  45. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }