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

383 lines
29 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// Pseudo for numerical work: sized types, FLOAT32, conversions, the numerical built-ins, USE IEEE, and what the
// prover may and may not say about them. The corpus programs 49 to 52 show the same features in use; this file
// pins each rule on its own.
import test from 'node:test';
import assert from 'node:assert/strict';
import { studyOf, runPseudo, provePseudo } from '../dist/services/pseudo.js';
import { fma, exactDecimal, nextAfter, ulp, roundDyadic, wrap } from '../dist/engines/pseudo/numeric.js';
import { intRange, SIZED, formatFloat32 } from '../dist/engines/pseudo/lang.js';
const run = (src) => {
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, {});
};
const out = (src) => { const r = run(src); assert.equal(r.error, null, r.error?.message); return r.output; };
const errors = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => d.id);
test('the sized types have the ranges of their names (64-bit types cut at what a run holds exactly)', () => {
assert.deepEqual(intRange(SIZED.INT8), [-128, 127]);
assert.deepEqual(intRange(SIZED.UINT16), [0, 65535]);
assert.deepEqual(intRange(SIZED.INT32), [-2147483648, 2147483647]);
assert.deepEqual(intRange(SIZED.UINT32), [0, 4294967295]);
assert.deepEqual(intRange(SIZED.INT64), [-Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]);
});
test('a FLOAT made into a whole number is one the lab holds exactly, and ROUND takes halves away from zero without rounding twice', () => {
// INT, ROUND, FLOOR and CEIL give an INTEGER, so a result past 2^53 - 1 stops the run as any other does (mojo-fuzz found
// INT(6.02e23) going on as 6.02e23, and the Mojo made from it printing something else)
for (const f of ['INT', 'ROUND', 'FLOOR', 'CEIL']) {
const r = run(`DECLARE X : FLOAT <- 6.02e23\nOUTPUT ${f}(X)`);
assert.equal(r.error?.kind, 'overflow', f);
assert.deepEqual(out(`DECLARE X : FLOAT <- 9007199254740991.0\nOUTPUT ${f}(X)`), ['9007199254740991'], f);
}
assert.equal(run('USE IEEE\nOUTPUT INT(INF())').error?.kind, 'overflow', 'infinity is no whole number, with USE IEEE or without');
assert.deepEqual(out('OUTPUT ROUND(2.5), " ", ROUND(-2.5), " ", ROUND(0.5), " ", ROUND(1.5), " ", ROUND(-0.5), " ", ROUND(2.4999)'), ['3 -3 1 2 -1 2']);
// 0.49999999999999994 + 0.5 rounds to 1.0, and 4503599627370497.0 + 0.5 to the even neighbor: neither is the nearest whole number
assert.deepEqual(out('OUTPUT ROUND(0.49999999999999994), " ", ROUND(-0.49999999999999994), " ", ROUND(4503599627370497.0), " ", ROUND(-4503599627370497.0)'), ['0 0 4503599627370497 -4503599627370497']);
// never minus zero: an INTEGER has one zero
assert.deepEqual(out('OUTPUT INT(-0.5), " ", CEIL(-0.5), " ", ROUND(-0.2)'), ['0 0 0']);
assert.deepEqual(out('OUTPUT ROUND(7), " ", FLOOR(-2.3), " ", CEIL(2.1), " ", INT(-2.7)'), ['7 -3 3 -2']);
});
test('literals: underscores, hexadecimal and binary', () => {
assert.deepEqual(out('OUTPUT 1_000_000, " ", 0xFF, " ", 0b1010, " ", 1_000.5'), ['1000000 255 10 1000.5']);
assert.ok(errors('OUTPUT 1__0').includes('bad-number'));
assert.ok(errors('OUTPUT 0xFG').includes('bad-number'));
});
test('a sized result that leaves its range stops the run; WRAP wraps on purpose', () => {
const r = run('DECLARE X : INT8 <- 127\nX <- X + 1\n');
assert.equal(r.error?.kind, 'overflow');
assert.equal(r.error.line, 2);
assert.match(r.error.message, /INT8 holds -128 to 127/);
assert.deepEqual(out('DECLARE X : INT8 <- 127\nOUTPUT WRAP(X + 1, INT8), " ", WRAP(-1, UINT8), " ", WRAP(70000, UINT16)'), ['-128 255 4464']);
assert.equal(run('DECLARE U : UINT8 <- 0\nU <- U - 1\n').error?.kind, 'overflow');
assert.equal(run('DECLARE X : INT8\nX <- -128\nX <- -X\n').error?.kind, 'overflow');
});
test('storing, passing and returning check the range of the receiving type', () => {
assert.equal(run('DECLARE N : INTEGER <- 300\nDECLARE B : UINT8\nB <- N\n').error?.kind, 'overflow');
const src = 'FUNCTION Id(X : UINT8) RETURNS UINT8\n RETURN X\nENDFUNCTION\nDECLARE N : INTEGER <- 256\nOUTPUT Id(N)\n';
const r = run(src);
assert.equal(r.error?.kind, 'overflow');
assert.match(r.error.message, /Parameter X of Id/);
assert.equal(run('FUNCTION Big(N : INTEGER) RETURNS INT16\n RETURN N * 1000\nENDFUNCTION\nOUTPUT Big(40)\n').error?.kind, 'overflow');
});
test('conversions: a FLOAT loses its fraction toward zero, and a value that does not fit stops the run', () => {
assert.deepEqual(out('OUTPUT INT32(-2.9), " ", UINT8(200), " ", INT64(2.5e9), " ", FLOAT32(0.1) == FLOAT32(0.1)'), ['-2 200 2500000000 TRUE']);
assert.deepEqual(errors('OUTPUT INT8(200)'), ['literal-range']); // a literal is checked before any run
assert.equal(run('DECLARE N : INTEGER <- 200\nOUTPUT INT8(N)').error?.kind, 'overflow');
assert.equal(run('DECLARE F : FLOAT <- 3.0e9\nOUTPUT INT32(F)').error?.kind, 'overflow');
});
test('the checker: widths and precisions do not mix silently, and literals take the type beside them', () => {
assert.deepEqual(errors('DECLARE A : INT8 <- 1\nDECLARE B : INT32 <- 2\nOUTPUT A + B'), ['mixed-width']);
assert.deepEqual(errors('DECLARE A : FLOAT32 <- 1.0\nDECLARE B : FLOAT <- 2.0\nOUTPUT A * B'), ['mixed-precision']);
assert.deepEqual(errors('DECLARE A : INT8\nA <- 300'), ['literal-range']);
assert.deepEqual(errors('DECLARE A : INT8 <- 1\nDECLARE B : INT32 <- 2\nB <- A'), ['type']);
assert.deepEqual(errors('DECLARE A : FLOAT32\nDECLARE D : FLOAT <- 0.5\nA <- D'), ['type']);
assert.deepEqual(errors('DECLARE A : FLOAT32\nDECLARE D : FLOAT <- 0.5\nA <- FLOAT32(D)'), []);
assert.deepEqual(errors('DECLARE A : FLOAT32\nA <- 0.1'), []);
assert.deepEqual(errors('OUTPUT INT8'), ['type-as-value']);
assert.deepEqual(errors('OUTPUT MAX_VALUE(INT64)'), ['exact-limit']);
assert.deepEqual(errors('OUTPUT WRAP(5, UINT64)'), ['wrap-64']);
assert.deepEqual(errors('DECLARE X : INTEGER <- 1\nOUTPUT EPSILON(INT32)'), ['type']);
});
test('FLOAT32: results round to single precision, a literal next to one is a single, and it prints as the shortest decimal', () => {
assert.deepEqual(out('DECLARE X : FLOAT32 <- 0.1\nOUTPUT X, " ", X == 0.1, " ", FLOAT64(X) == 0.1, " ", X + 0.2'), ['0.1 TRUE FALSE 0.3']);
assert.deepEqual(out('DECLARE X : FLOAT32 <- 16777216.0\nOUTPUT X + 1.0, " ", FLOAT64(X) + 1.0'), ['16777216.0 16777217.0']);
assert.equal(formatFloat32(Math.fround(0.1)), '0.1');
assert.equal(formatFloat32(Math.fround(1 / 3)), '0.33333334');
});
test('exact helpers: FMA rounds once, EXACT shows every digit, ULP and NEXT_AFTER step by one value', () => {
assert.equal(fma(0.1, 10, -1), 5.551115123125783e-17);
assert.equal(0.1 * 10 - 1, 0);
assert.equal(fma(2, 3, 4), 10);
assert.equal(fma(Math.fround(0.1), 10, -1, true), Math.fround(1.4901161193847656e-8));
assert.equal(exactDecimal(0.1), '0.1000000000000000055511151231257827021181583404541015625');
assert.equal(exactDecimal(-2.5), '-2.5');
assert.equal(exactDecimal(1e21), '1000000000000000000000.0');
assert.equal(exactDecimal(Number.MIN_VALUE).length, 1076); // 4.94...e-324 has 1074 digits after the point
assert.equal(nextAfter(1, 2), 1 + Number.EPSILON);
assert.equal(nextAfter(0, -1), -Number.MIN_VALUE);
assert.equal(nextAfter(1, 2, true), 1 + 2 ** -23);
assert.equal(ulp(1), Number.EPSILON);
assert.equal(ulp(Number.MAX_VALUE), 2 ** 971);
assert.equal(ulp(1, true), 2 ** -23);
assert.equal(roundDyadic(3n, -1), 1.5);
assert.equal(roundDyadic((1n << 54n) + 1n, 0), 2 ** 54); // ties and below round to even
assert.equal(wrap(-129, 8, true), 127);
assert.deepEqual(out('OUTPUT FMA(0.1, 10.0, -1.0), " ", ULP(1.0) == EPSILON(FLOAT64), " ", EXACT(0.5)'), ['5.551115123125783e-17 TRUE 0.5']);
});
test('whole numbers bit by bit, two\'s complement for negative values', () => {
assert.deepEqual(out('OUTPUT BIT_AND(-1, 255), " ", BIT_OR(-8, 1), " ", BIT_XOR(5, 3), " ", BIT_NOT(5), " ", BIT_NOT(UINT8(5))'), ['255 -7 6 -6 250']);
assert.deepEqual(out('OUTPUT SHIFT_LEFT(3, 4), " ", SHIFT_RIGHT(-9, 1), " ", SHIFT_RIGHT(UINT8(255), 4), " ", GCD(-12, 18), " ", LCM(6, 8)'), ['48 -5 15 6 24']);
assert.equal(run('DECLARE B : UINT8 <- 200\nOUTPUT SHIFT_LEFT(B, 1)').error?.kind, 'overflow');
});
test('the limits of each type', () => {
assert.deepEqual(out('OUTPUT MAX_VALUE(INT8), " ", MIN_VALUE(INT16), " ", MAX_VALUE(UINT32), " ", MIN_VALUE(UINT64)'), ['127 -32768 4294967295 0']);
assert.deepEqual(out('OUTPUT MAX_VALUE(FLOAT64) > 1e308, " ", EPSILON(FLOAT64), " ", MAX_VALUE(FLOAT32)'), ['TRUE 2.220446049250313e-16 3.4028235e38']);
});
test('domains: without USE IEEE the run stops where IEEE 754 would give NaN or an infinity; with it, the values flow', () => {
for (const [e, kind] of [['SQRT(-1.0)', 'value'], ['LOG(0.0)', 'value'], ['ASIN(2.0)', 'value'], ['1.0 / 0.0', 'divide'], ['MAX_VALUE(FLOAT64) * 2.0', 'overflow'], ['INF()', 'overflow'], ['LOG1P(-1.0)', 'value']]) {
assert.equal(run(`OUTPUT ${e}`).error?.kind, kind, e);
}
assert.deepEqual(out('USE IEEE\nOUTPUT SQRT(-1.0), " ", 1.0 / 0.0, " ", -1.0 / 0.0, " ", IS_NAN(NAN()), " ", NAN() == NAN()'), ['not a number infinity -infinity TRUE FALSE']);
// whole-number division by zero still stops: IEEE 754 is about FLOAT values
assert.equal(run('USE IEEE\nOUTPUT 1 DIV 0').error?.kind, 'divide');
});
test('names: a program may still call its variable E or its function GCD, and its own definition wins', () => {
assert.deepEqual(out('DECLARE E : FLOAT <- 2.0\nOUTPUT E * 2.0, " ", ROUND(E() * 100)'), ['4.0 272']);
assert.deepEqual(out('FUNCTION GCD(A : INTEGER, B : INTEGER) RETURNS INTEGER\n RETURN 7\nENDFUNCTION\nOUTPUT GCD(12, 18)'), ['7']);
assert.ok(errors('DECLARE LENGTH : INTEGER').includes('builtin-name'));
assert.ok(errors('DECLARE INT8 : INTEGER').length > 0);
});
test('the prover: sized arithmetic carries an overflow obligation; FLOAT32 is a function of its own; USE IEEE proves nothing', () => {
const report = (src) => provePseudo(studyOf(src)).units.flatMap((u) => u.obligations.map((o) => ({ ...o, unit: u.name, notes: u.notes })));
const guarded = report('FUNCTION Inc(X : INT8) RETURNS INT8\n REQUIRE X < 127\n RETURN X + 1\nENDFUNCTION\n');
assert.deepEqual(guarded.filter((o) => o.kind === 'overflow').map((o) => o.status), ['proved']);
const open = report('FUNCTION Inc(X : INT8) RETURNS INT8\n RETURN X + 1\nENDFUNCTION\n');
assert.deepEqual(open.filter((o) => o.kind === 'overflow').map((o) => o.status), ['broken']); // the fuzzer tries 127
const narrow = report('FUNCTION N(K : INTEGER) RETURNS UINT8\n REQUIRE 0 <= K AND K < 256\n RETURN K\nENDFUNCTION\n');
assert.deepEqual(narrow.filter((o) => o.kind === 'overflow').map((o) => o.status), ['proved']);
const loop = report('FUNCTION Count(N : UINT8) RETURNS UINT8\n DECLARE C : UINT8 <- 0\n FOR I <- 0 TO N\n INVARIANT C == I\n C <- C + 1\n NEXT I\n RETURN C\nENDFUNCTION\n');
assert.ok(loop.filter((o) => o.kind === 'overflow').every((o) => o.status === 'proved'), JSON.stringify(loop.map((o) => [o.kind, o.status])));
const half = report('FUNCTION Half(X : FLOAT32) RETURNS FLOAT32\n ENSURE RESULT == X / 2.0\n RETURN X / 2.0\nENDFUNCTION\n');
assert.ok(half.find((o) => o.kind === 'ensure').status === 'proved');
const roots = report('FUNCTION R(X : FLOAT) RETURNS FLOAT\n RETURN SQRT(X)\nENDFUNCTION\n');
assert.deepEqual(roots.filter((o) => o.kind === 'argument').map((o) => o.status), ['broken']);
const ieee = report('USE IEEE\nFUNCTION F(X : INTEGER) RETURNS INTEGER\n REQUIRE X > 0\n RETURN 10 DIV X\nENDFUNCTION\n');
assert.ok(ieee.length && ieee.every((o) => o.status !== 'proved'));
assert.ok(ieee[0].notes.some((n) => /USE IEEE/.test(n)));
});
test('the fuzzer reaches the edges of a sized type and generates FLOAT32 values', async () => {
const { checkProperties } = await import('../dist/engines/pseudo/testing.js');
const st = studyOf('FUNCTION F(X : UINT8, Y : FLOAT32) RETURNS BOOL\n ASSERT X != 255\n ASSERT FLOAT32(Y) == Y\n RETURN TRUE\nENDFUNCTION\n');
const fails = checkProperties(st.program.program, 'F', { count: 300 }).failures;
assert.ok(fails.some((f) => f.kind === 'assert' && f.line === 2), 'X = 255 is never tried');
assert.ok(!fails.some((f) => f.line === 3), 'a generated FLOAT32 is not a single');
});
test('a whole number past 2^53 - 1 does not enter a run: INPUT, TO_INTEGER, a FOR counter and SUM stop as + does', () => {
// DIV and MOD on such a number are computed in doubles and break what the prover knows about them
// (library/pseudo/prover/input-past-exact.pseudo: N MOD 11 came to 64).
const withInput = (src, input) => runPseudo(studyOf(src), { input });
assert.equal(withInput('DECLARE N : INTEGER\nINPUT N\nOUTPUT N MOD 11', ['-516719406483505152']).error?.kind, 'overflow');
assert.deepEqual(withInput('DECLARE N : INTEGER\nINPUT N\nOUTPUT N', ['9007199254740991']).output, ['9007199254740991']);
assert.equal(run('OUTPUT TO_INTEGER("-516719406483505152") MOD 11').error?.kind, 'overflow');
assert.equal(run('FOR I <- 9007199254740990 TO 9007199254740991 STEP 2\n OUTPUT I\nNEXT I').error?.kind, 'overflow');
assert.equal(run('DECLARE L : LIST OF INTEGER\nL <- [9007199254740991, 1, 1]\nOUTPUT SUM K IN [0, 3) : L[K]').error?.kind, 'overflow');
assert.equal(run('DECLARE L : LIST OF FLOAT\nL <- [1E308, 1E308]\nOUTPUT SUM K IN [0, 2) : L[K]').error?.kind, 'overflow');
// a FLOAT read by INPUT is a finite number, unless the program says USE IEEE
assert.equal(withInput('DECLARE F : FLOAT\nINPUT F\nOUTPUT F', ['Infinity']).error?.kind, 'input');
assert.equal(withInput('USE IEEE\nDECLARE F : FLOAT\nINPUT F\nOUTPUT IS_INF(F)', ['Infinity']).error, null);
});
test('text stays what its type says: a CHAR is one character, and CHR gives no half of a surrogate pair', () => {
// the prover counts every CHAR as one character (library/pseudo/prover/chr-surrogate.pseudo)
assert.deepEqual(out("OUTPUT UPPER('ß'), \" \", LENGTH(UPPER('ß') & \"\"), \" \", UPPER('a'), \" \", UPPER(\"straße\")"), ['ß 1 A STRASSE']);
assert.equal(run('OUTPUT LENGTH(CHR(55357) & CHR(56832))').error?.kind, 'value');
assert.deepEqual(out('OUTPUT LENGTH(CHR(128512) & "")'), ['1']);
});
test('RAND(X) is below X even for the smallest FLOAT, where the product would round up to X', () => {
assert.equal(run('DECLARE R : FLOAT\nFOR I <- 0 TO 20\n R <- RAND(5E-324)\n ASSERT R < 5E-324\nNEXT I').error, null);
assert.equal(run('DECLARE R : FLOAT\nFOR I <- 0 TO 20\n R <- RAND(-5E-324)\n ASSERT R > -5E-324\nNEXT I').error, null);
});
test('a type says what a value is: CLAMP and a set literal widen, a FLOAT32 answer is rounded, a sized list checks what it receives', () => {
// CLAMP(0, 0.5, 2.5) is 0.5: typed INTEGER (its first value's type) it reached an INTEGER parameter as 0.5
assert.ok(errors('DECLARE N : INTEGER\nN <- CLAMP(0, 0.5, 2.5)').includes('type'));
assert.deepEqual(out('OUTPUT CLAMP(0, 0.5, 2.5), " ", CLAMP(7, 0, 5)'), ['0.5 5']);
// {1, 2.5} is a SET OF FLOAT, as [1, 2.5] is a LIST OF FLOAT
assert.ok(errors('DECLARE N : INTEGER\nFOR EACH X IN {1, 2.5}\n N <- X\nNEXT X').includes('type'));
// MAX of a FLOAT32 and a whole number is a FLOAT32, so the whole number is rounded to one
assert.deepEqual(out('DECLARE X : FLOAT32 <- 1.0\nDECLARE I : INTEGER <- 16777217\nOUTPUT MAX(X, I) == 16777216, " ", MIN(X, I) == 1'), ['TRUE TRUE']);
// APPEND and INSERT store a value as the list's own type
assert.deepEqual(out('DECLARE L : LIST OF FLOAT32\nDECLARE N : INTEGER <- 16777217\nAPPEND(L, N)\nINSERT(L, 0, N)\nOUTPUT L[0] == 16777216, " ", L[1] == 16777216'), ['TRUE TRUE']);
assert.equal(run('DECLARE L : LIST OF INT8\nDECLARE N : INTEGER <- 300\nAPPEND(L, N)').error?.kind, 'overflow');
});
test('an ARRAY or HASHMAP given as an argument is checked value by value', () => {
const src = 'PROCEDURE Q(A : ARRAY[1] OF INTEGER)\n OUTPUT A[0]\nENDPROCEDURE\nPROCEDURE H(M : HASHMAP OF STRING : INTEGER)\n OUTPUT LENGTH(M)\nENDPROCEDURE';
const st = studyOf(src);
assert.throws(() => runPseudo(st, { call: 'Q([2.5])' }), /ARRAY/);
assert.throws(() => runPseudo(st, { call: 'H({"a": 2.5})' }), /HASHMAP/);
assert.deepEqual(runPseudo(st, { call: 'Q([2])' }).output, ['2']);
assert.deepEqual(runPseudo(st, { call: 'H({"a": 2})' }).output, ['1']);
});
// ---- what the audit of the number model found (work/revive/interp-audit-report.md, section A)
test('MOD is the exact remainder with the divisor\'s sign, past 2^53 in the product too', () => {
assert.deepEqual(out('OUTPUT 9007199254740991 MOD -3, " ", 9007199254740991 MOD (-2287), " ", (-8306069099471541) MOD 1859998429852587, " ", (-9007199254740991) MOD 3'), ['-2 -420 993923049791394 2']);
assert.deepEqual(out('OUTPUT -7 DIV 2, " ", -7 MOD 2, " ", 7 MOD -2, " ", 7 DIV -2, " ", 6 MOD -3, " ", -6 MOD 3'), ['-4 1 -1 -4 0 0']);
});
test('CHOOSE is exact for every count a run can hold', () => {
assert.deepEqual(out('OUTPUT CHOOSE(56, 23), " ", CHOOSE(57, 24), " ", CHOOSE(58, 23), " ", CHOOSE(56, 28), " ", CHOOSE(5, 2), " ", CHOOSE(5, 7), " ", CHOOSE(0, 0)'),
['3167295784216200 7522327487513475 8799226775309880 7648690600760440 10 0 1']);
assert.equal(run('OUTPUT CHOOSE(60, 30)').error?.kind, 'overflow');
});
test('a sum of whole numbers stops at a partial sum past 2^53 - 1, as + does', () => {
const head = 'DECLARE L : LIST OF INTEGER <- [9007199254740991, 2, -3]\n';
assert.equal(run(`${head}OUTPUT SUM_OF(L)`).error?.kind, 'overflow');
assert.equal(run(`${head}OUTPUT MEAN_OF(L)`).error?.kind, 'overflow');
assert.deepEqual(out('OUTPUT SUM_OF([9007199254740990, -2, 3]), " ", MEAN_OF([1, 2, 6])'), ['9007199254740991 3.0']);
});
test('a literal no run could hold is a literal-range error', () => {
assert.ok(errors('OUTPUT 9007199254740993').includes('literal-range'));
assert.ok(errors('OUTPUT 99999999999999999999').includes('literal-range'));
assert.ok(errors('OUTPUT 1e400').includes('literal-range'));
assert.ok(errors('DECLARE X : FLOAT32 <- 1e39').includes('literal-range'));
assert.deepEqual(errors('DECLARE X : INT64 <- 9223372036854775807').filter((id) => id === 'literal-range').length, 1);
assert.deepEqual(out('OUTPUT 9007199254740991, " ", -9007199254740991, " ", 1e308'), ['9007199254740991 -9007199254740991 1.0e308']);
});
test('sized results that were not checked: ABS, GCD, BIT_NOT of a UINT64, ADD, a declared FOR counter', () => {
assert.equal(run('DECLARE X : INT8 <- -128\nOUTPUT ABS(X)').error?.kind, 'overflow');
assert.equal(run('DECLARE X : INT8 <- -128\nOUTPUT GCD(X, INT8(0))').error?.kind, 'overflow');
assert.equal(run('DECLARE A : UINT64 <- 85\nOUTPUT BIT_NOT(A)').error?.kind, 'overflow');
assert.deepEqual(out('DECLARE X : INT8 <- -127\nDECLARE U : UINT8 <- 5\nOUTPUT ABS(X), " ", GCD(X, INT8(0)), " ", BIT_NOT(U), " ", BIT_NOT(X)'), ['127 127 250 126']);
assert.equal(run('DECLARE S : SET OF INT8\nDECLARE K : INTEGER <- 1000\nADD(S, K)').error?.kind, 'overflow');
assert.deepEqual(out('DECLARE S : SET OF FLOAT32\nDECLARE K : INTEGER <- 16777217\nADD(S, K)\nOUTPUT S'), ['{16777216.0}']);
const r = run('DECLARE I : INT8\nFOR I <- 126 TO 130\n OUTPUT I\nNEXT I');
assert.deepEqual(r.output, ['126', '127']);
assert.equal(r.error?.kind, 'overflow');
assert.deepEqual(out('DECLARE I : INT8\nFOR I <- 125 TO 128\n OUTPUT I\nNEXT I'), ['125', '126', '127']);
// the prover asks the same obligation where the run now stops
const rep = provePseudo(studyOf('FUNCTION F(X : INT8) RETURNS INT8\n RETURN ABS(X)\nENDFUNCTION'));
assert.ok(rep.units.flatMap((u) => u.obligations).some((o) => o.kind === 'overflow' && /ABS\(X\) fits INT8/.test(o.label) && o.status !== 'proved'));
});
test('two different sized types do not mix in MIN, MAX or a list either', () => {
const head = 'DECLARE A : INT8 <- 1\nDECLARE B : INT16 <- 2\n';
assert.ok(errors(`${head}OUTPUT MAX(A, B)`).includes('mixed-width'));
assert.ok(errors(`${head}OUTPUT MIN(A, 3, B)`).includes('mixed-width'));
assert.ok(errors(`${head}OUTPUT [A, B]`).includes('mixed-width'));
assert.deepEqual(out(`${head}OUTPUT MAX(A, 3), " ", MIN(A, INT8(B)), " ", [A, 3]`), ['3 1 [1, 3]']);
});
test('a decimal that becomes a FLOAT32 is rounded once, from its text', () => {
assert.deepEqual(out('DECLARE X : FLOAT32 <- 1.0000000596046448\nOUTPUT EXACT(X), " ", EXACT(FLOAT32(10780.08740234374999999999892))'), ['1.00000011920928955078125 10780.0869140625']);
assert.deepEqual(out('DECLARE X : FLOAT32 <- 0.1\nDECLARE Y : FLOAT32 <- -0.1\nOUTPUT EXACT(X), " ", EXACT(Y), " ", EXACT(FLOAT32(1e-45)), " ", EXACT(FLOAT32(16777217.0)), " ", X + 1.5e0'),
['0.100000001490116119384765625 -0.100000001490116119384765625 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125 16777216.0 1.6']);
});
test('USE IEEE: infinities compare as numbers, not-a-number sorts last, FMA keeps an infinite addend', () => {
assert.deepEqual(out('USE IEEE\nOUTPUT INF() <= INF(), " ", INF() >= INF(), " ", -INF() <= -INF(), " ", INF() < INF(), " ", NAN() < 1.0, " ", NAN() > 1.0, " ", NAN() >= NAN()'), ['TRUE TRUE TRUE FALSE FALSE FALSE FALSE']);
assert.deepEqual(out('USE IEEE\nOUTPUT SORTED([3.0, NAN(), 1.0, 2.0])'), ['[1.0, 2.0, 3.0, not a number]']);
assert.deepEqual(out('USE IEEE\nOUTPUT FMA(1e308, 10.0, -INF()), " ", FMA(-1e308, 10.0, INF()), " ", FMA(INF(), 0.0, 1.0)'), ['-infinity infinity not a number']);
});
test('a whole number is never minus zero, and a FLOAT that is prints as -0.0', () => {
assert.deepEqual(out('OUTPUT 0 * (-1), " ", EXACT(TO_FLOAT(0 * (-1))), " ", COPYSIGN(1.0, 0 * (-1)), " ", EXACT(TO_FLOAT(0 DIV -5)), " ", EXACT(TO_FLOAT(-4 MOD 2)), " ", EXACT(TO_FLOAT(-0))'), ['0 0.0 1.0 0.0 0.0 0.0']);
assert.deepEqual(out('OUTPUT -0.0, " ", 0.0, " ", TO_STRING(-0.0), " ", EXACT(TO_FLOAT(TO_STRING(-0.0))), " ", 0.0 * -1.0'), ['-0.0 0.0 -0.0 -0.0 -0.0']);
});
test('stops say what is true: the limit that was passed, the sign, the built-in', () => {
assert.match(run('DECLARE X : FLOAT32 <- 3e38\nX <- X * 10.0').error.message, /3\.4 × 10\^38.*FLOAT32/);
assert.match(run('OUTPUT (-8.0) ^ 0.5').error.message, /negative number is raised to a power that is not whole/);
assert.match(run('OUTPUT BIT_XOR(-1, 9007199254740991)').error.message, /less than -9,007,199,254,740,991/);
assert.match(run('OUTPUT TO_INTEGER("-9007199254740993")').error.message, /less than -9,007,199,254,740,991/);
assert.match(run('DECLARE L : LIST OF INTEGER <- [1, 2]\nREMOVE(L, 5)').error.message, /^REMOVE\(L, 5\) asks for position 5/);
});
test('valid programs the checker used to turn away', () => {
assert.deepEqual(out('DECLARE L : LIST OF FLOAT\nL <- [1, 2]\nOUTPUT L'), ['[1.0, 2.0]']);
assert.deepEqual(out('DECLARE L : LIST OF FLOAT32\nAPPEND(L, 0.1)\nINSERT(L, 0, 0.7)\nOUTPUT EXACT(L[1]), " ", EXACT(L[0])'), ['0.100000001490116119384765625 0.699999988079071044921875']);
assert.deepEqual(out('DECLARE S : SET OF FLOAT32\nADD(S, 0.1)\nOUTPUT FLOAT32(0.1) IN S'), ['TRUE']);
assert.deepEqual(out('FUNCTION F() RETURNS INTEGER\n WHILE TRUE\n RETURN 1\n ENDWHILE\nENDFUNCTION\nOUTPUT F()'), ['1']);
assert.ok(errors('FUNCTION F(X : INTEGER) RETURNS INTEGER\n WHILE TRUE\n IF X > 0 THEN\n BREAK\n ENDIF\n RETURN 1\n ENDWHILE\nENDFUNCTION').includes('missing-return'));
});
test('USE IEEE: MIN and MAX with NaN, ULP of an infinity, and POW of 1 follow IEEE 754', () => {
// MIN and MAX: a NaN anywhere among the values gives NaN (754-2019 minimum and maximum), not only in first place
assert.deepEqual(out('USE IEEE\nOUTPUT MAX(1.0, NAN()), " ", MAX(NAN(), 1.0), " ", MIN(2.0, NAN(), 1.0), " ", MIN([3.0, NAN()]), " ", MAX(1.0, INF())'), ['not a number not a number not a number not a number infinity']);
// ULP: an infinity's gap is infinite; NaN's is NaN
assert.deepEqual(out('USE IEEE\nOUTPUT ULP(INF()), " ", ULP(-INF()), " ", IS_NAN(ULP(NAN())), " ", ULP(1.0) == EPSILON(FLOAT64)'), ['infinity infinity TRUE TRUE']);
assert.equal(ulp(Infinity), Infinity);
// pow: 1 to any power is 1, NaN included, and -1 to an infinite power is 1; JavaScript's ** gives NaN for both
assert.deepEqual(out('USE IEEE\nOUTPUT POW(1.0, NAN()), " ", 1.0 ^ NAN(), " ", POW(-1.0, INF()), " ", (-1.0) ^ -INF(), " ", POW(NAN(), 0.0), " ", IS_NAN(POW(2.0, NAN()))'), ['1.0 1.0 1.0 1.0 1.0 TRUE']);
// without USE IEEE nothing changes: such values cannot arise
assert.deepEqual(out('OUTPUT MAX(1.0, 2.5), " ", POW(1.0, 3.0), " ", ULP(1.0) == EPSILON(FLOAT64)'), ['2.5 1.0 TRUE']);
});
test('messages and small things: ^ with no answer, INPUT into a FLOAT32, SORTED of lists, deep calls', () => {
// 0.0 ^ -1 divides by zero and (-8.0) ^ 0.5 has no real answer: each says so (they were called overflows), and the
// bracket that makes -8.0 the base is kept (-8.0 ^ 0.5 is -(8.0 ^ 0.5))
const z = run('OUTPUT 0.0 ^ -1');
assert.equal(z.error?.kind, 'value');
assert.match(z.error.message, /^0\.0 \^ -1 has no answer: zero raised to a negative power divides by zero/);
const n = run('OUTPUT (-8.0) ^ 0.5');
assert.equal(n.error?.kind, 'value');
assert.match(n.error.message, /^\(-8\.0\) \^ 0\.5 has no real answer/);
assert.deepEqual(out('OUTPUT -8.0 ^ 0.5 < 0.0, " ", (-2.0) ^ 2, " ", -2 ^ 2'), ['TRUE 4.0 -4']);
// BIT_NOT of a UINT64 is past what a run holds: the message gives the exact answer, not a rounded double
const b = run('DECLARE X : UINT64 <- 85\nOUTPUT BIT_NOT(X)');
assert.equal(b.error?.kind, 'overflow');
assert.match(b.error.message, /comes to 18,446,744,073,709,551,530 /);
// INPUT into a FLOAT32 rounds once, from the text, as a literal does (1.0000000596046448 lies just past a tie)
const st = studyOf('DECLARE X : FLOAT32\nINPUT X\nOUTPUT EXACT(X)');
assert.deepEqual(runPseudo(st, { input: ['1.0000000596046448'] }).output, ['1.00000011920928955078125']);
assert.deepEqual(runPseudo(st, { input: ['-0.1'] }).output, [out('OUTPUT EXACT(FLOAT32(-0.1))')[0]]);
assert.deepEqual(runPseudo(studyOf('DECLARE X : FLOAT32\nINPUT X\nOUTPUT X'), { input: ['.5'] }).output, ['0.5']);
// SORTED of lists is in dictionary order, value by value, not the order of their text
assert.deepEqual(out('OUTPUT SORTED([[2], [1, 5], [10], [1]])'), ['[[1], [1, 5], [2], [10]]']);
// calls that go deeper than the engine underneath can follow stop on a line
const deep = run('FUNCTION F(N : INTEGER) RETURNS INTEGER\n IF N == 0 THEN\n RETURN 0\n ENDIF\n WHILE TRUE\n IF TRUE THEN\n IF TRUE THEN\n RETURN F(N - 1) + 1\n ENDIF\n ENDIF\n ENDWHILE\nENDFUNCTION\nOUTPUT F(398)');
assert.equal(deep.error?.kind, 'depth');
assert.equal(typeof deep.error.line, 'number');
});
test('TO_FLOAT and INPUT read a decimal number, as the reference says, and nothing else', () => {
assert.deepEqual(out('OUTPUT TO_FLOAT("2.5"), " ", TO_FLOAT(" -1e3 "), " ", TO_FLOAT(".5"), " ", TO_FLOAT("7")'), ['2.5 -1000.0 0.5 7.0']);
for (const t of ['"0x10"', '"0b11"', '""', '"1,5"', '"Infinity"', '"NaN"']) assert.equal(run(`OUTPUT TO_FLOAT(${t})`).error?.kind, 'value', t);
assert.equal(run('OUTPUT TO_FLOAT("1e400")').error?.kind, 'overflow');
assert.deepEqual(out('USE IEEE\nOUTPUT TO_FLOAT("-Infinity")'), ['-infinity']);
const st = studyOf('DECLARE X : FLOAT\nINPUT X\nOUTPUT X');
assert.equal(runPseudo(st, { input: ['0x1A'] }).error?.kind, 'input');
assert.deepEqual(runPseudo(st, { input: ['26'] }).output, ['26.0']);
// TO_INTEGER of a CHAR digit is the digit, not its character code
assert.deepEqual(out("OUTPUT TO_INTEGER('7') + 1"), ['8']);
});
test('DOT over whole numbers checks every product and partial sum, as * and + do', () => {
// the first product passes 2^53: it used to be rounded, and the total came back inside the range, wrong (261134297)
const r = run('OUTPUT DOT([94906267, 1], [94906267, -9007199254740991])');
assert.equal(r.error?.kind, 'overflow');
assert.equal(run('OUTPUT DOT([9007199254740991, 1], [1, 1])').error?.kind, 'overflow');
assert.deepEqual(out('OUTPUT DOT([3, 4], [5, 6]), " ", DOT([1.5, 2.0], [2.0, 4.0]), " ", DOT([1, 2], [0.5, 0.25]), " ", DOT([9007199254740991, 1], [1, -1])'), ['39 11.0 1.0 9007199254740990']);
});
test('more stops and checker messages say what is true (claims ledger, diagnostics)', () => {
// INPUT of a FLOAT past the largest says so, rather than that the line was no FLOAT
const big = runPseudo(studyOf('DECLARE F : FLOAT\nINPUT F'), { input: ['1e400'] });
assert.equal(big.error?.kind, 'input');
assert.match(big.error.message, /read 1e400, which is past the largest FLOAT/);
// INTEGER's limits, and the 64-bit types' "beyond", not "more than", for a negative number
const msg = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join(' | ');
assert.match(msg('OUTPUT MAX_VALUE(INTEGER)'), /from -9,007,199,254,740,991 to 9,007,199,254,740,991/);
assert.match(msg('OUTPUT MIN_VALUE(INT64)'), /-9,223,372,036,854,775,808, beyond what this lab holds exactly/);
// a range too large to visit: the counted loop is offered only when it could run in the steps a run has
assert.doesNotMatch(run('FOR EACH I IN [0, 3000000)\n OUTPUT I\nNEXT I').error.message, /A counted loop/);
assert.match(run('FOR EACH I IN [0, 3000000)\n OUTPUT I\nNEXT I').error.message, /more passes than a run may take/);
// NORM_OF of one value stops where its square passes the largest FLOAT, about 1.34e154
assert.deepEqual(out('OUTPUT NORM_OF([1.3e154])'), ['1.3e154']);
assert.equal(run('OUTPUT NORM_OF([1.4e154])').error?.kind, 'overflow');
});