Files
pplus-core/tests/mojo-robustness.test.mjs
T
alexm 401084bab3
Test / gate (push) Failing after 19m0s
Initial commit
2026-11-12 22:23:54 -05:00

183 lines
12 KiB
JavaScript

// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// Mojo answers any text with diagnostics and any run with an output, a stop or a refusal: never with an exception of
// the engine underneath, a run without end, or an allocation the size of the program's mistake. Each test is a shape
// that once threw, hung or ran out of memory (a program can arrive in a share link), proven just past its limit with
// inputs small enough to keep the test quick (docs/limits.md lists every limit).
import test from 'node:test';
import assert from 'node:assert/strict';
import { parseModule, MAX_NESTING, MAX_TREE } from '../dist/mojo/parse.js';
import { checkMojo } from '../dist/mojo/check.js';
import { runMojo, callMojo, RUN_LIMITS } from '../dist/mojo/interp.js';
import { practiceDiagnostics } from '../dist/mojo/practices.js';
import { mojoProperties } from '../dist/mojo/properties.js';
import { mojoFunctions, mojoRoutine } from '../dist/mojo/flowir.js';
import { tokenizeMojo } from '../dist/mojo/tokenize.js';
import { lifetimes } from '../dist/mojo/lifetimes.js';
const mainOf = (body) => `def main():\n${body.split('\n').map((l) => ' ' + l).join('\n')}\n`;
const fnOf = (body) => `def f(x: Int) -> Int:\n${body.split('\n').map((l) => ' ' + l).join('\n')}\n\n` + mainOf('print(f(1))');
const notes = (src) => checkMojo(src).diagnostics.filter((d) => d.id === 'unread');
const errors = (src) => checkMojo(src).diagnostics.filter((d) => d.severity === 'error');
const run = (src, opts = {}) => runMojo(parseModule(src), src, { record: false, ...opts });
/** Every stage a share link reaches, on one text: none may throw. */
function everyStage(src) {
tokenizeMojo(src);
const { module } = checkMojo(src);
practiceDiagnostics(src, module);
for (const n of mojoFunctions(module).slice(0, 3)) mojoRoutine(module, n);
runMojo(module, src, {});
mojoProperties(module, src, 5);
return module;
}
test('nesting past MAX_NESTING is one unread note where the limit is passed, never an error: the compiler accepts 5,000 levels', () => {
assert.equal(MAX_NESTING, 200);
const deep = [
mainOf(`print(${'('.repeat(30000)}1${')'.repeat(30000)})`),
mainOf(`print(${'('.repeat(30000)}1`),
mainOf(`var a = ${'['.repeat(5000)}1${']'.repeat(5000)}`),
mainOf(`print(${'abs('.repeat(5000)}1${')'.repeat(5000)})`),
mainOf(`print(${'-'.repeat(30000)}1)`),
mainOf(`print(${'not '.repeat(30000)}True)`),
mainOf(`print(1${' ** 1'.repeat(30000)})`),
mainOf(`print(${'1 if True else '.repeat(30000)}0)`),
mainOf(`var f = ${'lambda: '.repeat(30000)}1`),
mainOf(`var x = ${'[y for y in '.repeat(5000)}[1]${']'.repeat(5000)}`),
mainOf(`var x: ${'ref[a] '.repeat(30000)}Int = 1`),
`def main():\n${Array.from({ length: 2000 }, (_, k) => ' '.repeat(k + 1) + 'if True:').join('\n')}\n${' '.repeat(2001)}print(1)\n`,
];
for (const src of deep) {
const n = notes(src);
assert.equal(n.length, 1, src.slice(0, 60));
assert.match(n[0].message, /^nested more than 200 levels deep; Programming\+ cannot read this yet/);
assert.equal(n[0].severity, 'note');
assert.deepEqual(errors(src), [], 'never an error: the compiler accepts what is merely deep');
const m = everyStage(src);
assert.ok(m.unread > 0);
assert.match(runMojo(m, src, {}).refused, /cannot read yet/);
}
// just under it, the same shapes read, check and run
const ok = mainOf(`print(${'('.repeat(150)}1${')'.repeat(150)})`);
assert.deepEqual(notes(ok), []);
assert.equal(run(ok).stdout, '1\n');
});
test('a tree taller than MAX_TREE (a long chain, a long elif) is one unread note; the compiler accepts a 10,000-term sum and 5,000 elif arms', () => {
assert.equal(MAX_TREE, 1000);
for (const src of [
fnOf(`return x${' + 1'.repeat(5000)}`),
mainOf(`print(True${' and True'.repeat(5000)})`),
mainOf(`var s = String("a")\nprint(s${'.copy()'.repeat(5000)})`),
mainOf(`var x = [0]\nprint(x${'[0]'.repeat(5000)})`),
fnOf(`if x == 0:\n return 0\n${Array.from({ length: 5000 }, (_, k) => `elif x == ${k + 1}:\n return ${k}`).join('\n')}\nelse:\n return -1`),
]) {
const n = notes(src);
assert.equal(n.length, 1);
assert.match(n[0].message, /^more than 1000 levels deep once read/);
assert.deepEqual(errors(src), []);
everyStage(src);
}
// under it, every stage walks the tree and the run answers
assert.equal(run(fnOf(`return x${' + 1'.repeat(900)}`)).stdout, '901\n');
const elifs = fnOf(`if x == 0:\n return 0\n${Array.from({ length: 900 }, (_, k) => `elif x == ${k + 1}:\n return ${k}`).join('\n')}\nelse:\n return -1`);
assert.deepEqual(notes(elifs), []);
everyStage(elifs);
assert.equal(run(elifs).stdout, '0\n');
});
test('a long flat program reads, checks, gets its advice and runs without a stack or quadratic time', { timeout: 20000 }, () => {
everyStage(mainOf(Array.from({ length: 10000 }, (_, k) => `var x${k} = ${k}`).join('\n') + '\nprint(x0)'));
everyStage(`@fieldwise_init\nstruct S:\n${Array.from({ length: 10000 }, (_, k) => ` var f${k}: Int`).join('\n')}\n\n` + mainOf('print(1)'));
assert.equal(run(mainOf(Array.from({ length: 10000 }, () => 'print(1)').join('\n'))).stdout.length, 20000);
// highlighting a 30,000-operator line is linear (it looked each token's neighbor up by searching the line)
tokenizeMojo(mainOf(`print(${'1 if True else '.repeat(30000)}0)`));
});
test('loops nested deep: the liveness and moved-value analyses give up past their budget instead of taking 2^depth', { timeout: 20000 }, () => {
const nested = (n) => `def main():\n var x = 1\n${Array.from({ length: n }, (_, k) => ' '.repeat(k + 4) + 'while x < 0:').join('\n')}\n${' '.repeat(n + 4)}x = x + 1\n print(x)\n`;
const src = nested(60);
assert.deepEqual(errors(src), []);
const life = lifetimes(parseModule(src).body[0]);
assert.ok(life.unmodeled.has('x'), 'given up: every name is unmodeled, so nothing is destroyed on a guess');
assert.equal(run(src).stdout, '1\n');
// a shallow one is still analyzed
assert.ok(!lifetimes(parseModule(nested(3)).body[0]).unmodeled.has('x'));
});
test('a function of thousands of parameters, or a program of thousands of functions, gets no generated inputs past the first, not a stack overflow or a hang', () => {
const src = `def f(${Array.from({ length: 3000 }, (_, k) => `p${k}: Int`).join(', ')}) -> Int:\n return p0\n\n` + mainOf('print(1)');
const [r] = mojoProperties(parseModule(src), src, 5);
assert.match(r.problem, /more than 64 parameters/);
const many = Array.from({ length: 101 }, (_, k) => `def f${k}(x: Int) -> Int:\n return x + ${k}\n`).join('\n') + mainOf('print(1)');
const reports = mojoProperties(parseModule(many), many, 2);
assert.equal(reports.length, 101);
assert.equal(reports[99].problem, undefined);
assert.match(reports[100].problem, /first 100 functions/);
});
test('a value that grows without bound stops the run as its step limit does (limited), before memory runs away', () => {
const limited = (body, opts, re) => {
const r = run(mainOf(body), opts);
assert.equal(r.limited, true, body);
assert.equal(r.exitCode, -1);
assert.match(r.trace.error.message, re);
return r;
};
// the real limits, where reaching them costs little
assert.equal(RUN_LIMITS.text, 10_000_000);
limited('var t = String("ab")\nfor i in range(40):\n t = t + t\nprint(len(t))', {}, /String of 16,777,216 characters, more than Programming\+ holds \(10,000,000\)/);
limited('var t = String("ab")\nfor i in range(40):\n t += t', {}, /String of 16,777,216/);
limited('var s = String("ab")\nprint(len(s * 1000000000))', {}, /String of 2,000,000,000 characters/);
limited('var xs = [1, 2]\nfor i in range(40):\n xs = xs + xs', {}, /List of 2,097,152 values, more than Programming\+ holds \(2,000,000\)/);
limited('var xs: List[Int] = [1, 2]\nfor i in range(40):\n xs.extend(xs)', {}, /List of 2,097,152 values/);
limited('var s = String("x") * 1000000\nwhile True:\n print(s)', {}, /Stopped after printing 10,000,000 characters/);
limited('var s = String("x") * 10000000\nvar n = 0\nwhile True:\n n += s.find("z")', {}, /searching through 100,000,000 characters and values/);
// a range is never listed past the step limit
limited('var t = 0\nfor i in range(1000000000):\n t += i', {}, /Stopped after 20,000 steps/);
// each limit, made small for the test, is checked where values grow
limited('var d = Dict[Int, Int]()\nfor i in range(100):\n d[i] = i', { limits: { values: 50 } }, /Dict of 51 values, more than Programming\+ holds \(50\)/);
limited('var d = Set[Int]()\nfor i in range(100):\n d.add(i)', { limits: { values: 50 } }, /Set of 51 values/);
limited('var xs = List[Int]()\nwhile True:\n xs.append(1)', { limits: { values: 50 } }, /List of 51 values/);
limited('var xs = List[Int]()\nfor i in range(1000):\n xs.append(i)\nfor k in range(100):\n var ys = xs.copy()\n print(len(ys))', { limits: { made: 5000 } }, /making or copying 5,000 values/);
limited('var xs = List[Int]()\nfor i in range(1000):\n xs.append(i)\nfor k in range(100):\n var ys = List[Int](xs[0:1000])\n print(len(ys))', { limits: { made: 5000 } }, /making or copying 5,000 values/);
limited('var t = String("")\nfor i in range(100):\n t = t + "abcdefghij"', { limits: { work: 10000 } }, /searching through 10,000 characters/);
// std.math's gcd of an even number and a much larger odd one subtracts about 10^15 times in Mojo 1.2.0, and comb loops k times
const math = (body) => run(`from std.math import gcd, comb\n\n\ndef main():\n ${body}\n`);
for (const body of ['print(gcd(6, 4503599627370597))', 'print(comb(100000000, 50000000))']) {
const r = math(body);
assert.equal(r.limited, true, body);
assert.match(r.trace.error.message, /more than Programming\+ follows in one call/);
}
assert.equal(math('print(gcd(6, 4503599))').stdout, '1\n');
limited('var a = 1\nvar b = 2\nwhile True:\n a += 1', { record: true, limits: { recorded: 100 } }, /recording 100 values of variables/);
// a lower limit can be asked for, not a higher one
assert.equal(run(mainOf('print((String("ab") * 3).byte_length())'), { limits: { text: 1e12 } }).stdout, '6\n');
// the same program, small, prints what it always printed
assert.equal(run(mainOf('var t = String("ab")\nfor i in range(3):\n t = t + t\nprint(t.byte_length())')).stdout, '16\n');
assert.equal(run(mainOf('var xs: List[Int] = [1, 2]\nfor i in range(3):\n xs.extend(xs)\nprint(len(xs))')).stdout, '16\n');
});
test('what the engine underneath would refuse is declined, not thrown', () => {
const refused = (body, re) => { const r = run(mainOf(body)); assert.match(r.refused ?? '', re, body); };
refused('print(1 << 100)', /a shift by more than 64 bits/);
refused('var f: Float64 = 1.0\nprint(Int(f / 0.0))', /converting inf to Int/);
refused('print(1.0e999999 * 2.0)', /exponent past 10,000/);
// [[[...]]] evaluated its first item twice per level: 2^40 evaluations
assert.equal(run(mainOf(`var a = ${'['.repeat(40)}1${']'.repeat(40)}\nprint(len(a))`)).stdout, '1\n');
// and a call that cannot finish says so
const src = 'def f(n: Int) -> Int:\n return f(n + 1)\n\n' + mainOf('print(f(1))');
assert.equal(callMojo(parseModule(src), src, 'f', [{ k: 'int', v: 1n, width: 64, signed: true, type: 'Int' }]).kind, 'limit');
});
test('the trace shows a long value cut short, not copied whole into every step', () => {
const src = mainOf('var s = String("x") * 2000\nvar xs = List[Int]()\nfor i in range(30):\n xs.append(i)\nvar d = Dict[Int, Int]()\nfor i in range(30):\n d[i] = i\nprint(s.byte_length())');
const r = runMojo(parseModule(src), src, {});
const vars = r.trace.states.at(-1).frames[0].vars;
assert.match(vars.s.text, /^"x{1000}…" \(2,000 characters\)$/);
assert.match(vars.xs.text, /, … 10 more\]$/);
assert.match(vars.d.text, /^\{0: 0, 1: 1, .*, … 10 more\}$/);
assert.equal(r.stdout, '2000\n', 'print still writes the whole value');
});