About: how pages are checked

tests/sims/inode.test.ts

The automated tests for the /simulators/filesystem 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. Index-block counts and lookup hops match an explicitly built pointer tree.
  2. Ext2 maximum file size with 4 KiB blocks: 12 + 1024 + 1024² + 1024³ blocks.
// /simulators/filesystem: inode block pointers.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { capacity, locate, footprint, type Cfg } from '../../src/lib/sims/inode.ts';
import { checker } from './check.ts';

// Builds the pointer tree one data block at a time, the way a file system fills direct pointers and
// then each indirect tree in turn, and counts index blocks and the hops to reach any data block.
type Ix = { kids: (Ix | number)[] };
function build(c: Cfg, nData: number) {
	const K = Math.floor(c.block / c.ptr);
	let nextBlock = 0, indexBlocks = 0;
	const roots: { lv: number; node: Ix | number }[] = [];
	const slots: number[] = [];
	[c.direct, c.single, c.double, c.triple].forEach((n, lv) => { for (let i = 0; i < n; i++) slots.push(lv); });
	const add = (node: Ix, d: number): boolean => {
		if (d === 1) {
			if (node.kids.length >= K) return false;
			node.kids.push(nextBlock++);
			return true;
		}
		const last = node.kids[node.kids.length - 1] as Ix | undefined;
		if (last && add(last, d - 1)) return true;
		if (node.kids.length >= K) return false;
		const fresh: Ix = { kids: [] };
		indexBlocks++;
		node.kids.push(fresh);
		return add(fresh, d - 1);
	};
	let si = 0;
	for (let i = 0; i < nData; i++) {
		for (;;) {
			if (si >= slots.length) return null;
			const lv = slots[si];
			if (lv === 0) {
				if (roots.length === si) { roots.push({ lv: 0, node: nextBlock++ }); si++; break; }
			} else {
				if (roots.length === si) { roots.push({ lv, node: { kids: [] } }); indexBlocks++; }
				if (add(roots[si].node as Ix, lv)) break;
				si++;
			}
		}
	}
	const find = (L: number) => {
		let seen = 0;
		for (const r of roots) {
			if (r.lv === 0) { if (seen === L) return { hops: 0 }; seen++; continue; }
			const walk = (node: Ix, d: number, hops: number): { hops: number } | null => {
				for (const k of node.kids) {
					if (d === 1) { if (seen === L) return { hops }; seen++; }
					else { const x = walk(k as Ix, d - 1, hops + 1); if (x) return x; }
				}
				return null;
			};
			const x = walk(r.node as Ix, r.lv, 1);
			if (x) return x;
		}
		return null;
	};
	return { indexBlocks, find };
}

test('index-block counts and lookup hops match an explicitly built pointer tree', () => {
	const { ok, done } = checker();
	const cfgs: Cfg[] = [
		{ block: 64, ptr: 4, direct: 3, single: 1, double: 1, triple: 1 },
		{ block: 32, ptr: 8, direct: 2, single: 2, double: 1, triple: 1 },
		{ block: 64, ptr: 8, direct: 0, single: 0, double: 2, triple: 0 }
	];
	for (const c of cfgs) {
		const cap = Number(capacity(c).blocks);
		for (const n of [0, 1, 2, 3, 4, 5, 17, 19, 20, 100, 275, 276, 277, Math.min(cap, 3000), Math.min(cap, 4372), cap]) {
			if (n > cap) continue;
			const t = build(c, n);
			if (!t) { ok(false, `could not build ${n} blocks`); continue; }
			const f = footprint(c, BigInt(n) * BigInt(c.block));
			ok(Number(f.index) === t.indexBlocks && Number(f.data) === n && f.fits, `footprint ${JSON.stringify(c)} n=${n}: ${f.index} vs ${t.indexBlocks}`);
			if (n === Math.min(cap, 3000)) for (let L = 0; L < n; L += 7) {
				const loc = locate(c, BigInt(L) * BigInt(c.block) + 5n, true);
				const e = t.find(L);
				ok(!!(loc.ok && e && loc.indexReads === e.hops && loc.reads === e.hops + 1), `locate block ${L} in ${JSON.stringify(c)}`);
			}
		}
		ok(!locate(c, BigInt(cap) * BigInt(c.block), true).ok, 'offset past the maximum file size was accepted');
		ok(!footprint(c, BigInt(cap + 1) * BigInt(c.block)).fits, 'file larger than the maximum was accepted');
	}
	done();
});

test('ext2 maximum file size with 4 KiB blocks: 12 + 1024 + 1024² + 1024³ blocks', () => {
	const c = capacity({ block: 4096, ptr: 4, direct: 12, single: 1, double: 1, triple: 1 });
	assert.equal(c.blocks, 12n + 1024n + 1024n ** 2n + 1024n ** 3n);
	assert.equal(c.bytes, c.blocks * 4096n);
});

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

Published