Home | History | Annotate | Download | only in hash
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.hash;
     18 
     19 import com.google.common.collect.ImmutableList;
     20 import com.google.common.hash.HashTestUtils.RandomHasherAction;
     21 
     22 import junit.framework.TestCase;
     23 
     24 import java.io.ByteArrayOutputStream;
     25 import java.nio.ByteBuffer;
     26 import java.nio.charset.Charset;
     27 import java.util.Arrays;
     28 import java.util.List;
     29 import java.util.Random;
     30 
     31 /**
     32  * Tests for AbstractNonStreamingHashFunction.
     33  */
     34 public class AbstractNonStreamingHashFunctionTest extends TestCase {
     35   /**
     36    * Constructs two trivial HashFunctions (output := input), one streaming and one non-streaming,
     37    * and checks that their results are identical, no matter which newHasher version we used.
     38    */
     39   public void testExhaustive() {
     40     List<Hasher> hashers = ImmutableList.of(
     41         new StreamingVersion().newHasher(),
     42         new StreamingVersion().newHasher(52),
     43         new NonStreamingVersion().newHasher(),
     44         new NonStreamingVersion().newHasher(123));
     45     Random random = new Random(0);
     46     for (int i = 0; i < 200; i++) {
     47       RandomHasherAction.pickAtRandom(random).performAction(random, hashers);
     48     }
     49     HashCode[] codes = new HashCode[hashers.size()];
     50     for (int i = 0; i < hashers.size(); i++) {
     51       codes[i] = hashers.get(i).hash();
     52     }
     53     for (int i = 1; i < codes.length; i++) {
     54       assertEquals(codes[i - 1], codes[i]);
     55     }
     56   }
     57 
     58   public void testPutStringWithLowSurrogate() {
     59     // we pad because the dummy hash function we use to test this, merely copies the input into
     60     // the output, so the input must be at least 32 bits, since the output has to be that long
     61     assertPutString(new char[] { 'p', HashTestUtils.randomLowSurrogate(new Random()) });
     62   }
     63 
     64   public void testPutStringWithHighSurrogate() {
     65     // we pad because the dummy hash function we use to test this, merely copies the input into
     66     // the output, so the input must be at least 32 bits, since the output has to be that long
     67     assertPutString(new char[] { 'p', HashTestUtils.randomHighSurrogate(new Random()) });
     68   }
     69 
     70   public void testPutStringWithLowHighSurrogate() {
     71     assertPutString(new char[] {
     72         HashTestUtils.randomLowSurrogate(new Random()),
     73         HashTestUtils.randomHighSurrogate(new Random()) });
     74   }
     75 
     76   public void testPutStringWithHighLowSurrogate() {
     77     assertPutString(new char[] {
     78         HashTestUtils.randomHighSurrogate(new Random()),
     79         HashTestUtils.randomLowSurrogate(new Random()) });
     80   }
     81 
     82   private static void assertPutString(char[] chars) {
     83     Hasher h1 = new NonStreamingVersion().newHasher();
     84     Hasher h2 = new NonStreamingVersion().newHasher();
     85     String s = new String(chars);
     86     // this is the correct implementation of the spec
     87     for (int i = 0; i < s.length(); i++) {
     88       h1.putChar(s.charAt(i));
     89     }
     90     h2.putUnencodedChars(s);
     91     assertEquals(h1.hash(), h2.hash());
     92   }
     93 
     94   static class StreamingVersion extends AbstractStreamingHashFunction {
     95     @Override
     96     public int bits() {
     97       return 32;
     98     }
     99 
    100     @Override
    101     public Hasher newHasher() {
    102       return new AbstractStreamingHasher(4, 4) {
    103         final ByteArrayOutputStream out = new ByteArrayOutputStream();
    104         @Override
    105         HashCode makeHash() {
    106           return HashCode.fromBytes(out.toByteArray());
    107         }
    108 
    109         @Override
    110         protected void process(ByteBuffer bb) {
    111           while (bb.hasRemaining()) {
    112             out.write(bb.get());
    113           }
    114         }
    115 
    116         @Override
    117         protected void processRemaining(ByteBuffer bb) {
    118           while (bb.hasRemaining()) {
    119             out.write(bb.get());
    120           }
    121         }
    122       };
    123     }
    124   }
    125 
    126   static class NonStreamingVersion extends AbstractNonStreamingHashFunction {
    127     @Override
    128     public int bits() {
    129       return 32;
    130     }
    131 
    132     @Override
    133     public HashCode hashBytes(byte[] input) {
    134       return HashCode.fromBytes(input);
    135     }
    136 
    137     @Override
    138     public HashCode hashBytes(byte[] input, int off, int len) {
    139       return HashCode.fromBytes(Arrays.copyOfRange(input, off, off + len));
    140     }
    141 
    142     @Override
    143     public HashCode hashString(CharSequence input, Charset charset) {
    144       throw new UnsupportedOperationException();
    145     }
    146 
    147     @Override
    148     public HashCode hashLong(long input) {
    149       throw new UnsupportedOperationException();
    150     }
    151 
    152     @Override
    153     public HashCode hashInt(int input) {
    154       throw new UnsupportedOperationException();
    155     }
    156   }
    157 }
    158