Debian Codegraph @

src/test/java/io/airlift/slice/TestMurmur3Hash128.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.HashCode;
17 import com.google.common.hash.Hashing;
18 import org.testng.annotations.Test;
19
20 import java.util.concurrent.ThreadLocalRandom;
21
22 import static org.testng.Assert.assertEquals;
23
24 public class TestMurmur3Hash128
25 {
26 @Test(invocationCount = 100)
27 public void testLessThan16Bytes()
28 throws Exception
29 {
30 byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(16));
31
32 HashCode expected = Hashing.murmur3_128().hashBytes(data);
33 Slice actual = Murmur3Hash128.hash(Slices.wrappedBuffer(data));
34
35 assertEquals(actual.getBytes(), expected.asBytes());
36 }
37
38 @Test(invocationCount = 100)
39 public void testMoreThan16Bytes()
40 throws Exception
41 {
42 byte[] data = randomBytes(131);
43
44 HashCode expected = Hashing.murmur3_128().hashBytes(data);
45 Slice actual = Murmur3Hash128.hash(Slices.wrappedBuffer(data));
46
47 assertEquals(actual.getBytes(), expected.asBytes());
48 }
49
50 @Test(invocationCount = 100)
51 public void testOffsetAndLength()
52 throws Exception
53 {
54 byte[] data = randomBytes(131);
55
56 int offset = 13;
57 int length = 55;
58
59 HashCode expected = Hashing.murmur3_128().hashBytes(data, offset, length);
60 Slice actual = Murmur3Hash128.hash(Slices.wrappedBuffer(data), offset, length);
61
62 assertEquals(actual.getBytes(), expected.asBytes());
63 }
64
65 @Test(invocationCount = 100)
66 public void testNonDefaultSeed()
67 throws Exception
68 {
69 byte[] data = randomBytes(131);
70
71 int seed = 123456789;
72
73 HashCode expected = Hashing.murmur3_128(seed).hashBytes(data);
74 Slice actual = Murmur3Hash128.hash(seed, Slices.wrappedBuffer(data), 0, data.length);
75
76 assertEquals(actual.getBytes(), expected.asBytes());
77 }
78
79 @Test
80 public void testTail()
81 throws Exception
82 {
83 for (int i = 0; i < 16; i++) {
84 byte[] data = randomBytes(50 + i);
85
86 HashCode expected = Hashing.murmur3_128().hashBytes(data);
87 Slice actual = Murmur3Hash128.hash(Slices.wrappedBuffer(data));
88
89 assertEquals(actual.getBytes(), expected.asBytes());
90 }
91 }
92
93 @Test(invocationCount = 100)
94 public void testLessThan16Bytes64()
95 throws Exception
96 {
97 byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(16));
98
99 long expected = Murmur3Hash128.hash(Slices.wrappedBuffer(data)).getLong(0);
100 long actual = Murmur3Hash128.hash64(Slices.wrappedBuffer(data));
101
102 assertEquals(actual, expected);
103 }
104
105 @Test(invocationCount = 100)
106 public void testMoreThan16Bytes64()
107 throws Exception
108 {
109 byte[] data = randomBytes(131);
110
111 long expected = Murmur3Hash128.hash(Slices.wrappedBuffer(data)).getLong(0);
112 long actual = Murmur3Hash128.hash64(Slices.wrappedBuffer(data));
113
114 assertEquals(actual, expected);
115 }
116
117 @Test(invocationCount = 100)
118 public void testOffsetAndLength64()
119 throws Exception
120 {
121 byte[] data = randomBytes(131);
122
123 int offset = 13;
124 int length = 55;
125
126 long expected = Murmur3Hash128.hash(Slices.wrappedBuffer(data), offset, length).getLong(0);
127 long actual = Murmur3Hash128.hash64(Slices.wrappedBuffer(data), offset, length);
128
129 assertEquals(actual, expected);
130 }
131
132 @Test(invocationCount = 100)
133 public void testNonDefaultSeed64()
134 throws Exception
135 {
136 byte[] data = randomBytes(131);
137
138 int seed = 123456789;
139
140 long expected = Murmur3Hash128.hash(seed, Slices.wrappedBuffer(data), 0, data.length).getLong(0);
141 long actual = Murmur3Hash128.hash64(seed, Slices.wrappedBuffer(data), 0, data.length);
142
143 assertEquals(actual, expected);
144 }
145
146 @Test
147 public void testTail64()
148 throws Exception
149 {
150 for (int i = 0; i < 16; i++) {
151 byte[] data = randomBytes(50 + i);
152
153 long expected = Murmur3Hash128.hash(Slices.wrappedBuffer(data)).getLong(0);
154 long actual = Murmur3Hash128.hash64(Slices.wrappedBuffer(data));
155
156 assertEquals(actual, expected);
157 }
158 }
159
160 @Test(invocationCount = 100)
161 public void test64ReturnsMsb()
162 throws Exception
163 {
164 byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(200));
165
166 long expected = Murmur3Hash128.hash(Slices.wrappedBuffer(data)).getLong(0);
167 long actual = Murmur3Hash128.hash64(Slices.wrappedBuffer(data));
168
169 assertEquals(actual, expected);
170 }
171
172 @Test(invocationCount = 100)
173 public void testSingleLong()
174 throws Exception
175 {
176 long value = ThreadLocalRandom.current().nextLong();
177
178 Slice slice = Slices.allocate(8);
179 slice.setLong(0, value);
180 long expected = Murmur3Hash128.hash64(slice);
181 long actual = Murmur3Hash128.hash64(value);
182 assertEquals(actual, expected);
183 }
184
185 private static byte[] randomBytes(int length)
186 {
187 byte[] result = new byte[length];
188 ThreadLocalRandom.current().nextBytes(result);
189 return result;
190 }
191 }