About: how pages are checked

tests/sims/disk.test.ts

The automated tests for the /simulators/disk-scheduling 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 Silberschatz queue (head 53): FCFS 640, SSTF 236, SCAN toward 0 236.
  2. Every algorithm serves each request once, matches its closed-form distance, and SSTF is greedy.
// /simulators/disk-scheduling
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { schedule, formula, rng, ALGOS, type Opts } from '../../src/lib/sims/disk.ts';
import { checker } from './check.ts';

test('the Silberschatz queue (head 53): FCFS 640, SSTF 236, SCAN toward 0 236', () => {
	const Q = [98, 183, 37, 122, 14, 124, 65, 67];
	const base: Opts = { head: 53, cylinders: 200, up: false, countJump: true, sstfTie: 'direction' };
	assert.equal(schedule('fcfs', Q, base).total, 640);
	assert.equal(schedule('sstf', Q, base).total, 236);
	assert.equal(schedule('scan', Q, base).total, 236);
});

test('every algorithm serves each request once, matches its closed-form distance, and SSTF is greedy', () => {
	const { ok, done } = checker();
	const r = rng(5);
	for (let k = 0; k < 4000; k++) {
		const cyl = 50 + Math.floor(r() * 300), n = 1 + Math.floor(r() * 12);
		const q = Array.from({ length: n }, () => Math.floor(r() * cyl));
		const o: Opts = { head: Math.floor(r() * cyl), cylinders: cyl, up: r() < 0.5, countJump: r() < 0.5, sstfTie: r() < 0.5 ? 'direction' : 'lower' };
		for (const a of ALGOS) {
			const res = schedule(a.id, q, o);
			ok(res.order.length === q.length && [...res.order].sort((x, y) => x - y).join() === [...q].sort((x, y) => x - y).join(), `${a.id} did not serve every request`);
			const f = formula(a.id, q, o);
			if (f !== null && !q.includes(o.head)) ok(f === res.total, `${a.id} formula ${f} vs simulated ${res.total}`);
			ok(res.moves.every((m) => m.to >= 0 && m.to < cyl), `${a.id} went off the disk`);
		}
		const s = schedule('sstf', q, o);
		let head = o.head;
		const pend = [...q];
		for (const m of s.moves) {
			const best = Math.min(...pend.map((x) => Math.abs(x - head)));
			ok(Math.abs(m.to - head) === best, 'SSTF skipped a nearer request');
			pend.splice(pend.indexOf(m.to), 1);
			head = m.to;
		}
	}
	done();
});

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

Published