tests/sims/suffix.test.ts
The automated tests for the /simulators/suffix-tree 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.
- Suffix array, LCP array, suffix tree and searches match brute force on random strings.
- Banana: suffix array 6 5 3 1 0 4 2 and longest repeat “ana”.
// /simulators/suffix-tree
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { SuffixTree, suffixArray, lcpArray, saSearch, stats, type SNode } from '../../src/lib/sims/suffix.ts';
import { checker, lcg } from './check.ts';
test('suffix array, LCP array, suffix tree and searches match brute force on random strings', () => {
const { ok, done } = checker();
const r = lcg(13);
for (let trial = 0; trial < 300; trial++) {
const n = 1 + Math.floor(r() * 25);
const A = 'abc'.slice(0, 1 + Math.floor(r() * 3));
let s = '';
for (let i = 0; i < n; i++) s += A[Math.floor(r() * A.length)];
const t = s + '$';
const sa = suffixArray(s);
const ref = Array.from({ length: t.length }, (_, i) => i).sort((a, b) => {
const x = t.slice(a).replace(/\$/g, '\u0000'), y = t.slice(b).replace(/\$/g, '\u0000');
return x < y ? -1 : x > y ? 1 : 0;
});
ok(sa.join() === ref.join(), `suffix array of ${s}`);
const lcp = lcpArray(s, sa);
for (let i = 1; i < sa.length; i++) {
let h = 0;
while (t[sa[i] + h] === t[sa[i - 1] + h] && sa[i] + h < t.length) h++;
ok(lcp[i] === h, `LCP of ${s} at ${i}`);
}
const st = new SuffixTree(s);
const all = st.nodes();
ok(all.filter((x) => !x.kids.length).length === t.length, `${s}: one leaf per suffix`);
ok(all.every((x) => x === st.root || !x.kids.length || x.kids.length >= 2), `${s}: an internal node does not branch`);
ok(all.length <= 2 * t.length, `${s}: more than 2n nodes`);
const order: number[] = [];
const walk = (x: SNode) => { if (!x.kids.length) order.push(x.leaf!); x.kids.forEach(walk); };
walk(st.root);
ok(order.join() === sa.join(), `${s}: leaves in order are not the suffix array`);
for (let q = 0; q < 8; q++) {
const L = 1 + Math.floor(r() * 4);
let p = '';
for (let i = 0; i < L; i++) p += 'abcd'[Math.floor(r() * 4)];
const occ: number[] = [];
for (let i = 0; i + p.length <= s.length; i++) if (s.slice(i, i + p.length) === p) occ.push(i);
const f = st.find(p);
ok(f.found === occ.length > 0 && f.occ.join() === occ.join(), `find ${p} in ${s}`);
const b = saSearch(s, sa, p);
ok(b.hi - b.lo === occ.length && sa.slice(b.lo, b.hi).sort((x, y) => x - y).join() === occ.join(), `binary search for ${p} in ${s}`);
}
const subs = new Set<string>();
for (let i = 0; i < s.length; i++) for (let j = i + 1; j <= s.length; j++) subs.add(s.slice(i, j));
const S = stats(s);
ok(S.distinct === subs.size, `${s}: ${S.distinct} distinct substrings vs ${subs.size}`);
let lrs = '';
for (const x of subs) {
let c = 0;
for (let i = 0; i + x.length <= s.length; i++) if (s.slice(i, i + x.length) === x) c++;
if (c >= 2 && x.length > lrs.length) lrs = x;
}
ok(S.lrs.length === lrs.length, `${s}: longest repeat ${S.lrs} vs ${lrs}`);
}
done();
});
test('banana: suffix array 6 5 3 1 0 4 2 and longest repeat “ana”', () => {
const b = stats('banana');
assert.equal(b.sa.join(' '), '6 5 3 1 0 4 2');
assert.equal(b.lrs, 'ana');
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.