aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/hash/AbstractStreamingHasherTest.java
blob: 8f1cbb4c5c3e31b6461315a2d44bc3ded0b29e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2011 Google Inc. All Rights Reserved.

package com.google.common.hash;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.hash.AbstractStreamingHashFunction.AbstractStreamingHasher;
import com.google.common.hash.HashTestUtils.RandomHasherAction;

import junit.framework.TestCase;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * Tests for AbstractHashSink.
 * 
 * @author andreou@google.com (Dimitris Andreou)
 */
public class AbstractStreamingHasherTest extends TestCase {
  /** Test we get the HashCode that is created by the sink. Later we ignore the result */
  public void testSanity() { 
    Sink sink = new Sink(4);
    assertEquals(0xDeadBeef, sink.makeHash().asInt());
  }
  
  public void testBytes() {
    Sink sink = new Sink(4); // byte order insignificant here
    byte[] expected = { 1, 2, 3, 4, 5, 6, 7, 8 };
    sink.putByte((byte) 1);
    sink.putBytes(new byte[] { 2, 3, 4, 5, 6 });
    sink.putByte((byte) 7);
    sink.putBytes(new byte[] { });
    sink.putBytes(new byte[] { 8 });
    sink.hash();
    sink.assertInvariants(8);
    sink.assertBytes(expected);
  }
  
  public void testShort() {
    Sink sink = new Sink(4);
    sink.putShort((short) 0x0201);
    sink.hash();
    sink.assertInvariants(2);
    sink.assertBytes(new byte[] { 1, 2, 0, 0 }); // padded with zeros
  }
  
  public void testInt() {
    Sink sink = new Sink(4);
    sink.putInt(0x04030201);
    sink.hash();
    sink.assertInvariants(4);
    sink.assertBytes(new byte[] { 1, 2, 3, 4 });
  }
  
  public void testLong() {
    Sink sink = new Sink(8);
    sink.putLong(0x0807060504030201L);
    sink.hash();
    sink.assertInvariants(8);
    sink.assertBytes(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
  }
  
  public void testChar() {
    Sink sink = new Sink(4);
    sink.putChar((char) 0x0201);
    sink.hash();
    sink.assertInvariants(2);
    sink.assertBytes(new byte[] { 1, 2, 0, 0  }); // padded with zeros
  }
  
  public void testFloat() {
    Sink sink = new Sink(4);
    sink.putFloat(Float.intBitsToFloat(0x04030201));
    sink.hash();
    sink.assertInvariants(4);
    sink.assertBytes(new byte[] { 1, 2, 3, 4 });
  }
  
  public void testDouble() {
    Sink sink = new Sink(8);
    sink.putDouble(Double.longBitsToDouble(0x0807060504030201L));
    sink.hash();
    sink.assertInvariants(8);
    sink.assertBytes(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
  }
  
  public void testCorrectExceptions() {
    Sink sink = new Sink(4);
    try {
      sink.putBytes(new byte[8], -1, 4);
      fail();
    } catch (IndexOutOfBoundsException ok) {}
    try {
      sink.putBytes(new byte[8], 0, 16);
      fail();
    } catch (IndexOutOfBoundsException ok) {}
    try {
      sink.putBytes(new byte[8], 0, -1);
      fail();
    } catch (IndexOutOfBoundsException ok) {}
  }
  
  /**
   * This test creates a long random sequence of inputs, then a lot of differently configured
   * sinks process it; all should produce the same answer, the only difference should be the
   * number of process()/processRemaining() invocations, due to alignment.  
   */
  public void testExhaustive() throws Exception {
    Random random = new Random(0); // will iteratively make more debuggable, each time it breaks
    for (int totalInsertions = 0; totalInsertions < 200; totalInsertions++) {
      
      List<Sink> sinks = Lists.newArrayList();
      for (int chunkSize = 4; chunkSize <= 32; chunkSize++) {
        for (int bufferSize = chunkSize; bufferSize <= chunkSize * 4; bufferSize += chunkSize) {
          // yes, that's a lot of sinks!
          sinks.add(new Sink(chunkSize, bufferSize));
          // For convenience, testing only with big endianness, to match DataOutputStream.
          // I regard highly unlikely that both the little endianness tests above and this one
          // passes, and there is still a little endianness bug lurking around. 
        }
      }
      
      Control control = new Control();
      Hasher controlSink = control.newHasher(1024);
      
      Iterable<Hasher> sinksAndControl = Iterables.concat(
          sinks, Collections.singleton(controlSink));
      for (int insertion = 0; insertion < totalInsertions; insertion++) {
        RandomHasherAction.pickAtRandom(random).performAction(random, sinksAndControl); 
      }
      for (Sink sink : sinks) {
        sink.hash();
      }
      
      byte[] expected = controlSink.hash().asBytes();
      for (Sink sink : sinks) {
        sink.assertInvariants(expected.length);
        sink.assertBytes(expected);
      }
    }
  }
  
  private static class Sink extends AbstractStreamingHasher {
    final int chunkSize;
    final int bufferSize;
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    
    int processCalled = 0;
    boolean remainingCalled = false;
    
    Sink(int chunkSize, int bufferSize) {
      super(chunkSize, bufferSize);
      this.chunkSize = chunkSize;
      this.bufferSize = bufferSize;
    }

    Sink(int chunkSize) {
      super(chunkSize);
      this.chunkSize = chunkSize;
      this.bufferSize = chunkSize;
    }

    @Override HashCode makeHash() {
      return HashCodes.fromInt(0xDeadBeef);
    }

    @Override protected void process(ByteBuffer bb) {
      processCalled++;
      assertEquals(ByteOrder.LITTLE_ENDIAN, bb.order());
      assertTrue(bb.remaining() >= chunkSize);
      for (int i = 0; i < chunkSize; i++) {
        out.write(bb.get());
      }
    }
    
    @Override protected void processRemaining(ByteBuffer bb) {
      assertFalse(remainingCalled);
      remainingCalled = true;
      assertEquals(ByteOrder.LITTLE_ENDIAN, bb.order());
      assertTrue(bb.remaining() > 0);
      assertTrue(bb.remaining() < bufferSize);
      int before = processCalled;
      super.processRemaining(bb);
      int after = processCalled;
      assertEquals(before + 1, after); // default implementation pads and calls process()
      processCalled--; // don't count the tail invocation (makes tests a bit more understandable)
    }
    
    // ensures that the number of invocations looks sane
    void assertInvariants(int expectedBytes) {
      // we should have seen as many bytes as the next multiple of chunk after expectedBytes - 1 
      assertEquals(out.toByteArray().length, ceilToMultiple(expectedBytes, chunkSize));
      assertEquals(expectedBytes / chunkSize, processCalled);
      assertEquals(expectedBytes % chunkSize != 0, remainingCalled);
    }
    
    // returns the minimum x such as x >= a && (x % b) == 0
    private static int ceilToMultiple(int a, int b) {
      int remainder = a % b;
      return remainder == 0 ? a : a + b - remainder;
    }
    
    void assertBytes(byte[] expected) {
      byte[] got = out.toByteArray();
      for (int i = 0; i < expected.length; i++) {
        assertEquals(expected[i], got[i]);
      }
    }
  }
  
  private static class Control extends AbstractNonStreamingHashFunction {
    @Override
    public HashCode hashBytes(byte[] input) {
      return HashCodes.fromBytes(input);
    }
    
    @Override
    public HashCode hashBytes(byte[] input, int off, int len) {
      return hashBytes(Arrays.copyOfRange(input, off, off + len));
    }

    @Override
    public int bits() {
      throw new UnsupportedOperationException();
    }

    @Override
    public HashCode hashString(CharSequence input) {
      throw new UnsupportedOperationException();
    }

    @Override
    public HashCode hashString(CharSequence input, Charset charset) {
      throw new UnsupportedOperationException();
    }

    @Override
    public HashCode hashLong(long input) {
      throw new UnsupportedOperationException();
    }
  }
}