About: how pages are checked

tests/sims/skiplist.test.ts

The automated tests for the /simulators/skip-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. The bottom level is the sorted set, every higher level is a sorted subset, and search agrees.
// /simulators/skip-list
import { test } from 'node:test';
import { create, insert, remove, search, levelKeys } from '../../src/lib/sims/skiplist.ts';
import { checker, lcgInt } from './check.ts';

test('the bottom level is the sorted set, every higher level is a sorted subset, and search agrees', () => {
	const { ok, done } = checker();
	const r = lcgInt(3);
	for (let trial = 0; trial < 300; trial++) {
		const l = create(trial % 2 ? 0.5 : 0.25, 8, trial + 1);
		const set = new Set<number>();
		for (let i = 0; i < 80; i++) {
			const k = r() % 60;
			if (r() % 3 === 0) {
				ok(remove(l, k).found === set.has(k), 'remove reported the wrong result');
				set.delete(k);
			} else {
				insert(l, k, r() % 4 === 0 ? 1 + (r() % 5) : undefined);
				set.add(k);
			}
			const sorted = [...set].sort((a, b) => a - b);
			ok(JSON.stringify(levelKeys(l, 0)) === JSON.stringify(sorted), 'level 0 is not the sorted set');
			for (let lv = 1; lv < l.level; lv++) {
				const ks = levelKeys(l, lv);
				ok(ks.every((x, j) => j === 0 || ks[j - 1] < x) && ks.every((x) => set.has(x)), `level ${lv} is not a sorted subset`);
			}
			ok(l.size === set.size, 'size');
		}
		for (let k = 0; k < 60; k++) ok(search(l, k).found === set.has(k), `search ${k}`);
	}
	done();
});

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

Published