tests/sims/check.ts
Small helpers shared by the simulator tests: a seeded random number generator, so a failing case can be replayed, and a checker that reports the first mismatches.
// Shared helpers for the simulator tests. Each test runs many randomised cases against a reference
// (brute force, a textbook example or a closed-form result) and fails with the first few mismatches.
import assert from 'node:assert/strict';
export function checker() {
const fails: string[] = [];
let n = 0;
const ok = (cond: boolean, msg: string) => {
n++;
if (!cond && fails.length < 200) fails.push(msg);
};
const done = () => assert.ok(fails.length === 0, `${fails.length} of ${n} checks failed:\n ${fails.slice(0, 10).join('\n ')}`);
return { ok, done };
}
/** A small seeded generator, so a failing case can be replayed. Returns floats in [0, 1). */
export function lcg(seed: number) {
let s = seed >>> 0;
return () => {
s = (Math.imul(s, 1103515245) + 12345) >>> 0;
return s / 4294967296;
};
}
/** Same generator, returning the raw 32-bit state. */
export function lcgInt(seed: number) {
let s = seed >>> 0;
return () => {
s = (Math.imul(s, 1103515245) + 12345) >>> 0;
return s;
};
}