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.base.Charsets.UTF_16LE;
     18 import static org.junit.Assert.assertArrayEquals;
     19 
     20 import junit.framework.TestCase;
     21 
     22 import java.io.ByteArrayOutputStream;
     23 import java.util.Random;
     24 
     25 /**
     26  * Tests for AbstractByteHasher.
     27  *
     28  * @author Colin Decker
     29  */
     30 public class AbstractByteHasherTest extends TestCase {
     31 
     32   public void testBytes() {
     33     TestHasher hasher = new TestHasher(); // byte order insignificant here
     34     byte[] expected = {1, 2, 3, 4, 5, 6, 7, 8};
     35     hasher.putByte((byte) 1);
     36     hasher.putBytes(new byte[]{2, 3, 4, 5, 6});
     37     hasher.putByte((byte) 7);
     38     hasher.putBytes(new byte[]{});
     39     hasher.putBytes(new byte[]{8});
     40     hasher.assertBytes(expected);
     41   }
     42 
     43   public void testShort() {
     44     TestHasher hasher = new TestHasher();
     45     hasher.putShort((short) 0x0201);
     46     hasher.assertBytes(new byte[]{1, 2});
     47   }
     48 
     49   public void testInt() {
     50     TestHasher hasher = new TestHasher();
     51     hasher.putInt(0x04030201);
     52     hasher.assertBytes(new byte[]{1, 2, 3, 4});
     53   }
     54 
     55   public void testLong() {
     56     TestHasher hasher = new TestHasher();
     57     hasher.putLong(0x0807060504030201L);
     58     hasher.assertBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
     59   }
     60 
     61   public void testChar() {
     62     TestHasher hasher = new TestHasher();
     63     hasher.putChar((char) 0x0201);
     64     hasher.assertBytes(new byte[]{1, 2});
     65   }
     66 
     67   public void testString() {
     68     Random random = new Random();
     69     for (int i = 0; i < 100; i++) {
     70       byte[] bytes = new byte[64];
     71       random.nextBytes(bytes);
     72       String s = new String(bytes, UTF_16LE); // so all random strings are valid
     73       assertEquals(
     74           new TestHasher().putUnencodedChars(s).hash(),
     75           new TestHasher().putBytes(s.getBytes(UTF_16LE)).hash());
     76       assertEquals(
     77           new TestHasher().putUnencodedChars(s).hash(),
     78           new TestHasher().putString(s, UTF_16LE).hash());
     79     }
     80   }
     81 
     82   public void testFloat() {
     83     TestHasher hasher = new TestHasher();
     84     hasher.putFloat(Float.intBitsToFloat(0x04030201));
     85     hasher.assertBytes(new byte[]{1, 2, 3, 4});
     86   }
     87 
     88   public void testDouble() {
     89     TestHasher hasher = new TestHasher();
     90     hasher.putDouble(Double.longBitsToDouble(0x0807060504030201L));
     91     hasher.assertBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
     92   }
     93 
     94   public void testCorrectExceptions() {
     95     TestHasher hasher = new TestHasher();
     96     try {
     97       hasher.putBytes(new byte[8], -1, 4);
     98       fail();
     99     } catch (IndexOutOfBoundsException expected) {
    100     }
    101     try {
    102       hasher.putBytes(new byte[8], 0, 16);
    103       fail();
    104     } catch (IndexOutOfBoundsException expected) {
    105     }
    106     try {
    107       hasher.putBytes(new byte[8], 0, -1);
    108       fail();
    109     } catch (IndexOutOfBoundsException expected) {
    110     }
    111   }
    112 
    113   private class TestHasher extends AbstractByteHasher {
    114 
    115     private final ByteArrayOutputStream out = new ByteArrayOutputStream();
    116 
    117     @Override
    118     protected void update(byte b) {
    119       out.write(b);
    120     }
    121 
    122     @Override
    123     protected void update(byte[] b, int off, int len) {
    124       out.write(b, off, len);
    125     }
    126 
    127     byte[] bytes() {
    128       return out.toByteArray();
    129     }
    130 
    131     void assertBytes(byte[] expected) {
    132       assertArrayEquals(expected, bytes());
    133     }
    134 
    135     @Override
    136     public HashCode hash() {
    137       return HashCode.fromBytesNoCopy(bytes());
    138     }
    139   }
    140 }
    141