Initial Commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { describe, expect, it } from "@suaveplan/testing/runner";
|
||||
import { extraDataMatches } from "./extra-data.js";
|
||||
|
||||
function bytewise(a: Uint8Array, b: Uint8Array): boolean {
|
||||
if (a.length !== b.length) return false;
|
||||
let acc = 0;
|
||||
for (let i = 0; i < a.length; i++)
|
||||
acc |= (a[i] as number) ^ (b[i] as number);
|
||||
return acc === 0;
|
||||
}
|
||||
|
||||
describe("extraDataMatches", () => {
|
||||
it("returns true for equal buffers", { timeout: 5000 }, () => {
|
||||
const a = new Uint8Array([1, 2, 3, 4]);
|
||||
const b = new Uint8Array([1, 2, 3, 4]);
|
||||
expect(extraDataMatches(a, b, bytewise)).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for differing content (same length)", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
const a = new Uint8Array([1, 2, 3, 4]);
|
||||
const b = new Uint8Array([1, 2, 3, 5]);
|
||||
expect(extraDataMatches(a, b, bytewise)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false on length mismatch without calling equal", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
let called = false;
|
||||
const fn = () => {
|
||||
called = true;
|
||||
return true;
|
||||
};
|
||||
const out = extraDataMatches(
|
||||
new Uint8Array([1, 2, 3]),
|
||||
new Uint8Array([1, 2]),
|
||||
fn
|
||||
);
|
||||
expect(out).toBe(false);
|
||||
expect(called).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true on empty buffers", { timeout: 5000 }, () => {
|
||||
expect(
|
||||
extraDataMatches(new Uint8Array(0), new Uint8Array(0), bytewise)
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user