Skip to content

Commit 852c61e

Browse files
committed
Add a MurmurHash3_64.update unit-test for TypedArrays which share the same underlying ArrayBuffer (PR 12534 follow-up)
This probably ought to have been included in PR 12534, but better late than never I suppose, since it helps to more clearly demonstrate the bug in a way that a reference-test alone just cannot do. When writing this unit-test I also noticed that it required a certain amount of "luck" to actually trigger the bug, prior to the patch, since it seems that the bug only reproduced for certain *unfortunate* sequences of TypedArray data. (The added unit-test contains one such, purposely simple, example.)
1 parent ea4d88a commit 852c61e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/unit/murmurhash3_spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,34 @@ describe("MurmurHash3_64", function () {
6060
const hexdigest2 = hash.hexdigest();
6161
expect(hexdigest1).not.toEqual(hexdigest2);
6262
});
63+
64+
it(
65+
"generates correct hashes for TypedArrays which share the same " +
66+
"underlying ArrayBuffer (issue 12533)",
67+
function () {
68+
// prettier-ignore
69+
const typedArray = new Uint8Array([
70+
0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
71+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1
72+
]);
73+
const startArray = new Uint8Array(typedArray.buffer, 0, 10);
74+
const endArray = new Uint8Array(typedArray.buffer, 10, 10);
75+
76+
expect(startArray).not.toEqual(endArray);
77+
78+
const startHash = new MurmurHash3_64();
79+
startHash.update(startArray);
80+
const startHexdigest = startHash.hexdigest();
81+
82+
const endHash = new MurmurHash3_64();
83+
endHash.update(endArray);
84+
const endHexdigest = endHash.hexdigest();
85+
86+
// The two hashes *must* be different.
87+
expect(startHexdigest).not.toEqual(endHexdigest);
88+
89+
expect(startHexdigest).toEqual("a49de339cc5b0819");
90+
expect(endHexdigest).toEqual("f81a92d9e214ab35");
91+
}
92+
);
6393
});

0 commit comments

Comments
 (0)