1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.digest;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import org.junit.jupiter.api.Test;
24
25 public class MurmurHash2Test {
26
27
28 static final byte[][] input = {
29 { (byte) 0xed, (byte) 0x53, (byte) 0xc4, (byte) 0xa5, (byte) 0x3b, (byte) 0x1b, (byte) 0xbd, (byte) 0xc2,
30 (byte) 0x52, (byte) 0x7d, (byte) 0xc3, (byte) 0xef, (byte) 0x53, (byte) 0x5f, (byte) 0xae,
31 (byte) 0x3b },
32 { (byte) 0x21, (byte) 0x65, (byte) 0x59, (byte) 0x4e, (byte) 0xd8, (byte) 0x12, (byte) 0xf9, (byte) 0x05,
33 (byte) 0x80, (byte) 0xe9, (byte) 0x1e, (byte) 0xed, (byte) 0xe4, (byte) 0x56, (byte) 0xbb },
34 { (byte) 0x2b, (byte) 0x02, (byte) 0xb1, (byte) 0xd0, (byte) 0x3d, (byte) 0xce, (byte) 0x31, (byte) 0x3d,
35 (byte) 0x97, (byte) 0xc4, (byte) 0x91, (byte) 0x0d, (byte) 0xf7, (byte) 0x17 },
36 { (byte) 0x8e, (byte) 0xa7, (byte) 0x9a, (byte) 0x02, (byte) 0xe8, (byte) 0xb9, (byte) 0x6a, (byte) 0xda,
37 (byte) 0x92, (byte) 0xad, (byte) 0xe9, (byte) 0x2d, (byte) 0x21 },
38 { (byte) 0xa9, (byte) 0x6d, (byte) 0xea, (byte) 0x77, (byte) 0x06, (byte) 0xce, (byte) 0x1b, (byte) 0x85,
39 (byte) 0x48, (byte) 0x27, (byte) 0x4c, (byte) 0xfe },
40 { (byte) 0xec, (byte) 0x93, (byte) 0xa0, (byte) 0x12, (byte) 0x60, (byte) 0xee, (byte) 0xc8, (byte) 0x0a,
41 (byte) 0xc5, (byte) 0x90, (byte) 0x62 },
42 { (byte) 0x55, (byte) 0x6d, (byte) 0x93, (byte) 0x66, (byte) 0x14, (byte) 0x6d, (byte) 0xdf, (byte) 0x00,
43 (byte) 0x58, (byte) 0x99 },
44 { (byte) 0x3c, (byte) 0x72, (byte) 0x20, (byte) 0x1f, (byte) 0xd2, (byte) 0x59, (byte) 0x19, (byte) 0xdb,
45 (byte) 0xa1 },
46 { (byte) 0x23, (byte) 0xa8, (byte) 0xb1, (byte) 0x87, (byte) 0x55, (byte) 0xf7, (byte) 0x8a, (byte) 0x4b,
47
48 }, { (byte) 0xe2, (byte) 0x42, (byte) 0x1c, (byte) 0x2d, (byte) 0xc1, (byte) 0xe4, (byte) 0x3e },
49 { (byte) 0x66, (byte) 0xa6, (byte) 0xb5, (byte) 0x5a, (byte) 0x74, (byte) 0xd9 },
50 { (byte) 0xe8, (byte) 0x76, (byte) 0xa8, (byte) 0x90, (byte) 0x76 },
51 { (byte) 0xeb, (byte) 0x25, (byte) 0x3f, (byte) 0x87 }, { (byte) 0x37, (byte) 0xa0, (byte) 0xa9 },
52 { (byte) 0x5b, (byte) 0x5d }, { (byte) 0x7e }, {} };
53
54
55
56
57
58
59 static final int[] results32_standard = { 0x96814fb3, 0x485dcaba, 0x331dc4ae, 0xc6a7bf2f, 0xcdf35de0, 0xd9dec7cc,
60 0x63a7318a, 0xd0d3c2de, 0x90923aef, 0xaf35c1e2, 0x735377b2, 0x366c98f3, 0x9c48ee29, 0x0b615790, 0xb4308ac1,
61 0xec98125a, 0x106e08d9 };
62
63
64 static final int[] results32_seed = { 0xd92e493e, 0x8b50903b, 0xc3372a7b, 0x48f07e9e, 0x8a5e4a6e, 0x57916df4,
65 0xa346171f, 0x1e319c86, 0x9e1a03cd, 0x9f973e6c, 0x2d8c77f5, 0xabed8751, 0x296708b6, 0x24f8078b, 0x111b1553,
66 0xa7da1996, 0xfe776c70 };
67
68
69 static final long[] results64_standard = { 0x4987cb15118a83d9L, 0x28e2a79e3f0394d9L, 0x8f4600d786fc5c05L,
70 0xa09b27fea4b54af3L, 0x25f34447525bfd1eL, 0x32fad4c21379c7bfL, 0x4b30b99a9d931921L, 0x4e5dab004f936cdbL,
71 0x06825c27bc96cf40L, 0xff4bf2f8a4823905L, 0x7f7e950c064e6367L, 0x821ade90caaa5889L, 0x6d28c915d791686aL,
72 0x9c32649372163ba2L, 0xd66ae956c14d5212L, 0x38ed30ee5161200fL, 0x9bfae0a4e613fc3cL, };
73
74
75 static final long[] results64_seed = { 0x0822b1481a92e97bL, 0xf8a9223fef0822ddL, 0x4b49e56affae3a89L,
76 0xc970296e32e1d1c1L, 0xe2f9f88789f1b08fL, 0x2b0459d9b4c10c61L, 0x377e97ea9197ee89L, 0xd2ccad460751e0e7L,
77 0xff162ca8d6da8c47L, 0xf12e051405769857L, 0xdabba41293d5b035L, 0xacf326b0bb690d0eL, 0x0617f431bc1a8e04L,
78 0x15b81f28d576e1b2L, 0x28c1fe59e4f8e5baL, 0x694dd315c9354ca9L, 0xa97052a8f088ae6cL };
79
80
81 static final String text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
82
83 @Test
84 public void testHash32ByteArrayInt() {
85 for (int i = 0; i < input.length; i++) {
86 final int hash = MurmurHash2.hash32(input[i], input[i].length);
87 if (hash != results32_standard[i]) {
88 fail(String.format("Unexpected hash32 result for example %d: 0x%08x instead of 0x%08x", i, hash,
89 results32_standard[i]));
90 }
91 }
92 }
93
94 @Test
95 public void testHash32ByteArrayIntInt() {
96 for (int i = 0; i < input.length; i++) {
97 final int hash = MurmurHash2.hash32(input[i], input[i].length, 0x71b4954d);
98 if (hash != results32_seed[i]) {
99 fail(String.format("Unexpected hash32 result for example %d: 0x%08x instead of 0x%08x", i, hash,
100 results32_seed[i]));
101 }
102 }
103 }
104
105 @Test
106 public void testHash32String() {
107 final int hash = MurmurHash2.hash32(text);
108 assertEquals(0xb3bf597e, hash);
109 }
110
111 @Test
112 public void testHash32StringIntInt() {
113 final int hash = MurmurHash2.hash32(text, 2, text.length() - 4);
114 assertEquals(0x4d666d90, hash);
115 }
116
117 @Test
118 public void testHash64ByteArrayInt() {
119 for (int i = 0; i < input.length; i++) {
120 final long hash = MurmurHash2.hash64(input[i], input[i].length);
121 if (hash != results64_standard[i]) {
122 fail(String.format("Unexpected hash64 result for example %d: 0x%016x instead of 0x%016x", i, hash,
123 results64_standard[i]));
124 }
125 }
126 }
127
128 @Test
129 public void testHash64ByteArrayIntInt() {
130 for (int i = 0; i < input.length; i++) {
131 final long hash = MurmurHash2.hash64(input[i], input[i].length, 0x344d1f5c);
132 if (hash != results64_seed[i]) {
133 fail(String.format("Unexpected hash64 result for example %d: 0x%016x instead of 0x%016x", i, hash,
134 results64_seed[i]));
135 }
136 }
137 }
138
139 @Test
140 public void testHash64String() {
141 final long hash = MurmurHash2.hash64(text);
142 assertEquals(0x0920e0c1b7eeb261L, hash);
143 }
144
145 @Test
146 public void testHash64StringIntInt() {
147 final long hash = MurmurHash2.hash64(text, 2, text.length() - 4);
148 assertEquals(0xa8b33145194985a2L, hash);
149 }
150 }