• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.hash;
16 
17 import static com.google.common.hash.Hashing.ChecksumType.ADLER_32;
18 import static com.google.common.hash.Hashing.ChecksumType.CRC_32;
19 
20 import java.util.zip.Checksum;
21 import junit.framework.TestCase;
22 
23 /**
24  * Tests for ChecksumHashFunction.
25  *
26  * @author Colin Decker
27  */
28 public class ChecksumHashFunctionTest extends TestCase {
29 
testCrc32_equalsChecksumValue()30   public void testCrc32_equalsChecksumValue() throws Exception {
31     assertChecksum(CRC_32, "");
32     assertChecksum(CRC_32, "Z");
33     assertChecksum(CRC_32, "foobar");
34   }
35 
testAdler32_equalsChecksumValue()36   public void testAdler32_equalsChecksumValue() throws Exception {
37     assertChecksum(ADLER_32, "");
38     assertChecksum(ADLER_32, "Z");
39     assertChecksum(ADLER_32, "foobar");
40   }
41 
testCrc32_knownValues()42   public void testCrc32_knownValues() throws Exception {
43     assertHash32(0x1C8600E3, CRC_32, "hell");
44     assertHash32(0x3610A686, CRC_32, "hello");
45     assertHash32(0xED81F9F6, CRC_32, "hello ");
46     assertHash32(0x4850DDC2, CRC_32, "hello w");
47     assertHash32(0x7A2D6005, CRC_32, "hello wo");
48     assertHash32(0x1C192672, CRC_32, "hello wor");
49     assertHash32(0x414FA339, CRC_32, "The quick brown fox jumps over the lazy dog");
50     assertHash32(0x4400B5BC, CRC_32, "The quick brown fox jumps over the lazy cog");
51   }
52 
testAdler32_knownValues()53   public void testAdler32_knownValues() throws Exception {
54     assertHash32(0x041701A6, ADLER_32, "hell");
55     assertHash32(0x062C0215, ADLER_32, "hello");
56     assertHash32(0x08610235, ADLER_32, "hello ");
57     assertHash32(0x0B0D02AC, ADLER_32, "hello w");
58     assertHash32(0x0E28031B, ADLER_32, "hello wo");
59     assertHash32(0x11B5038D, ADLER_32, "hello wor");
60     assertHash32(0x5BDC0FDA, ADLER_32, "The quick brown fox jumps over the lazy dog");
61     assertHash32(0x5BD90FD9, ADLER_32, "The quick brown fox jumps over the lazy cog");
62   }
63 
assertChecksum(ImmutableSupplier<Checksum> supplier, String input)64   private static void assertChecksum(ImmutableSupplier<Checksum> supplier, String input) {
65     byte[] bytes = HashTestUtils.ascii(input);
66 
67     Checksum checksum = supplier.get();
68     checksum.update(bytes, 0, bytes.length);
69     long value = checksum.getValue();
70 
71     String toString = "name";
72     HashFunction func = new ChecksumHashFunction(supplier, 32, toString);
73     assertEquals(toString, func.toString());
74     assertEquals(value, func.hashBytes(bytes).padToLong());
75   }
76 
assertHash32( int expected, ImmutableSupplier<Checksum> supplier, String input)77   private static void assertHash32(
78       int expected, ImmutableSupplier<Checksum> supplier, String input) {
79     byte[] bytes = HashTestUtils.ascii(input);
80     String toString = "name";
81     HashFunction func = new ChecksumHashFunction(supplier, 32, toString);
82     assertEquals(expected, func.hashBytes(bytes).asInt());
83     assertEquals(toString, func.toString());
84   }
85 }
86