2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "util/crc32c.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
namespace crc32c {
|
|
|
|
|
|
|
|
class CRC { };
|
|
|
|
|
|
|
|
TEST(CRC, StandardResults) {
|
|
|
|
// From rfc3720 section B.4.
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0x8a9136aaU, Value(buf, sizeof(buf)));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
memset(buf, 0xff, sizeof(buf));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0x62a8ab43U, Value(buf, sizeof(buf)));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
buf[i] = i;
|
|
|
|
}
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0x46dd794eU, Value(buf, sizeof(buf)));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
buf[i] = 31 - i;
|
|
|
|
}
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0x113fdb5cU, Value(buf, sizeof(buf)));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
unsigned char data[48] = {
|
|
|
|
0x01, 0xc0, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x14, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x04, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x14,
|
|
|
|
0x00, 0x00, 0x00, 0x18,
|
|
|
|
0x28, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x02, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
};
|
|
|
|
ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CRC, Values) {
|
|
|
|
ASSERT_NE(Value("a", 1), Value("foo", 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CRC, Extend) {
|
|
|
|
ASSERT_EQ(Value("hello world", 11),
|
|
|
|
Extend(Value("hello ", 6), "world", 5));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CRC, Mask) {
|
|
|
|
uint32_t crc = Value("foo", 3);
|
|
|
|
ASSERT_NE(crc, Mask(crc));
|
|
|
|
ASSERT_NE(crc, Mask(Mask(crc)));
|
|
|
|
ASSERT_EQ(crc, Unmask(Mask(crc)));
|
|
|
|
ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace crc32c
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|