About: how pages are checked

tests/sims/dp.test.ts

The automated tests for the /simulators/dp-lcs 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. The LCS length and the full set of longest common subsequences match trying every subsequence.
  2. Edit distance matches the plain recursive definition.
  3. The traced path spells out a real LCS, and the edit script turns the first string into the second at the stated cost.
  4. GNU diff --minimal changes exactly len(a) + len(b) - 2 x LCS lines.
// /simulators/dp-lcs: longest common subsequence and edit distance tables, against brute force and GNU diff.
import { test } from 'node:test';
import { execFileSync } from 'node:child_process';
import { mkdtempSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fill, traceback, allLcs } from '../../src/lib/sims/dp.ts';
import { checker, lcg } from './check.ts';

const r = lcg(5);
const rs = (n: number, al: string) => Array.from({ length: n }, () => al[Math.floor(r() * al.length)]).join('');
const isSub = (s: string, t: string) => { let k = 0; for (const c of t) if (k < s.length && s[k] === c) k++; return k === s.length; };
function bruteLcs(a: string, b: string) {
	const set = new Set<string>();
	let best = 0;
	for (let mask = 0; mask < 1 << a.length; mask++) {
		let s = '';
		for (let i = 0; i < a.length; i++) if (mask & (1 << i)) s += a[i];
		if (s.length >= best && isSub(s, b)) { if (s.length > best) { best = s.length; set.clear(); } set.add(s); }
	}
	return { best, set };
}
function editRec(a: string, b: string): number {
	if (!a.length) return b.length;
	if (!b.length) return a.length;
	return Math.min(editRec(a.slice(1), b) + 1, editRec(a, b.slice(1)) + 1, editRec(a.slice(1), b.slice(1)) + (a[0] === b[0] ? 0 : 1));
}
const pairs = Array.from({ length: 300 }, () => [rs(1 + Math.floor(r() * 9), 'ABCD'), rs(1 + Math.floor(r() * 9), 'ABCD')]);

test('the LCS length and the full set of longest common subsequences match trying every subsequence', () => {
	const { ok, done } = checker();
	for (const [a, b] of pairs) {
		const br = bruteLcs(a, b);
		ok(fill('lcs', a, b).T[a.length][b.length] === br.best, `LCS length of ${a}, ${b}`);
		const all = allLcs(a, b);
		ok(all.length === br.set.size && all.every((s) => br.set.has(s)), `all LCSs of ${a}, ${b}: ${all} vs ${[...br.set]}`);
	}
	done();
});

test('edit distance matches the plain recursive definition', () => {
	const { ok, done } = checker();
	for (const [a, b] of pairs) if (a.length + b.length <= 12) ok(fill('edit', a, b).T[a.length][b.length] === editRec(a, b), `edit distance ${a} -> ${b}`);
	done();
});

test('the traced path spells out a real LCS, and the edit script turns the first string into the second at the stated cost', () => {
	const { ok, done } = checker();
	for (const [a, b] of pairs) {
		const L = fill('lcs', a, b), E = fill('edit', a, b);
		const kept = traceback('lcs', a, b, L.dir).ops.filter((o) => o.kind === 'keep').map((o) => o.a).join('');
		ok(bruteLcs(a, b).set.has(kept), `traceback ${kept} is not an LCS of ${a}, ${b}`);
		const ops = traceback('edit', a, b, E.dir).ops;
		ok(ops.filter((o) => o.kind !== 'keep').length === E.T[a.length][b.length], `edit script length for ${a} -> ${b}`);
		ok(ops.filter((o) => o.kind !== 'ins').map((o) => o.a).join('') === a && ops.filter((o) => o.kind !== 'del').map((o) => o.b).join('') === b, `edit script for ${a} -> ${b} does not spell both strings`);
	}
	done();
});

let diff = true;
try { execFileSync('diff', ['--version']); } catch { diff = false; }

test('GNU diff --minimal changes exactly len(a) + len(b) - 2 x LCS lines', { skip: !diff && 'diff is not installed' }, () => {
	const { ok, done } = checker();
	const dir = mkdtempSync(join(tmpdir(), 'lcs-'));
	for (let t = 0; t < 40; t++) {
		const a = rs(40, 'ABCDE'), b = rs(35, 'ABCDE');
		writeFileSync(join(dir, 'a'), [...a].join('\n') + '\n');
		writeFileSync(join(dir, 'b'), [...b].join('\n') + '\n');
		let out = '';
		try { out = execFileSync('diff', ['--minimal', join(dir, 'a'), join(dir, 'b')]).toString(); } catch (e: any) { out = e.stdout.toString(); }
		const changed = out.split('\n').filter((l) => l.startsWith('< ') || l.startsWith('> ')).length;
		const L = fill('lcs', a, b).T[a.length][b.length];
		ok(changed === a.length + b.length - 2 * L, `diff changed ${changed} lines, LCS says ${a.length + b.length - 2 * L}`);
	}
	done();
});

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

Published