About: how pages are checked

tests/sims/alloc.test.ts

The automated tests for the /simulators/memory-allocator 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 4,000 random mallocs and frees, blocks tile the heap with no gaps or overlaps and the books balance.
  2. Knuth's fifty-percent rule holds: at steady state, free blocks are about half as many as allocated blocks times p.
// /simulators/memory-allocator: first fit, next fit, best fit, buddy and size classes on a 64 KiB heap.
import { test } from 'node:test';
import { make, fiftyPercent, HEAP, resetIds, chunkOf, type Policy, type Block } from '../../src/lib/sims/alloc.ts';
import { checker, lcg } from './check.ts';

const POLICIES: Policy[] = ['first', 'next', 'best', 'buddy', 'classes'];

test('after 4,000 random mallocs and frees, blocks tile the heap with no gaps or overlaps and the books balance', () => {
	const { ok, done } = checker();
	const r = lcg(9);
	for (const p of POLICIES) {
		resetIds();
		const a = make(p);
		const live: { id: number; req: number }[] = [];
		for (let k = 0; k < 4000; k++) {
			if (live.length && (r() < 0.5 || live.length > 150)) {
				const i = Math.floor(r() * live.length);
				ok(a.free(live[i].id), `${p}: free of a live block failed`);
				live.splice(i, 1);
			} else {
				const req = 1 + Math.floor(r() * r() * 3000);
				const res = a.malloc(req);
				if (res.ok) { live.push({ id: res.id, req }); ok(res.size >= (p === 'classes' ? req : chunkOf(req)), `${p}: ${res.size} bytes given for ${req}`); }
			}
			if (k % 50) continue;
			const bl: Block[] = a.blocks();
			if (p !== 'classes') {
				let at = 0;
				for (const b of bl) { ok(b.start === at, `${p}: gap or overlap at ${at}`); at = b.start + b.size; }
				ok(at === HEAP, `${p}: blocks end at ${at}, heap is ${HEAP}`);
			}
			if (p === 'first' || p === 'next' || p === 'best') for (let i = 1; i < bl.length; i++) ok(!(bl[i].free && bl[i - 1].free), `${p}: two free blocks side by side were not coalesced`);
			if (p === 'buddy') for (const b of bl) ok((b.size & (b.size - 1)) === 0 && b.start % b.size === 0, `buddy block ${b.start}+${b.size} is not a power of two aligned to its size`);
			const s = a.stats();
			ok(s.liveObjects === live.length, `${p}: ${s.liveObjects} live objects, expected ${live.length}`);
			ok(s.requested === live.reduce((x, y) => x + y.req, 0), `${p}: requested bytes do not match`);
		}
		for (const l of live) a.free(l.id);
		const s = a.stats();
		ok(s.free === HEAP && s.largestFree === HEAP, `${p}: after freeing everything, free ${s.free} and largest ${s.largestFree}, expected ${HEAP}`);
	}
	done();
});

test("Knuth's fifty-percent rule holds: at steady state, free blocks are about half as many as allocated blocks times p", () => {
	const { ok, done } = checker();
	for (const p of ['first', 'next', 'best'] as Policy[]) for (const t of [60, 150]) {
		const f = fiftyPercent(p, t);
		ok(Math.abs(f.ratio - f.p / 2) < 0.03, `${p} with ${t} live blocks: ratio ${f.ratio.toFixed(3)}, rule predicts ${(f.p / 2).toFixed(3)}`);
	}
	done();
});

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

Published