Skip to content

Commit 114b135

Browse files
committed
Add tests for Murmur3 hash
References #348
1 parent 2d513de commit 114b135

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/main/java/com/rabbitmq/stream/impl/HashUtils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ private HashUtils() {}
2626
// https://github.com/apache/commons-codec/blob/rel/commons-codec-1.15/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
2727
static class Murmur3 implements ToIntFunction<String> {
2828

29+
private static final int DEFAULT_SEED = 104729;
30+
2931
private static final int C1_32 = 0xcc9e2d51;
3032
private static final int C2_32 = 0x1b873593;
3133
private static final int R1_32 = 15;
@@ -57,12 +59,21 @@ private static int fmix32(int hash) {
5759
return hash;
5860
}
5961

62+
private final int seed;
63+
64+
Murmur3() {
65+
this(DEFAULT_SEED);
66+
}
67+
68+
Murmur3(int seed) {
69+
this.seed = seed;
70+
}
71+
6072
@Override
6173
public int applyAsInt(String value) {
6274
byte[] data = value.getBytes(StandardCharsets.UTF_8);
6375
final int offset = 0;
6476
final int length = data.length;
65-
final int seed = 104729;
6677
int hash = seed;
6778
final int nblocks = length >> 2;
6879

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2023 VMware, Inc. or its affiliates. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
4+
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
5+
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
6+
// please see LICENSE-APACHE2.
7+
//
8+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
9+
// either express or implied. See the LICENSE file for specific language governing
10+
// rights and limitations of this software.
11+
//
12+
// If you have any questions regarding licensing, please contact us at
13+
14+
package com.rabbitmq.stream.impl;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
import org.junit.jupiter.params.provider.CsvSource;
20+
21+
public class HashUtilsTest {
22+
23+
@ParameterizedTest
24+
@CsvSource({"hello,1321743225", "brave,3825276426", "new,2970740106", "world,2453398188"})
25+
void murmur3WithDefaultSeed(String input, String unsignedHash) {
26+
int expectedHash = Integer.parseUnsignedInt(unsignedHash);
27+
assertThat(HashUtils.MURMUR3.applyAsInt(input)).isEqualTo(expectedHash);
28+
}
29+
30+
@ParameterizedTest
31+
@CsvSource({"hello,3806057185", "brave,619588758", "new,1483300585", "world,1145629360"})
32+
void murmur3WithSeed(String input, String unsignedHash) {
33+
int expectedHash = Integer.parseUnsignedInt(unsignedHash);
34+
HashUtils.Murmur3 hash = new HashUtils.Murmur3(42);
35+
assertThat(hash.applyAsInt(input)).isEqualTo(expectedHash);
36+
}
37+
}

0 commit comments

Comments
 (0)