About: how pages are checked

tests/sims/recursion.test.ts

The automated tests for the /simulators/recursion simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.

  1. Results and call counts match closed forms for every built-in function.
  2. Every recorded call has exactly one return.
// /simulators/recursion
import { test } from 'node:test';
import { run } from '../../src/lib/sims/recursion.ts';
import { checker } from './check.ts';

const F = (n: number) => { let a = 0n, b = 1n; for (let i = 0; i < n; i++) [a, b] = [b, a + b]; return a; };
const C = (n: number, k: number) => { let r = 1n; for (let i = 1; i <= k; i++) r = (r * BigInt(n - k + i)) / BigInt(i); return r; };
const gcd = (a: bigint, b: bigint): bigint => (b === 0n ? a : gcd(b, a % b));

test('results and call counts match closed forms for every built-in function', () => {
	const { ok, done } = checker();
	for (let n = 0; n <= 25; n++) { const t = run('fib', { n }, false, false); ok(t.result === F(n).toString() && t.calls === Number(2n * F(n + 1) - 1n) && t.maxDepth === Math.max(1, n), `fib(${n}): ${t.calls} calls`); }
	for (let n = 1; n <= 90; n++) { const t = run('fib', { n }, true, false); ok(t.result === F(n).toString() && t.calls === 2 * n - 1 && (n < 2 || t.distinct === n + 1), `memoised fib(${n}): ${t.calls} calls`); }
	for (let n = 0; n <= 50; n++) { const t = run('fact', { n }, false); let f = 1n; for (let i = 2; i <= n; i++) f *= BigInt(i); ok(t.result === f.toString() && t.calls === n + 1 && t.maxDepth === n + 1, `fact(${n})`); }
	for (let n = 1; n <= 14; n++) { const t = run('hanoi', { n }, false); ok(t.result === String(2 ** n - 1) && t.calls === 2 ** n - 1 && t.moves.length === Math.min(4096, 2 ** n - 1), `hanoi(${n})`); }
	for (let n = 0; n <= 18; n++) for (let k = 0; k <= n; k++) {
		const t = run('binom', { n, k }, false, false);
		ok(t.result === C(n, k).toString() && t.calls === Number(2n * C(n, k) - 1n), `C(${n},${k})`);
		ok(run('binom', { n, k }, true, false).result === C(n, k).toString(), `memoised C(${n},${k})`);
	}
	for (const [x, n] of [[2, 13], [3, 0], [3, 1], [-2, 7], [7, 100], [2, 1000]]) { const t = run('pow', { x, n }, false, false); ok(t.result === (BigInt(x) ** BigInt(n)).toString() && t.calls === (n === 0 ? 1 : Math.floor(Math.log2(n)) + 2), `pow(${x},${n}): ${t.calls} calls`); }
	for (const [a, b] of [[252, 105], [1071, 462], [0, 5], [5, 0], [832040, 514229]]) ok(run('gcd', { a, b }, false).result === gcd(BigInt(a), BigInt(b)).toString(), `gcd(${a},${b})`);
	for (const n of [1, 2, 3, 7, 8, 16, 100, 1024]) {
		const t = run('msort', { n }, false, false);
		ok(t.calls === 2 * n - 1, `merge sort of ${n}: ${t.calls} calls`);
		if ((n & (n - 1)) === 0 && n > 1) ok(t.levelWork.every((w) => w === n) && t.levelWork.length === Math.log2(n) && t.result === String(n * Math.log2(n)), `merge sort work per level, n=${n}`);
	}
	done();
});

test('every recorded call has exactly one return', () => {
	const { ok, done } = checker();
	for (const [fn, a, m] of [['fib', { n: 6 }, false], ['fib', { n: 6 }, true], ['binom', { n: 6, k: 3 }, true], ['hanoi', { n: 4 }, false]] as const) {
		const t = run(fn as never, a as never, m);
		const calls = t.events.filter((e) => e.kind === 'call').length;
		const ends = t.events.filter((e) => e.kind !== 'call').length;
		ok(calls === ends && calls === t.calls && t.nodes.length === t.calls, `trace for ${fn}`);
	}
	done();
});

Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.

Published