54 lines
3.3 KiB
JavaScript
54 lines
3.3 KiB
JavaScript
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// The words a step carries ("IF X > 0 (X is 3) is TRUE", "ASSERT on line 4 is FALSE: ..., where the left side is 9")
|
|
// are made by looking at the condition a second time. Looking must not be running: a routine called in a condition
|
|
// runs once, a random number is drawn once, and nothing is shown twice. The statement fuzzer found the first case
|
|
// (work/release/progress.md): IF LENGTH(F(1)) > 0 ran F twice.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
|
|
|
|
const run = (src, opts = {}) => {
|
|
const st = studyOf(src);
|
|
assert.ok(st.ok, st.diagnostics.filter((d) => d.severity === 'error').map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | '));
|
|
return runPseudo(st, opts);
|
|
};
|
|
const F = `FUNCTION F(N : INTEGER) RETURNS LIST OF INTEGER
|
|
OUTPUT "called ", N
|
|
RETURN [N, N]
|
|
ENDFUNCTION
|
|
`;
|
|
const texts = (r) => r.steps.map((s) => s.text).join('\n');
|
|
|
|
test('a routine called in an IF, WHILE or UNTIL condition runs once per test', () => {
|
|
assert.deepEqual(run(`${F}IF LENGTH(F(1)) > 0 THEN\n OUTPUT "yes"\nENDIF`).output, ['called 1', 'yes']);
|
|
assert.deepEqual(run(`${F}DECLARE I : INTEGER\nI <- 0\nWHILE F(I)[1] < 2\n I <- I + 1\nENDWHILE`).output, ['called 0', 'called 1', 'called 2']);
|
|
assert.deepEqual(run(`${F}DECLARE I : INTEGER\nI <- 2\nREPEAT\n I <- I - 1\nUNTIL F(I)[0] <= 0`).output, ['called 1', 'called 0']);
|
|
});
|
|
|
|
test('the parts of a condition that call nothing are still described with their values', () => {
|
|
const r = run(`${F}DECLARE XS : LIST OF INTEGER\nXS <- [5, 6]\nIF LENGTH(F(1)) > 0 AND LENGTH(XS) == 2 AND XS[1] == 6 THEN\n OUTPUT "yes"\nENDIF`);
|
|
assert.deepEqual(r.output, ['called 1', 'yes']);
|
|
assert.match(texts(r), /LENGTH\(XS\) is 2/);
|
|
assert.match(texts(r), /XS\[1\] is 6/);
|
|
assert.doesNotMatch(texts(r), /LENGTH\(F\(1\)\) is/);
|
|
});
|
|
|
|
test('a random number in a condition is drawn once, so the run is the same with and without the description', () => {
|
|
const src = `DECLARE XS : LIST OF INTEGER\nXS <- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\nDECLARE N : INTEGER\nN <- 0\nWHILE XS[RANDINT(0, 10)] < 8\n N <- N + 1\nENDWHILE\nOUTPUT N, " ", RANDINT(0, 1000000)`;
|
|
const plain = `DECLARE XS : LIST OF INTEGER\nXS <- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\nDECLARE N : INTEGER\nN <- 0\nDECLARE T : INTEGER\nT <- XS[RANDINT(0, 10)]\nWHILE T < 8\n N <- N + 1\n T <- XS[RANDINT(0, 10)]\nENDWHILE\nOUTPUT N, " ", RANDINT(0, 1000000)`;
|
|
for (const seed of [1, 2, 3, 4, 5]) assert.deepEqual(run(src, { seed }).output, run(plain, { seed }).output, `seed ${seed}`);
|
|
});
|
|
|
|
test('explaining a failed ASSERT shows what a routine printed once, and gives the values that failed', () => {
|
|
const r = run(`${F}ASSERT F(9)[1] == 8`);
|
|
assert.deepEqual(r.output, ['called 9']);
|
|
assert.match(texts(r), /ASSERT on line 5 is FALSE: F\(9\)\[1\] == 8, where the left side is 9 and the right side is 8/);
|
|
});
|
|
|
|
test('a failed ASSERT on a random number is explained without values, since a second draw would give other ones', () => {
|
|
const r = run('ASSERT RANDINT(0, 1000000) == -1');
|
|
assert.match(texts(r), /ASSERT on line 1 is FALSE: RANDINT\(0, 1000000\) == -1\./);
|
|
assert.doesNotMatch(texts(r), /where the left side/);
|
|
});
|