tests/sims/sorting.test.ts
The automated tests for the /simulators/sorting-visualizer 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.
- Every algorithm sorts, its recorded swaps and writes replay to the sorted array, and stable sorts are stable.
- Exact comparison counts on sorted and reversed input match the textbook formulas.
- No algorithm averages fewer comparisons than the log2(n!) lower bound.
// /simulators/sorting-visualizer: comparison sorts, their operation traces and counts.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { trace, ALGOS, makeInput, log2Factorial, inversions, type Item, type Op } from '../../src/lib/sims/sorting.ts';
import { checker } from './check.ts';
const replay = (input: Item[], ops: Op[]) => {
const a = input.map((x) => ({ ...x }));
for (const o of ops) { if (o.t === 'swap') [a[o.i], a[o.j]] = [a[o.j], a[o.i]]; else if (o.t === 'set') a[o.i] = o.item; }
return a;
};
test('every algorithm sorts, its recorded swaps and writes replay to the sorted array, and stable sorts are stable', () => {
const { ok, done } = checker();
for (let seed = 1; seed <= 200; seed++) for (const kind of ['random', 'nearly', 'reversed', 'few'] as const) {
const n = 1 + (seed % 97);
const inp = makeInput(kind, n, seed);
const want = inp.map((x) => x.v).sort((a, b) => a - b).join();
const inv = inversions(inp);
for (const A of ALGOS) {
const t = trace(A.id, inp);
ok(t.out.map((x) => x.v).join() === want, `${A.id} did not sort (${kind}, n=${n})`);
ok(replay(inp, t.ops).map((x) => x.v).join() === want, `${A.id}: replaying its operations gives a different array`);
if (A.stable) ok(t.out.every((x, i) => i === 0 || t.out[i - 1].v < x.v || t.out[i - 1].id < x.id), `${A.id} reordered equal keys (${kind})`);
if (A.id === 'insertion') ok(t.comparisons >= inv && t.comparisons <= inv + n - 1, `insertion: ${t.comparisons} comparisons for ${inv} inversions`);
if (A.id === 'bubble') ok(t.writes === 2 * inv, `bubble: ${t.writes} writes for ${inv} inversions`);
if (A.id === 'selection') ok(t.comparisons === (n * (n - 1)) / 2, 'selection comparisons');
if (A.id === 'merge' && n > 1) { const L = Math.ceil(Math.log2(n)); ok(t.comparisons <= n * L - 2 ** L + 1, `merge sort over its worst case for n=${n}`); }
}
}
done();
});
test('exact comparison counts on sorted and reversed input match the textbook formulas', () => {
const distinct = (n: number, rev = false) => Array.from({ length: n }, (_, i) => ({ v: rev ? n - i : i + 1, id: i }));
for (const n of [8, 24, 48, 96]) {
const s = distinct(n), r = distinct(n, true), m = (n * (n - 1)) / 2;
assert.equal(trace('bubble', s).comparisons, n - 1);
assert.equal(trace('bubble', r).comparisons, m);
assert.equal(trace('insertion', s).comparisons, n - 1);
assert.equal(trace('insertion', r).comparisons, m);
assert.equal(trace('quick', s).comparisons, m); // last-element pivot on sorted input: the worst case
assert.equal(trace('quick', r).comparisons, m);
assert.equal(trace('insertion', s).writes, 0);
if ((n & (n - 1)) === 0) assert.equal(trace('merge', s).comparisons, (n / 2) * Math.log2(n));
}
});
test('no algorithm averages fewer comparisons than the log2(n!) lower bound', () => {
for (const n of [24, 96]) {
const lb = log2Factorial(n);
for (const A of ALGOS) {
let sum = 0;
for (let s = 1; s <= 200; s++) sum += trace(A.id, makeInput('random', n, 1000 + s)).comparisons;
assert.ok(sum / 200 >= lb, `${A.id} averages ${sum / 200} comparisons for n=${n}, below ${lb}`);
}
}
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.