Debian Codegraph @

src/test/java/io/airlift/slice/TestMurmur3Hash32.java

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice;
15
16 import com.google.common.hash.Hashing;
17 import org.testng.annotations.Test;
18
19 import java.util.concurrent.ThreadLocalRandom;
20
21 import static org.testng.Assert.assertEquals;
22
23 public class TestMurmur3Hash32
24 {
25 @Test(invocationCount = 100)
26 public void testLessThan4Bytes()
27 throws Exception
28 {
29 byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(4));
30
31 int expected = Hashing.murmur3_32().hashBytes(data).asInt();
32 int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data));
33
34 assertEquals(actual, expected);
35 }
36
37 @Test(invocationCount = 100)
38 public void testMoreThan4Bytes()
39 throws Exception
40 {
41 byte[] data = randomBytes(131);
42
43 int expected = Hashing.murmur3_32().hashBytes(data).asInt();
44 int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data));
45
46 assertEquals(actual, expected);
47 }
48
49 @Test(invocationCount = 100)
50 public void testOffsetAndLength()
51 throws Exception
52 {
53 byte[] data = randomBytes(131);
54
55 int offset = 13;
56 int length = 55;
57
58 int expected = Hashing.murmur3_32().hashBytes(data, offset, length).asInt();
59 int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data), offset, length);
60
61 assertEquals(actual, expected);
62 }
63
64 @Test(invocationCount = 100)
65 public void testNonDefaultSeed()
66 throws Exception
67 {
68 byte[] data = randomBytes(131);
69
70 int seed = 123456789;
71
72 int expected = Hashing.murmur3_32(seed).hashBytes(data).asInt();
73 int actual = Murmur3Hash32.hash(seed, Slices.wrappedBuffer(data), 0, data.length);
74
75 assertEquals(actual, expected);
76 }
77
78 @Test
79 public void testTail()
80 throws Exception
81 {
82 for (int i = 0; i < 4; i++) {
83 byte[] data = randomBytes(50 + i);
84
85 int expected = Hashing.murmur3_32().hashBytes(data).asInt();
86 int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data));
87
88 assertEquals(actual, expected);
89 }
90 }
91
92 @Test(invocationCount = 100)
93 public void testSingleInt()
94 throws Exception
95 {
96 int value = ThreadLocalRandom.current().nextInt();
97
98 Slice slice = Slices.allocate(4);
99 slice.setInt(0, value);
100 int expected = Murmur3Hash32.hash(slice);
101 int actual = Murmur3Hash32.hash(value);
102 assertEquals(actual, expected);
103 }
104
105 @Test(invocationCount = 100)
106 public void testSingleLong()
107 throws Exception
108 {
109 long value = ThreadLocalRandom.current().nextLong();
110
111 Slice slice = Slices.allocate(8);
112 slice.setLong(0, value);
113 int expected = Murmur3Hash32.hash(slice);
114 int actual = Murmur3Hash32.hash(value);
115 assertEquals(actual, expected);
116 }
117
118 private static byte[] randomBytes(int length)
119 {
120 byte[] result = new byte[length];
121 ThreadLocalRandom.current().nextBytes(result);
122 return result;
123 }
124 }