Home | History | Annotate | Download | only in hash
      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 com.google.common.base.Supplier;
     21 
     22 import junit.framework.TestCase;
     23 
     24 import java.util.zip.Checksum;
     25 
     26 /**
     27  * Tests for ChecksumHashFunction.
     28  *
     29  * @author Colin Decker
     30  */
     31 public class ChecksumHashFunctionTest extends TestCase {
     32 
     33   public void testCrc32_equalsChecksumValue() throws Exception {
     34     assertChecksum(CRC_32, "");
     35     assertChecksum(CRC_32, "Z");
     36     assertChecksum(CRC_32, "foobar");
     37   }
     38 
     39   public void testAdler32_equalsChecksumValue() throws Exception {
     40     assertChecksum(ADLER_32, "");
     41     assertChecksum(ADLER_32, "Z");
     42     assertChecksum(ADLER_32, "foobar");
     43   }
     44 
     45   public void testCrc32_knownValues() throws Exception {
     46     assertHash32(0x1C8600E3, CRC_32, "hell");
     47     assertHash32(0x3610A686, CRC_32, "hello");
     48     assertHash32(0xED81F9F6, CRC_32, "hello ");
     49     assertHash32(0x4850DDC2, CRC_32, "hello w");
     50     assertHash32(0x7A2D6005, CRC_32, "hello wo");
     51     assertHash32(0x1C192672, CRC_32, "hello wor");
     52     assertHash32(0x414FA339, CRC_32, "The quick brown fox jumps over the lazy dog");
     53     assertHash32(0x4400B5BC, CRC_32, "The quick brown fox jumps over the lazy cog");
     54   }
     55 
     56   public void testAdler32_knownValues() throws Exception {
     57     assertHash32(0x041701A6, ADLER_32, "hell");
     58     assertHash32(0x062C0215, ADLER_32, "hello");
     59     assertHash32(0x08610235, ADLER_32, "hello ");
     60     assertHash32(0x0B0D02AC, ADLER_32, "hello w");
     61     assertHash32(0x0E28031B, ADLER_32, "hello wo");
     62     assertHash32(0x11B5038D, ADLER_32, "hello wor");
     63     assertHash32(0x5BDC0FDA, ADLER_32, "The quick brown fox jumps over the lazy dog");
     64     assertHash32(0x5BD90FD9, ADLER_32, "The quick brown fox jumps over the lazy cog");
     65   }
     66 
     67   private static void assertChecksum(Supplier<Checksum> supplier, String input) {
     68     byte[] bytes = HashTestUtils.ascii(input);
     69 
     70     Checksum checksum = supplier.get();
     71     checksum.update(bytes, 0, bytes.length);
     72     long value = checksum.getValue();
     73 
     74     String toString = "name";
     75     HashFunction func = new ChecksumHashFunction(supplier, 32, toString);
     76     assertEquals(toString, func.toString());
     77     assertEquals(value, func.hashBytes(bytes).padToLong());
     78   }
     79 
     80   private static void assertHash32(int expected, Supplier<Checksum> supplier, String input) {
     81     byte[] bytes = HashTestUtils.ascii(input);
     82     String toString = "name";
     83     HashFunction func = new ChecksumHashFunction(supplier, 32, toString);
     84     assertEquals(expected, func.hashBytes(bytes).asInt());
     85     assertEquals(toString, func.toString());
     86   }
     87 }
     88