作者: 谢瑞阳 10225101483 徐翔宇 10225101535
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

86 líneas
2.2 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. TEST(CRC, Benchmark) {
  55. std::string data(1048576 * 100, 'x');
  56. double start = Env::Default()->NowMicros() * 1e-6;
  57. static const int kIters = 10;
  58. uint32_t crc = 0;
  59. for (int i = 0; i < kIters; i++) {
  60. crc |= Value(data.data(), data.size());
  61. }
  62. double finish = Env::Default()->NowMicros() * 1e-6;
  63. double mb = (static_cast<long long int>(data.size()) * kIters) / 1048576.0;
  64. fprintf(stderr, "CRC %0.0f MB: %.3f secs; %.1f MB/s, crc=0x%08x\n",
  65. mb, (finish - start), mb / (finish - start), crc);
  66. }
  67. }
  68. }
  69. int main(int argc, char** argv) {
  70. return leveldb::test::RunAllTests();
  71. }