tests/sims/arraypat.test.ts
The automated tests for the /simulators/algorithm-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.
- Each technique gives the brute-force answer on random arrays, and its highlighted range is real.
- The published examples give the published answers: CLRS maximum subarray and LeetCode 209.
// /simulators/algorithm-visualizer: Kadane, sliding windows, two pointers and prefix sums.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { runTech } from '../../src/lib/sims/arraypat.ts';
import { checker, lcg } from './check.ts';
// Brute-force answers, written here rather than imported, by trying every subarray or pair.
const sum = (a: number[], i: number, j: number) => a.slice(i, j + 1).reduce((s, x) => s + x, 0);
function maxSub(a: number[]) { let best = -Infinity; for (let i = 0; i < a.length; i++) for (let j = i; j < a.length; j++) best = Math.max(best, sum(a, i, j)); return best; }
function maxFixed(a: number[], k: number) { let best = -Infinity; for (let i = 0; i + k <= a.length; i++) best = Math.max(best, sum(a, i, i + k - 1)); return best; }
function minLen(a: number[], t: number) { let best = Infinity; for (let i = 0; i < a.length; i++) for (let j = i; j < a.length; j++) if (sum(a, i, j) >= t) best = Math.min(best, j - i + 1); return best === Infinity ? 0 : best; }
function hasPair(a: number[], t: number) { for (let i = 0; i < a.length; i++) for (let j = i + 1; j < a.length; j++) if (a[i] + a[j] === t) return true; return false; }
function countK(a: number[], k: number) { let c = 0; for (let i = 0; i < a.length; i++) for (let j = i; j < a.length; j++) if (sum(a, i, j) === k) c++; return c; }
test('each technique gives the brute-force answer on random arrays, and its highlighted range is real', () => {
const { ok, done } = checker();
const r = lcg(7);
const arr = (n: number, lo: number, hi: number) => Array.from({ length: n }, () => lo + Math.floor(r() * (hi - lo + 1)));
for (let it = 0; it < 2000; it++) {
const n = 1 + Math.floor(r() * 14);
const a = arr(n, -9, 9), pos = arr(n, 1, 9);
const k = 1 + Math.floor(r() * n), t = Math.floor(r() * 30) - 5;
const kd = runTech('kadane', a, 0);
ok(kd.value === maxSub(a), `kadane ${a}: ${kd.value} vs ${maxSub(a)}`);
if (kd.range) ok(sum(a, kd.range[0], kd.range[1]) === kd.value, `kadane range ${kd.range} of ${a}`);
const fx = runTech('fixed', a, k);
ok(fx.value === maxFixed(a, k), `fixed k=${k} ${a}: ${fx.value} vs ${maxFixed(a, k)}`);
if (fx.range) ok(fx.range[1] - fx.range[0] + 1 === k && sum(a, fx.range[0], fx.range[1]) === fx.value, `fixed range ${fx.range}`);
const target = Math.abs(t) + 1;
const ml = runTech('minlen', pos, target);
ok((ml.value ?? 0) === minLen(pos, target), `minlen t=${target} ${pos}: ${ml.value} vs ${minLen(pos, target)}`);
if (ml.range) ok(sum(pos, ml.range[0], ml.range[1]) >= target && ml.range[1] - ml.range[0] + 1 === ml.value, `minlen range ${ml.range}`);
if (n >= 2) {
const ts = runTech('twosum', a, t % 15);
ok(!ts.error, `twosum error ${ts.error}`);
ok((ts.range !== null) === hasPair(a, t % 15), `twosum t=${t % 15} ${a}: found ${ts.range}, brute ${hasPair(a, t % 15)}`);
if (ts.range) ok(ts.arr[ts.range[0]] + ts.arr[ts.range[1]] === t % 15, `twosum pair ${ts.range}`);
}
const px = runTech('prefix', a, t % 8);
ok(px.value === countK(a, t % 8), `prefix k=${t % 8} ${a}: ${px.value} vs ${countK(a, t % 8)}`);
for (const st of px.steps) if (st.win) ok(sum(a, st.win[0], st.win[1]) === t % 8, `prefix window ${st.win}`);
}
done();
});
test('the published examples give the published answers: CLRS maximum subarray and LeetCode 209', () => {
// CLRS 4.1 and LeetCode 53: the maximum subarray of this array is [4, -1, 2, 1] = 6
assert.equal(runTech('kadane', [-2, 1, -3, 4, -1, 2, 1, -5, 4], 0).value, 6);
// LeetCode 209: shortest subarray with sum ≥ 7 is [4, 3]
assert.equal(runTech('minlen', [2, 3, 1, 2, 4, 3], 7).value, 2);
// all negative: Kadane returns the largest single element
assert.equal(runTech('kadane', [-3, -1, -2], 0).value, -1);
// the variable window refuses arrays with negatives, where it gives wrong answers
assert.ok(runTech('minlen', [1, -1, 5], 4).error);
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.