1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.hash;
16
17 import static com.google.common.base.Charsets.UTF_8;
18
19 import junit.framework.TestCase;
20
21 import java.util.Arrays;
22
23
24
25
26
27
28
29 public class Crc32cHashFunctionTest extends TestCase {
30
31 public void testZeros() {
32
33 byte[] zeros = new byte[32];
34 Arrays.fill(zeros, (byte) 0x00);
35 assertCrc(0x8a9136aa, zeros);
36 }
37
38 public void testFull() {
39
40 byte[] fulls = new byte[32];
41 Arrays.fill(fulls, (byte) 0xFF);
42 assertCrc(0x62a8ab43, fulls);
43 }
44
45 public void testAscending() {
46
47 byte[] ascending = new byte[32];
48 for (int i = 0; i < 32; i++) {
49 ascending[i] = (byte) i;
50 }
51 assertCrc(0x46dd794e, ascending);
52 }
53
54 public void testDescending() {
55
56 byte[] descending = new byte[32];
57 for (int i = 0; i < 32; i++) {
58 descending[i] = (byte) (31 - i);
59 }
60 assertCrc(0x113fdb5c, descending);
61 }
62
63 public void testScsiReadCommad() {
64
65 byte[] scsiReadCommand = new byte[] {
66 0x01, (byte) 0xc0, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00,
70 0x14, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x04, 0x00,
72 0x00, 0x00, 0x00, 0x14,
73 0x00, 0x00, 0x00, 0x18,
74 0x28, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00,
76 0x02, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00 };
78 assertCrc(0xd9963a56, scsiReadCommand);
79 }
80
81
82 public void testSomeOtherKnownValues() {
83 assertCrc(0x22620404, "The quick brown fox jumps over the lazy dog".getBytes(UTF_8));
84 assertCrc(0xE3069283, "123456789".getBytes(UTF_8));
85 assertCrc(0xf3dbd4fe, "1234567890".getBytes(UTF_8));
86 assertCrc(0xBFE92A83, "23456789".getBytes(UTF_8));
87 }
88
89
90
91
92
93
94
95 private static void assertCrc(int expectedCrc, byte[] data) {
96 int actualCrc = Hashing.crc32c().hashBytes(data).asInt();
97 assertEquals(expectedCrc, actualCrc);
98 }
99
100
101
102
103
104 private static final int CRC32C_GENERATOR = 0x1EDC6F41;
105 private static final int CRC32C_GENERATOR_FLIPPED = Integer.reverse(CRC32C_GENERATOR);
106
107 public void testCrc32cLookupTable() {
108
109 int[] expected = new int[256];
110 for (int i = 0; i < expected.length; i++) {
111 int crc = i;
112 for (int j = 7; j >= 0; j--) {
113 int mask = -(crc & 1);
114 crc = ((crc >>> 1) ^ (CRC32C_GENERATOR_FLIPPED & mask));
115 }
116 expected[i] = crc;
117 }
118
119 int[] actual = Crc32cHashFunction.Crc32cHasher.CRC_TABLE;
120 assertTrue(
121 "Expected: \n" + Arrays.toString(expected) + "\nActual:\n" + Arrays.toString(actual),
122 Arrays.equals(expected, actual));
123 }
124 }