forked from ashvardanian/StringZilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
384 lines (293 loc) · 12.4 KB
/
test.js
File metadata and controls
384 lines (293 loc) · 12.4 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import test from "node:test";
import assert from "node:assert";
// Import our zero-copy buffer-only StringZilla
import stringzilla from "../javascript/stringzilla.js";
test("Buffer Find - Positive Case", () => {
const haystack = Buffer.from("hello world, hello john");
const needle = Buffer.from("hello");
const result = stringzilla.find(haystack, needle);
assert.strictEqual(result, 0n);
});
test("Buffer Find - Negative Case (Not Found)", () => {
const haystack = Buffer.from("hello world");
const needle = Buffer.from("xyz");
const result = stringzilla.find(haystack, needle);
assert.strictEqual(result, -1n);
});
test("Buffer Find - Empty Needle", () => {
const haystack = Buffer.from("hello world");
const needle = Buffer.alloc(0);
const result = stringzilla.find(haystack, needle);
assert.strictEqual(result, 0n);
});
test("Buffer Count - Single Occurrence", () => {
const haystack = Buffer.from("hello world");
const needle = Buffer.from("world");
const result = stringzilla.count(haystack, needle);
assert.strictEqual(result, 1n);
});
test("Buffer Count - Multiple Occurrences", () => {
const haystack = Buffer.from("hello world, hello John");
const needle = Buffer.from("hello");
const result = stringzilla.count(haystack, needle, false);
assert.strictEqual(result, 2n);
});
test("Buffer Count - Overlapping Test", () => {
const haystack = Buffer.from("abababab");
const needle = Buffer.from("aba");
// Non-overlapping count
const resultNonOverlap = stringzilla.count(haystack, needle, false);
assert.strictEqual(resultNonOverlap, 2n);
// Overlapping count
const resultOverlap = stringzilla.count(haystack, needle, true);
assert.strictEqual(resultOverlap, 3n);
});
test("Buffer Hash - Basic Hashing", () => {
const buffer = Buffer.from("hello world");
const hash = stringzilla.hash(buffer);
assert.strictEqual(typeof hash, "bigint");
assert(hash > 0n);
});
test("Buffer Hash - Same Input Same Output", () => {
const buffer1 = Buffer.from("hello world");
const buffer2 = Buffer.from("hello world");
const hash1 = stringzilla.hash(buffer1);
const hash2 = stringzilla.hash(buffer2);
assert.strictEqual(hash1, hash2);
});
test("Buffer Hash - Different Seeds Different Output", () => {
const buffer = Buffer.from("hello world");
const hash1 = stringzilla.hash(buffer, 0n);
const hash2 = stringzilla.hash(buffer, 123n);
assert.notStrictEqual(hash1, hash2);
});
test("Hasher Class - Single Buffer", () => {
const buffer = Buffer.from("hello world");
const hasher = new stringzilla.Hasher();
hasher.update(buffer);
const hashStreaming = hasher.digest();
const hashSingle = stringzilla.hash(buffer);
assert.strictEqual(hashSingle, hashStreaming);
});
test("Hasher Class - Multiple Buffers", () => {
const bufferPrefix = Buffer.from("hello ");
const bufferSuffix = Buffer.from("world");
const bufferCombined = Buffer.from("hello world");
const hasher = new stringzilla.Hasher();
hasher.update(bufferPrefix).update(bufferSuffix);
const hashStreaming = hasher.digest();
const hashCombined = stringzilla.hash(bufferCombined);
// Progressive hashing should match single-shot hashing
assert.strictEqual(hashCombined, hashStreaming);
});
test("Hasher Class - Reset Functionality", () => {
const buffer = Buffer.from("hello world");
const hasher = new stringzilla.Hasher();
hasher.update(buffer);
const hash1 = hasher.digest();
hasher.reset();
hasher.update(buffer);
const hash2 = hasher.digest();
assert.strictEqual(hash1, hash2);
});
test("Find Last - Basic Test", () => {
const haystack = Buffer.from("hello world, hello john");
const needle = Buffer.from("hello");
const result = stringzilla.findLast(haystack, needle);
assert.strictEqual(result, 13n);
});
test("Find Byte - Basic Test", () => {
const haystack = Buffer.from("hello world");
const byte = "o".charCodeAt(0);
const result = stringzilla.findByte(haystack, byte);
assert.strictEqual(result, 4n);
});
test("Find Last Byte - Basic Test", () => {
const haystack = Buffer.from("hello world");
const byte = "o".charCodeAt(0);
const result = stringzilla.findLastByte(haystack, byte);
assert.strictEqual(result, 7n);
});
test("Find Byte From - Basic Test", () => {
const haystack = Buffer.from("hello world");
const charset = Buffer.from("aeiou");
const result = stringzilla.findByteFrom(haystack, charset);
assert.strictEqual(result, 1n); // First vowel 'e'
});
test("Find Last Byte From - Basic Test", () => {
const haystack = Buffer.from("hello world");
const charset = Buffer.from("aeiou");
const result = stringzilla.findLastByteFrom(haystack, charset);
assert.strictEqual(result, 7n); // Last vowel 'o'
});
test("Equal - Basic Test", () => {
const buffer1 = Buffer.from("hello");
const buffer2 = Buffer.from("hello");
const buffer3 = Buffer.from("world");
assert.strictEqual(stringzilla.equal(buffer1, buffer2), true);
assert.strictEqual(stringzilla.equal(buffer1, buffer3), false);
});
test("Compare - Basic Test", () => {
const buffer1 = Buffer.from("abc");
const buffer2 = Buffer.from("abc");
const buffer3 = Buffer.from("def");
const buffer4 = Buffer.from("ab");
assert.strictEqual(stringzilla.compare(buffer1, buffer2), 0);
assert(stringzilla.compare(buffer1, buffer3) < 0);
assert(stringzilla.compare(buffer3, buffer1) > 0);
assert(stringzilla.compare(buffer1, buffer4) > 0);
});
test("Byte Sum - Basic Test", () => {
const buffer = Buffer.from([1, 2, 3, 4, 5]);
const expectedSum = 1 + 2 + 3 + 4 + 5;
const result = stringzilla.byteSum(buffer);
assert.strictEqual(result, BigInt(expectedSum));
});
test("Zero-Copy Performance Test", () => {
// Test with larger buffers to demonstrate zero-copy benefits
const largeHaystack = Buffer.alloc(10000, "a");
const needle = Buffer.from("aaa");
// This should be fast due to zero-copy buffer access
const result = stringzilla.find(largeHaystack, needle);
assert.strictEqual(result, 0n);
// Hash performance test
const hash = stringzilla.hash(largeHaystack);
assert.strictEqual(typeof hash, "bigint");
// Byte sum performance test
const byteSum = stringzilla.byteSum(largeHaystack);
assert.strictEqual(typeof byteSum, "bigint");
// Find byte performance test
const byteResult = stringzilla.findByte(largeHaystack, 97); // 'a'
assert.strictEqual(byteResult, 0n);
});
test("Edge Cases - Empty Buffers", () => {
const haystack = Buffer.from("hello world");
const empty = Buffer.alloc(0);
// Finding empty in non-empty should return 0
assert.strictEqual(stringzilla.find(haystack, empty), 0n);
assert.strictEqual(stringzilla.findLast(haystack, empty), BigInt(haystack.length));
// Finding non-empty in empty should return -1
assert.strictEqual(stringzilla.find(empty, haystack), -1n);
assert.strictEqual(stringzilla.findLast(empty, haystack), -1n);
// Empty in empty
assert.strictEqual(stringzilla.find(empty, empty), 0n);
assert.strictEqual(stringzilla.count(empty, empty), 0n);
});
test("Find Byte - Boundary Values", () => {
const buffer = Buffer.from([0, 127, 128, 255]);
// Test boundary byte values
assert.strictEqual(stringzilla.findByte(buffer, 0), 0n);
assert.strictEqual(stringzilla.findByte(buffer, 127), 1n);
assert.strictEqual(stringzilla.findByte(buffer, 128), 2n);
assert.strictEqual(stringzilla.findByte(buffer, 255), 3n);
// Test not found
assert.strictEqual(stringzilla.findByte(buffer, 1), -1n);
});
test("UTF-8 Multi-byte Character Handling", () => {
const haystack = Buffer.from("Hello 世界 World");
const needle = Buffer.from("世界");
// Should work at byte level, not character level
const result = stringzilla.find(haystack, needle);
assert(result > 0n);
// Test with emoji
const emojiBuffer = Buffer.from("Hello 👋 World");
const emoji = Buffer.from("👋");
assert(stringzilla.find(emojiBuffer, emoji) > 0n);
});
test("Pattern at Buffer Boundaries", () => {
const haystack = Buffer.from("abcdefghijk");
// Pattern at start
assert.strictEqual(stringzilla.find(haystack, Buffer.from("abc")), 0n);
// Pattern at end
assert.strictEqual(stringzilla.find(haystack, Buffer.from("ijk")), 8n);
assert.strictEqual(stringzilla.findLast(haystack, Buffer.from("ijk")), 8n);
// Pattern spans entire buffer
assert.strictEqual(stringzilla.find(haystack, haystack), 0n);
});
test("Repeated Patterns", () => {
const haystack = Buffer.from("aaaaaaaaaa");
const needle = Buffer.from("aa");
// Test first and last occurrence
assert.strictEqual(stringzilla.find(haystack, needle), 0n);
assert.strictEqual(stringzilla.findLast(haystack, needle), 8n);
// Count with and without overlap
assert.strictEqual(stringzilla.count(haystack, needle, false), 5n);
assert.strictEqual(stringzilla.count(haystack, needle, true), 9n);
});
test("Find Byte From - Edge Cases", () => {
const haystack = Buffer.from("1234567890");
// Empty charset
const emptyCharset = Buffer.alloc(0);
assert.strictEqual(stringzilla.findByteFrom(haystack, emptyCharset), -1n);
// Charset with all possible bytes
const allBytes = Buffer.alloc(256);
for (let i = 0; i < 256; i++) allBytes[i] = i;
assert.strictEqual(stringzilla.findByteFrom(haystack, allBytes), 0n);
// Charset with duplicates
const duplicates = Buffer.from("1111");
assert.strictEqual(stringzilla.findByteFrom(haystack, duplicates), 0n);
});
test("Binary Data Handling", () => {
// Test with null bytes and binary data
const binaryData = Buffer.from([0x00, 0x01, 0x02, 0x00, 0x03, 0x00]);
const nullByte = Buffer.from([0x00]);
assert.strictEqual(stringzilla.find(binaryData, nullByte), 0n);
assert.strictEqual(stringzilla.findLast(binaryData, nullByte), 5n);
assert.strictEqual(stringzilla.count(binaryData, nullByte), 3n);
// Test hash consistency with binary data
const hash1 = stringzilla.hash(binaryData);
const hash2 = stringzilla.hash(binaryData);
assert.strictEqual(hash1, hash2);
});
test("Large Buffer Operations", () => {
const size = 100000; // 100KB (smaller than 1MB for faster tests)
const largeBuffer = Buffer.alloc(size);
// Fill with pattern
for (let i = 0; i < size; i++) {
largeBuffer[i] = i % 256;
}
// Test operations on large buffer
const pattern = Buffer.from([0, 1, 2, 3]);
assert(stringzilla.count(largeBuffer, pattern) > 0n);
// Test hash performance
const start = Date.now();
const hash = stringzilla.hash(largeBuffer);
const duration = Date.now() - start;
assert(duration < 100); // Should be fast
assert(typeof hash === "bigint");
});
test("Hasher - Incremental vs Single Shot", () => {
const data = Buffer.from("a".repeat(1000));
// Single shot
const hashSingle = stringzilla.hash(data);
// Progressive hashing with different chunk sizes should be consistent
const hasher1 = new stringzilla.Hasher();
hasher1.update(data.subarray(0, 100));
hasher1.update(data.subarray(100, 500));
hasher1.update(data.subarray(500));
const hashProgressive1 = hasher1.digest();
const hasher2 = new stringzilla.Hasher();
hasher2.update(data.subarray(0, 300));
hasher2.update(data.subarray(300));
const hashProgressive2 = hasher2.digest();
// Progressive hashing with same data should be consistent
assert.strictEqual(hashProgressive1, hashProgressive2);
// Test that single-shot and progressive produce valid hashes
assert.strictEqual(typeof hashSingle, "bigint");
assert.strictEqual(typeof hashProgressive1, "bigint");
assert(hashSingle > 0n);
assert(hashProgressive1 > 0n);
});
test("Compare - Special Cases", () => {
// Different lengths
assert(stringzilla.compare(Buffer.from("a"), Buffer.from("aa")) < 0);
assert(stringzilla.compare(Buffer.from("aa"), Buffer.from("a")) > 0);
// Empty buffers
assert.strictEqual(stringzilla.compare(Buffer.alloc(0), Buffer.alloc(0)), 0);
assert(stringzilla.compare(Buffer.alloc(0), Buffer.from("a")) < 0);
assert(stringzilla.compare(Buffer.from("a"), Buffer.alloc(0)) > 0);
// Binary data comparison
const binary1 = Buffer.from([0x00, 0x01, 0x02]);
const binary2 = Buffer.from([0x00, 0x01, 0x03]);
assert(stringzilla.compare(binary1, binary2) < 0);
});