About: how pages are checked

tests/sims/linkedlist.test.ts

The automated tests for the /simulators/linked-list 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. 300 mixed operations on singly linked, doubly linked and array lists leave the same values as a plain array.
  2. Floyd's tortoise and hare finds the same cycle start and length as remembering every visited node.
  3. Walking an array costs one cache miss per 64-byte line (8 elements), and a linked list laid out in order one per line its nodes fill.
// /simulators/linked-list: list operations, Floyd's cycle detection and cache misses.
import { test } from 'node:test';
import { build, run, floyd, traversalMisses, setSeed, chunk, NODE_BYTES, type Kind, type Op } from '../../src/lib/sims/linkedList.ts';
import { checker } from './check.ts';

test('300 mixed operations on singly linked, doubly linked and array lists leave the same values as a plain array', () => {
	const { ok, done } = checker();
	const OPS: Op[] = ['pushFront', 'pushBack', 'insertMid', 'removeMid', 'find', 'reverse', 'middle'];
	setSeed(3);
	for (const kind of ['singly', 'doubly', 'array'] as Kind[]) for (const scattered of [false, true]) {
		let l = build(kind, [10, 20, 30, 40, 50], scattered);
		let ref = [10, 20, 30, 40, 50];
		for (let k = 0; k < 300; k++) {
			const op = OPS[(k * 7 + 3) % OPS.length];
			const arg = op === 'find' ? ref[(k * 5) % Math.max(1, ref.length)] ?? 1 : 100 + k;
			const r = run(l, op, arg, scattered);
			const n = ref.length;
			if (op === 'pushFront') ref.unshift(arg);
			else if (op === 'pushBack') ref.push(arg);
			else if (op === 'insertMid') ref.splice(Math.floor(n / 2), 0, arg);
			else if (op === 'removeMid' && n) ref.splice(Math.floor(n / 2), 1);
			else if (op === 'reverse') ref.reverse();
			ok(JSON.stringify(r.list.values) === JSON.stringify(ref), `${kind} after ${op}`);
			ok(r.list.addr.length === r.list.values.length && new Set(r.list.addr).size === r.list.addr.length, `${kind} ${op}: node addresses not unique`);
			if (op === 'find') ok(r.result.includes(ref.indexOf(arg) >= 0 ? `position ${ref.indexOf(arg)}` : 'not found'), `${kind} find ${arg}`);
			if (op === 'middle') ok(r.result === `middle is ${ref[Math.floor(ref.length / 2)]}`, `${kind} middle: ${r.result}`);
			l = r.list;
			if (ref.length > 60) { l = build(kind, ref.slice(0, 20), scattered); ref = ref.slice(0, 20); }
		}
	}
	done();
});

test("Floyd's tortoise and hare finds the same cycle start and length as remembering every visited node", () => {
	const { ok, done } = checker();
	for (let n = 1; n < 60; n++) for (let c = -1; c < n; c++) {
		const f = floyd(n, c);
		const next = (i: number) => (i + 1 < n ? i + 1 : c >= 0 ? c : -1);
		const seen = new Map<number, number>();
		let i = 0, t = 0, start = -1, len = 0;
		while (i >= 0) { if (seen.has(i)) { start = i; len = t - seen.get(i)!; break; } seen.set(i, t++); i = next(i); }
		ok(f.cycle === c >= 0, `n=${n}, tail joins ${c}: cycle ${f.cycle}`);
		if (c >= 0) ok(f.start === start && f.length === len, `n=${n}, tail joins ${c}: start ${f.start} length ${f.length}, expected ${start} ${len}`);
	}
	done();
});

test('walking an array costs one cache miss per 64-byte line (8 elements), and a linked list laid out in order one per line its nodes fill', () => {
	const { ok, done } = checker();
	for (const n of [1000, 10000]) {
		const a = traversalMisses('array', n, false), s = traversalMisses('singly', n, false);
		ok(a.misses === Math.ceil((n * 8) / 64) || a.misses === Math.ceil((n * 8) / 64) + 1, `array of ${n}: ${a.misses} misses`);
		ok(Math.abs(s.misses - (n * chunk(NODE_BYTES.singly)) / 64) <= 2, `list of ${n}: ${s.misses} misses`);
	}
	done();
});

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

Published