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#include "util/testharness.h"
7
8namespace leveldb {
9namespace crc32c {
10
11class CRC { };
12
13TEST(CRC, StandardResults) {
14  // From rfc3720 section B.4.
15  char buf[32];
16
17  memset(buf, 0, sizeof(buf));
18  ASSERT_EQ(0x8a9136aa, Value(buf, sizeof(buf)));
19
20  memset(buf, 0xff, sizeof(buf));
21  ASSERT_EQ(0x62a8ab43, Value(buf, sizeof(buf)));
22
23  for (int i = 0; i < 32; i++) {
24    buf[i] = i;
25  }
26  ASSERT_EQ(0x46dd794e, Value(buf, sizeof(buf)));
27
28  for (int i = 0; i < 32; i++) {
29    buf[i] = 31 - i;
30  }
31  ASSERT_EQ(0x113fdb5c, Value(buf, sizeof(buf)));
32
33  unsigned char data[48] = {
34    0x01, 0xc0, 0x00, 0x00,
35    0x00, 0x00, 0x00, 0x00,
36    0x00, 0x00, 0x00, 0x00,
37    0x00, 0x00, 0x00, 0x00,
38    0x14, 0x00, 0x00, 0x00,
39    0x00, 0x00, 0x04, 0x00,
40    0x00, 0x00, 0x00, 0x14,
41    0x00, 0x00, 0x00, 0x18,
42    0x28, 0x00, 0x00, 0x00,
43    0x00, 0x00, 0x00, 0x00,
44    0x02, 0x00, 0x00, 0x00,
45    0x00, 0x00, 0x00, 0x00,
46  };
47  ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
48}
49
50TEST(CRC, Values) {
51  ASSERT_NE(Value("a", 1), Value("foo", 3));
52}
53
54TEST(CRC, Extend) {
55  ASSERT_EQ(Value("hello world", 11),
56            Extend(Value("hello ", 6), "world", 5));
57}
58
59TEST(CRC, Mask) {
60  uint32_t crc = Value("foo", 3);
61  ASSERT_NE(crc, Mask(crc));
62  ASSERT_NE(crc, Mask(Mask(crc)));
63  ASSERT_EQ(crc, Unmask(Mask(crc)));
64  ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
65}
66
67}  // namespace crc32c
68}  // namespace leveldb
69
70int main(int argc, char** argv) {
71  return leveldb::test::RunAllTests();
72}
73