About: how pages are checked

tests/sims/linhash.test.ts

The automated tests for the /simulators/linear-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. Every key sits in the bucket Litwin’s address function gives, and the load trigger holds the load.
// /simulators/linear-hashing
import { test } from 'node:test';
import { create, insert, remove, address, size, loadFactor, type Opts } from '../../src/lib/sims/linHash.ts';
import { checker, lcgInt } from './check.ts';

test('every key sits in the bucket Litwin’s address function gives, and the load trigger holds the load', () => {
	const { ok, done } = checker();
	const r = lcgInt(77);
	for (const trigger of ['overflow', 'load'] as const) for (const initial of [1, 2, 3, 4]) for (const capacity of [1, 2, 3, 4]) for (let trial = 0; trial < 40; trial++) {
		const o: Opts = { initial, capacity, trigger, threshold: 0.75 };
		const t = create(o);
		const keys: number[] = [];
		for (let i = 0; i < 80; i++) {
			if (keys.length && r() % 5 === 0) {
				const k = keys.splice(r() % keys.length, 1)[0];
				ok(remove(t, k).ok, 'remove failed');
				continue;
			}
			const k = r() % 1000;
			if (keys.includes(k)) continue;
			insert(t, k);
			keys.push(k);
			ok(t.buckets.length === size(t) + t.next, 'bucket count is not N·2^level + next');
			t.buckets.forEach((b, j) => b.keys.forEach((x) => ok(address(t, x) === j, `key ${x} in bucket ${j}, address says ${address(t, x)}`)));
			if (trigger === 'load') ok(loadFactor(t) <= o.threshold + 1e-9, `load ${loadFactor(t)} above threshold`);
		}
		for (const k of keys) ok(t.buckets[address(t, k)].keys.includes(k), 'lookup failed');
	}
	done();
});

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

Published