67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
import { diffFuzz } from "./diff-fuzz.js";
|
|
|
|
describe("diffFuzz", () => {
|
|
it("reports zero mismatches when impls agree", { timeout: 5000 }, () => {
|
|
const result = diffFuzz({
|
|
iterations: 50,
|
|
sample: (src) => src(4),
|
|
implA: (buf: Uint8Array) => buf[0] ?? 0,
|
|
implB: (buf: Uint8Array) => buf[0] ?? 0,
|
|
equals: (a, b) => a === b,
|
|
});
|
|
expect(result.mismatches).toBe(0);
|
|
expect(result.firstMismatch).toBeUndefined();
|
|
});
|
|
|
|
it("captures first reproducer", { timeout: 5000 }, () => {
|
|
let count = 0;
|
|
const result = diffFuzz({
|
|
iterations: 10,
|
|
sample: () => ++count,
|
|
implA: (n: number) => n,
|
|
implB: (n: number) => (n === 3 ? 999 : n),
|
|
equals: (a, b) => a === b,
|
|
});
|
|
expect(result.mismatches).toBe(1);
|
|
expect(result.firstMismatch?.iter).toBe(2); // 3rd iter, 0-indexed
|
|
expect(result.firstMismatch?.input).toBe(3);
|
|
expect(result.firstMismatch?.outputA).toBe(3);
|
|
expect(result.firstMismatch?.outputB).toBe(999);
|
|
});
|
|
|
|
it("uses injected byteSource deterministically", { timeout: 5000 }, () => {
|
|
let counter = 0;
|
|
const result = diffFuzz({
|
|
iterations: 3,
|
|
sample: (src) => src(2),
|
|
implA: (b: Uint8Array) => b[0],
|
|
implB: (b: Uint8Array) => b[0],
|
|
equals: (a, b) => a === b,
|
|
byteSource: (n) => {
|
|
const out = new Uint8Array(n);
|
|
for (let i = 0; i < n; i++) out[i] = counter++;
|
|
return out;
|
|
},
|
|
});
|
|
expect(result.iterations).toBe(3);
|
|
expect(result.mismatches).toBe(0);
|
|
expect(counter).toBe(6);
|
|
});
|
|
|
|
it("counts all mismatches even after first reproducer", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
let count = 0;
|
|
const result = diffFuzz({
|
|
iterations: 10,
|
|
sample: () => ++count,
|
|
implA: () => 1,
|
|
implB: () => 2,
|
|
equals: (a, b) => a === b,
|
|
});
|
|
expect(result.mismatches).toBe(10);
|
|
expect(result.firstMismatch?.iter).toBe(0);
|
|
});
|
|
});
|