Files
pplus-web/tests/corpus.test.mjs
T
2026-10-05 03:03:38 +00:00

39 lines
2.8 KiB
JavaScript

// The pseudo stress corpus: 37 programs across domains (numbers, text, finance, graphs, simulation,
// statistics, cryptography, records, sets, recursion), plus runs that must stop and mistakes the checker
// must refuse. Each file states what it expects in header comments. See docs/pseudo-stress-test.md.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
const dir = path.join(path.dirname(fileURLToPath(import.meta.url)), 'corpus');
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.pseudo')).sort();
for (const f of files) {
test(`corpus: ${f}`, () => {
const src = fs.readFileSync(path.join(dir, f), 'utf8');
const expect = [...src.matchAll(/^\/\/ EXPECT: (.*)$/gm)].map((m) => m[1]);
const input = [...src.matchAll(/^\/\/ INPUT: (.*)$/gm)].map((m) => m[1]);
const stops = /^\/\/ STOPS: (\w+)$/m.exec(src)?.[1];
const errorsWanted = /^\/\/ ERRORS: (\d+)$/m.exec(src)?.[1];
const st = studyOf(src);
const errs = st.diagnostics.filter((d) => d.severity === 'error');
if (errorsWanted) { assert.equal(errs.length, Number(errorsWanted), errs.map((d) => `L${d.line} ${d.id}`).join(', ')); return; }
assert.ok(st.ok, errs.map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | '));
const tr = runPseudo(st, { input, maxSteps: 200000 });
if (stops) { assert.equal(tr.error?.kind, stops, tr.error?.message || 'no error'); return; }
assert.equal(tr.error, null, tr.error?.message);
assert.deepEqual(tr.output.filter((l) => !/^\w+ returned /.test(l)), expect);
});
}
test('the corpus covers every statement kind and every built-in group', () => {
const all = files.map((f) => fs.readFileSync(path.join(dir, f), 'utf8')).join('\n');
for (const kw of ['DECLARE', 'CONSTANT', 'TYPE', 'DEFINE', 'IF', 'ELSEIF', 'ELSE', 'CASE', 'OTHERWISE', 'FOR', 'STEP', 'FOR EACH', 'WHILE', 'REPEAT', 'UNTIL', 'BREAK', 'CONTINUE', 'FUNCTION', 'PROCEDURE', 'BYREF', 'CALL', 'RETURN', 'INPUT', 'OUTPUT', 'REQUIRE', 'INVARIANT', 'DECREASES', 'FORALL', 'EXISTS', 'SUM', 'DIV', 'MOD', 'IN', 'ARRAY', 'LIST', 'HASHMAP', 'SET', 'PARALLEL FOR']) {
assert.ok(new RegExp(`\\b${kw}\\b`).test(all), `corpus never uses ${kw}`);
}
for (const b of ['LENGTH', 'ROWS', 'INT', 'ROUND', 'ABS', 'MIN', 'MAX', 'SLICE', 'UPPER', 'LOWER', 'ORD', 'CHR', 'TO_STRING', 'TO_INTEGER', 'KEYS', 'SORTED', 'APPEND', 'REMOVE', 'ADD', 'SORT', 'RANDINT', 'SPLIT', 'JOIN', 'INDEX_OF', 'REPLACE', 'FLOOR', 'CEIL', 'LOG10', 'SIN', 'COS', 'ATAN2', 'PI', 'CHOOSE', 'SUM_OF', 'MEAN_OF', 'VARIANCE_OF', 'STDEV_OF', 'MEDIAN_OF', 'DOT', 'NORM_OF', 'FILL']) assert.ok(all.includes(b + '('), `corpus never calls ${b}`);
});