forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFNV1aHashTest.java
More file actions
216 lines (179 loc) · 6.85 KB
/
FNV1aHashTest.java
File metadata and controls
216 lines (179 loc) · 6.85 KB
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
package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class FNV1aHashTest {
@Test
void testHash32EmptyString() {
// Empty string should return the FNV offset basis
assertEquals(0x811c9dc5, FNV1aHash.hash32(""));
}
@Test
void testHash32SingleCharacter() {
assertEquals(0xe40c292c, FNV1aHash.hash32("a"));
assertEquals(0xe70c2de5, FNV1aHash.hash32("b"));
assertEquals(0xe60c2c52, FNV1aHash.hash32("c"));
}
@Test
void testHash32SimpleStrings() {
assertEquals(0x4ed566da, FNV1aHash.hash32("Hello"));
assertEquals(0xdcfc127b, FNV1aHash.hash32("World"));
assertEquals(0xc0bb652b, FNV1aHash.hash32("Algorithms"));
}
@Test
void testHash32LongString() {
assertEquals(0x048fff90, FNV1aHash.hash32("The quick brown fox jumps over the lazy dog"));
}
@Test
void testHash32Consistency() {
// Same input should always produce same output
String input = "test";
int hash1 = FNV1aHash.hash32(input);
int hash2 = FNV1aHash.hash32(input);
assertEquals(hash1, hash2);
}
@Test
void testHash32Differentiation() {
// Different inputs should produce different outputs
int hash1 = FNV1aHash.hash32("test");
int hash2 = FNV1aHash.hash32("Test");
assertNotEquals(hash1, hash2);
}
@Test
void testHash32WithByteArray() {
byte[] data = "Hello".getBytes(java.nio.charset.StandardCharsets.UTF_8);
assertEquals(0x4ed566da, FNV1aHash.hash32(data));
}
@Test
void testHash32WithEmptyByteArray() {
byte[] data = new byte[0];
assertEquals(0x811c9dc5, FNV1aHash.hash32(data));
}
@Test
void testHash32NullStringThrowsException() {
assertThrows(IllegalArgumentException.class, () -> FNV1aHash.hash32((String) null));
}
@Test
void testHash32NullByteArrayThrowsException() {
assertThrows(IllegalArgumentException.class, () -> FNV1aHash.hash32((byte[]) null));
}
@Test
void testHash64EmptyString() {
// Empty string should return the FNV offset basis
assertEquals(0xcbf29ce484222325L, FNV1aHash.hash64(""));
}
@Test
void testHash64SingleCharacter() {
assertEquals(0xaf63dc4c8601ec8cL, FNV1aHash.hash64("a"));
assertEquals(0xaf63df4c8601f1a5L, FNV1aHash.hash64("b"));
assertEquals(0xaf63de4c8601f012L, FNV1aHash.hash64("c"));
}
@Test
void testHash64SimpleStrings() {
assertEquals(0x63f4e1f2c97e89ebL, FNV1aHash.hash64("Hello"));
assertEquals(0x0f18f77b44424a53L, FNV1aHash.hash64("World"));
assertEquals(0x289a4c6f7f076f3bL, FNV1aHash.hash64("Algorithms"));
}
@Test
void testHash64LongString() {
assertEquals(0xf3f9b7f5e7e47110L, FNV1aHash.hash64("The quick brown fox jumps over the lazy dog"));
}
@Test
void testHash64Consistency() {
// Same input should always produce same output
String input = "test";
long hash1 = FNV1aHash.hash64(input);
long hash2 = FNV1aHash.hash64(input);
assertEquals(hash1, hash2);
}
@Test
void testHash64Differentiation() {
// Different inputs should produce different outputs
long hash1 = FNV1aHash.hash64("test");
long hash2 = FNV1aHash.hash64("Test");
assertNotEquals(hash1, hash2);
}
@Test
void testHash64WithByteArray() {
byte[] data = "Hello".getBytes(java.nio.charset.StandardCharsets.UTF_8);
assertEquals(0x63f4e1f2c97e89ebL, FNV1aHash.hash64(data));
}
@Test
void testHash64WithEmptyByteArray() {
byte[] data = new byte[0];
assertEquals(0xcbf29ce484222325L, FNV1aHash.hash64(data));
}
@Test
void testHash64NullStringThrowsException() {
assertThrows(IllegalArgumentException.class, () -> FNV1aHash.hash64((String) null));
}
@Test
void testHash64NullByteArrayThrowsException() {
assertThrows(IllegalArgumentException.class, () -> FNV1aHash.hash64((byte[]) null));
}
@Test
void testHash32HexFormat() {
assertEquals("4ed566da", FNV1aHash.hash32Hex("Hello"));
assertEquals("dcfc127b", FNV1aHash.hash32Hex("World"));
assertEquals("811c9dc5", FNV1aHash.hash32Hex("")); // Empty string
}
@Test
void testHash64HexFormat() {
assertEquals("63f4e1f2c97e89eb", FNV1aHash.hash64Hex("Hello"));
assertEquals("0f18f77b44424a53", FNV1aHash.hash64Hex("World"));
assertEquals("cbf29ce484222325", FNV1aHash.hash64Hex("")); // Empty string
}
@Test
void testHash32HexNullThrowsException() {
assertThrows(IllegalArgumentException.class, () -> FNV1aHash.hash32Hex(null));
}
@Test
void testHash64HexNullThrowsException() {
assertThrows(IllegalArgumentException.class, () -> FNV1aHash.hash64Hex(null));
}
@Test
void testHash32WithUnicodeCharacters() {
// Test with Unicode characters
assertDoesNotThrow(() -> FNV1aHash.hash32("Hello 世界"));
assertDoesNotThrow(() -> FNV1aHash.hash32("🚀 Rocket"));
// Different Unicode strings should have different hashes
assertNotEquals(FNV1aHash.hash32("Hello"), FNV1aHash.hash32("Hello 世界"));
}
@Test
void testHash64WithUnicodeCharacters() {
// Test with Unicode characters
assertDoesNotThrow(() -> FNV1aHash.hash64("Hello 世界"));
assertDoesNotThrow(() -> FNV1aHash.hash64("🚀 Rocket"));
// Different Unicode strings should have different hashes
assertNotEquals(FNV1aHash.hash64("Hello"), FNV1aHash.hash64("Hello 世界"));
}
@Test
void testHash32Distribution() {
// Test that similar strings produce different hashes
int hash1 = FNV1aHash.hash32("algorithm");
int hash2 = FNV1aHash.hash32("algorithms");
int hash3 = FNV1aHash.hash32("algorithmz");
assertNotEquals(hash1, hash2);
assertNotEquals(hash2, hash3);
assertNotEquals(hash1, hash3);
}
@Test
void testHash64Distribution() {
// Test that similar strings produce different hashes
long hash1 = FNV1aHash.hash64("algorithm");
long hash2 = FNV1aHash.hash64("algorithms");
long hash3 = FNV1aHash.hash64("algorithmz");
assertNotEquals(hash1, hash2);
assertNotEquals(hash2, hash3);
assertNotEquals(hash1, hash3);
}
@Test
void testHash32WithNumericStrings() {
assertNotEquals(FNV1aHash.hash32("123"), FNV1aHash.hash32("1234"));
assertNotEquals(FNV1aHash.hash32("999"), FNV1aHash.hash32("1000"));
}
@Test
void testHash64WithNumericStrings() {
assertNotEquals(FNV1aHash.hash64("123"), FNV1aHash.hash64("1234"));
assertNotEquals(FNV1aHash.hash64("999"), FNV1aHash.hash64("1000"));
}
}