crc32c_unittest.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under the BSD-style license found in the
  3. // LICENSE file in the root directory of this source tree. An additional grant
  4. // of patent rights can be found in the PATENTS file in the same 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 <gtest/gtest.h>
  10. #include "butil/crc32c.h"
  11. namespace butil {
  12. namespace crc32c {
  13. class CRC : public testing::Test { };
  14. TEST_F(CRC, StandardResults) {
  15. // From rfc3720 section B.4.
  16. char buf[32];
  17. memset(buf, 0, sizeof(buf));
  18. ASSERT_EQ(0x8a9136aaU, Value(buf, sizeof(buf)));
  19. memset(buf, 0xff, sizeof(buf));
  20. ASSERT_EQ(0x62a8ab43U, Value(buf, sizeof(buf)));
  21. for (int i = 0; i < 32; i++) {
  22. buf[i] = i;
  23. }
  24. ASSERT_EQ(0x46dd794eU, Value(buf, sizeof(buf)));
  25. for (int i = 0; i < 32; i++) {
  26. buf[i] = 31 - i;
  27. }
  28. ASSERT_EQ(0x113fdb5cU, Value(buf, sizeof(buf)));
  29. unsigned char data[48] = {
  30. 0x01, 0xc0, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00,
  34. 0x14, 0x00, 0x00, 0x00,
  35. 0x00, 0x00, 0x04, 0x00,
  36. 0x00, 0x00, 0x00, 0x14,
  37. 0x00, 0x00, 0x00, 0x18,
  38. 0x28, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00,
  40. 0x02, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00,
  42. };
  43. ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
  44. }
  45. TEST_F(CRC, Values) {
  46. ASSERT_NE(Value("a", 1), Value("foo", 3));
  47. }
  48. TEST_F(CRC, Extend) {
  49. ASSERT_EQ(Value("hello world", 11),
  50. Extend(Value("hello ", 6), "world", 5));
  51. }
  52. TEST_F(CRC, Mask) {
  53. uint32_t crc = Value("foo", 3);
  54. ASSERT_NE(crc, Mask(crc));
  55. ASSERT_NE(crc, Mask(Mask(crc)));
  56. ASSERT_EQ(crc, Unmask(Mask(crc)));
  57. ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
  58. }
  59. TEST_F(CRC, fast_is_on) {
  60. std::cout << "IsFastCrc32Supported=" << IsFastCrc32Supported() << std::endl;
  61. }
  62. } // namespace crc32c
  63. } // namespace butil