Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
crc32c_test.cc
Go to the documentation of this file.
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 
8 namespace leveldb {
9 namespace crc32c {
10 
11 class CRC { };
12 
13 TEST(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 
50 TEST(CRC, Values) {
51  ASSERT_NE(Value("a", 1), Value("foo", 3));
52 }
53 
55  ASSERT_EQ(Value("hello world", 11),
56  Extend(Value("hello ", 6), "world", 5));
57 }
58 
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 
70 int main(int argc, char** argv) {
72 }
int RunAllTests()
Definition: testharness.cc:36
uint32_t Unmask(uint32_t masked_crc)
Definition: crc32c.h:37
uint32_t Mask(uint32_t crc)
Definition: crc32c.h:31
#define ASSERT_EQ(a, b)
Definition: testharness.h:107
int main(int argc, char **argv)
Definition: crc32c_test.cc:70
unsigned int uint32_t
Definition: stdint.h:21
TEST(CRC, StandardResults)
Definition: crc32c_test.cc:13
#define ASSERT_NE(a, b)
Definition: testharness.h:108
uint32_t Value(const char *data, size_t n)
Definition: crc32c.h:20
uint32_t Extend(uint32_t crc, const char *buf, size_t size)
Definition: crc32c.cc:286