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