Home | History | Annotate | Download | only in hash
      1 // Copyright 2011 Google Inc. All Rights Reserved.
      2 
      3 package com.google.common.hash;
      4 
      5 import com.google.common.collect.ImmutableList;
      6 import com.google.common.hash.HashTestUtils.RandomHasherAction;
      7 
      8 import junit.framework.TestCase;
      9 
     10 import java.io.ByteArrayOutputStream;
     11 import java.nio.ByteBuffer;
     12 import java.nio.charset.Charset;
     13 import java.util.Arrays;
     14 import java.util.List;
     15 import java.util.Random;
     16 
     17 /**
     18  * Tests for AbstractNonStreamingHashFunction.
     19  */
     20 public class AbstractNonStreamingHashFunctionTest extends TestCase {
     21   /**
     22    * Constructs two trivial HashFunctions (output := input), one streaming and one non-streaming,
     23    * and checks that their results are identical, no matter which newHasher version we used.
     24    */
     25   public void test() {
     26     List<Hasher> hashers = ImmutableList.of(
     27         new StreamingVersion().newHasher(),
     28         new StreamingVersion().newHasher(52),
     29         new NonStreamingVersion().newHasher(),
     30         new NonStreamingVersion().newHasher(123));
     31     Random random = new Random(0);
     32     for (int i = 0; i < 200; i++) {
     33       RandomHasherAction.pickAtRandom(random).performAction(random, hashers);
     34     }
     35     HashCode[] codes = new HashCode[hashers.size()];
     36     for (int i = 0; i < hashers.size(); i++) {
     37       codes[i] = hashers.get(i).hash();
     38     }
     39     for (int i = 1; i < codes.length; i++) {
     40       assertEquals(codes[i - 1], codes[i]);
     41     }
     42   }
     43 
     44   static class StreamingVersion extends AbstractStreamingHashFunction {
     45     @Override
     46     public int bits() {
     47       return 32;
     48     }
     49 
     50     @Override
     51     public Hasher newHasher() {
     52       return new AbstractStreamingHasher(4, 4) {
     53         final ByteArrayOutputStream out = new ByteArrayOutputStream();
     54         @Override
     55         HashCode makeHash() {
     56           return HashCodes.fromBytes(out.toByteArray());
     57         }
     58 
     59         @Override
     60         protected void process(ByteBuffer bb) {
     61           while (bb.hasRemaining()) {
     62             out.write(bb.get());
     63           }
     64         }
     65 
     66         @Override
     67         protected void processRemaining(ByteBuffer bb) {
     68           while (bb.hasRemaining()) {
     69             out.write(bb.get());
     70           }
     71         }
     72       };
     73     }
     74   }
     75 
     76   static class NonStreamingVersion extends AbstractNonStreamingHashFunction {
     77     @Override
     78     public int bits() {
     79       return 32;
     80     }
     81 
     82     @Override
     83     public HashCode hashBytes(byte[] input) {
     84       return HashCodes.fromBytes(input);
     85     }
     86 
     87     @Override
     88     public HashCode hashBytes(byte[] input, int off, int len) {
     89       return HashCodes.fromBytes(Arrays.copyOfRange(input, off, off + len));
     90     }
     91 
     92     @Override
     93     public HashCode hashString(CharSequence input) {
     94       throw new UnsupportedOperationException();
     95     }
     96 
     97     @Override
     98     public HashCode hashString(CharSequence input, Charset charset) {
     99       throw new UnsupportedOperationException();
    100     }
    101 
    102     @Override
    103     public HashCode hashLong(long input) {
    104       throw new UnsupportedOperationException();
    105     }
    106   }
    107 }
    108