About: how pages are checked

tests/sims/exthash.test.ts

The automated tests for the /simulators/extendible-hashing 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. After inserts, the table is the unique canonical one for its keys, and global depth = max local depth.
  2. Deletes with merging keep every invariant, and draining shrinks the table back to one bucket.
// /simulators/extendible-hashing
import { test } from 'node:test';
import { create, insert, remove, lookup, canonical, hashOf, indexOf, type Opts, type Table } from '../../src/lib/sims/extHash.ts';
import { checker, lcgInt } from './check.ts';

function invariants(ok: (c: boolean, m: string) => void, t: Table, keys: number[], label: string) {
	ok(t.dir.length === 2 ** t.global, `${label}: directory size is not 2^global`);
	let maxDepth = 0;
	for (const id of new Set<number>(t.dir)) {
		const b = t.buckets.get(id)!;
		maxDepth = Math.max(maxDepth, b.depth);
		const entries = t.dir.map((x, i) => [x, i]).filter(([x]) => x === id).map(([, i]) => i);
		ok(entries.length === 2 ** (t.global - b.depth), `${label}: a bucket has the wrong number of directory entries`);
		ok(b.keys.length <= t.opts.capacity, `${label}: bucket over capacity`);
		for (const h of b.keys) ok(entries.includes(indexOf(t, h)), `${label}: key stored under the wrong entry`);
	}
	for (const k of keys) ok(lookup(t, k) !== null, `${label}: lookup ${k} failed`);
	return maxDepth;
}

test('after inserts, the table is the unique canonical one for its keys, and global depth = max local depth', () => {
	const { ok, done } = checker();
	const r = lcgInt(12345);
	for (const bits of ['low', 'high'] as const) for (const capacity of [1, 2, 3, 4]) for (let trial = 0; trial < 120; trial++) {
		const opts: Opts = { capacity, bits, width: 8, merge: 'never' };
		const t = create(opts);
		const keys: number[] = [];
		const n = 5 + (r() % 40);
		for (let i = 0; i < n; i++) {
			const k = r() % 256;
			if (!keys.includes(k) && insert(t, k).ok) keys.push(k);
		}
		const maxDepth = invariants(ok, t, keys, `${bits} c${capacity}`);
		const can = canonical(opts, keys.map((k) => hashOf(t, k)));
		const key = (g: { depth: number; keys: number[] }) => g.depth + ':' + [...g.keys].sort((a, b) => a - b).join(',');
		const mine = [...new Set<number>(t.dir)].map((id) => key(t.buckets.get(id)!)).sort();
		ok(JSON.stringify(can.groups.map(key).sort()) === JSON.stringify(mine), `${bits} c${capacity}: not the canonical table`);
		ok(t.global === maxDepth && t.global === can.global, `${bits} c${capacity}: global depth`);
	}
	done();
});

test('deletes with merging keep every invariant, and draining shrinks the table back to one bucket', () => {
	const { ok, done } = checker();
	const r = lcgInt(54321);
	for (const merge of ['empty', 'fits'] as const) for (const bits of ['low', 'high'] as const) for (let trial = 0; trial < 150; trial++) {
		const t = create({ capacity: 2, bits, width: 8, merge });
		const keys: number[] = [];
		for (let i = 0; i < 60; i++) {
			if (keys.length && r() % 3 === 0) {
				const k = keys.splice(r() % keys.length, 1)[0];
				ok(remove(t, k).ok, 'remove failed');
			} else {
				const k = r() % 256;
				if (!keys.includes(k) && insert(t, k).ok) keys.push(k);
			}
			invariants(ok, t, keys, `merge ${merge} ${bits}`);
		}
		for (const k of [...keys]) {
			remove(t, k);
			keys.splice(keys.indexOf(k), 1);
			invariants(ok, t, keys, 'drain');
		}
		if (merge === 'fits') ok(t.global === 0 && t.dir.length === 1, `drained table did not shrink (${bits}, global ${t.global})`);
	}
	done();
});

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

Published