tests/sims/bloom.test.ts
The automated tests for the /simulators/bloom-filter 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 item gets k distinct positions inside the bit array.
- There are never false negatives.
- The measured false-positive rate matches the exact rate for k distinct positions per item.
- Sizing: 1 million items at 1% need about 9.6 bits per item and 7 hash functions.
// /simulators/bloom-filter: Bloom filter positions, false positives and sizing.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { BloomFilter, positionsFor, measure, fpRate, size } from '../../src/lib/sims/bloom.ts';
import { lcg } from './check.ts';
const word = (r: () => number) => Math.floor(r() * 2 ** 40).toString(36);
test('each item gets k distinct positions inside the bit array', () => {
const r = lcg(1);
for (const m of [32, 64, 128, 256]) for (const k of [1, 2, 3, 4, 5, 7]) for (let i = 0; i < 2000; i++) {
const p = positionsFor(word(r), m, k);
assert.equal(new Set(p).size, k);
assert.ok(p.every((x) => x >= 0 && x < m));
}
});
test('there are never false negatives', () => {
const r = lcg(2);
for (let t = 0; t < 2000; t++) {
const m = [32, 64, 128, 256][t % 4], k = [1, 2, 3, 4, 5, 7][t % 6];
const f = new BloomFilter(m, k);
const set = Array.from({ length: 1 + Math.floor(r() * m) }, () => word(r));
set.forEach((w) => f.add(w));
for (const w of set) assert.ok(f.test(w).maybe, `${w} added but reported absent`);
}
});
test('the measured false-positive rate matches the exact rate for k distinct positions per item', () => {
// exact probability that k given distinct positions are all set after n items each set k distinct uniform positions
const C = (a: number, b: number) => { if (b < 0 || b > a) return 0; let x = 1; for (let i = 0; i < b; i++) x = (x * (a - i)) / (i + 1); return x; };
const exact = (m: number, k: number, n: number) => { let s = 0; for (let j = 0; j <= k; j++) s += (-1) ** j * C(k, j) * (C(m - j, k) / C(m, k)) ** n; return s; };
const r = lcg(3);
for (const [m, k, n] of [[64, 3, 20], [64, 1, 20], [128, 4, 30], [256, 3, 60], [32, 2, 16]]) {
let sum = 0;
const T = 1500;
for (let t = 0; t < T; t++) {
const f = new BloomFilter(m, k), set = new Set<string>();
while (set.size < n) { const w = word(r); set.add(w); f.add(w); }
sum += measure(f, (s) => set.has(s), 1000).rate;
}
const ex = exact(m, k, n);
assert.ok(Math.abs(sum / T - ex) / ex < 0.06, `m=${m} k=${k} n=${n}: measured ${sum / T}, exact ${ex}`);
}
});
test('sizing: 1 million items at 1% need about 9.6 bits per item and 7 hash functions', () => {
const s = size(1e6, 0.01);
assert.ok(Math.abs(s.m / 1e6 - 9.585) < 0.01, `${s.m / 1e6} bits per item`);
assert.equal(s.k, 7);
assert.ok(Math.abs(fpRate(s.m, s.k, 1e6) - 0.01) < 0.0005);
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.