Baseline: tree as shared

This commit is contained in:
alexm committed 2026-10-05 03:03:38 +00:00
commit 659c03bbf8
1471 files changed
+990535

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
// The assembly census (scripts/asm-census.mjs) as a ratchet: every function the
// real compiler produced for the user's own code assembles on the model machine,
// the library functions that assemble never get fewer, and the instructions
// outside the model's set are only the 256-bit AVX2 ones the model says it
// does not have. A new corpus item that brings a new instruction fails here,
// which is the point.
import test from 'node:test';
import assert from 'node:assert/strict';
import { census } from '../scripts/asm-census.mjs';
import { parseAsm, runAsmProgram } from '../dist/services/asm.js';
import { intelLine } from '../dist/mojo/att.js';
const LIBRARY_AT_LEAST = 438; // raise as the machine learns more
const OUTSIDE = new Set(['vextracti128', 'vinserti128', 'vpblendvb', 'vperm2i128', 'vpalignr']); // ymm: 256-bit AVX2
test('every user function in the corpus listings assembles on the model machine', () => {
const c = census();
assert.deepEqual(c.userFailures, []);
assert.equal(c.userOk, c.user);
assert.ok(c.user >= 7, `only ${c.user} user functions in the corpus`);
});
test('the library functions that assemble do not get fewer, and what is outside the set is only AVX2', () => {
const c = census();
assert.ok(c.libOk >= LIBRARY_AT_LEAST, `${c.libOk} library functions assemble, fewer than ${LIBRARY_AT_LEAST}`);
const unexpected = c.missing.map(([op]) => op).filter((op) => !OUTSIDE.has(op));
assert.deepEqual(unexpected, [], 'instructions the corpus uses that the model neither has nor lists as outside it');
});
test('the AT&T translator strips the size suffix from the BMI and atomic mnemonics and keeps lock', () => {
const map = new Map();
assert.equal(intelLine('andnq %rax, %r9, %rax', map), 'andn rax, r9, rax');
assert.equal(intelLine('mulxq %rcx, %rdx, %rdx', map), 'mulx rdx, rdx, rcx');
assert.equal(intelLine('lock xaddq %rax, (%rcx)', map), 'lock xadd qword ptr [rcx], rax');
assert.equal(intelLine('shlxq %rcx, %rbx, %r11', map), 'shlx r11, rbx, rcx');
assert.equal(intelLine('vpcmpeqb %xmm1, %xmm0, %xmm3', map), 'vpcmpeqb xmm3, xmm0, xmm1');
});
test('the instructions compiled Mojo uses compute what the manual says', () => {
const run = (src, setup = '') => { const p = parseAsm(src); assert.ok(p.ok, p.diagnostics.map((d) => d.message).join('; ')); return runAsmProgram(p, { setup, machine: 'teaching', maxSteps: 200 }); };
const st = run([
'mov rdx, 6', 'mov rcx, 7', 'mulx r8, r9, rcx',
'mov rax, 0xff00', 'mov rbx, 0x0ff0', 'andn r10, rax, rbx',
'mov rcx, 3', 'shlx r11, rbx, rcx', 'sarx r12, rax, rcx', 'bzhi r13, rbx, rcx',
'mov rsi, 12', 'blsr rdi, rsi', 'blsi r14, rsi', 'blsmsk r15, rsi', 'rorx rbx, rsi, 1',
'mov rax, 1', 'mov qword ptr [0x1000], 10', 'lock xadd qword ptr [0x1000], rax',
'mov rax, 11', 'mov rcx, 99', 'lock cmpxchg qword ptr [0x1000], rcx',
'bts rdx, 4', 'mov eax, 0x12345678', 'bswap eax',
].join('\n'), 'memory 0x1000 "........"');
const s = st.states[st.count];
assert.equal(st.error, null);
assert.equal(s.regs.r9, 42n); assert.equal(s.regs.r8, 0n);
assert.equal(s.regs.r10, 0xf0n); assert.equal(s.regs.r11, 0x7f80n); assert.equal(s.regs.r12, 0x1fe0n); assert.equal(s.regs.r13, 0n);
assert.equal(s.regs.rdi, 8n); assert.equal(s.regs.r14, 4n); assert.equal(s.regs.r15, 7n); assert.equal(s.regs.rbx, 6n);
assert.equal(s.regs.rdx, 22n); assert.equal(s.regs.rax, 0x78563412n); assert.equal(s.flags.ZF, true);
assert.equal(s.mem.find((m) => m.addr === 0x1000).bytes[0], 99, 'cmpxchg stored the new value');
// vectors: a broadcast compare finds every A in GATTACAGATTACA
const v = run(['vmovdqu xmm1, xmmword ptr [0x1000]', 'mov eax, 0x41', 'vmovd xmm4, eax', 'vpbroadcastb xmm4, xmm4', 'vpcmpeqb xmm3, xmm1, xmm4', 'vpmovmskb r8d, xmm3', 'vpextrb eax, xmm1, 1', 'vpshufd xmm5, xmm1, 0x1b'].join('\n'), 'memory 0x1000 "GATTACAGATTACA.."');
const vs = v.states[v.count];
assert.equal(vs.regs.r8, 0b10100101010010n); assert.equal(vs.regs.rax, 0x41n);
// ud2 stops the run and says why
const u = run('mov rax, 1\nud2');
assert.match(u.error.message, /invalid-opcode/);
// a stub for the standard library stops the run and names the function
const stub = run('call std_io_io_print\nstd_io_io_print:\n ud2 ; std_io_io_print: standard library, not on the model machine');
assert.match(stub.error.message, /std_io_io_print: standard library, not on the model machine/);
});
+254
View File
@@ -0,0 +1,254 @@
// Scalar double arithmetic, the parity flag, shld/shrd, and the compiler's constant pool.
import test from 'node:test';
import assert from 'node:assert/strict';
import { parseAsm, runAsmProgram } from '../dist/services/asm.js';
import { rflagsValue } from '../dist/engines/asm86/interp.js';
import { intelLine, forSimulator, simulatorSetup } from '../dist/mojo/att.js';
import { listingFunctions } from '../dist/services/godbolt.js';
const dv = new DataView(new ArrayBuffer(8));
const M64 = 0xffffffffffffffffn;
const bits = (x) => { dv.setFloat64(0, x); return dv.getBigUint64(0); };
const dbl = (b) => { dv.setBigUint64(0, BigInt(b) & M64); return dv.getFloat64(0); };
const le = (v) => Array.from({ length: 8 }, (_, i) => Number((BigInt(v) >> BigInt(8 * i)) & 0xffn));
const lo = (v) => BigInt(v) & M64;
function run(src, setup = '') {
const p = parseAsm(src);
assert.ok(p.ok, p.diagnostics?.map((d) => d.message).join('; '));
const r = runAsmProgram(p, { setup, machine: 'teaching', maxSteps: 300 });
return { s: r.states[r.count], error: r.error?.message };
}
const load = (reg, x) => [`mov rax, 0x${bits(x).toString(16)}`, `movq ${reg}, rax`];
const two = (op, a, b) => run([...load('xmm0', a), ...load('xmm1', b), `${op} xmm0, xmm1`].join('\n')).s.regs.xmm0;
const three = (op, a, b) => run([...load('xmm1', a), ...load('xmm2', b), `${op} xmm0, xmm1, xmm2`].join('\n')).s.regs.xmm0;
test('arithmetic is IEEE binary64, rounded to nearest even', () => {
assert.equal(lo(two('addsd', 0.1, 0.2)), 0x3FD3333333333334n);
assert.equal(lo(two('addsd', 0.1, 0.2)), bits(0.1 + 0.2));
assert.equal(dbl(lo(two('divsd', 1, 3))), 1 / 3);
assert.equal(dbl(lo(two('mulsd', 1e200, 1e200))), Infinity, 'overflow gives infinity');
assert.equal(dbl(lo(three('vsubsd', 4, 3))), 1);
assert.equal(dbl(lo(three('vdivsd', 4, 3))), 4 / 3);
const r = run([...load('xmm1', 2), 'vsqrtsd xmm0, xmm1, xmm1'].join('\n')).s.regs.xmm0;
assert.equal(dbl(lo(r)), Math.SQRT2);
});
test('special values: division by zero, invalid operations, NaN propagation', () => {
assert.equal(dbl(lo(two('divsd', 1, 0))), Infinity);
assert.equal(dbl(lo(two('divsd', -1, 0))), -Infinity);
assert.equal(lo(two('divsd', 0, 0)), 0xFFF8000000000000n, '0/0 is the default NaN');
const sq = (x) => lo(run([...load('xmm1', x), 'vsqrtsd xmm0, xmm1, xmm1'].join('\n')).s.regs.xmm0);
assert.equal(sq(-1), 0xFFF8000000000000n);
assert.equal(sq(-0), bits(-0), 'sqrt(-0) is -0');
// a quiet NaN with a payload is kept; a signalling one is quieted; with two NaNs the first operand wins
const nan = (payload) => `mov rax, 0x${payload.toString(16)}\nmovq xmm0, rax`;
const q = run(`${nan(0x7FF8000000000123n)}\n${load('xmm1', 1).join('\n')}\naddsd xmm0, xmm1`).s.regs.xmm0;
assert.equal(lo(q), 0x7FF8000000000123n);
const sig = run(`${nan(0x7FF0000000000001n)}\n${load('xmm1', 1).join('\n')}\naddsd xmm0, xmm1`).s.regs.xmm0;
assert.equal(lo(sig), 0x7FF8000000000001n);
const both = run(`${nan(0x7FF8000000000001n)}\nmov rax, 0x7FF8000000000002\nmovq xmm1, rax\naddsd xmm0, xmm1`).s.regs.xmm0;
assert.equal(lo(both), 0x7FF8000000000001n);
});
test('cvtsi2sd rounds to nearest even and reads the integer width asked for', () => {
const cvt = (setup, line) => lo(run(`${setup}\n${line}`).s.regs.xmm0);
assert.equal(cvt('mov rax, 9007199254740993', 'cvtsi2sd xmm0, rax'), bits(9007199254740992), '2^53+1 ties to even');
assert.equal(cvt('mov rax, 0x8000000000000000', 'cvtsi2sd xmm0, rax'), bits(-9223372036854775808));
assert.equal(cvt('mov eax, 0xffffffff', 'cvtsi2sd xmm0, eax'), bits(-1), '32-bit source is signed');
});
test('upper halves: the VEX forms copy from the first source, the SSE forms keep the destination', () => {
// 16 bytes at 0x1000: low half 5.0's bits, high half 0x1111; at 0x1010: low 7.0's bits, high 0x2222
const mem = `memory 0x1000 bytes ${[...le(bits(5)), ...le(0x1111n), ...le(bits(7)), ...le(0x2222n)].map((b) => '0x' + b.toString(16)).join(' ')}`;
const prog = (body) => run(['movdqu xmm0, xmmword ptr [0x1000]', 'movdqu xmm1, xmmword ptr [0x1010]', body].join('\n'), mem).s.regs.xmm0;
const hi = (v) => v >> 64n;
assert.equal(hi(prog('vaddsd xmm0, xmm1, xmm1')), 0x2222n, 'vaddsd takes the upper half of its first source');
assert.equal(dbl(lo(prog('vaddsd xmm0, xmm1, xmm1'))), 14);
assert.equal(hi(prog('addsd xmm0, xmm1')), 0x1111n, 'addsd keeps the destination\u2019s');
assert.equal(hi(prog('movsd xmm0, xmm1')), 0x1111n, 'register movsd merges the low half');
assert.equal(dbl(lo(prog('movsd xmm0, xmm1'))), 7);
assert.equal(hi(prog('movsd xmm0, qword ptr [0x1010]')), 0n, 'a movsd load clears the upper half');
assert.equal(hi(prog('vmovsd xmm0, xmm0, xmm1')), 0x1111n, 'three-operand vmovsd keeps the first source\u2019s upper half');
assert.equal(dbl(lo(prog('vmovsd xmm0, xmm0, xmm1'))), 7);
});
const flagsAfter = (a, b, op = 'ucomisd') => run([...load('xmm0', a), ...load('xmm1', b), `${op} xmm0, xmm1`].join('\n')).s.flags;
const pick = (f) => ['ZF', 'PF', 'CF'].map((k) => (f[k] ? 1 : 0)).join('');
test('ucomisd and comisd: less sets CF, equal sets ZF, unordered sets all three', () => {
for (const op of ['ucomisd', 'comisd', 'vucomisd', 'vcomisd']) {
assert.equal(pick(flagsAfter(1, 2, op)), '001', `${op} less`);
assert.equal(pick(flagsAfter(2, 1, op)), '000', `${op} greater`);
assert.equal(pick(flagsAfter(2, 2, op)), '100', `${op} equal`);
assert.equal(pick(flagsAfter(NaN, 1, op)), '111', `${op} unordered`);
}
assert.equal(pick(flagsAfter(0, -0)), '100', '+0 equals -0');
const f = flagsAfter(NaN, 1);
assert.equal(f.OF, false);
assert.equal(f.SF, false);
});
test('conditions after a compare: ja, jb, je, jp and setp', () => {
const took = (a, b, j) => run([...load('xmm0', a), ...load('xmm1', b), 'ucomisd xmm0, xmm1', 'mov rcx, 0', `${j} yes`, 'jmp done', 'yes: mov rcx, 1', 'done: nop'].join('\n')).s.regs.rcx;
assert.equal(took(2, 1, 'ja'), 1n);
assert.equal(took(1, 2, 'ja'), 0n);
assert.equal(took(1, 2, 'jb'), 1n);
assert.equal(took(3, 3, 'je'), 1n);
assert.equal(took(NaN, 3, 'jp'), 1n);
assert.equal(took(3, 3, 'jp'), 0n);
assert.equal(took(NaN, 3, 'jnp'), 0n);
const set = run([...load('xmm0', NaN), ...load('xmm1', 1), 'ucomisd xmm0, xmm1', 'setp al', 'setnp bl'].join('\n')).s.regs;
assert.equal(set.rax & 0xffn, 1n);
assert.equal(set.rbx & 0xffn, 0n);
});
test('the parity flag: set by logic, arithmetic and shifts; left alone by rotates; cleared by popcnt', () => {
const pf = (src) => run(src).s.flags.PF;
assert.equal(pf('mov rax, 3\ntest rax, rax'), true, '0b11 has two set bits');
assert.equal(pf('mov rax, 1\ntest rax, rax'), false);
assert.equal(pf('mov rax, 5\nxor rax, rax'), true, 'zero has even parity');
assert.equal(pf('mov rax, 1\nshl rax, 1'), false);
assert.equal(pf('mov rax, 2\ninc rax'), true);
assert.equal(pf('mov rax, 0\ntest rax, rax\nrol rax, 1'), true, 'rol does not touch PF');
assert.equal(pf('mov rax, 0\ntest rax, rax\npopcnt rbx, rax'), false);
assert.equal((rflagsValue({ ZF: false, SF: false, CF: false, OF: false, PF: true }) >> 2n) & 1n, 1n, 'PF is bit 2 of RFLAGS');
assert.equal((rflagsValue({ ZF: false, SF: false, CF: false, OF: false, PF: false }) >> 2n) & 1n, 0n);
});
test('shld and shrd shift in bits from the second register', () => {
const r = (src) => run(src, 'memory 0x1000 zero 16').s;
assert.equal(r('mov rax, 0xff\nmov rsi, 0xf000000000000000\nshld rax, rsi, 4').regs.rax, 0xfffn);
assert.equal(r('mov rbx, 0xff\nmov rdi, 0xf\nshrd rbx, rdi, 4').regs.rbx, 0xf00000000000000fn);
assert.equal(r('mov rax, 1\nmov rcx, 4\nmov rsi, 0xf000000000000000\nshld rax, rsi, cl').regs.rax, 0x1fn, 'count in cl');
// count zero changes nothing, flags included
const z = r('mov rax, 3\ntest rax, rax\nmov rsi, 1\nshld rax, rsi, 0');
assert.equal(z.regs.rax, 3n);
assert.equal(z.flags.PF, true);
// 32-bit counts are masked to five bits: 33 acts as 1
assert.equal(r('mov eax, 1\nmov esi, 0x80000000\nshld eax, esi, 33').regs.rax, 3n);
// the same register on both sides is a rotate
assert.equal(r('mov rax, 0x8000000000000001\nshld rax, rax, 1').regs.rax, 3n);
// carry is the last bit shifted out of the destination
assert.equal(r('mov rax, 0x8000000000000000\nmov rsi, 0\nshld rax, rsi, 1').flags.CF, true);
// a memory destination
assert.equal(r('mov qword ptr [0x1000], 0xff\nmov rsi, 0xf000000000000000\nshld qword ptr [0x1000], rsi, 4\nmov rax, qword ptr [0x1000]').regs.rax, 0xfffn);
});
test('shld refuses a 16-bit operand and nonsense forms', () => {
for (const bad of ['shld ax, bx, 1', 'vaddsd xmm0, rax', 'cvtsi2sd rax, xmm0', 'ucomisd rax, rbx', 'sqrtsd xmm0']) {
const p = parseAsm(bad);
const failed = !p.ok || (() => { const r = runAsmProgram(p, { setup: '', machine: 'teaching', maxSteps: 10 }); return !!r.error; })();
assert.ok(failed, bad);
}
});
test('AT&T spellings of the new instructions become Intel', () => {
const t = (att) => intelLine(att, new Map());
assert.equal(t('vcvtsi2sd %rax, %xmm0, %xmm0'), 'vcvtsi2sd xmm0, xmm0, rax');
assert.equal(t('vcvtsi2sdl (%rax), %xmm0, %xmm0'), 'vcvtsi2sd xmm0, xmm0, dword ptr [rax]');
assert.equal(t('vcvtsi2sdq 8(%rsp), %xmm1, %xmm1'), 'vcvtsi2sd xmm1, xmm1, qword ptr [rsp + 8]');
assert.equal(t('vmovsd 8(%rsp), %xmm0'), 'vmovsd xmm0, [rsp + 8]');
assert.equal(t('shldq $3, %rsi, %rax'), 'shld rax, rsi, 3');
assert.equal(t('shldq %cl, %rsi, %rax'), 'shld rax, rsi, cl');
assert.equal(t('setp %al'), 'setp al');
assert.equal(t('vdivsd .LCPI0_0+8(%rip), %xmm0, %xmm0'), 'vdivsd xmm0, xmm0, [rip + .LCPI0_0 + 8]');
});
// LLVM's output for `fn scale(n: Int) -> Float64: return Float64(n) / 1_000_000.0`, trimmed to what matters.
const LISTING = [
'"example::scale(::Int)":',
' vcvtsi2sd %rdi, %xmm0, %xmm0',
' vdivsd .LCPI0_0(%rip), %xmm0, %xmm0',
' retq',
'.LCPI0_0:',
' .quad 0x412e848000000000 # double 1.0E+6',
'.LCPI0_1:',
' .quad 0x4000000000000000',
' .quad 0x4008000000000000',
].map((text, i) => ({ text, line: i === 1 ? 3 : null }));
test('a constant pool in the listing is placed in memory and the code points at it', () => {
const fns = listingFunctions(LISTING);
const fn = fns.find((f) => f.kind === 'user');
assert.ok(fn, 'the user function is found');
const sim = forSimulator(fns, fn);
assert.doesNotMatch(sim.source, /LCPI/, 'no constant-pool label is left in the code');
assert.equal(sim.data.length, 1, 'only the blocks the code uses are placed');
assert.match(sim.data[0], /^memory 0x6000 bytes 0x00 0x00 0x00 0x00 0x80 0x84 0x2e 0x41 /);
const setup = simulatorSetup(fn, sim.data).replace('rdi = 0', 'rdi = 5000000');
const p = parseAsm(sim.source);
assert.ok(p.ok, p.diagnostics?.[0]?.message);
const r = runAsmProgram(p, { setup, machine: 'teaching', maxSteps: 50 });
assert.ok(!r.error, String(r.error?.message));
assert.equal(dbl(lo(r.states[r.count].regs.xmm0)), 5);
});
test('a constant block with a symbol in it is not placed, so the instruction using it is refused as before', () => {
const fns = listingFunctions([
{ text: '"example::f(::Int)":' }, { text: ' vmovsd .LJT(%rip), %xmm0' }, { text: ' retq' },
{ text: '.LJT:' }, { text: ' .quad .LBB0_1' },
]);
const sim = forSimulator(fns, fns[0]);
assert.equal(sim.data.length, 0);
});
test('a call to the debugger raise hook (every -O0 loop over a range ends with one) returns instead of stopping the run', () => {
const fns = listingFunctions([
{ text: '"example::count(::Int)":' },
{ text: ' movq %rdi, %rax' },
{ text: ' callq "std::builtin::error::__mojo_debugger_raise_hook()"' },
{ text: ' callq "std::builtin::string::String::__init__()"' },
{ text: ' retq' },
]);
const sim = forSimulator(fns, fns[0]);
assert.deepEqual(sim.refusedCalls, ['std_builtin_string_String___init__'], 'only real library code is refused');
assert.match(sim.source, /std_builtin_error___mojo_debugger_raise_hook:\n\s+ret\b/);
// with the String call removed, the function runs to its ret
const fns2 = listingFunctions([{ text: '"example::count(::Int)":' }, { text: ' movq %rdi, %rax' }, { text: ' callq "std::builtin::error::__mojo_debugger_raise_hook()"' }, { text: ' retq' }]);
const p = parseAsm(forSimulator(fns2, fns2[0]).source);
assert.ok(p.ok, p.diagnostics?.[0]?.message);
const r = runAsmProgram(p, { setup: 'rdi = 285', machine: 'teaching', maxSteps: 50 });
assert.ok(!r.error, String(r.error?.message));
assert.equal(r.states[r.count].regs.rax, 285n);
});
test('read-only strings in every form the listing writes them are placed: .asciz, an empty string as .zero 1, pieces, and an alias', () => {
const lines = [
'"example::f()":',
' leaq static_string_e(%rip), %rax',
' leaq static_string_s(%rip), %rbx',
' leaq static_string_p(%rip), %rcx',
' leaq static_string_a(%rip), %rdx',
' retq',
'static_string_e:', ' .zero 1',
'static_string_s:', ' .asciz " "',
'static_string_p:', ' .ascii "ab"', ' .byte 99, 0',
'static_string_x:', ' .asciz "hello"',
' .set static_string_a, static_string_x+2',
].map((text) => ({ text }));
const fns = listingFunctions(lines);
const sim = forSimulator(fns, fns.find((f) => f.kind === 'user'));
assert.doesNotMatch(sim.source, /rip \+/, 'every address became a number');
assert.doesNotMatch(sim.source, /does not define/);
const p = parseAsm(sim.source);
assert.ok(p.ok, p.diagnostics?.[0]?.message);
const r = runAsmProgram(p, { setup: sim.data.join('\n'), machine: 'teaching', maxSteps: 20 });
assert.ok(!r.error, String(r.error?.message));
const regs = r.states[r.count].regs;
const mem = (addr, n) => sim.data.find((d) => d.startsWith(`memory 0x${addr.toString(16)} `));
assert.match(mem(regs.rax), /bytes 0x00/, 'the empty string is one zero byte');
assert.match(mem(regs.rbx), /" "/);
assert.match(mem(regs.rcx), /bytes 0x61 0x62 0x63 0x00/);
assert.equal(regs.rdx, regs.rdx, 'alias resolves');
const hello = sim.data.find((d) => /"hello"/.test(d));
assert.ok(hello, 'the aliased string is placed');
assert.equal(regs.rdx, BigInt(parseInt(/memory (0x[0-9a-f]+)/.exec(hello)[1], 16) + 2), 'the alias points two bytes in');
});
test('vpcmpgtb compares signed bytes', () => {
const r = run(['mov rax, 0x00000000000080ff', 'movq xmm1, rax', 'mov rax, 0x0000000000000102', 'movq xmm2, rax', 'vpcmpgtb xmm0, xmm2, xmm1'].join('\n')).s.regs.xmm0;
// lane 0: 2 > -1 yes; lane 1: 1 > -128 yes; lanes 2..15: 0 > 0 no
assert.equal(r & 0xffffn, 0xffffn);
assert.equal(r >> 16n, 0n);
});
+38
View File
@@ -0,0 +1,38 @@
// 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}`);
});
+26
View File
@@ -0,0 +1,26 @@
// Numbers: every arithmetic operator, both numeric types, and the conversions between them.
// EXPECT: 3.5
// EXPECT: 3
// EXPECT: 1
// EXPECT: -4
// EXPECT: 512
// EXPECT: 2.0
// EXPECT: 27
// EXPECT: 28
// EXPECT: 5
// EXPECT: 7 -7 -7
DECLARE A, B : INTEGER
DECLARE X : FLOAT
A <- 7
B <- 2
OUTPUT A / B
OUTPUT A DIV B
OUTPUT A MOD B
OUTPUT -2 ^ 2
OUTPUT 2 ^ 3 ^ 2
OUTPUT 4 / 2
X <- 27.54
OUTPUT INT(X)
OUTPUT ROUND(X)
OUTPUT ABS(-5)
OUTPUT 7 DIV 1, " ", -7 DIV 1, " ", (-7) DIV 1
+11
View File
@@ -0,0 +1,11 @@
// A FLOAT formula with a FUNCTION; classic first program in any language.
// EXPECT: 212.0
// EXPECT: 37.0
FUNCTION TO_FAHRENHEIT(C : FLOAT) RETURNS FLOAT
RETURN C * 9 / 5 + 32
ENDFUNCTION
FUNCTION TO_CELSIUS(F : FLOAT) RETURNS FLOAT
RETURN (F - 32) * 5 / 9
ENDFUNCTION
OUTPUT TO_FAHRENHEIT(100.0)
OUTPUT TO_CELSIUS(98.6)
+11
View File
@@ -0,0 +1,11 @@
// Finance flavor: a loop that updates a FLOAT, with ROUND for reporting.
// EXPECT: 1628.89
FUNCTION BALANCE_AFTER(Principal : FLOAT, Rate : FLOAT, Years : INTEGER) RETURNS FLOAT
DECLARE B : FLOAT
B <- Principal
FOR Y <- 0 TO Years
B <- B * (1 + Rate)
NEXT Y
RETURN B
ENDFUNCTION
OUTPUT ROUND(BALANCE_AFTER(1000.0, 0.05, 10) * 100) / 100
+13
View File
@@ -0,0 +1,13 @@
// Recursion with a DECREASES measure and a REQUIRE.
// EXPECT: 6
// EXPECT: 1
FUNCTION GCD(A : INTEGER, B : INTEGER) RETURNS INTEGER
REQUIRE A >= 0 AND B >= 0
DECREASES B
IF B == 0 THEN
RETURN A
ENDIF
RETURN GCD(B, A MOD B)
ENDFUNCTION
OUTPUT GCD(48, 18)
OUTPUT GCD(17, 5)
+19
View File
@@ -0,0 +1,19 @@
// An ARRAY of BOOL, nested loops with STEP, and a LIST answer.
// EXPECT: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
FUNCTION PRIMES_UP_TO(N : INTEGER) RETURNS LIST OF INTEGER
DECLARE IsPrime : ARRAY[N + 1] OF BOOL
DECLARE Found : LIST OF INTEGER
FOR I <- 0 TO N + 1
IsPrime[I] <- I >= 2
NEXT I
FOR I <- 2 TO N + 1
IF IsPrime[I] THEN
APPEND(Found, I)
FOR J <- I * I TO N + 1 STEP I
IsPrime[J] <- FALSE
NEXT J
ENDIF
NEXT I
RETURN Found
ENDFUNCTION
OUTPUT PRIMES_UP_TO(30)
+25
View File
@@ -0,0 +1,25 @@
// Memoised recursion. A FUNCTION cannot take BYREF, so the memo table has to travel through a PROCEDURE
// with the answer coming back BYREF too. Finding: this is the idiom; it reads heavier than in most languages.
// EXPECT: 6765
// EXPECT: 12586269025
PROCEDURE FIB(N : INTEGER, BYREF Memo : HASHMAP OF INTEGER : INTEGER, BYREF Result : INTEGER)
IF N < 2 THEN
Result <- N
RETURN
ENDIF
IF N IN Memo THEN
Result <- Memo[N]
RETURN
ENDIF
DECLARE A, B : INTEGER
CALL FIB(N - 1, Memo, A)
CALL FIB(N - 2, Memo, B)
Memo[N] <- A + B
Result <- A + B
ENDPROCEDURE
DECLARE M : HASHMAP OF INTEGER : INTEGER
DECLARE R : INTEGER
CALL FIB(20, M, R)
OUTPUT R
CALL FIB(50, M, R)
OUTPUT R
+34
View File
@@ -0,0 +1,34 @@
// Text flavor: splitting a sentence into words by hand, since the notation has no SPLIT, then tallying.
// EXPECT: {"a": 2, "is": 2, "rose": 3}
// A HASHMAP prints its keys smallest first, whatever order they were added in.
FUNCTION WORDS(S : STRING) RETURNS LIST OF STRING
DECLARE Out : LIST OF STRING
DECLARE W : STRING
W <- ""
FOR EACH C IN S
IF C == ' ' THEN
IF LENGTH(W) > 0 THEN
APPEND(Out, W)
ENDIF
W <- ""
ELSE
W <- W & C
ENDIF
NEXT C
IF LENGTH(W) > 0 THEN
APPEND(Out, W)
ENDIF
RETURN Out
ENDFUNCTION
FUNCTION TALLY(Ws : LIST OF STRING) RETURNS HASHMAP OF STRING : INTEGER
DECLARE T : HASHMAP OF STRING : INTEGER
FOR EACH W IN Ws
IF W IN T THEN
T[W] <- T[W] + 1
ELSE
T[W] <- 1
ENDIF
NEXT W
RETURN T
ENDFUNCTION
OUTPUT TALLY(WORDS("a rose is a rose is rose"))
+17
View File
@@ -0,0 +1,17 @@
// Cryptography flavor: CHAR arithmetic with ORD and CHR, MOD to wrap.
// EXPECT: KHOOR, ZRUOG!
// EXPECT: HELLO, WORLD!
FUNCTION SHIFT(S : STRING, K : INTEGER) RETURNS STRING
DECLARE Out : STRING
Out <- ""
FOR EACH C IN S
IF C >= 'A' AND C <= 'Z' THEN
Out <- Out & CHR((ORD(C) - ORD('A') + K) MOD 26 + ORD('A'))
ELSE
Out <- Out & C
ENDIF
NEXT C
RETURN Out
ENDFUNCTION
OUTPUT SHIFT("HELLO, WORLD!", 3)
OUTPUT SHIFT(SHIFT("HELLO, WORLD!", 3), 23)
+17
View File
@@ -0,0 +1,17 @@
// Two positions moving toward each other; a WHILE with a compound guard.
// EXPECT: TRUE
// EXPECT: FALSE
// EXPECT: TRUE
FUNCTION IS_PALINDROME(S : STRING) RETURNS BOOL
DECLARE Lo, Hi : INTEGER
Lo <- 0
Hi <- LENGTH(S) - 1
WHILE Lo < Hi AND S[Lo] == S[Hi]
Lo <- Lo + 1
Hi <- Hi - 1
ENDWHILE
RETURN Lo >= Hi
ENDFUNCTION
OUTPUT IS_PALINDROME("racecar")
OUTPUT IS_PALINDROME("rose")
OUTPUT IS_PALINDROME("")
+19
View File
@@ -0,0 +1,19 @@
// An in-place sort with a PROCEDURE and BYREF, plus a loop invariant.
// EXPECT: [1, 2, 3, 5, 8, 9]
PROCEDURE INSERTION_SORT(BYREF L : LIST OF INTEGER)
FOR I <- 1 TO LENGTH(L)
INVARIANT FORALL K IN [1, I) : L[K - 1] <= L[K]
DECLARE Key, J : INTEGER
Key <- L[I]
J <- I - 1
WHILE J >= 0 AND L[J] > Key
L[J + 1] <- L[J]
J <- J - 1
ENDWHILE
L[J + 1] <- Key
NEXT I
ENDPROCEDURE
DECLARE Xs : LIST OF INTEGER
Xs <- [5, 2, 9, 1, 8, 3]
CALL INSERTION_SORT(Xs)
OUTPUT Xs
+27
View File
@@ -0,0 +1,27 @@
// Two-dimensional ARRAYs, triple nested loops, ROWS and COLUMNS.
// EXPECT: 19 22
// EXPECT: 43 50
FUNCTION MATMUL(A : ARRAY[2, 2] OF INTEGER, B : ARRAY[2, 2] OF INTEGER) RETURNS ARRAY[2, 2] OF INTEGER
DECLARE C : ARRAY[ROWS(A), COLUMNS(B)] OF INTEGER
FOR I <- 0 TO ROWS(A)
FOR J <- 0 TO COLUMNS(B)
C[I, J] <- 0
FOR K <- 0 TO COLUMNS(A)
C[I, J] <- C[I, J] + A[I, K] * B[K, J]
NEXT K
NEXT J
NEXT I
RETURN C
ENDFUNCTION
DECLARE A, B, C : ARRAY[2, 2] OF INTEGER
A[0, 0] <- 1
A[0, 1] <- 2
A[1, 0] <- 3
A[1, 1] <- 4
B[0, 0] <- 5
B[0, 1] <- 6
B[1, 0] <- 7
B[1, 1] <- 8
C <- MATMUL(A, B)
OUTPUT C[0, 0], " ", C[0, 1]
OUTPUT C[1, 0], " ", C[1, 1]
+37
View File
@@ -0,0 +1,37 @@
// Records in a LIST, a FUNCTION over them, and "find the best".
// EXPECT: Bob
// EXPECT: 86.5
TYPE Student
DECLARE Name : STRING
DECLARE Marks : LIST OF INTEGER
ENDTYPE
FUNCTION MEAN(Xs : LIST OF INTEGER) RETURNS FLOAT
REQUIRE LENGTH(Xs) >= 1
DECLARE Total : INTEGER
Total <- 0
FOR EACH X IN Xs
Total <- Total + X
NEXT X
RETURN Total / LENGTH(Xs)
ENDFUNCTION
FUNCTION TOP(Ss : LIST OF Student) RETURNS Student
REQUIRE LENGTH(Ss) >= 1
DECLARE Best : Student
Best <- Ss[0]
FOR EACH S IN Ss
IF MEAN(S.Marks) > MEAN(Best.Marks) THEN
Best <- S
ENDIF
NEXT S
RETURN Best
ENDFUNCTION
DECLARE A, B : Student
DECLARE Class : LIST OF Student
A.Name <- "Ann"
A.Marks <- [70, 80, 90]
B.Name <- "Bob"
B.Marks <- [88, 85]
APPEND(Class, A)
APPEND(Class, B)
OUTPUT TOP(Class).Name
OUTPUT MEAN(TOP(Class).Marks)
+25
View File
@@ -0,0 +1,25 @@
// A LIST used as a stack: APPEND to push, REMOVE the last to pop. Balanced brackets.
// EXPECT: TRUE
// EXPECT: FALSE
// EXPECT: FALSE
FUNCTION BALANCED(S : STRING) RETURNS BOOL
DECLARE Stack : LIST OF CHAR
DECLARE Pairs : HASHMAP OF CHAR : CHAR
Pairs[')'] <- '('
Pairs[']'] <- '['
Pairs['}'] <- '{'
FOR EACH C IN S
IF C == '(' OR C == '[' OR C == '{' THEN
APPEND(Stack, C)
ELSEIF C IN Pairs THEN
IF LENGTH(Stack) == 0 OR Stack[LENGTH(Stack) - 1] != Pairs[C] THEN
RETURN FALSE
ENDIF
REMOVE(Stack, LENGTH(Stack) - 1)
ENDIF
NEXT C
RETURN LENGTH(Stack) == 0
ENDFUNCTION
OUTPUT BALANCED("{[()()]}")
OUTPUT BALANCED("([)]")
OUTPUT BALANCED("((")
+30
View File
@@ -0,0 +1,30 @@
// Graph flavor: breadth-first search with a LIST as a queue (REMOVE at 0) and a SET of seen nodes.
// Finding: a neighbour cannot be called Next, because names ignore case and NEXT is a keyword.
// EXPECT: [0, 1, 2, 3, 4]
FUNCTION BFS(Adj : HASHMAP OF INTEGER : LIST OF INTEGER, Start : INTEGER) RETURNS LIST OF INTEGER
DECLARE Queue, Order : LIST OF INTEGER
DECLARE Seen : SET OF INTEGER
APPEND(Queue, Start)
ADD(Seen, Start)
WHILE LENGTH(Queue) > 0
DECLARE Node : INTEGER
Node <- Queue[0]
REMOVE(Queue, 0)
APPEND(Order, Node)
IF Node IN Adj THEN
FOR EACH Nb IN Adj[Node]
IF NOT (Nb IN Seen) THEN
ADD(Seen, Nb)
APPEND(Queue, Nb)
ENDIF
NEXT Nb
ENDIF
ENDWHILE
RETURN Order
ENDFUNCTION
DECLARE G : HASHMAP OF INTEGER : LIST OF INTEGER
G[0] <- [1, 2]
G[1] <- [3]
G[2] <- [3, 4]
G[3] <- [4]
OUTPUT BFS(G, 0)
+49
View File
@@ -0,0 +1,49 @@
// Simulation flavor: one step of Conway's Life on a small two-dimensional ARRAY; a blinker turns.
// Finding: the function cannot be called STEP, nor the new grid Next: both are keywords, whatever their case.
// EXPECT: 0 1 0
// EXPECT: 0 1 0
// EXPECT: 0 1 0
FUNCTION NEIGHBOURS(G : ARRAY[3, 3] OF INTEGER, R : INTEGER, C : INTEGER) RETURNS INTEGER
DECLARE N : INTEGER
N <- 0
FOR DR <- -1 TO 2
FOR DC <- -1 TO 2
IF NOT (DR == 0 AND DC == 0) THEN
IF R + DR >= 0 AND R + DR < ROWS(G) AND C + DC >= 0 AND C + DC < COLUMNS(G) THEN
N <- N + G[R + DR, C + DC]
ENDIF
ENDIF
NEXT DC
NEXT DR
RETURN N
ENDFUNCTION
FUNCTION LIFE_STEP(G : ARRAY[3, 3] OF INTEGER) RETURNS ARRAY[3, 3] OF INTEGER
DECLARE After : ARRAY[3, 3] OF INTEGER
FOR R <- 0 TO 3
FOR C <- 0 TO 3
DECLARE N : INTEGER
N <- NEIGHBOURS(G, R, C)
IF G[R, C] == 1 AND (N == 2 OR N == 3) THEN
After[R, C] <- 1
ELSEIF G[R, C] == 0 AND N == 3 THEN
After[R, C] <- 1
ELSE
After[R, C] <- 0
ENDIF
NEXT C
NEXT R
RETURN After
ENDFUNCTION
DECLARE G : ARRAY[3, 3] OF INTEGER
FOR R <- 0 TO 3
FOR C <- 0 TO 3
G[R, C] <- 0
NEXT C
NEXT R
G[1, 0] <- 1
G[1, 1] <- 1
G[1, 2] <- 1
G <- LIFE_STEP(G)
FOR R <- 0 TO 3
OUTPUT G[R, 0], " ", G[R, 1], " ", G[R, 2]
NEXT R
+29
View File
@@ -0,0 +1,29 @@
// Statistics flavor: mean and variance in one pass (Welford), returning two values through a record.
// EXPECT: 5.0
// EXPECT: 4.0
TYPE Stats
DECLARE Mean : FLOAT
DECLARE Variance : FLOAT
ENDTYPE
FUNCTION RUNNING(Xs : LIST OF FLOAT) RETURNS Stats
REQUIRE LENGTH(Xs) >= 2
DECLARE N : INTEGER
DECLARE Mean, M2, Delta : FLOAT
DECLARE Out : Stats
N <- 0
Mean <- 0.0
M2 <- 0.0
FOR EACH X IN Xs
N <- N + 1
Delta <- X - Mean
Mean <- Mean + Delta / N
M2 <- M2 + Delta * (X - Mean)
NEXT X
Out.Mean <- Mean
Out.Variance <- M2 / N
RETURN Out
ENDFUNCTION
DECLARE S : Stats
S <- RUNNING([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])
OUTPUT S.Mean
OUTPUT S.Variance
+17
View File
@@ -0,0 +1,17 @@
// CASE with half-open ranges on a FLOAT, and OTHERWISE.
// EXPECT: A
// EXPECT: B
// EXPECT: F
FUNCTION GRADE(Score : FLOAT) RETURNS STRING
DECLARE G : STRING
CASE OF Score
90 TO 101 : G <- "A"
80 TO 90 : G <- "B"
70 TO 80 : G <- "C"
OTHERWISE : G <- "F"
ENDCASE
RETURN G
ENDFUNCTION
OUTPUT GRADE(95.5)
OUTPUT GRADE(80.0)
OUTPUT GRADE(12.0)
+10
View File
@@ -0,0 +1,10 @@
// REPEAT ... UNTIL with INPUT, validating until a legal value arrives.
// INPUT: 55
// INPUT: -3
// INPUT: 20
// EXPECT: 20
DECLARE Threshold : INTEGER
REPEAT
INPUT Threshold
UNTIL Threshold >= 0 AND Threshold <= 40
OUTPUT Threshold
+16
View File
@@ -0,0 +1,16 @@
// Kadane: find the best, with an early return for the empty case.
// EXPECT: 6
// EXPECT: -1
FUNCTION MAX_SUBARRAY(Xs : LIST OF INTEGER) RETURNS INTEGER
REQUIRE LENGTH(Xs) >= 1
DECLARE Best, Here : INTEGER
Best <- Xs[0]
Here <- Xs[0]
FOR I <- 1 TO LENGTH(Xs)
Here <- MAX(Xs[I], Here + Xs[I])
Best <- MAX(Best, Here)
NEXT I
RETURN Best
ENDFUNCTION
OUTPUT MAX_SUBARRAY([-2, 1, -3, 4, -1, 2, 1, -5, 4])
OUTPUT MAX_SUBARRAY([-1])
+18
View File
@@ -0,0 +1,18 @@
// A HASHMAP from value to position: the lookup pattern every interview asks for.
// EXPECT: [0, 2]
// EXPECT: []
FUNCTION TWO_SUM(Xs : LIST OF INTEGER, Target : INTEGER) RETURNS LIST OF INTEGER
DECLARE Seen : HASHMAP OF INTEGER : INTEGER
DECLARE Answer : LIST OF INTEGER
FOR I <- 0 TO LENGTH(Xs)
IF Target - Xs[I] IN Seen THEN
APPEND(Answer, Seen[Target - Xs[I]])
APPEND(Answer, I)
RETURN Answer
ENDIF
Seen[Xs[I]] <- I
NEXT I
RETURN Answer
ENDFUNCTION
OUTPUT TWO_SUM([3, 5, 7, 9], 10)
OUTPUT TWO_SUM([1, 2], 10)
+17
View File
@@ -0,0 +1,17 @@
// SETs: fixed with DEFINE, growing with ADD, membership, LENGTH, SORTED.
// EXPECT: TRUE
// EXPECT: FALSE
// EXPECT: 4
// EXPECT: ["ACG", "CGT", "GTA", "TAC"]
TYPE BaseSet = SET OF CHAR
DEFINE Purines ('A', 'G') : BaseSet
OUTPUT 'A' IN Purines
OUTPUT 'C' IN Purines
DECLARE Seen : SET OF STRING
DECLARE T : STRING
T <- "ACGTACG"
FOR I <- 0 TO LENGTH(T) - 3 + 1
ADD(Seen, SLICE(T, I, I + 3))
NEXT I
OUTPUT LENGTH(Seen)
OUTPUT SORTED(Seen)
+25
View File
@@ -0,0 +1,25 @@
// Number bases: a STRING of digits read left to right, and the reverse with DIV and MOD.
// EXPECT: 13
// EXPECT: 1101
FUNCTION FROM_BINARY(S : STRING) RETURNS INTEGER
DECLARE V : INTEGER
V <- 0
FOR EACH C IN S
V <- V * 2 + TO_INTEGER(C)
NEXT C
RETURN V
ENDFUNCTION
FUNCTION TO_BINARY(N : INTEGER) RETURNS STRING
REQUIRE N >= 1
DECLARE S : STRING
DECLARE M : INTEGER
S <- ""
M <- N
WHILE M > 0
S <- TO_STRING(M MOD 2) & S
M <- M DIV 2
ENDWHILE
RETURN S
ENDFUNCTION
OUTPUT FROM_BINARY("1101")
OUTPUT TO_BINARY(13)
+17
View File
@@ -0,0 +1,17 @@
// Randomness: RANDINT in a loop and a tally; the lab seeds its random numbers so a run repeats.
// EXPECT: 100
DECLARE Tally : ARRAY[7] OF INTEGER
DECLARE Total : INTEGER
FOR F <- 0 TO 7
Tally[F] <- 0
NEXT F
FOR Roll <- 0 TO 100
DECLARE Face : INTEGER
Face <- RANDINT(1, 7)
Tally[Face] <- Tally[Face] + 1
NEXT Roll
Total <- 0
FOR F <- 1 TO 7
Total <- Total + Tally[F]
NEXT F
OUTPUT Total
+17
View File
@@ -0,0 +1,17 @@
// What the notation offers for text: SLICE, &, UPPER, LOWER, ORD, CHR, TO_STRING, TO_INTEGER.
// EXPECT: ATT
// EXPECT: GATTACA!
// EXPECT: hello
// EXPECT: DNA
// EXPECT: 65
// EXPECT: B
// EXPECT: 42 apples
// EXPECT: 43
OUTPUT SLICE("GATTACA", 1, 4)
OUTPUT "GATTACA" & "!"
OUTPUT LOWER("HeLLo")
OUTPUT UPPER("dna")
OUTPUT ORD('A')
OUTPUT CHR(66)
OUTPUT TO_STRING(42) & " apples"
OUTPUT TO_INTEGER("42") + 1
+20
View File
@@ -0,0 +1,20 @@
// A HASHMAP of LISTs built up, then printed in sorted key order with KEYS.
// Finding: the list cannot be called Keys, because built-in names are reserved whatever their case.
// EXPECT: a: [1, 3]
// EXPECT: b: [2]
DECLARE Groups : HASHMAP OF STRING : LIST OF INTEGER
DECLARE Ks : LIST OF STRING
DECLARE Names : LIST OF STRING
DECLARE Values : LIST OF INTEGER
Names <- ["a", "b", "a"]
Values <- [1, 2, 3]
FOR I <- 0 TO LENGTH(Names)
IF NOT (Names[I] IN Groups) THEN
Groups[Names[I]] <- []
ENDIF
APPEND(Groups[Names[I]], Values[I])
NEXT I
Ks <- KEYS(Groups)
FOR EACH K IN Ks
OUTPUT K & ": " & TO_STRING(Groups[K])
NEXT K
+22
View File
@@ -0,0 +1,22 @@
// BYREF versus by value, side by side.
// EXPECT: 2 1
// EXPECT: 2 1
PROCEDURE SWAP(BYREF X : INTEGER, BYREF Y : INTEGER)
DECLARE Temp : INTEGER
Temp <- X
X <- Y
Y <- Temp
ENDPROCEDURE
PROCEDURE TRY_SWAP(X : INTEGER, Y : INTEGER)
DECLARE Temp : INTEGER
Temp <- X
X <- Y
Y <- Temp
ENDPROCEDURE
DECLARE A, B : INTEGER
A <- 1
B <- 2
CALL SWAP(A, B)
OUTPUT A, " ", B
CALL TRY_SWAP(A, B)
OUTPUT A, " ", B
+11
View File
@@ -0,0 +1,11 @@
// The reasoning sublanguage used as values: FORALL, EXISTS, SUM, and a set written by rule.
// EXPECT: TRUE
// EXPECT: FALSE
// EXPECT: 15
// EXPECT: 3
DECLARE L : LIST OF INTEGER
L <- [1, 2, 3, 4, 5]
OUTPUT FORALL K IN [0, LENGTH(L)) : L[K] > 0
OUTPUT EXISTS K IN [0, LENGTH(L)) : L[K] > 10
OUTPUT SUM K IN [0, LENGTH(L)) : L[K]
OUTPUT LENGTH({ K IN [0, LENGTH(L)) : L[K] MOD 2 == 1 })
+7
View File
@@ -0,0 +1,7 @@
// What FLOAT does and does not promise.
// EXPECT: FALSE
// EXPECT: TRUE
// EXPECT: 0.30000000000000004
OUTPUT 0.1 + 0.2 == 0.3
OUTPUT ABS((0.1 + 0.2) - 0.3) < 0.000001
OUTPUT 0.1 + 0.2
+38
View File
@@ -0,0 +1,38 @@
// Linear search three ways: with a flag, with BREAK, with RETURN; all three agree.
// EXPECT: 2 2 2
// EXPECT: -1 -1 -1
FUNCTION FIND_FLAG(Xs : LIST OF INTEGER, X : INTEGER) RETURNS INTEGER
DECLARE Found, I : INTEGER
Found <- -1
I <- 0
WHILE I < LENGTH(Xs) AND Found == -1
IF Xs[I] == X THEN
Found <- I
ENDIF
I <- I + 1
ENDWHILE
RETURN Found
ENDFUNCTION
FUNCTION FIND_BREAK(Xs : LIST OF INTEGER, X : INTEGER) RETURNS INTEGER
DECLARE Found : INTEGER
Found <- -1
FOR I <- 0 TO LENGTH(Xs)
IF Xs[I] == X THEN
Found <- I
BREAK
ENDIF
NEXT I
RETURN Found
ENDFUNCTION
FUNCTION FIND_RETURN(Xs : LIST OF INTEGER, X : INTEGER) RETURNS INTEGER
FOR I <- 0 TO LENGTH(Xs)
IF Xs[I] == X THEN
RETURN I
ENDIF
NEXT I
RETURN -1
ENDFUNCTION
DECLARE L : LIST OF INTEGER
L <- [4, 8, 15, 16]
OUTPUT FIND_FLAG(L, 15), " ", FIND_BREAK(L, 15), " ", FIND_RETURN(L, 15)
OUTPUT FIND_FLAG(L, 7), " ", FIND_BREAK(L, 7), " ", FIND_RETURN(L, 7)
+37
View File
@@ -0,0 +1,37 @@
// A state machine over a STRING: does a token look like a decimal number? CASE on a STRING state.
// EXPECT: TRUE
// EXPECT: TRUE
// EXPECT: FALSE
// EXPECT: FALSE
FUNCTION IS_NUMBER(S : STRING) RETURNS BOOL
DECLARE State : STRING
State <- "start"
FOR EACH C IN S
CASE OF State
"start" : IF C >= '0' AND C <= '9' THEN
State <- "int"
ELSE
State <- "bad"
ENDIF
"int" : IF C == '.' THEN
State <- "dot"
ELSEIF NOT (C >= '0' AND C <= '9') THEN
State <- "bad"
ENDIF
"dot" : IF C >= '0' AND C <= '9' THEN
State <- "frac"
ELSE
State <- "bad"
ENDIF
"frac" : IF NOT (C >= '0' AND C <= '9') THEN
State <- "bad"
ENDIF
OTHERWISE : State <- "bad"
ENDCASE
NEXT C
RETURN State == "int" OR State == "frac"
ENDFUNCTION
OUTPUT IS_NUMBER("42")
OUTPUT IS_NUMBER("3.14")
OUTPUT IS_NUMBER("3.")
OUTPUT IS_NUMBER("x1")
+20
View File
@@ -0,0 +1,20 @@
// Edge semantics every reader should know: chained comparisons, string order, MIN of a list, SORT of text, nested lists.
// EXPECT: TRUE
// EXPECT: TRUE
// EXPECT: 1
// EXPECT: ["apple", "fig", "pear"]
// EXPECT: 6
// EXPECT: FALSE
DECLARE I : INTEGER
I <- 3
OUTPUT 0 <= I < 5
OUTPUT "apple" < "banana"
OUTPUT MIN([4, 1, 9])
DECLARE Fruit : LIST OF STRING
Fruit <- ["pear", "apple", "fig"]
SORT(Fruit)
OUTPUT Fruit
DECLARE Grid : LIST OF LIST OF INTEGER
Grid <- [[1, 2, 3], [4, 5, 6]]
OUTPUT Grid[1][2]
OUTPUT "" IN Fruit
+7
View File
@@ -0,0 +1,7 @@
// A run that stops: reading one past the end. The lab names the position and the length.
// STOPS: index
DECLARE T : STRING
T <- "GATTACA"
FOR I <- 0 TO LENGTH(T) + 1
OUTPUT T[I]
NEXT I
+11
View File
@@ -0,0 +1,11 @@
// A run that stops: dividing by zero inside a function; the lab blames the line and shows the values.
// STOPS: divide
FUNCTION MEAN(Xs : LIST OF INTEGER) RETURNS FLOAT
DECLARE Total : INTEGER
Total <- 0
FOR EACH X IN Xs
Total <- Total + X
NEXT X
RETURN Total / LENGTH(Xs)
ENDFUNCTION
OUTPUT MEAN([])
+6
View File
@@ -0,0 +1,6 @@
// A run that stops: recursion with no base case.
// STOPS: depth
FUNCTION DOWN(N : INTEGER) RETURNS INTEGER
RETURN DOWN(N - 1)
ENDFUNCTION
OUTPUT DOWN(10)
+4
View File
@@ -0,0 +1,4 @@
// A run that stops: a declared name read before anything is stored in it.
// STOPS: unset
DECLARE Total : INTEGER
OUTPUT Total + 1
+6
View File
@@ -0,0 +1,6 @@
// A run that stops: a FOR counter read after its loop.
// STOPS: unset
DECLARE I : INTEGER
FOR I <- 0 TO 3
NEXT I
OUTPUT I
+14
View File
@@ -0,0 +1,14 @@
// What the checker refuses before anything runs. Each line below is one mistake the guide names.
// ERRORS: 5
DECLARE T : STRING
DECLARE N : INTEGER
T <- "GATTACA"
N <- 7 / 2
T[0] <- 'C'
IF T[0] == "G" THEN
OUTPUT "g"
ENDIF
FOR I <- 0 TO 3
I <- I + 1
NEXT I
N = 5
+11
View File
@@ -0,0 +1,11 @@
// CONTINUE skips a pass; here, unknown bases are not evidence.
// EXPECT: 6
DECLARE Total : INTEGER
Total <- 0
FOR EACH B IN "GATNACA"
IF B == 'N' THEN
CONTINUE
ENDIF
Total <- Total + 1
NEXT B
OUTPUT Total
+29
View File
@@ -0,0 +1,29 @@
// CONSTANT names a number once; STEP walks codon starts; a HASHMAP codon table; a SET of stops.
// EXPECT: MAK
CONSTANT BasesPerCodon = 3
FUNCTION TRANSLATE(T : STRING) RETURNS STRING
DECLARE Table : HASHMAP OF STRING : CHAR
DECLARE Stops : SET OF STRING
DECLARE Out : STRING
Table["ATG"] <- 'M'
Table["GCC"] <- 'A'
Table["AAA"] <- 'K'
ADD(Stops, "TAG")
ADD(Stops, "TAA")
ADD(Stops, "TGA")
Out <- ""
FOR I <- 0 TO LENGTH(T) - BasesPerCodon + 1 STEP BasesPerCodon
DECLARE Codon : STRING
Codon <- SLICE(T, I, I + BasesPerCodon)
IF Codon IN Stops THEN
BREAK
ENDIF
IF Codon IN Table THEN
Out <- Out & Table[Codon]
ELSE
Out <- Out & '?'
ENDIF
NEXT I
RETURN Out
ENDFUNCTION
OUTPUT TRANSLATE("ATGGCCAAATAGTTT")
+24
View File
@@ -0,0 +1,24 @@
// The four text built-ins added after the stress test: SPLIT, JOIN, INDEX_OF, REPLACE.
// EXPECT: ["the", "quick", "", "brown", "fox"]
// EXPECT: the quick brown fox
// EXPECT: 3 -1 2
// EXPECT: GAUUACA
// EXPECT: read_1:GAT
FUNCTION WORDS(S : STRING) RETURNS LIST OF STRING
DECLARE Out : LIST OF STRING
FOR EACH W IN SPLIT(S, " ")
IF LENGTH(W) > 0 THEN
APPEND(Out, W)
ENDIF
NEXT W
RETURN Out
ENDFUNCTION
DECLARE S : STRING
S <- "the quick brown fox"
OUTPUT SPLIT(S, " ")
OUTPUT JOIN(WORDS(S), " ")
OUTPUT INDEX_OF("GATTACA", "TAC"), " ", INDEX_OF("GATTACA", 'N'), " ", INDEX_OF([4, 8, 15], 15)
OUTPUT REPLACE("GATTACA", "T", "U")
DECLARE Fields : LIST OF STRING
Fields <- SPLIT("read_1\tGAT\t30", "\t")
OUTPUT Fields[0], ":", Fields[1]
+7
View File
@@ -0,0 +1,7 @@
// A keyword used as a name is one error per name, not a cascade: the rest of the program still parses.
// ERRORS: 2
DECLARE Step : INTEGER
Step <- 1
FOR EACH Next IN [1, 2]
OUTPUT Next + Step
NEXT Next
+17
View File
@@ -0,0 +1,17 @@
// The numeric built-ins added for mathematics, physics, chemistry and statistics.
// EXPECT: 2 3 -3
// EXPECT: 1.0 1.0
// EXPECT: 10 120
// EXPECT: 6 2.5 2.0 2.5
// EXPECT: 4.571428571428571 2.138089935299395
// EXPECT: 32 5.0
// EXPECT: [0.0, 0.0, 0.0] 4
// EXPECT: 3.0 3.142
OUTPUT FLOOR(2.7), " ", CEIL(2.1), " ", FLOOR(-2.3)
OUTPUT ROUND(SIN(PI() / 2) * 1000) / 1000, " ", ROUND(COS(0.0) * 1000) / 1000
OUTPUT CHOOSE(5, 2), " ", CHOOSE(10, 3)
OUTPUT SUM_OF([1, 2, 3]), " ", MEAN_OF([1, 2, 3, 4]), " ", MEDIAN_OF([3, 1, 2]), " ", MEDIAN_OF([4, 1, 3, 2])
OUTPUT VARIANCE_OF([2, 4, 4, 4, 5, 5, 7, 9]), " ", STDEV_OF([2, 4, 4, 4, 5, 5, 7, 9])
OUTPUT DOT([1, 2, 3], [4, 5, 6]), " ", NORM_OF([3.0, 4.0])
OUTPUT FILL(3, 0.0), " ", LENGTH(FILL(4, 'A'))
OUTPUT LOG10(1000.0), " ", ROUND(ATAN2(1.0, 1.0) * 4 * 1000) / 1000
+30
View File
@@ -0,0 +1,30 @@
// GPU thinking: a PARALLEL FOR whose passes each own one slot (a map), then a sequential reduce.
// EXPECT: [1.0, 4.0, 9.0, 16.0]
// EXPECT: 30.0
// EXPECT: [5, 7, 9]
FUNCTION SQUARES(Xs : LIST OF FLOAT) RETURNS LIST OF FLOAT
DECLARE Out : LIST OF FLOAT
Out <- FILL(LENGTH(Xs), 0.0)
PARALLEL FOR I <- 0 TO LENGTH(Xs)
DECLARE X : FLOAT
X <- Xs[I]
Out[I] <- X * X
NEXT I
RETURN Out
ENDFUNCTION
FUNCTION ADD_VECTORS(A : LIST OF INTEGER, B : LIST OF INTEGER) RETURNS LIST OF INTEGER
REQUIRE LENGTH(A) == LENGTH(B)
DECLARE C : LIST OF INTEGER
C <- FILL(LENGTH(A), 0)
PARALLEL FOR I <- 0 TO LENGTH(A)
C[I] <- A[I] + B[I]
NEXT I
RETURN C
ENDFUNCTION
DECLARE Sq : LIST OF FLOAT
Sq <- SQUARES([1.0, 2.0, 3.0, 4.0])
OUTPUT Sq
OUTPUT SUM_OF(Sq)
OUTPUT ADD_VECTORS([1, 2, 3], [4, 5, 6])
+10
View File
@@ -0,0 +1,10 @@
// A shared running total inside a PARALLEL FOR is a data race, and the lab says so.
// STOPS: race
DECLARE Xs : LIST OF FLOAT
DECLARE Acc : FLOAT
Xs <- [1.0, 2.0, 3.0]
Acc <- 0.0
PARALLEL FOR I <- 0 TO LENGTH(Xs)
Acc <- Acc + Xs[I]
NEXT I
OUTPUT Acc
@@ -0,0 +1,8 @@
// Reading a neighbour that another pass writes is also a race, even with no shared scalar.
// STOPS: race
DECLARE A : LIST OF INTEGER
A <- [5, 6, 7, 8]
PARALLEL FOR I <- 0 TO 3
A[I] <- A[I + 1]
NEXT I
OUTPUT A
+25
View File
@@ -0,0 +1,25 @@
// Physics: where a projectile lands, by stepping Newton's laws, against the closed form.
// EXPECT: 10.2 10.2
FUNCTION RANGE_BY_STEPS(Speed : FLOAT, Angle : FLOAT, Dt : FLOAT) RETURNS FLOAT
DECLARE X, Y, Vx, Vy, G : FLOAT
G <- 9.81
X <- 0.0
Y <- 0.0
Vx <- Speed * COS(Angle)
Vy <- Speed * SIN(Angle)
REPEAT
X <- X + Vx * Dt
Y <- Y + Vy * Dt
Vy <- Vy - G * Dt
UNTIL Y <= 0.0
RETURN X
ENDFUNCTION
FUNCTION RANGE_EXACT(Speed : FLOAT, Angle : FLOAT) RETURNS FLOAT
RETURN Speed * Speed * SIN(2.0 * Angle) / 9.81
ENDFUNCTION
DECLARE Angle : FLOAT
Angle <- PI() / 4
// Stepping forward overshoots a little, so compare to one decimal place.
OUTPUT ROUND(RANGE_BY_STEPS(10.0, Angle, 0.0001) * 10) / 10, " ", ROUND(RANGE_EXACT(10.0, Angle) * 10) / 10
+22
View File
@@ -0,0 +1,22 @@
// Probability and chemistry: a binomial probability from CHOOSE, and pH from LOG10.
// EXPECT: 0.2461
// EXPECT: 1.0
// EXPECT: 3.0 7.0
FUNCTION BINOMIAL(N : INTEGER, K : INTEGER, P : FLOAT) RETURNS FLOAT
REQUIRE 0 <= K AND K <= N
RETURN CHOOSE(N, K) * P ^ K * (1.0 - P) ^ (N - K)
ENDFUNCTION
FUNCTION PH(Concentration : FLOAT) RETURNS FLOAT
REQUIRE Concentration > 0.0
RETURN -LOG10(Concentration)
ENDFUNCTION
DECLARE Sum : FLOAT
Sum <- 0.0
FOR K <- 0 TO 11
Sum <- Sum + BINOMIAL(10, K, 0.5)
NEXT K
OUTPUT ROUND(BINOMIAL(10, 5, 0.5) * 10000) / 10000
OUTPUT ROUND(Sum * 1000) / 1000
OUTPUT PH(0.001), " ", PH(0.0000001)
+32
View File
@@ -0,0 +1,32 @@
// Declaring with a starting value on one line, beside the two-line form, at the top, in a routine, and in a loop.
// EXPECT: 3
// EXPECT: 7 3
// EXPECT: a <- b
// EXPECT: 6
// EXPECT: 2
FUNCTION COUNT_BASE(B : CHAR, T : STRING) RETURNS INTEGER
DECLARE N : INTEGER <- LENGTH(T)
DECLARE Total : INTEGER <- 0 // the running total
FOR I <- 0 TO N
IF T[I] == B THEN
Total <- Total + 1
ENDIF
NEXT I
RETURN Total
ENDFUNCTION
DECLARE Seq : STRING <- "GATTACA"
DECLARE Count : INTEGER
Count <- COUNT_BASE('A', Seq)
OUTPUT Count
DECLARE Len : INTEGER <- LENGTH(Seq)
OUTPUT Len, " ", Count
DECLARE Arrow : STRING <- "a <- b"
OUTPUT Arrow
DECLARE Sum : INTEGER <- 0
FOR K <- 0 TO 3
DECLARE Twice : INTEGER <- 2 * K
Sum <- Sum + Twice
NEXT K
OUTPUT Sum
DECLARE Xs : LIST OF INTEGER <- [4, 9]
OUTPUT LENGTH(Xs)
+117
View File
@@ -0,0 +1,117 @@
// The lesson corpus verifies the interpreter: the dependency map must be strict and every program must run.
import test from 'node:test';
import assert from 'node:assert/strict';
import { LESSONS, CONCEPTS, CONCEPT_BY_ID, FLAVORS, teacherOf, prerequisitesOf, depthOf, allPrerequisitesOf, statusOf, solutionOf, starterOf } from '../dist/curriculum/index.js';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
import { problemById } from '../dist/core/catalog.js';
const conceptIds = new Set(CONCEPTS.map((c) => c.id));
const flavorIds = new Set(FLAVORS.map((f) => f.id));
test('lesson ids are unique and every concept id is real', () => {
const ids = new Set();
for (const L of LESSONS) {
assert.ok(!ids.has(L.id), `duplicate lesson id ${L.id}`); ids.add(L.id);
for (const c of [...L.teaches, ...L.requires]) assert.ok(conceptIds.has(c), `${L.id} names unknown concept ${c}`);
assert.ok(L.teaches.length >= 1, `${L.id} teaches nothing`);
assert.ok(L.purpose.length > 20 && L.explanation.length >= 3 && L.checks.length >= 2 && L.flavors.length >= 2, `${L.id} is thin`);
for (const f of L.flavors) assert.ok(flavorIds.has(f.id), `${L.id} has unknown flavor ${f.id}`);
for (const c of L.checks) assert.ok(c.answer >= 0 && c.answer < c.options.length, `${L.id} check answer out of range`);
for (const p of L.problems || []) assert.ok(problemById(p), `${L.id} points at missing problem ${p}`);
if (L.showAsmOf) assert.ok(problemById(L.showAsmOf)?.sources.asm, `${L.id} shows asm of a problem without any`);
}
});
test('every concept is taught by exactly one lesson, and every concept is used', () => {
for (const c of CONCEPTS) {
const teachers = LESSONS.filter((l) => l.teaches.includes(c.id));
assert.equal(teachers.length, 1, `${c.id} is taught by ${teachers.map((l) => l.id).join(', ') || 'nobody'}`);
}
// every foundations and core concept must feed a later lesson; only advanced concepts may be frontier
const required = new Set(LESSONS.flatMap((l) => l.requires));
for (const L of LESSONS.filter((l) => l.stage !== 'advanced')) for (const c of L.teaches) assert.ok(required.has(c), `${c} (from ${L.id}) is never required by a later lesson`);
});
test('the dependency map is strict: every requirement is taught by an earlier lesson, and there are no cycles', () => {
const seen = new Set();
for (const L of LESSONS) {
for (const c of L.requires) {
const t = teacherOf(c);
assert.ok(t, `${L.id} requires ${c}, which nothing teaches`);
assert.ok(seen.has(t.id), `${L.id} requires ${c}, taught by ${t.id}, which comes later in the order`);
assert.ok(!L.teaches.includes(c), `${L.id} both teaches and requires ${c}`);
}
seen.add(L.id);
}
for (const L of LESSONS) assert.ok(depthOf(L) >= 0, `${L.id} is in a cycle`);
const roots = LESSONS.filter((l) => l.requires.length === 0);
assert.equal(roots.length, 1, `exactly one lesson should assume nothing: ${roots.map((l) => l.id).join(', ')}`);
for (const L of LESSONS.slice(1)) assert.ok(allPrerequisitesOf(L).some((p) => p.id === roots[0].id), `${L.id} does not trace back to ${roots[0].id}`);
});
test('a lesson is available exactly when every prerequisite lesson is done', () => {
const none = { done: [], checks: {} };
const first = LESSONS[0];
assert.equal(statusOf(first, none), 'available');
assert.equal(statusOf(LESSONS[LESSONS.length - 1], none), 'locked');
const last = LESSONS[LESSONS.length - 1];
const all = { done: allPrerequisitesOf(last).map((l) => l.id), checks: {} };
assert.equal(statusOf(last, all), 'available');
const direct = prerequisitesOf(last)[0].id;
const butOne = { done: all.done.filter((id) => id !== direct), checks: {} };
assert.notEqual(statusOf(last, butOne), 'available');
const order = ['foundations', 'core', 'advanced'];
for (const L of LESSONS) for (const p of prerequisitesOf(L)) assert.ok(order.indexOf(p.stage) <= order.indexOf(L.stage), `${L.id} depends on a later stage`);
});
function runProgram(label, prog) {
const st = studyOf(solutionOf(prog));
const errs = st.diagnostics.filter((d) => d.severity === 'error');
assert.equal(errs.length, 0, `${label}: ${errs.map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | ')}`);
const tr = runPseudo(st, { input: prog.input || [], maxSteps: 200000 });
const out = tr.output.filter((l) => !/^\w+ returned /.test(l));
if (prog.stops) { assert.equal(tr.error?.kind, prog.stops, `${label}: expected a ${prog.stops} stop, got ${tr.error?.kind || 'none'}`); }
else assert.equal(tr.error, null, `${label}: ${tr.error?.message}`);
assert.deepEqual(out, prog.expect, label);
return st;
}
for (const L of LESSONS) {
test(`lesson ${L.id}: the example and every flavor run and print what they say`, () => {
runProgram(`${L.id}/example`, L.example);
for (const f of L.flavors) {
runProgram(`${L.id}/${f.id}`, f.solution);
const starter = f.starter ?? starterOf(f.solution);
assert.ok(starter.length > 0);
if (!f.starter) assert.ok(/\/\/ TODO:/.test(starter) || !/\/\/ ~/.test(f.solution.pseudo), `${L.id}/${f.id}: starter has no blanks`);
assert.ok(!/\/\/ ~/.test(starter), `${L.id}/${f.id}: marker leaked into the starter`);
assert.ok(!/\/\/ ~/.test(solutionOf(f.solution)), `${L.id}/${f.id}: marker leaked into the solution`);
}
});
}
test('a starter blanks exactly the marked lines', () => {
const p = { pseudo: 'A <- 1\nB <- 2 // ~ set B\nC <- 3', expect: [] };
assert.equal(starterOf(p), 'A <- 1\n// TODO: set B\nC <- 3');
assert.equal(solutionOf(p), 'A <- 1\nB <- 2\nC <- 3');
});
test('every lesson uses only keywords its prerequisites (or it) have introduced', () => {
// a rough but real check: the statement keyword that introduces a concept must not appear
// in a lesson that neither teaches nor (transitively) requires that concept
const introduces = {
'IF': 'selection', 'FOR': 'for-loop', 'WHILE': 'while', 'REPEAT': 'repeat', 'FUNCTION': 'function', 'PROCEDURE': 'procedure-sub', 'BYREF': 'byref', 'CASE': 'case',
'HASHMAP': 'hashmap', 'SET': 'set', 'TYPE': 'record', 'ARRAY': 'array', 'INVARIANT': 'invariant', 'ENSURE': 'specification', 'BREAK': 'early-exit', 'CONTINUE': 'early-exit', 'INPUT': 'io',
};
const exempt = { 'sets': ['TYPE'], 'what-stops-a-run': ['FUNCTION'] };
const violations = []; // TYPE … = SET OF is the set lesson's own form; the stops lesson reviews a function
for (const L of LESSONS) {
const held = new Set([...L.teaches, ...allPrerequisitesOf(L).flatMap((p) => p.teaches), ...L.requires]);
const programs = [L.example.pseudo, ...L.flavors.flatMap((f) => [f.solution.pseudo, f.starter || ''])].join('\n');
for (const [kw, concept] of Object.entries(introduces)) {
if ((exempt[L.id] || []).includes(kw)) continue;
if (new RegExp(`(^|[^\\w])${kw}\\b`, 'm').test(programs.replace(/"[^"]*"/g, '""').replace(/\/\/.*$/gm, '')) && !held.has(concept)) violations.push(`${L.id} uses ${kw} but neither teaches nor requires ${concept}`);
}
}
assert.deepEqual(violations, []);
});
+38
View File
@@ -0,0 +1,38 @@
// Every exercise file is checked here, so adding one is safe.
import test from 'node:test';
import assert from 'node:assert/strict';
import { EXERCISES } from '../dist/exercises/index.js';
import { PROBLEMS, stateForProblem } from '../dist/core/catalog.js';
import { Session } from '../dist/core/session.js';
import template from '../dist/exercises/_template.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
test('exercise ids are unique, URL-safe, and the template is not registered', () => {
const ids = EXERCISES.map((e) => e.id);
assert.equal(new Set(ids).size, ids.length);
for (const id of ids) assert.match(id, /^[a-z0-9]+(-[a-z0-9]+)*$/, id);
assert.ok(!ids.includes(template.id));
});
test('every exercise with an expected answer produces it', () => {
for (const p of PROBLEMS) {
if (!p.expect) continue;
const st = stateForProblem(p);
if (st.sources.pseudo) st.lang = 'pseudo';
const s = new Session(st);
const tr = s.trace();
assert.ok(tr && !tr.error, `${p.id}: ${s.runError || tr?.error?.message}`);
const last = tr.output.at(-1);
assert.ok(last.endsWith(` returned ${p.expect}`), `${p.id}: expected …returned ${p.expect}, got ${last}`);
}
});
test('the template itself is a valid exercise', () => {
const s = new Session(stateForProblem({ ...template, lang: 'pseudo' }));
const tr = s.trace();
assert.ok(tr && !tr.error, s.runError || tr?.error?.message);
assert.ok(tr.output.at(-1).endsWith(`returned ${template.expect}`));
assert.equal(s.proof().report.counts.open + s.proof().report.counts.broken, 0);
});
+90
View File
@@ -0,0 +1,90 @@
// The fuzzer behind the Tests view (core/fuzz.ts): it says what it tried and what it checked, and what it reports
// is what the engines do. Run on every exercise, in pseudo and in Mojo.
import test from 'node:test';
import assert from 'node:assert/strict';
import { Session } from '../dist/core/session.js';
import { PROBLEMS, stateForProblem, problemById } from '../dist/core/catalog.js';
import { STRATEGY } from '../dist/core/fuzz.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
const open = (p, lang, call) => { const st = stateForProblem(p); st.lang = lang; if (call) st.call = call; return new Session(st, { area: lang === 'mojo' ? 'dev' : 'bridge', storageId: null }); };
const ALL = [];
for (const p of PROBLEMS) for (const lang of ['pseudo', 'mojo']) if ((p.sources?.[lang] ?? '').trim()) ALL.push({ p, lang, s: open(p, lang) });
test('every report says what it tried, what it checked, and what happened, and adds up', () => {
let routines = 0;
for (const { p, lang, s } of ALL) {
for (const r of s.fuzz({ budget: 120 })) {
routines++;
const id = `${p.id} ${lang} ${r.name}`;
if (!r.cases.length) { assert.ok(r.problem, `${id}: an untested routine says why`); continue; }
assert.ok(r.checks.length >= 3, `${id}: the checks are listed`);
assert.equal(r.params.length, (r.signature.match(/\(([^)]*)\)/)?.[1].trim() ? r.signature.match(/\(([^)]*)\)/)[1].split(',').length : 0), `${id}: one generator per parameter`);
for (const prm of r.params) assert.ok(prm.how.length > 10, `${id}: ${prm.name}'s generator is described`);
assert.equal(r.strategies.reduce((a, x) => a + x.count, 0), r.cases.length, `${id}: the strategies' counts add up to the cases`);
for (const x of r.strategies) assert.ok(STRATEGY[x.id], `${id}: ${x.id} is a known strategy`);
assert.equal(Object.values(r.counts).reduce((a, b) => a + b, 0), r.cases.length, `${id}: the outcomes add up`);
const tried = r.cases.filter((c) => c.strategy !== 'shrink' && c.strategy !== 'repeat');
assert.ok(tried.length <= r.budget, `${id}: the budget holds`);
assert.equal(new Set(r.cases.filter((c) => c.strategy !== 'repeat').map((c) => c.call)).size, r.cases.filter((c) => c.strategy !== 'repeat').length, `${id}: no input is tried twice (except to repeat it)`);
r.cases.forEach((c, i) => assert.equal(c.n, i + 1));
for (const c of r.coverage) assert.ok(c.inputs <= r.cases.length && (c.inputs === 0) === (c.hits === 0), `${id}: line ${c.line}'s coverage`);
if (r.cases.some((c) => c.outcome === 'pass')) assert.ok(r.coverage.some((c) => c.inputs > 0), `${id}: passing inputs reach lines`);
for (const c of r.cases) if (c.outcome === 'fail') assert.ok(c.message, `${id}: a failed case says what went wrong`);
for (const f of r.failures) {
assert.equal(f.shrink[0], f.found, `${id}: shrinking starts at the input that found it`);
assert.equal(f.shrink[f.shrink.length - 1], f.call, `${id}: and ends at the one reported`);
}
}
}
assert.ok(routines >= 30, `the exercises have routines to fuzz (${routines})`);
});
test('the same seed makes the same inputs; another seed makes others', () => {
const s = open(problemById('count-base'), 'pseudo');
const a = s.fuzz({ seed: 7, budget: 100 })[0].cases.map((c) => c.call);
const b = new Session(s.state, { storageId: null }).fuzz({ seed: 7, budget: 100 })[0].cases.map((c) => c.call);
const c = s.fuzz({ seed: 8, budget: 100 })[0].cases.map((c) => c.call);
assert.deepEqual(a, b);
assert.notDeepEqual(a, c);
// the edge cases and every short string do not depend on the seed; only the random and mutated ones do
const first = (xs) => xs.slice(0, 40);
assert.deepEqual(first(a), first(c));
});
test('the off-by-one bug is found, shrunk to its smallest input, and stepping that input fails the same way', () => {
const s = open(problemById('match-off-by-one'), 'pseudo');
const [r] = s.fuzz();
assert.ok(r.failures.length >= 1, 'the bug is found');
const f = r.failures[0];
assert.match(f.call, /^MATCH\(/);
assert.ok(f.shrink.length >= 1);
// shrinking never makes an input bigger
const size = (call) => call.replace(/[^ACGTN]/g, '').length;
for (let i = 1; i < f.shrink.length; i++) assert.ok(size(f.shrink[i]) <= size(f.shrink[i - 1]), 'each step is no bigger');
// the stepper on that call stops with the same kind of error, on the same line
const run = open(problemById('match-off-by-one'), 'pseudo', f.call).trace();
assert.ok(run.error, 'the run stops');
assert.equal(run.error.line, f.line);
// and the brief report the home page reads agrees
assert.ok(s.tests()[0].failures.some((x) => x.call === f.call));
});
test('coverage-guided mutation: the inputs that reached new lines are the ones mutated', () => {
const [r] = open(problemById('tally'), 'mojo').fuzz();
const fresh = r.cases.filter((c) => c.newLines?.length);
assert.ok(fresh.length >= 1, 'some inputs reach lines nothing earlier reached');
assert.ok(r.cases.some((c) => c.strategy === 'mutate'), 'and mutations of them are tried');
const reached = new Set(fresh.flatMap((c) => c.newLines));
for (const c of r.coverage) if (c.inputs) assert.ok(reached.has(c.line), `line ${c.line} was first reached by a case marked new`);
});
test('a REQUIRE skips an input instead of failing it, and says so', () => {
const withRequire = ALL.filter(({ lang, s }) => lang === 'pseudo' && s.fuzz({ budget: 120 }).some((r) => r.checks.some((c) => /break a REQUIRE \(/.test(c))));
for (const { s } of withRequire) for (const r of s.fuzz({ budget: 120 })) for (const c of r.cases) if (c.outcome === 'skip') assert.ok(c.kind === 'require' || c.kind === 'declined');
});
test('every report repeats some inputs to check the answer does not change', () => {
for (const { s } of ALL.slice(0, 12)) for (const r of s.fuzz({ budget: 120 })) if (r.counts.pass > 8) assert.ok(r.cases.some((c) => c.strategy === 'repeat'), r.name);
});
+194
View File
@@ -0,0 +1,194 @@
// The programs the sync translators are held to: every exercise with pseudo, every program in tests/corpus/,
// and small fixtures that each exercise one construct. Shared by the translator tests and the coverage report.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { EXERCISES } from '../../dist/exercises/index.js';
const here = path.dirname(fileURLToPath(import.meta.url));
export const corpusDir = path.join(here, '..', 'corpus');
/** One construct each, with the call that runs it. */
export const FIXTURES = {
'fixture:loop-step': [`FUNCTION EVERY_THIRD(L : LIST OF INTEGER) RETURNS INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR I <- 0 TO LENGTH(L) STEP 3
Total <- Total + L[I]
NEXT I
RETURN Total
ENDFUNCTION`, 'EVERY_THIRD([1, 2, 3, 4, 5, 6, 7])'],
'fixture:loop-down': [`FUNCTION COUNTDOWN(N : INTEGER) RETURNS INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR I <- N TO 0 STEP -1
Total <- Total * 2 + I
NEXT I
RETURN Total
ENDFUNCTION`, 'COUNTDOWN(4)'],
'fixture:break-continue': [`FUNCTION FIRST_BIG(L : LIST OF INTEGER, M : INTEGER) RETURNS INTEGER
DECLARE Found : INTEGER
Found <- -1
FOR I <- 0 TO LENGTH(L)
IF L[I] < 0 THEN
CONTINUE
ENDIF
IF L[I] > M THEN
Found <- L[I]
BREAK
ENDIF
NEXT I
RETURN Found
ENDFUNCTION`, 'FIRST_BIG([1, -5, 7, 20], 6)'],
'fixture:early-return': [`FUNCTION HAS_NEG(L : LIST OF INTEGER) RETURNS BOOL
FOR EACH X IN L
IF X < 0 THEN
RETURN TRUE
ENDIF
NEXT X
RETURN FALSE
ENDFUNCTION`, 'HAS_NEG([1, 2, -3])'],
'fixture:min-max': [`FUNCTION SPAN(L : LIST OF INTEGER) RETURNS INTEGER
DECLARE Lo : INTEGER
DECLARE Hi : INTEGER
Lo <- L[0]
Hi <- L[0]
FOR EACH X IN L
Lo <- MIN(Lo, X)
Hi <- MAX(Hi, X + 0)
NEXT X
RETURN Hi - Lo
ENDFUNCTION`, 'SPAN([4, 9, 2, 7])'],
'fixture:string-build': [`FUNCTION DOUBLED(T : STRING) RETURNS STRING
DECLARE Out : STRING
Out <- ""
FOR EACH C IN T
Out <- Out & C & C
NEXT C
RETURN Out
ENDFUNCTION`, 'DOUBLED("GATTACA")'],
'fixture:slice': [`FUNCTION HEAD3(T : STRING) RETURNS STRING
RETURN SLICE(T, 0, 3)
ENDFUNCTION`, 'HEAD3("GATTACA")'],
'fixture:set': [`FUNCTION DISTINCT(T : STRING) RETURNS INTEGER
DECLARE Seen : SET OF CHAR
FOR EACH C IN T
ADD(Seen, C)
NEXT C
RETURN LENGTH(Seen)
ENDFUNCTION`, 'DISTINCT("GATTACA")'],
'fixture:routine-calls': [`FUNCTION DOUBLE(N : INTEGER) RETURNS INTEGER
RETURN N * 2
ENDFUNCTION
FUNCTION QUAD(N : INTEGER) RETURNS INTEGER
RETURN DOUBLE(DOUBLE(N))
ENDFUNCTION`, 'QUAD(3)'],
'fixture:procedure': [`PROCEDURE NOTE_SQUARE(N : INTEGER)
OUTPUT N, " ", N * N
ENDPROCEDURE
PROCEDURE RUN(N : INTEGER)
CALL NOTE_SQUARE(N)
CALL NOTE_SQUARE(N + 1)
ENDPROCEDURE`, 'RUN(3)'],
'fixture:case': [`FUNCTION GRADE(S : INTEGER) RETURNS STRING
DECLARE G : STRING
CASE OF S
90 TO 101 : G <- "A"
80 TO 90 : G <- "B"
OTHERWISE : G <- "C"
ENDCASE
RETURN G
ENDFUNCTION`, 'GRADE(85)'],
'fixture:float-literal': [`FUNCTION HALF(N : INTEGER) RETURNS FLOAT
DECLARE X : FLOAT
X <- 0.5
RETURN X * N
ENDFUNCTION`, 'HALF(3)'],
'fixture:div-mod': [`FUNCTION DIGITS(N : INTEGER) RETURNS INTEGER
DECLARE M : INTEGER
DECLARE C : INTEGER
M <- N
C <- 0
WHILE M > 0
M <- M DIV 10
C <- C + 1
ENDWHILE
RETURN C + N MOD 7
ENDFUNCTION`, 'DIGITS(12345)'],
'fixture:forall': [`FUNCTION SORTED_UP(L : LIST OF INTEGER) RETURNS BOOL
RETURN FORALL K IN [0, LENGTH(L) - 1) : L[K] <= L[K + 1]
ENDFUNCTION`, 'SORTED_UP([1, 2, 3])'],
'fixture:comments': [`// compute it
FUNCTION TWICE(N : INTEGER) RETURNS INTEGER
// set up
DECLARE R : INTEGER
R <- N * 2 // doubled
RETURN R
ENDFUNCTION`, 'TWICE(4)'],
'fixture:list-item': [`FUNCTION BUMP(L : LIST OF INTEGER) RETURNS LIST OF INTEGER
DECLARE Out : LIST OF INTEGER
FOR EACH X IN L
APPEND(Out, X + 1)
NEXT X
Out[0] <- 99
RETURN Out
ENDFUNCTION`, 'BUMP([1, 2, 3])'],
'fixture:value-set-in-branch': [`FUNCTION SIGN_WORD(N : INTEGER) RETURNS STRING
DECLARE W : STRING
IF N < 0 THEN
W <- "minus"
ELSEIF N == 0 THEN
W <- "zero"
ELSE
W <- "plus"
ENDIF
RETURN W
ENDFUNCTION`, 'SIGN_WORD(-4)'],
};
/** Every program: { id, name, pseudo, call, input }. */
export function programs() {
const out = [];
for (const e of EXERCISES) if (e.sources.pseudo) out.push({ id: `ex:${e.id}`, name: e.id, pseudo: e.sources.pseudo, call: e.call ?? null, input: [] });
for (const f of fs.readdirSync(corpusDir).filter((x) => x.endsWith('.pseudo')).sort()) {
const src = fs.readFileSync(path.join(corpusDir, f), 'utf8');
out.push({ id: f, name: f, pseudo: src, call: null, input: [...src.matchAll(/^\/\/ INPUT: (.*)$/gm)].map((m) => m[1]) });
}
for (const [id, [pseudo, call]] of Object.entries(FIXTURES)) out.push({ id, name: id, pseudo, call, input: [] });
return out;
}
/** Remove // comments and blank lines: the lowering strips comments, so a round trip never brings them back. */
export const stripComments = (t) => t.split('\n').map((l) => l.replace(/\s*\/\/.*$/, '')).filter((l) => l.trim()).join('\n');
/** [matho source, a call that runs its last function]. The same sets tests/matho.test.mjs runs. */
export const EQUATIONS = {
'mean-and-variance': ['\\bar{x} = \\frac{1}{n} \\sum_{i=0}^{n-1} x_i\n\\sigma^2 = \\frac{1}{n - 1} \\sum_{i=0}^{n-1} (x_i - \\bar{x})^2', 'SIGMA_SQUARED(4, [2.0, 4.0, 4.0, 6.0])'],
'mean': ['\\bar{x} = \\frac{1}{n} \\sum_{i=0}^{n-1} x_i', 'X_BAR(4, [2.0, 4.0, 4.0, 6.0])'],
'sum-from-one': ['\\bar{x} = \\frac{1}{n} \\sum_{i=1}^{n} x_i', 'X_BAR(4, [1.0, 2.0, 3.0, 4.0])'],
'sum-no-bounds': ['S = \\sum_i x_i^2', 'S([1.0, 2.0, 3.0])'],
'binomial': ['P(k) = \\binom{n}{k} p^k (1 - p)^{n - k}', 'P(2, 4, 0.5)'],
'height': ['h(t) = v_0 t - \\frac{1}{2} g t^2', 'H(2.0, 10.0, 9.81)'],
'kinetic': ['\\mathrm{KE} = \\frac{1}{2} m v^2', 'KE(3.0, 4.0)'],
'gravity': ['F = \\frac{G m_1 m_2}{r^2}', 'F(2.0, 3.0, 4.0, 2.0)'],
'omega': ['\\omega = 2 \\pi f', 'OMEGA(0.5)'],
'normal-density': ['f(x) = \\frac{1}{\\sqrt{2\\pi\\sigma^2}} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}', 'F(0.0, 1.0, 0.0)'],
'ph': ['\\text{pH} = -\\log_{10} [H]', 'PH(0.001)'],
'log2': ['b = \\log_2 n', 'B(8.0)'],
'ln-and-log': ['y = \\ln x + \\log x', 'Y(10.0)'],
'factorial': ['n! = \\prod_{k=1}^{n} k', 'FACTORIAL(5)'],
'choose': ['c = \\frac{n!}{k! (n-k)!}', 'C(5, 2)'],
'cosine': ['\\cos\\theta = \\frac{\\vec{a} \\cdot \\vec{b}}{\\|\\vec{a}\\| \\, \\|\\vec{b}\\|}', 'COS_THETA([1.0, 0.0], [1.0, 0.0])'],
'distance': ['d = \\sqrt{\\sum_{i=0}^{n-1} (p_i - q_i)^2}', 'D(2, [0.0, 0.0], [3.0, 4.0])'],
'matrix-row': ['y_i = \\sum_{j=0}^{n-1} A_{ij} x_j', 'Y(1, 2, [[1.0, 2.0], [3.0, 4.0]], [1.0, 1.0])'],
'nested-sums': ['s = \\sum_{i=0}^{n-1} \\sum_{j=0}^{m-1} a_{ij}', 'S(2, 2, [[1.0, 2.0], [3.0, 4.0]])'],
'relu': ['\\mathrm{relu}(x) = \\begin{cases} 0 & \\text{if } x < 0 \\\\ x & \\text{otherwise} \\end{cases}', 'RELU(-2.0)'],
'piecewise': ['f(x) = \\begin{cases} 0 & x < 0 \\\\ x & 0 \\le x < 1 \\\\ 2 - x & 1 \\le x \\le 2 \\\\ 0 & \\text{otherwise} \\end{cases}', 'F(1.5)'],
'calls-an-earlier-equation': ['g(x) = x^2 + 1\nh(x) = 2 g(x) + c', 'H(3.0, 1.0)'],
'sum-over-a-set': ['m = \\sum_{x \\in S} x', 'M([1.0, 2.0, 3.5])'],
'floor': ['h = \\lfloor n / 2 \\rfloor', 'H(7)'],
'ceiling': ['h = \\lceil n / 2 \\rceil', 'H(7)'],
'absolute': ['d = |a - b|', 'D(2.0, 5.0)'],
'max-min': ['m = \\max(a, b) - \\min(a, b)', 'M(2.0, 5.0)'],
'mod': ['r = a \\bmod b', 'R(7, 3)'],
};
+75
View File
@@ -0,0 +1,75 @@
// scripts/mojo/listings.mjs: godbolt's saved answers become x86-64 panes, and the program <-> exercise links hold.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { stripHeader, readHeader, findGolden, buildListing, render } from '../scripts/mojo/listings.mjs';
import { forSimulator, simulatorSetup } from '../dist/mojo/att.js';
import { listingFunctions } from '../dist/services/godbolt.js';
import { parseAsm } from '../dist/services/asm.js';
const ROOT = new URL('..', import.meta.url).pathname;
const GOLDEN = ROOT + 'mojo/golden';
const mods = { forSimulator, simulatorSetup, listingFunctions };
test('stripHeader drops what emit_ts.py drops, and says how many lines went', () => {
const text = '# origin: x\n# probe: y\n# mode: both\n\ndef main():\n pass\n';
const { source, dropped } = stripHeader(text);
assert.equal(source, 'def main():\n pass\n');
assert.equal(dropped, 4); // three header lines and the blank one after
assert.equal(stripHeader('# a comment, not a header key\ndef f(): pass').dropped, 0);
});
test('readHeader reads the keys verify.py knows and ignores other comments', () => {
const h = readHeader('# probe: q\n# mode: both\n# args: -O0 --debug-level=line-tables\n# a note\n# expect: pass\ndef main(): pass');
assert.equal(h.mode, 'both');
assert.equal(h.args, '-O0 --debug-level=line-tables');
assert.equal(h.expect, 'pass');
assert.equal(h.note, undefined);
});
test('a golden is used only for the exact source, mode and arguments it answered', () => {
const probe = fs.readFileSync(ROOT + 'mojo/probes/godbolt-line-tables-O0.mojo', 'utf8');
const h = readHeader(probe);
assert.ok(findGolden(GOLDEN, probe, h.mode, h.args ?? ''), 'the committed answer for this probe is found');
assert.equal(findGolden(GOLDEN, probe + ' ', h.mode, h.args ?? ''), null, 'one extra character and the answer no longer applies');
assert.equal(findGolden(GOLDEN, probe, 'run', h.args ?? ''), null, 'a run-mode question is a different question');
});
test('real godbolt answers become x86-64 panes the assembler accepts, with lines that point into the shown source', () => {
let made = 0;
for (const f of fs.readdirSync(ROOT + 'mojo/probes').filter((x) => x.endsWith('.mojo'))) {
const text = fs.readFileSync(ROOT + 'mojo/probes/' + f, 'utf8');
if (!/^# mode: (asm|both)/m.test(text)) continue;
const r = buildListing(text, GOLDEN, mods);
if (r.why) continue; // e.g. built without -O0, so the optimizer inlined every function
made++;
const p = parseAsm(r.asm);
assert.ok(p.ok, `${f}: ${p.diagnostics[0]?.message}`);
assert.match(r.setup, /^#/, `${f}: the setup starts with the comment saying where the arguments arrive`);
const shown = stripHeader(text).source.split('\n').length;
for (const m of r.asm.matchAll(/; mojo line (\d+)/g)) assert.ok(+m[1] >= 1 && +m[1] <= shown, `${f}: line ${m[1]} is outside the ${shown}-line program the pane shows`);
}
assert.ok(made >= 1, 'at least one committed probe has a usable listing; if none does, the corpus lost its assembly');
});
test('a program with no answer, or one that was never asked for a listing, says why instead of inventing one', () => {
assert.match(buildListing('# mode: both\n# args: -O0\ndef main(): print(1)\n', GOLDEN, mods).why, /no godbolt answer/);
assert.match(buildListing('def main(): print(1)\n', GOLDEN, mods).why, /mode: both/);
});
test('render keeps the header and interface, and writes programs in name order', () => {
const out = render({ zeta: { entry: 'z(Int)', asm: 'z:\n ret', setup: '# z', build: '' }, alpha: { entry: 'a(Int)', asm: 'a:\n ret', setup: '# a', build: '-O0' } });
assert.match(out, /export interface MojoListing/);
assert.ok(out.indexOf('"alpha"') < out.indexOf('"zeta"'));
assert.ok(out.endsWith('};\n'));
});
test('every exercise that names a Mojo program has that program in mojo/local/', () => {
const dir = ROOT + 'src/exercises/';
const missing = [];
for (const f of fs.readdirSync(dir).filter((x) => x.endsWith('.ts') && !x.startsWith('_'))) {
const m = /mojoProgram:\s*['"]([^'"]+)['"]/.exec(fs.readFileSync(dir + f, 'utf8'));
if (m && !fs.existsSync(ROOT + `mojo/local/${m[1]}.mojo`)) missing.push(`${f} -> mojo/local/${m[1]}.mojo`);
}
assert.deepEqual(missing, [], 'create the file, or remove `mojoProgram` from the exercise; until then `pixi run play` will not regenerate programs.ts, because the catalog would throw');
});
+184
View File
@@ -0,0 +1,184 @@
// matho (equations, written in LaTeX) → pseudo: formulas from statistics, probability, physics, chemistry
// and linear algebra, each run through the translation and checked against a value worked out by hand; plus
// the refusals, which have to say what finite procedure to write instead.
import test from 'node:test';
import assert from 'node:assert/strict';
import { parseMatho, runMatho, lowerMatho, defaultCallMatho, splitEquations, tokenizeMatho } from '../dist/services/matho.js';
import { studyOf } from '../dist/services/pseudo.js';
const value = (src, call) => {
const r = parseMatho(src);
const errs = r.diagnostics.filter((d) => d.severity === 'error');
assert.ok(r.ok, errs.map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | ') + '\n' + r.program.pseudo);
const tr = runMatho(r, { call, maxSteps: 200000 });
assert.equal(tr.error, null, tr.error?.message);
const m = /returned (.*)$/.exec(tr.output[tr.output.length - 1]);
return { v: Number(m[1]), r, tr };
};
const close = (a, b, eps = 1e-9) => assert.ok(Math.abs(a - b) < eps, `${a} is not within ${eps} of ${b}`);
test('matho: mean and variance, the second referring to the first and hoisted out of the loop', () => {
const src = '\\bar{x} = \\frac{1}{n} \\sum_{i=0}^{n-1} x_i\n\\sigma^2 = \\frac{1}{n - 1} \\sum_{i=0}^{n-1} (x_i - \\bar{x})^2';
const { v, r } = value(src, 'SIGMA_SQUARED(4, [2.0, 4.0, 4.0, 6.0])');
close(v, 8 / 3);
assert.match(r.program.pseudo, /FUNCTION X_BAR\(N : INTEGER, X : LIST OF FLOAT\) RETURNS FLOAT/);
assert.match(r.program.pseudo, /XBar <- X_BAR\(N, X\)\n\s+Total <- 0\.0\n\s+FOR I <- 0 TO N/);
assert.ok(r.diagnostics.some((d) => d.id === 'matho-hoist'));
assert.ok(r.diagnostics.some((d) => d.id === 'matho-list' && /X is indexed/.test(d.message)));
assert.equal(value(src, 'X_BAR(4, [2.0, 4.0, 4.0, 6.0])').v, 4);
});
test('matho: a sum from 1 shifts the subscript to a 0-based position, and says so', () => {
const { v, r } = value('\\bar{x} = \\frac{1}{n} \\sum_{i=1}^{n} x_i', 'X_BAR(4, [1.0, 2.0, 3.0, 4.0])');
assert.equal(v, 2.5);
assert.match(r.program.pseudo, /FOR I <- 1 TO N \+ 1\n\s+Total <- Total \+ X\[I - 1\]/);
assert.ok(r.diagnostics.some((d) => d.id === 'matho-one-based'));
});
test('matho: a sum with no bounds runs over the whole list', () => {
const { v, r } = value('S = \\sum_i x_i^2', 'S([1.0, 2.0, 3.0])');
assert.equal(v, 14);
assert.match(r.program.pseudo, /FOR I <- 0 TO LENGTH\(X\)/);
});
test('matho: binomial probability, with the extra names becoming parameters and p renamed away from P', () => {
const { v, r } = value('P(k) = \\binom{n}{k} p^k (1 - p)^{n - k}', 'P(2, 4, 0.5)');
close(v, 0.375);
assert.match(r.program.pseudo, /FUNCTION P\(K : INTEGER, N : INTEGER, P2 : FLOAT\)/);
assert.ok(r.diagnostics.some((d) => d.id === 'matho-case'));
assert.ok(r.diagnostics.some((d) => d.id === 'matho-extra-params'));
});
test('matho: physics formulas with subscripted constants and Greek letters', () => {
close(value('h(t) = v_0 t - \\frac{1}{2} g t^2', 'H(2.0, 10.0, 9.81)').v, 20 - 0.5 * 9.81 * 4);
close(value('\\mathrm{KE} = \\frac{1}{2} m v^2', 'KE(3.0, 4.0)').v, 24);
close(value('F = \\frac{G m_1 m_2}{r^2}', 'F(2.0, 3.0, 4.0, 2.0)').v, 6);
const { r } = value('F = \\frac{G m_1 m_2}{r^2}', 'F(2.0, 3.0, 4.0, 2.0)');
assert.match(r.program.pseudo, /FUNCTION F\(G : FLOAT, M1 : FLOAT, M2 : FLOAT, R : FLOAT\)/);
close(value('\\omega = 2 \\pi f', 'OMEGA(0.5)').v, Math.PI);
});
test('matho: the normal density, with e^{...} as EXP and \\sqrt', () => {
const { v } = value('f(x) = \\frac{1}{\\sqrt{2\\pi\\sigma^2}} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}', 'F(0.0, 1.0, 0.0)');
close(v, 1 / Math.sqrt(2 * Math.PI));
});
test('matho: chemistry: pH from a concentration, log bases', () => {
close(value('\\text{pH} = -\\log_{10} [H]', 'PH(0.001)').v, 3);
close(value('b = \\log_2 n', 'B(8.0)').v, 3);
close(value('y = \\ln x + \\log x', 'Y(10.0)').v, Math.log(10) + 1);
});
test('matho: factorial as a product loop, both as n! and as \\prod', () => {
assert.equal(value('n! = \\prod_{k=1}^{n} k', 'FACTORIAL(5)').v, 120);
const { v, r } = value('c = \\frac{n!}{k! (n-k)!}', 'C(5, 2)');
assert.equal(v, 10);
assert.match(r.program.pseudo, /Factorial <- 1\.0/);
assert.match(r.program.pseudo, /Factorial2 <- 1\.0/);
});
test('matho: vectors: dot product, norm, the cosine of the angle', () => {
close(value('\\cos\\theta = \\frac{\\vec{a} \\cdot \\vec{b}}{\\|\\vec{a}\\| \\, \\|\\vec{b}\\|}', 'COS_THETA([1.0, 0.0], [0.0, 1.0])').v, 0);
const { r } = value('\\cos\\theta = \\frac{\\vec{a} \\cdot \\vec{b}}{\\|\\vec{a}\\| \\, \\|\\vec{b}\\|}', 'COS_THETA([1.0, 0.0], [1.0, 0.0])');
assert.match(r.program.pseudo, /RETURN DOT\(A, B\) \/ \(NORM_OF\(A\) \* NORM_OF\(B\)\)/);
close(value('d = \\sqrt{\\sum_{i=0}^{n-1} (p_i - q_i)^2}', 'D(2, [0.0, 0.0], [3.0, 4.0])').v, 5);
});
test('matho: a matrix row times a vector, from a two-letter subscript', () => {
const { v, r } = value('y_i = \\sum_{j=0}^{n-1} A_{ij} x_j', 'Y(1, 2, [[1.0, 2.0], [3.0, 4.0]], [1.0, 1.0])');
assert.equal(v, 7);
assert.match(r.program.pseudo, /FUNCTION Y\(I : INTEGER, N : INTEGER, A : LIST OF LIST OF FLOAT, X : LIST OF FLOAT\)/);
assert.match(r.program.pseudo, /A\[I\]\[J\] \* X\[J\]/);
});
test('matho: nested sums', () => {
const { v } = value('s = \\sum_{i=0}^{n-1} \\sum_{j=0}^{m-1} a_{ij}', 'S(2, 2, [[1.0, 2.0], [3.0, 4.0]])');
assert.equal(v, 10);
});
test('matho: piecewise definitions become IF / ELSE IF / ELSE', () => {
const src = '\\mathrm{relu}(x) = \\begin{cases} 0 & \\text{if } x < 0 \\\\ x & \\text{otherwise} \\end{cases}';
assert.equal(value(src, 'RELU(-2.0)').v, 0);
assert.equal(value(src, 'RELU(3.0)').v, 3);
const tri = 'f(x) = \\begin{cases} 0 & x < 0 \\\\ x & 0 \\le x < 1 \\\\ 2 - x & 1 \\le x \\le 2 \\\\ 0 & \\text{otherwise} \\end{cases}';
assert.equal(value(tri, 'F(0.5)').v, 0.5);
assert.equal(value(tri, 'F(1.5)').v, 0.5);
assert.equal(value(tri, 'F(5.0)').v, 0);
assert.match(parseMatho(tri).program.pseudo, /ELSEIF 0 <= X AND X < 1 THEN/);
});
test('matho: a defined function is called by name later, with explicit and implicit arguments', () => {
const src = 'g(x) = x^2 + 1\nh(x) = 2 g(x) + c';
const { v, r } = value(src, 'H(3.0, 1.0)');
assert.equal(v, 21);
assert.match(r.program.pseudo, /RETURN 2 \* G\(X\) \+ C/);
});
test('matho: a sum over a set uses FOR EACH', () => {
const { v, r } = value('m = \\sum_{x \\in S} x', 'M([1.0, 2.0, 3.5])');
assert.equal(v, 6.5);
assert.match(r.program.pseudo, /FOR EACH X IN S/);
});
test('matho: floors, ceilings, absolute values, max and min, mod', () => {
assert.equal(value('h = \\lfloor n / 2 \\rfloor', 'H(7)').v, 3);
assert.equal(value('h = \\lceil n / 2 \\rceil', 'H(7)').v, 4);
assert.equal(value('d = |a - b|', 'D(2.0, 5.0)').v, 3);
assert.equal(value('m = \\max(a, b) - \\min(a, b)', 'M(2.0, 5.0)').v, 3);
assert.equal(value('r = a \\bmod b', 'R(7, 3)').v, 1);
});
test('matho: the refusals say what to write instead, and the good equations still translate', () => {
const r = parseMatho('a = b^2\n\\int_0^1 f(x) dx\n\\sum_{i=0}^{\\infty} \\frac{1}{2^i}\n\\lim_{x \\to 0} \\frac{\\sin x}{x}\n\\frac{\\partial f}{\\partial x}');
assert.equal(r.ok, false);
const errs = r.diagnostics.filter((d) => d.severity === 'error');
assert.deepEqual(errs.map((d) => d.line), [2, 3, 4, 5]);
assert.ok(errs.every((d) => d.id === 'matho-refused'));
assert.match(errs[0].message, /sum it approximates/);
assert.match(errs[1].message, /upper bound/);
assert.match(errs[2].message, /not a procedure/);
assert.match(r.program.pseudo, /FUNCTION A\(B : FLOAT\)/);
});
test('matho: syntax problems are reported on the right line, with a sentence', () => {
const r = parseMatho('a = \\frac{1}{\nb = \\foo{x}\nc = x \\pm y\nd = \\begin{cases} 1 & x < 0 \\end{cases}');
const errs = r.diagnostics.filter((d) => d.severity === 'error');
assert.deepEqual(errs.map((d) => [d.line, d.id]), [[1, 'matho-syntax'], [2, 'matho-command'], [3, 'matho-pm'], [4, 'matho-cases-otherwise']]);
assert.match(errs[1].message, /\\foo is not a command this reader knows/);
});
test('matho: a chain of equalities and a bare comparison are refused as statements', () => {
const r = parseMatho('a = b = c\nx < 0');
assert.deepEqual(r.diagnostics.filter((d) => d.severity === 'error').map((d) => d.id), ['matho-chain', 'matho-condition']);
});
test('matho: equations split on lines, continue on operators, and ignore document wrappers', () => {
const parts = splitEquations('\\begin{align}\na &= b\n + c \\\\\nd &= e\n\\end{align}\n\nf = \\begin{cases}\n1 & x < 0 \\\\\n0 & \\text{otherwise}\n\\end{cases}\n% a comment\n$$ g = h $$');
assert.deepEqual(parts.map((p) => p.line), [2, 4, 7, 12]);
assert.match(parts[0].text, /a\s*= b\n\s*\+ c/);
assert.match(parts[2].text, /begin\{cases\}[\s\S]*end\{cases\}/);
});
test('matho: the pseudo it produces reads cleanly and lowers with a line map back to the equations', () => {
const r = parseMatho('\\bar{x} = \\frac{1}{n} \\sum_{i=0}^{n-1} x_i\n\n\\sigma^2 = \\frac{1}{n-1} \\sum_{i=0}^{n-1} (x_i - \\bar{x})^2');
const st = studyOf(r.program.pseudo);
assert.ok(st.ok, st.diagnostics.map((d) => d.message).join(' | '));
const low = lowerMatho(r, 'pseudo');
assert.equal(low.target, 'pseudo');
const from = new Set(low.map.map((m) => m.from));
assert.deepEqual([...from], [1, 3]);
assert.equal(defaultCallMatho(r), 'SIGMA_SQUARED(4, [1.0, 2.0, 3.0, 4.0])');
const tr = runMatho(r, {});
assert.equal(tr.lang, 'matho');
assert.ok(tr.hits.get(3) > 0, 'the trace is mapped back to equation lines');
});
test('matho: the tokenizer marks commands, refused commands, numbers and names', () => {
const toks = tokenizeMatho('\\frac{1}{n} \\sum_{i=0}^{n-1} x_i \\int % note');
const cls = (t) => toks.find((x) => x.text === t)?.cls;
assert.equal(cls('\\frac'), 'tok-keyword');
assert.equal(cls('\\int'), 'tok-error');
assert.equal(cls('1'), 'tok-number');
assert.equal(cls('x'), 'tok-name');
assert.equal(cls('% note'), 'tok-comment');
});
+226
View File
@@ -0,0 +1,226 @@
// Programming+'s Mojo checker, scored against the real compiler (godbolt's answers in mojo/golden,
// and the compiler's own diagnostic tests). Two rules never bend, and two numbers only go up:
// - no error on a program the compiler accepts (a false accusation teaches the wrong lesson)
// - no error on a rejected program that the compiler did not make there, in those words
// - starters matched exactly, and errors matching the compiler's own annotations: a ratchet
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import crypto from 'node:crypto';
import { checkMojo } from '../dist/mojo/check.js';
import { compilerDiagnostics } from '../dist/services/godbolt.js';
const R = new URL('..', import.meta.url).pathname;
const read = (p) => JSON.parse(fs.readFileSync(R + p, 'utf8'));
const golden = (key) => key && fs.existsSync(`${R}mojo/golden/${key}.json`) ? read(`mojo/golden/${key}.json`) : null;
const verdicts = new Map(read('mojo/golden/status.json').items.map((r) => [r.id, r]));
const errorsOf = (src) => checkMojo(src).diagnostics.filter((d) => d.severity === 'error');
/** What the compiler said, as (line, message): its <source> errors, and the one it gives no line. */
function compilerSaid(g) {
const ds = compilerDiagnostics(g.result.buildStderr).filter((d) => d.severity === 'error').map((d) => ({ line: d.line, message: d.message }));
const whole = /: error: (module does not contain a 'main' function)/.exec(g.result.buildStderr);
if (whole) ds.push({ line: 1, message: whole[1] });
return ds;
}
const MATCHED_AT_LEAST = 43; // raise when a stage makes more starters match (2a: 11, 2b: 23, 2c: 43)
const ANNOTATIONS_AT_LEAST = 21; // 2a: 14, 2b: 18, 2c: 19, 4c: 21
test('no false accusations: every program the compiler accepts gets no error', () => {
const items = [...read('mojo/corpus/quest/manifest.json').items.map((i) => ['quest', i]), ...read('mojo/corpus/manual/manifest.json').items.map((i) => ['manual', i])];
const accused = []; let checked = 0;
for (const [dir, it] of items) {
const g = golden(verdicts.get(it.id)?.key);
if (!g?.result.compiled) continue;
checked++;
for (const d of errorsOf(fs.readFileSync(`${R}mojo/corpus/${dir}/${it.file}`, 'utf8'))) accused.push(`${it.id}: line ${d.line}: ${d.message}`);
}
for (const f of fs.readdirSync(`${R}mojo/local`).filter((f) => f.endsWith('.mojo'))) for (const d of errorsOf(fs.readFileSync(`${R}mojo/local/${f}`, 'utf8'))) accused.push(`local/${f}: line ${d.line}: ${d.message}`);
// the stress programs: every one is meant to compile, so none may be accused, verified or not
for (const f of fs.readdirSync(`${R}mojo/stress`).filter((f) => f.endsWith('.mojo'))) {
const src = fs.readFileSync(`${R}mojo/stress/${f}`, 'utf8');
if (/^# expect: compile-error$/m.test(src)) continue; // kept as checker tests: meant to fail
for (const d of errorsOf(src)) accused.push(`stress/${f}: line ${d.line}: ${d.message}`);
}
assert.ok(checked > 200, `only ${checked} accepted programs found; run pixi run mojo-verify`);
assert.deepEqual(accused, []);
});
test('rejected starters: what Programming+ says is what the compiler said', () => {
const wrong = [], matched = [];
for (const it of read('mojo/corpus/quest/manifest.json').items.filter((i) => i.id.endsWith('/starter'))) {
const g = golden(verdicts.get(it.id)?.key);
if (!g || g.result.compiled) continue;
const ours = errorsOf(fs.readFileSync(`${R}mojo/corpus/quest/${it.file}`, 'utf8'));
if (!ours.length) continue;
const theirs = compilerSaid(g);
const bad = ours.filter((d) => !theirs.some((t) => t.line === d.line && t.message === d.message));
if (bad.length) wrong.push(`${it.id}: Programming+ said ${bad.map((d) => `line ${d.line}: ${d.message}`).join('; ')} | the compiler: ${theirs.map((t) => `line ${t.line}: ${t.message}`).join('; ')}`);
else matched.push(it.id);
}
assert.deepEqual(wrong, []);
assert.ok(matched.length >= MATCHED_AT_LEAST, `${matched.length} starters matched, fewer than ${MATCHED_AT_LEAST}: ${matched.join(' ')}`);
});
// The compiler's harness (MLIR's -verify-diagnostics) matches an annotation as a substring of the message.
test('the compiler\'s own diagnostic tests: every error Programming+ reports there is one they expect', () => {
const toRe = (a) => new RegExp(a.message.split(/(\{\{.*?\}\})/).map((p) => p.startsWith('{{') ? p.slice(2, -2) : p.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join(''));
const wrong = []; let matched = 0;
for (const it of read('mojo/corpus/diagnostics/manifest.json').items) {
// run with the isolated stdlib or other local files: they know other names, and that mode
// does not check for code at file scope; and as tests, none of them has a main
const special = it.run.some((r) => /isolated|%S\/|-I\s/.test(r));
const skip = new Set(['no-main', ...(special ? ['unknown-name', 'implicit-declaration', 'file-scope'] : [])]);
for (const d of errorsOf(fs.readFileSync(`${R}mojo/corpus/diagnostics/${it.file}`, 'utf8')).filter((d) => !skip.has(d.id))) {
if (it.annotations.some((a) => a.kind === 'error' && a.line === d.line && (a.regex ? toRe(a).test(d.message) : d.message.includes(a.message)))) matched++;
else wrong.push(`${it.id}: line ${d.line}: ${d.message}`);
}
}
assert.deepEqual(wrong, []);
assert.ok(matched >= ANNOTATIONS_AT_LEAST, `${matched} matched annotations, fewer than ${ANNOTATIONS_AT_LEAST}`);
});
test('the checks, one by one, in the compiler\'s words', () => {
const say = (src) => errorsOf(src).map((d) => `${d.line}:${d.col} ${d.message}`);
assert.deepEqual(say("def main():\nprint(1)\n").sort(), ["1:12 body must not be empty; use 'pass' or check that the lines below are indented", "2:1 expressions must not appear at file scope; move this into a function body"]);
assert.deepEqual(say('def f(a, b: Int):\n pass\ndef main():\n pass\n'), ['1:7 argument type must be specified']);
assert.deepEqual(say('def main():\n x = 1\n print(x)\n'), ["2:5 implicit declaration of 'x' is not allowed; add 'var' to declare a new name"]);
assert.deepEqual(say('def main():\n print(sqrt(2.0))\n'), ["2:11 use of unknown declaration 'sqrt'; did you mean to import it from 'std.math'? Add 'from std.math import sqrt'"]);
assert.deepEqual(say('def main():\n var count = 1\n print(coun)\n'), [], 'near a declared name the compiler suggests one; Programming+ says nothing rather than guess');
assert.deepEqual(say('struct S:\n def f() -> Int:\n return 1\ndef main():\n pass\n'), ['2:9 self argument must be present in instance method']);
assert.deepEqual(say('struct S:\n def __init__(x: Int, out self):\n pass\ndef main():\n pass\n'), [], 'out self may come after other parameters');
assert.deepEqual(say('def boot():\n pass\n'), ["1:1 module does not contain a 'main' function"]);
assert.deepEqual(say('def main():\n var y = 0123\n'), ["2:13 decimal integer literals must not use leading zeros; add '0o' for octal literals"]);
// code the parser cannot read: a note, and no other finding that could be wrong
const unread = checkMojo('def main():\n var x = 1 2\n y = 2\n').diagnostics;
assert.ok(unread.some((d) => d.severity === 'note' && /cannot read this yet/.test(d.message)));
assert.ok(!unread.some((d) => d.id === 'implicit-declaration'), 'no name checks on a file the parser could not fully read');
});
test('Stage 2b: signatures, conventions, raises, moves, constructors, in the compiler\'s words', () => {
const say = (src) => errorsOf(src).map((d) => `${d.line}:${d.col} ${d.message}`);
const notes = (src) => checkMojo(src).diagnostics.filter((d) => d.severity === 'note').map((d) => `${d.line}:${d.col} ${d.message}`);
const f = 'def area(w: Int, h: Int) -> Int:\n return w * h\n\n\n';
assert.deepEqual(say(f + 'def main():\n print(area(2))\n'), ["6:15 invalid call to 'area': missing required argument: 'h'"]);
assert.deepEqual(notes(f + 'def main():\n print(area(2))\n'), ['1:5 function declared here']);
assert.deepEqual(say(f + 'def main():\n print(area(2, 3, 4))\n'), ["6:22 invalid call to 'area': unexpected argument"]);
assert.deepEqual(say('def go(n: Int, *, fast: Bool) -> Int:\n return n\n\n\ndef main():\n print(go(1, True))\n'), ["6:13 invalid call to 'go': missing required argument: 'fast'"], 'missing wins over unexpected');
assert.deepEqual(say('def make(x: Int, out result: Int):\n result = x\n\n\ndef main():\n print(make(1))\n'), [], 'an out parameter is filled by the callee');
const r = 'def read(n: Int) raises -> Int:\n return n\n\n\n';
assert.deepEqual(say(r + 'def main():\n print(read(1))\n'), ['6:15 cannot call function that may raise in a context that cannot raise']);
assert.deepEqual(notes(r + 'def main():\n print(read(1))\n'), ["6:15 try surrounding the call in a 'try' block", "5:5 or mark surrounding function as 'raises'"]);
assert.deepEqual(say(r + 'def main():\n try:\n print(read(1))\n except e:\n print(e)\n'), []);
assert.deepEqual(say(r + 'def main() raises:\n print(read(1))\n'), []);
assert.deepEqual(say('def safe(n: Int) raises Never -> Int:\n return n\n\n\ndef main():\n print(safe(1))\n'), [], 'raises Never cannot raise');
assert.deepEqual(say('def main():\n var s = input()\n print(s)\n'), ['2:18 cannot call function that may raise in a context that cannot raise'], 'input() raises (harvested from the stdlib)');
assert.deepEqual(say('def add(total: Int, x: Int):\n total += x\n\n\ndef main():\n add(1, 2)\n'), ['2:5 expression must be mutable for in-place operator destination']);
assert.deepEqual(say('def add(mut total: Int, x: Int):\n total += x\n\n\ndef main():\n var t = 0\n add(t, 2)\n'), []);
assert.deepEqual(say('def main():\n var xs: List[Int] = [1, 2]\n for x in xs:\n x += 1\n'), ['4:9 expression must be mutable for in-place operator destination']);
assert.deepEqual(say('def main():\n var xs: List[Int] = [1, 2]\n for ref x in xs:\n x += 1\n'), [], 'for ref binds a mutable reference');
assert.deepEqual(say('def main():\n comptime for _ in range(2):\n _ = 1\n'), [], '_ binds nothing');
const st = 'struct C(Copyable, Movable):\n var n: Int\n\n def __init__(out self, n: Int):\n self.n = n\n\n def bump(self):\n self.n += 1\n\n\n';
assert.deepEqual(say(st + 'def main():\n var c = C(0)\n c.bump()\n'), ['8:13 expression must be mutable for in-place operator destination'], 'at the dot of self.n');
assert.deepEqual(say(st + 'def main():\n var c = C(1, 2)\n'), ['8:13 expression must be mutable for in-place operator destination', '12:14 no matching function in initialization'], 'bump still mutates an immutable self');
assert.deepEqual(say('struct P(Copyable, Movable):\n var x: Int\n var y: Int\n\n def __init__(out self):\n pass\n\n\ndef main():\n var p = P()\n'), ["5:9 'self.x' is uninitialized at the implicit return from this function"], 'only the first unset field');
assert.deepEqual(say('def main():\n var a: List[Int] = [1]\n var b = a^\n a.append(2)\n print(len(b))\n'), ["4:13 use of uninitialized value 'a'"]);
assert.deepEqual(say('def main():\n var a: List[Int] = [1]\n var b = a^\n a = [2]\n print(len(a), len(b))\n'), [], 'assigned again before the use');
});
test('Stage 2c: types, in the compiler\'s words, and silence where a type is not certain', () => {
const say = (src) => errorsOf(src).map((d) => `${d.line}:${d.col} ${d.message}`);
const S = 'struct P(Copyable, Movable):\n var x: Int\n\n def __init__(out self, x: Int):\n self.x = x\n\n\n';
assert.deepEqual(say(S + 'def main():\n var p = P(1)\n print(p.y)\n'), ["10:12 'P' value has no attribute 'y'"]);
assert.deepEqual(say(S + 'def main():\n var p = P(1)\n var q = p.copy()\n print(q.x)\n'), [], 'Copyable gives copy()');
assert.deepEqual(say(S + 'def main():\n var a = P(1)\n var b = P(2)\n print(a + b)\n'), ["11:13 'P' does not implement the '__add__' method"]);
assert.deepEqual(say(S + 'def main():\n var a = P(1)\n var b = P(2)\n print(a == b)\n'), ["11:13 'P' does not implement the '__eq__' method"]);
assert.deepEqual(say(S.replace('(Copyable, Movable)', '(Copyable, Movable, Equatable)') + 'def main():\n var a = P(1)\n var b = P(2)\n print(a == b)\n'), [], "Equatable's default __eq__ compares the fields");
assert.deepEqual(say(S + 'def main():\n var a = P(1)\n print(-a)\n'), ["10:11 'P' does not implement the '__neg__' method"]);
assert.deepEqual(say(S + 'def main():\n var a = P(1)\n print(a[0])\n'), ["10:12 'P' is not subscriptable, it does not implement the `__getitem__`/`__setitem__` methods"]);
const copy = checkMojo(S + 'def main():\n var a = P(1)\n var b = a\n print(b.x)\n').diagnostics.find((d) => d.id === 'implicit-copy');
assert.equal(`${copy.line}:${copy.col} ${copy.message}`, "10:13 value of type 'P' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'");
assert.deepEqual(copy.question.options.map((o) => o.text), ['^', '.copy()'], "the compiler's two fixes");
assert.deepEqual(say('def take(var xs: List[Int]) -> Int:\n return len(xs)\n\n\ndef main():\n var xs: List[Int] = [1]\n print(take(xs))\n'), ["7:16 value of type 'List[Int]' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'"], 'even at its last use');
assert.deepEqual(say('def main():\n var s = String("a")\n var t = s\n print(t)\n'), [], 'String is implicitly copyable');
assert.deepEqual(say('def main():\n var xs: List[Int] = [1]\n ref v = xs\n print(v[0])\n'), [], 'ref binds, it does not copy');
assert.deepEqual(say('def main():\n var n: Int = "5"\n'), ['2:18 cannot implicitly convert \'StringLiteral["5"]\' value to \'Int\'']);
assert.deepEqual(say('def f() -> Int:\n return None\n\n\ndef main():\n print(f())\n'), ["2:12 cannot implicitly convert 'None' value to 'Int'"]);
assert.deepEqual(say('def avg(t: Int, n: Int) -> Float64:\n return t / n\n\n\ndef main():\n print(avg(1, 2))\n'), ["2:14 cannot implicitly convert 'Int' value to 'Float64'"], 'Int / Int is an Int');
assert.deepEqual(say('def main():\n var n = 10\n var x = 2.5\n print(n * x)\n'), ["4:15 invalid call to '__mul__': value passed to 'rhs' cannot be converted from 'Float64' to 'Int'"]);
assert.deepEqual(say('def main():\n var n = 10\n print(n * 2.5)\n'), [], 'a literal converts');
assert.deepEqual(say('def main():\n var xs = List()\n xs.append(1)\n'), ['2:18 no matching function in initialization']);
assert.deepEqual(say('def main():\n var xs: List[Int] = List()\n xs.append(1)\n'), [], 'the annotation says what it holds');
assert.deepEqual(say('def main():\n var d = Dict[String, Int]()\n d["a"] = 1\n print(d["a"])\n'), ['4:12 cannot call function that may raise in a context that cannot raise'], 'reading a key raises; setting one does not');
const C = 'struct C(Copyable, Movable):\n var deg: Int\n\n def __init__(out self, deg: Int):\n self.deg = deg\n\n\ndef show(t: C):\n print(t.deg)\n\n\n';
assert.deepEqual(say(C + 'def main():\n show(25)\n'), ["13:10 invalid call to 'show': value passed to 't' cannot be converted from 'IntLiteral[25]' to 'C'"]);
assert.deepEqual(say(C.replace(' def __init__', ' @implicit\n def __init__') + 'def main():\n show(25)\n'), [], 'an @implicit constructor converts');
});
test('lifetime warnings: each is the compiler\'s, at its line and column', () => {
const programs = [];
for (const [dir, mf] of [['quest', 'quest'], ['manual', 'manual']]) for (const it of read(`mojo/corpus/${mf}/manifest.json`).items) programs.push([it.id, `mojo/corpus/${dir}/${it.file}`]);
for (const f of fs.readdirSync(`${R}mojo/probes`)) programs.push([`probes/${f.replace('.mojo', '')}`, `mojo/probes/${f}`]);
for (const f of fs.readdirSync(`${R}mojo/stress`)) programs.push([`stress/${f.replace('.mojo', '')}`, `mojo/stress/${f}`]);
const wrong = []; let matched = 0;
for (const [id, file] of programs) {
const g = golden(verdicts.get(id)?.key);
if (!g?.result.compiled) continue;
const theirs = [...g.result.buildStderr.matchAll(/<source>:(\d+):(\d+): warning: (.*)/g)].map((m) => ({ line: +m[1], col: +m[2], message: m[3] }));
for (const d of checkMojo(fs.readFileSync(R + file, 'utf8')).diagnostics.filter((d) => d.severity === 'warning')) {
if (theirs.some((t) => t.line === d.line && t.col === d.col && t.message === d.message)) matched++;
else wrong.push(`${id}: line ${d.line}:${d.col}: ${d.message}`);
}
}
assert.deepEqual(wrong, []);
assert.ok(matched >= 9, `${matched} warnings matched, fewer than 9`);
});
test('Stage 4c: a value read where it may be uninitialized, on any path, in the compiler\'s words', () => {
const say = (src) => errorsOf(src).map((d) => `${d.line}:${d.col} ${d.message}`);
const T = 'def take(var xs: List[Int]):\n pass\n\n\n';
// moved out on one path, read after the branch: at the ( of the call that reads it (probes/check-use-after-move-on-one-path)
assert.deepEqual(say(T + 'def run(flag: Bool):\n var a: List[Int] = [1]\n if flag:\n take(a^)\n print(len(a))\n\n\ndef main():\n run(True)\n'), ["9:14 use of uninitialized value 'a'"]);
assert.deepEqual(say(T + 'def run(flag: Bool):\n var a: List[Int] = [1]\n if flag:\n take(a^)\n else:\n print(len(a))\n\n\ndef main():\n run(True)\n'), [], 'read only on the path that did not move it');
assert.deepEqual(say(T + 'def run(flag: Bool):\n var a: List[Int] = [1]\n if flag:\n take(a^)\n a = [2]\n print(len(a))\n\n\ndef main():\n run(True)\n'), [], 'assigned again on the path that moved it');
// declared without a value
assert.deepEqual(say('def f() -> Int:\n var a: Int\n return a\n\n\ndef main():\n print(f())\n'), ["3:12 use of uninitialized value 'a'"]);
assert.deepEqual(say('def f(c: Bool) -> Int:\n var a: Int\n if c:\n a = 1\n else:\n a = 2\n return a\n\n\ndef main():\n print(f(True))\n'), [], 'assigned on every path');
assert.deepEqual(say('def f(c: Bool) -> Int:\n var a: Int\n while True:\n if c:\n a = 1\n break\n return a\n\n\ndef main():\n print(f(True))\n'), [], 'while True ends only through its breaks');
});
test('rejected stress programs: whatever Programming+ reports there is what the compiler said', () => {
const wrong = []; let matched = 0, current = 0;
for (const f of fs.readdirSync(`${R}mojo/stress`).filter((f) => f.endsWith('.mojo'))) {
const src = fs.readFileSync(`${R}mojo/stress/${f}`, 'utf8');
const g = golden(verdicts.get(`stress/${f.replace('.mojo', '')}`)?.key);
// a golden answers the exact source godbolt saw: an edited file waits for the next verify
if (!g || g.result.compiled || g.sourceSha256 !== crypto.createHash('sha256').update(src).digest('hex')) continue;
current++;
const theirs = compilerSaid(g);
for (const d of errorsOf(src)) {
if (theirs.some((t) => t.line === d.line && t.message === d.message)) matched++;
else wrong.push(`stress/${f}: line ${d.line}: ${d.message}`);
}
}
assert.deepEqual(wrong, []);
if (current >= 2) assert.ok(matched >= 5, `${matched} matched, fewer than the 5 the lambda programs give`);
});
test('a function type as an ordinary parameter\'s type: the compiler\'s errors, at its positions', () => {
const say = (src) => errorsOf(src).map((d) => `${d.line}:${d.col} ${d.message}`);
const src = 'def twice(f: def(Int) -> Int, x: Int) -> Int:\n return f(f(x))\n\n\ndef inc(n: Int) -> Int:\n return n + 1\n\n\ndef main():\n print(twice(inc, 1))\n';
assert.deepEqual(say(src), [
"2:13 invalid call to '__call__': value cannot be converted from type value '$0' to an instance of '$0'; did you mean to instantiate '$0'?",
"2:15 invalid call to '__call__': value cannot be converted from type value '$0' to an instance of '$0'; did you mean to instantiate '$0'?",
"10:17 invalid call to 'twice': value passed to 'f' cannot be converted from 'def inc(n: Int) thin -> Int' to 'def(Int) -> Int'",
]);
assert.deepEqual(say('def twice[F: def(Int) -> Int, //](f: F, x: Int) -> Int:\n return f(f(x))\n\n\ndef inc(n: Int) -> Int:\n return n + 1\n\n\ndef main():\n print(twice(inc, 1))\n'), [], 'through a type parameter, as the manual does');
});
test('a t-string is a TString of its values\' types, not a String (stress/string-builder)', () => {
const say = (src) => errorsOf(src).map((d) => `${d.line}:${d.col} ${d.message}`);
assert.deepEqual(say('def label(name: String, score: Int) -> String:\n return t"{name}: {score}"\n\n\ndef main():\n print(label("a", 1))\n'), ["2:12 cannot implicitly convert 'TString[String, Int]' value to 'String'"]);
assert.deepEqual(say('def main():\n var n = 3\n var s: String = t"n={n}"\n print(s)\n'), ["3:21 cannot implicitly convert 'TString[Int]' value to 'String'"]);
assert.deepEqual(say('def main():\n var n = 3\n print(t"n={n}")\n var s = String(t"n={n}")\n print(s)\n'), [], 'print takes a t-string, and String() makes one a String');
assert.deepEqual(say('def f() -> String:\n return t"{1 + 2}"\n\n\ndef main():\n print(f())\n'), [], 'an interpolated literal: its TString type is not verified, so nothing is said');
});
+182
View File
@@ -0,0 +1,182 @@
// Programming+'s Mojo interpreter, judged against the real compiler: for every program godbolt
// compiled and ran (mojo/golden), the interpreter either declines ("cannot run this yet") or
// prints exactly what godbolt's run printed and ends the same way. It never disagrees.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import crypto from 'node:crypto';
import { parseModule } from '../dist/mojo/parse.js';
import { runMojo } from '../dist/mojo/interp.js';
const R = new URL('..', import.meta.url).pathname;
const read = (p) => JSON.parse(fs.readFileSync(R + p, 'utf8'));
const verdicts = new Map(read('mojo/golden/status.json').items.map((r) => [r.id, r]));
const golden = (key) => key && fs.existsSync(`${R}mojo/golden/${key}.json`) ? read(`mojo/golden/${key}.json`) : null;
const run = (src, input = []) => runMojo(parseModule(src), src, { input, maxSteps: 200000 });
const AGREE_AT_LEAST = 203; // raise as the interpreter learns more (3a: 171, 4a: 180, 4b: 182, 4c: 191, 7a: 201, 7b: 203)
test('the interpreter never disagrees with godbolt, and agrees on at least the ratchet', () => {
const programs = [];
for (const [dir, mf] of [['quest', 'quest'], ['manual', 'manual']]) for (const it of read(`mojo/corpus/${mf}/manifest.json`).items) programs.push([it.id, `mojo/corpus/${dir}/${it.file}`]);
for (const f of fs.readdirSync(R + 'mojo/probes')) programs.push([`probes/${f.replace('.mojo', '')}`, `mojo/probes/${f}`]);
for (const f of fs.readdirSync(R + 'mojo/local')) programs.push([`local/${f.replace('.mojo', '')}`, `mojo/local/${f}`]);
for (const f of fs.readdirSync(R + 'mojo/stress')) programs.push([`stress/${f.replace('.mojo', '')}`, `mojo/stress/${f}`]);
const disagree = []; let agree = 0;
for (const [id, file] of programs) {
const g = golden(verdicts.get(id)?.key);
// only godbolt runs to compare with; and its run mode did not deliver stdin (probes/input-lines)
if (!g || !g.result.compiled || !g.result.executed || g.mode !== 'run' || g.stdin) continue;
const src = fs.readFileSync(R + file, 'utf8');
if (g.sourceSha256 && g.sourceSha256 !== crypto.createHash('sha256').update(src).digest('hex')) continue; // an edited file waits for the next verify
const r = runMojo(parseModule(src), src, { maxSteps: 200000, record: false });
if (r.refused || r.limited) continue;
const norm = (s) => s.replace(/\r\n/g, '\n').replace(/\n+$/, '');
if (norm(r.stdout) === norm(g.result.stdout) && r.exitCode === g.result.exitCode) agree++;
else disagree.push(`${id}: printed ${JSON.stringify(norm(r.stdout).slice(0, 120))} and exited ${r.exitCode}; godbolt printed ${JSON.stringify(norm(g.result.stdout).slice(0, 120))} and exited ${g.result.exitCode}`);
}
assert.deepEqual(disagree, []);
assert.ok(agree >= AGREE_AT_LEAST, `${agree} programs agree, fewer than ${AGREE_AT_LEAST}`);
});
test('the trace has the shape the stepping views read', () => {
const r = run('def main():\n var total = 0\n for i in range(3):\n total += i\n print(total)\n');
const t = r.trace;
assert.equal(r.stdout, '3\n');
assert.equal(t.lang, 'mojo');
assert.equal(t.states.length, t.steps.length + 1, 'a state before the first step, and one after each');
const step = t.steps.find((s) => s.text.startsWith('total += i'));
assert.equal(step.line, 4);
assert.deepEqual(step.writes.map((w) => w.name), ['total']);
const last = t.states.at(-1).frames.find((f) => f.name === 'main');
assert.equal(last.vars.total.text, '3');
assert.equal(last.vars.total.type, 'Int');
assert.equal(t.hits.get(4), 3, 'the loop body ran three times');
const loopState = t.states[t.steps.findIndex((s) => s.kind === 'loop') + 1];
assert.deepEqual(loopState.loops.map((l) => [l.kind, l.line, l.pass]), [['For', 3, 1]]);
});
test('conventions: mut aliases the caller, var copies, ^ moves and empties the source', () => {
assert.equal(run('def bump(mut n: Int):\n n += 1\n\n\ndef main():\n var x = 1\n bump(x)\n print(x)\n').stdout, '2\n');
assert.equal(run('def grow(var xs: List[Int]) -> Int:\n xs.append(9)\n return len(xs)\n\n\ndef main():\n var a: List[Int] = [1]\n print(grow(a.copy()), len(a))\n').stdout, '2 1\n', 'the copy grew, not a');
const moved = run('def main():\n var a: List[Int] = [1, 2]\n var b = a^\n print(len(b))\n');
assert.equal(moved.stdout, '2\n');
const after = moved.trace.states.at(-1).frames[0].vars;
assert.equal(after.a.text, '(moved out)');
});
test('it ends the way the compiled program does', () => {
const oob = run('def main():\n var l: List[Int] = [1, 2, 3]\n var i = 3\n print(l[i])\n');
assert.equal(oob.stdout, 'At: /app/example.mojo:4:12: Assert Error: index 3 is out of bounds, valid range is 0 to 2\n');
assert.equal(oob.exitCode, 132);
const raised = run('def main() raises:\n print("before")\n raise Error("boom")\n');
assert.equal(raised.stdout, 'before\n');
assert.equal(raised.exitCode, 1);
assert.match(raised.stderr, /Unhandled exception caught during execution: boom/);
const eof = run('def main() raises:\n var s = input()\n print(s)\n');
assert.match(eof.stderr, /Unhandled exception caught during execution: EOF/, 'input() at the end of input raises EOF');
assert.equal(run('def main() raises:\n var s = input()\n print(atol(s) + 1)\n', ['41']).stdout, '42\n');
const loop = run('def main():\n var x = 0\n while True:\n x += 1\n');
assert.equal(loop.limited, true, 'an endless loop is inconclusive, not an exit');
});
const LOUD = 'struct L(Copyable, Movable):\n var n: String\n\n def __init__(out self, n: String):\n self.n = n\n print("init", n)\n\n def __init__(out self, *, copy: Self):\n self.n = copy.n + "2"\n print("copy", copy.n)\n\n def __init__(out self, *, deinit move: Self):\n self.n = move.n^\n print("move", self.n)\n\n def __deinit__(deinit self):\n print("bye", self.n)\n\n\n';
test('Stage 4: a value is destroyed at its last use, and the trace shows it', () => {
const r = run(LOUD + 'def main():\n var a = L("a")\n var b = L("b")\n print("using", a.n)\n print("end")\n');
// b is never read: it dies as its declaration ends; a dies when the print that borrows a.n returns
assert.equal(r.stdout, 'init a\ninit b\nbye b\nusing a\nbye a\nend\n');
const destroyed = r.trace.steps.filter((s) => s.kind === 'destroy').map((s) => s.text);
assert.deepEqual(destroyed, ['b is destroyed', 'a is destroyed']);
const last = r.trace.states.at(-1).frames.find((f) => f.name === 'main').vars;
assert.equal(last.a.text, '(destroyed)');
});
test('Stage 4: moves and copies run their constructors where the compiler runs them', () => {
const r = run(LOUD + 'def take(var t: L):\n print("took", t.n)\n\n\ndef main():\n var a = L("a")\n var c = a.copy()\n take(c^)\n var d = a^\n print("kept", d.n)\n');
// into a var argument, ^ moves no bytes; into a new variable the move constructor runs
assert.equal(r.stdout, 'init a\ncopy a\ntook a2\nbye a2\nmove a\nkept a\nbye a\n');
});
test('Stage 4c: a value dies where the path ahead cannot read it', () => {
// a branch that does not read it: it dies as that branch begins (probes/life-branch-one-side)
const r = run(LOUD + 'def run(flag: Bool):\n var a = L("a")\n if flag:\n print("then", a.n)\n else:\n print("else")\n print("after")\n\n\ndef main():\n run(False)\n');
assert.equal(r.stdout, 'init a\nbye a\nelse\nafter\n');
// read in every pass: it dies when the loop ends (probes/life-loop-outer-read-inside)
assert.equal(run(LOUD + 'def main():\n var a = L("a")\n for i in range(2):\n print(i, a.n)\n print("after")\n').stdout, 'init a\n0 a\n1 a\nbye a\nafter\n');
});
test('what it does not model, it declines rather than guesses', () => {
const tryBlock = run(LOUD + 'def main():\n var a = L("a")\n try:\n print(a.n)\n except e:\n print(e)\n');
assert.match(tryBlock.refused, /does not follow yet/, 'a try\'s paths are not modelled');
assert.match(tryBlock.trace.error.message, /cannot run this yet/);
assert.ok(run('from std.python import Python\n\n\ndef main() raises:\n var np = Python.import_module("numpy")\n print(np)\n').refused, 'Python interop is declined');
});
test('Stage 4: the trace records each value\'s lifecycle for the Lifetimes view', () => {
const r = run(LOUD + 'def main():\n var a = L("x")\n var b = a.copy()\n var c = a^\n print(b.n, c.n)\n');
const events = r.trace.steps.flatMap((s) => (s.life ?? []).map((e) => `${e.kind} #${e.id}${e.from ? ` from #${e.from}` : ''}${e.name ? ` as ${e.name}` : ''}`));
// a move makes a new value (#3) from the old one (#1), which ends there; c and b die after the print, newest first
assert.deepEqual(events, ['made #1', 'named #1 as a', 'copied #2 from #1', 'named #2 as b', 'moved #3 from #1', 'named #3 as c', 'destroyed #3', 'destroyed #2']);
});
test('Stage 5: a Mojo function in the shapes the flowchart and data flow read', async () => {
const { mojoRoutine, mojoFunctions } = await import('../dist/mojo/flowir.js');
const src = 'def bump(mut n: Int, by: Int):\n n += by\n\n\ndef main():\n var x = 1\n var xs: List[Int] = [1]\n if x > 0:\n bump(x, 2)\n elif x < 0:\n print("neg")\n else:\n pass\n for v in xs:\n print(v)\n xs.append(x)\n';
const mod = parseModule(src);
assert.deepEqual(mojoFunctions(mod), ['bump', 'main']);
const r = mojoRoutine(mod, 'main');
assert.deepEqual(r.body.map((s) => s.kind), ['Assign', 'Assign', 'If', 'ForEach', 'BuiltinCall']);
const ifs = r.body[2];
assert.equal(ifs.branches.length, 2, 'an elif is a second branch of one If');
assert.deepEqual(ifs.branches[0].body.map((s) => s.kind), ['MojoCall']);
assert.deepEqual(ifs.branches[0].body[0].outs.map((o) => o.name), ['x'], 'the argument to a mut parameter changes');
assert.equal(r.body[3].var, 'v');
assert.deepEqual(r.symbols, [{ name: 'xs', type: 'List[Int]' }]);
const loop = run('def main():\n for i in range(2):\n print(i)\n');
assert.equal(loop.trace.steps.filter((s) => s.text === 'no more items').length, 1, 'the loop running out is a step, as a while loop\'s final test is');
});
test('Stage 5: generated inputs find what stops a compiled Mojo program, and nothing else', async () => {
const { mojoProperties } = await import('../dist/mojo/properties.js');
const src = 'def last(xs: List[Int]) -> Int:\n return xs[len(xs) - 1]\n\n\ndef ratio(a: Int, b: Int) -> Int:\n return a // b\n\n\ndef parse(s: String) raises -> Int:\n return atol(s)\n\n\ndef spin(n: Int) -> Int:\n var i = 0\n while i != n:\n i += 2\n return i\n\n\ndef grow(mut xs: List[Int]):\n xs.append(1)\n\n\ndef main():\n pass\n';
const by = Object.fromEntries(mojoProperties(parseModule(src), src).map((r) => [r.name, r]));
assert.deepEqual(by.last.failures.map((f) => [f.kind, f.call]), [['bounds', 'last([])']], 'the smallest input that aborts');
assert.match(by.last.failures[0].message, /index -1 is out of bounds/);
assert.deepEqual(by.ratio.failures, [], 'an Int division by zero is 0 in Mojo (probes/int-div-zero), not a failure');
assert.deepEqual(by.parse.failures.map((f) => [f.kind, f.call]), [['raises-declared', 'parse("")']], 'one shape of error, its smallest input; it says raises');
assert.deepEqual(by.spin.failures.map((f) => [f.kind, f.call]), [['limit', 'spin(1)']]);
assert.match(by.grow.problem, /passed as mut/);
assert.ok(by.last.checked >= 100);
});
test('Stage 7a: overloads, with, a failing assert, exact literals, block scope, SIMD', () => {
// an overload picked by count, then by type
assert.equal(run('def area(s: Int) -> Int:\n return s * s\n\n\ndef area(w: Int, h: Int) -> Int:\n return w * h\n\n\ndef show(x: Int):\n print("int", x)\n\n\ndef show(x: String):\n print("str", x)\n\n\ndef main():\n print(area(3), area(2, 5))\n show(1)\n show("a")\n').stdout, '9 10\nint 1\nstr a\n');
// the student's context manager: __enter__, the body, __exit__; an error goes to __exit__(err), True swallows it
const cm = '@fieldwise_init\nstruct G(Copyable, Movable):\n var name: String\n\n def __enter__(self) -> Self:\n print("enter", self.name)\n return self.copy()\n\n def __exit__(self):\n print("exit", self.name)\n\n def __exit__(self, err: Error) -> Bool:\n print("exit with", err)\n return True\n\n\n';
assert.equal(run(cm + 'def main() raises:\n with G("a"):\n print("body")\n with G("b"):\n raise Error("boom")\n print("after")\n').stdout, 'enter a\nbody\nexit a\nenter b\nexit with boom\nafter\n');
// a failing assert_equal raises the compiler's AssertionError, catchable like any other (quest/MQ-950/starter)
const failed = run('from std.testing import assert_equal\n\n\ndef main() raises:\n assert_equal(150, 200)\n');
assert.match(failed.stderr, /At \/app\/example\.mojo:5:17: AssertionError: `left == right` comparison failed:\n left: 150\n right: 200/);
// literal-only arithmetic is exact (manual: 3.0 * (4.0 / 3.0 - 1.0) is 1.0)
assert.equal(run('def main():\n var three = 3.0\n print(3.0 * (4.0 / 3.0 - 1.0), three * (4.0 / three - 1.0))\n').stdout, '1.0 0.9999999999999998\n');
// var is block-scoped: an inner var shadows, and the outer one is untouched
assert.equal(run('def main():\n var num = 1\n if num == 1:\n var num = 2\n print(num)\n print(num)\n').stdout, '2\n1\n');
// SIMD (probes/simd-basics), a scalar applying to every lane
assert.equal(run('def main():\n var v = SIMD[DType.int32, 4](1, 5, 1, 9)\n print(v + 1, v.reduce_max())\n').stdout, '[2, 6, 2, 10] 9\n');
});
test('Stage 7b: closures capture as their capture lists say (the manual\'s closures chapter)', () => {
// imm holds a reference: a later change is seen (test_imm_sees_live_updates)
assert.equal(run('def main():\n var limit = 10\n\n def check(x: Int) {imm limit} -> Bool:\n return x < limit\n\n print(check(5))\n limit = 3\n print(check(5))\n').stdout, 'True\nFalse\n');
// var is a snapshot taken when the closure is made (test_var_snapshot)
assert.equal(run('def main():\n var v = 42\n\n def frozen() {var v} -> Int:\n return v\n\n v = 999\n print(frozen(), v)\n').stdout, '42 999\n');
// a default alone, and a default with an override (test_default_convention)
assert.equal(run('def main():\n var a = 1\n var b = 2\n var z = "snap"\n\n def mixed() {mut, var z} -> String:\n a += 10\n b += 20\n return String(a) + " " + String(b) + " " + z\n\n print(mixed())\n z = "changed"\n print(mixed())\n').stdout, '11 22 snap\n21 42 snap\n');
// a lambda captures as imm: a later call sees new values (manual functions/lambda)
assert.equal(run('def main():\n var x = 3\n var f = lambda (k: Int) -> Int: k * x\n print(f(2))\n x = 5\n print(f(2))\n').stdout, '6\n10\n');
// a named function passed as a value, called through the parameter
assert.equal(run('def twice(n: Int) -> Int:\n return n * 2\n\n\ndef apply(f: def(Int) -> Int, n: Int) -> Int:\n return f(n)\n\n\ndef main():\n print(apply(twice, 4))\n').stdout, '8\n');
// without a capture list, using an outer variable: the default is not documented, so declined
assert.match(run('def main():\n var a = 1\n\n def f() -> Int:\n return a\n\n print(f())\n').refused, /without a capture list/);
});
+145
View File
@@ -0,0 +1,145 @@
// Stage 13a/13d: the best-practices rules. Every rule is held to its own example (the before and after both read,
// run, and print the same; applying the rule to the before gives the after), to the harvested standard-library
// index (every method a rewrite names exists), and to Modular's own tested programs (it must stay quiet there,
// within a small allowlist). Findings are always notes; applying every fix keeps a program passing the checker
// and printing what it printed.
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 { RULES, findPractices, applyPractices, applyQuickFix, practiceDiagnostics, PRACTICES_ORIGIN } from '../dist/mojo/practices.js';
import { checkMojo } from '../dist/mojo/check.js';
import { runMojo } from '../dist/mojo/interp.js';
import { pseudoToMojo } from '../dist/sync/pseudo-to-mojo.js';
import { programs } from './helpers/sync-programs.mjs';
import { EXERCISES } from '../dist/exercises/index.js';
const here = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(here, '..');
const errorsOf = (s) => checkMojo(s).diagnostics.filter((d) => d.severity === 'error');
const run = (s) => { const m = checkMojo(s); return runMojo(m.module, s); };
test('each rule\'s example: both programs read, run, and print the same; the rule fires on the before and not the after', () => {
const ids = new Set();
for (const r of RULES) {
assert.ok(!ids.has(r.id), `duplicate rule ${r.id}`); ids.add(r.id);
assert.ok(r.title && r.why && r.docUrl.startsWith('https://mojolang.org/docs/manual/'), r.id);
const { before, after, beforeRefused } = r.example;
if (beforeRefused) assert.ok(errorsOf(before).some((d) => d.message.includes(beforeRefused)), `${r.id}: the compiler should refuse the before`);
else assert.deepEqual(errorsOf(before), [], `${r.id}: before`);
assert.deepEqual(errorsOf(after), [], `${r.id}: after`);
const a = run(after);
assert.equal(a.refused, null, `${r.id}: the interpreter runs the after (${a.refused})`);
if (!beforeRefused) {
const b = run(before);
assert.equal(b.refused, null, `${r.id}: the interpreter runs the before (${b.refused})`);
assert.equal(a.stdout, b.stdout, `${r.id}: same output`);
}
assert.ok(findPractices(before).some((f) => f.rule === r.id), `${r.id}: fires on the before`);
assert.ok(!findPractices(after).some((f) => f.rule === r.id), `${r.id}: quiet on the after`);
const first = findPractices(before).find((f) => f.rule === r.id);
if (first.fix) assert.equal(applyPractices(before, new Set([r.id])).source, after, `${r.id}: the fix turns the before into the after`);
else assert.equal(r.id, 'comptime-constant', 'only a rule that cannot promise the rewrite goes without a fix');
}
});
test('grounding: every standard-library method a rewrite names is in the harvested index', () => {
const index = JSON.parse(fs.readFileSync(path.join(root, 'mojo', 'corpus', 'stdtypes', 'stdtypes.json'), 'utf8')).structs;
let n = 0;
for (const r of RULES) for (const [type, method] of r.apis) { n++; assert.ok(index[type]?.methods.includes(method), `${r.id}: ${type}.${method}`); }
assert.ok(n >= 4);
});
function mojoFiles(dir, out = []) {
for (const f of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, f.name);
if (f.isDirectory()) mojoFiles(p, out); else if (p.endsWith('.mojo')) out.push(p);
}
return out;
}
// What the rules say about Modular's own programs, which are tested and often written to teach one thing at a time
// (a variable declared apart from its value to show how `var` works, `imm` written out to name the convention). Each
// number is a reviewed count; a rule that starts firing more than this on code that is already good is too noisy.
const ALLOWED = { 'declare-then-assign': 10, 'augmented-assign': 6, 'argument-convention': 4, 'unneeded-raises': 3, 'comptime-constant': 3, 'codepoint-loop': 0, 'reserve': 2, 'transfer-last-use': 2, 'explicit-copy': 0 };
test('false-positive gate: over mojo/corpus/manual/ the rules fire no more than the reviewed allowlist', () => {
const files = mojoFiles(path.join(root, 'mojo', 'corpus', 'manual'));
assert.ok(files.length >= 100, `${files.length} programs`);
const counts = {};
for (const f of files) for (const x of findPractices(fs.readFileSync(f, 'utf8'))) counts[x.rule] = (counts[x.rule] ?? 0) + 1;
for (const r of RULES) assert.ok((counts[r.id] ?? 0) <= ALLOWED[r.id], `${r.id} fired ${counts[r.id] ?? 0} times on Modular's programs (allowed ${ALLOWED[r.id]})`);
});
test('findings are notes from "best practices", never errors or warnings, and a source that does not read gets none', () => {
for (const p of programs().filter((x) => x.id.startsWith('ex:'))) {
const r = pseudoToMojo(p.pseudo, { call: p.call });
if ('unsupported' in r) continue;
for (const d of practiceDiagnostics(r.source)) {
assert.equal(d.severity, 'note', p.id);
assert.equal(d.origin, PRACTICES_ORIGIN);
assert.ok(d.id.startsWith('practice-') && d.docUrl && d.message, p.id);
}
}
assert.deepEqual(findPractices('def main(:\n var x: Int\n x = 1\n'), []);
});
test('count-base: the literal translation draws the declare-then-assign, += and codepoint-loop findings', () => {
const ex = EXERCISES.find((e) => e.id === 'count-base');
const src = pseudoToMojo(ex.sources.pseudo, { call: ex.call }).source;
const found = new Set(findPractices(src).map((f) => f.rule));
for (const id of ['declare-then-assign', 'augmented-assign', 'codepoint-loop']) assert.ok(found.has(id), id);
const loop = findPractices(src).find((f) => f.rule === 'codepoint-loop');
assert.ok(loop.pseudoHint && /FOR EACH/.test(loop.pseudoHint));
// the same code pasted anywhere (the Developer area) draws the same findings
assert.deepEqual(new Set(findPractices(src.split('\n').slice(1).join('\n')).map((f) => f.rule)), found);
});
test('idiomatic version: for every exercise the safe rewrites keep the program checking and printing the same', () => {
let changed = 0, compared = 0;
for (const p of programs().filter((x) => x.id.startsWith('ex:'))) {
const r = pseudoToMojo(p.pseudo, { call: p.call });
if ('unsupported' in r) continue;
const { source, applied } = applyPractices(r.source);
if (!applied.length) { assert.equal(source, r.source); continue; }
changed++;
assert.ok(errorsOf(source).length <= errorsOf(r.source).length, `${p.id}: the rewrite introduced an error`);
const a = run(r.source), b = run(source);
if (a.refused === null && b.refused === null) { compared++; assert.equal(b.stdout, a.stdout, `${p.id}: same output`); }
else assert.equal(b.refused === null, a.refused === null, `${p.id}: the interpreter should run both or neither (${a.refused} / ${b.refused})`);
assert.ok(source.startsWith(r.source.split('\n')[0]), `${p.id}: the header comment stays`);
}
assert.ok(changed >= 5 && compared >= 5, `${changed} changed, ${compared} compared`);
});
test('Developer area: applying every fix to each mojo/local program keeps it checking and printing the same', () => {
const dir = path.join(root, 'mojo', 'local');
let fired = 0;
for (const f of fs.readdirSync(dir).filter((x) => x.endsWith('.mojo'))) {
const src = fs.readFileSync(path.join(dir, f), 'utf8');
const { source, applied } = applyPractices(src);
fired += applied.length;
assert.ok(errorsOf(source).length <= errorsOf(src).length, `${f}: an error appeared`);
const a = run(src), b = run(source);
if (a.refused === null) { assert.equal(b.refused, null, `${f}: ${b.refused}`); assert.equal(b.stdout, a.stdout, f); }
}
assert.ok(fired >= 1, 'the rules find something to say somewhere in the exercises\' own programs');
});
test('a fix is the edit the editor makes: whole lines, from line to endLine', () => {
const src = 'a\nb\nc\nd\n';
assert.equal(applyQuickFix(src, { label: '', line: 2, endLine: 3, col: 1, endCol: null, text: 'X\nY\nZ' }), 'a\nX\nY\nZ\nd\n');
});
test('every exercise\'s generated Mojo has its findings pinned (UPDATE_SNAPSHOTS=1 rewrites them for review)', () => {
const file = path.join(here, 'snapshots', 'practices.json');
const now = {};
for (const p of programs().filter((x) => x.id.startsWith('ex:'))) {
const r = pseudoToMojo(p.pseudo, { call: p.call });
if ('unsupported' in r) continue;
now[p.name] = findPractices(r.source).map((f) => `${f.line}${f.endLine ? '-' + f.endLine : ''} ${f.rule}`);
}
if (process.env.UPDATE_SNAPSHOTS) fs.writeFileSync(file, JSON.stringify(now, null, 2) + '\n');
assert.ok(fs.existsSync(file), 'no snapshot; run with UPDATE_SNAPSHOTS=1 and review the new file');
assert.deepEqual(now, JSON.parse(fs.readFileSync(file, 'utf8')), 'the findings for a generated program changed; if the change is wanted, run with UPDATE_SNAPSHOTS=1 and review the diff');
});
+52
View File
@@ -0,0 +1,52 @@
// DECLARE <name> : <type> <- <value>: one line that declares a name and gives it its starting value
// (src/services/pseudo-study.ts). The two-line form still reads exactly as before.
import test from 'node:test';
import assert from 'node:assert/strict';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
import { splitDeclareInits } from '../dist/services/pseudo-study.js';
import { pseudoToMojo } from '../dist/sync/pseudo-to-mojo.js';
import { checkMojo } from '../dist/mojo/check.js';
const fn = (decls) => `FUNCTION COUNT_BASE(B : CHAR, T : STRING) RETURNS INTEGER\n${decls}\n FOR I <- 0 TO N\n IF T[I] == B THEN\n Total <- Total + 1\n ENDIF\n NEXT I\n RETURN Total\nENDFUNCTION\n`;
const ONE = fn(' DECLARE N : INTEGER <- LENGTH(T)\n DECLARE Total : INTEGER <- 0');
const TWO = fn(' DECLARE N : INTEGER\n N <- LENGTH(T)\n DECLARE Total : INTEGER\n Total <- 0');
const CALL = `COUNT_BASE('A', "GATTACA")`;
test('the value is blanked out of the line with its columns kept, and found apart from strings and comments', () => {
const { source, inits } = splitDeclareInits('DECLARE S : STRING <- "x <- y" // a <- b\nDECLARE N : INTEGER');
assert.equal(source.split('\n')[0].length, 'DECLARE S : STRING <- "x <- y" // a <- b'.length);
assert.equal(source.split('\n')[0], `DECLARE S : STRING ${' '.repeat(12)}// a <- b`);
assert.deepEqual(inits.map((i) => [i.line, i.names, i.value, i.valueCol]), [[1, ['S'], '"x <- y"', 23]]);
});
test('one line and two lines read, run, and answer the same, with no diagnostics', () => {
for (const src of [ONE, TWO]) {
const st = studyOf(src);
assert.ok(st.ok);
assert.deepEqual(st.diagnostics.filter((d) => d.severity !== 'note'), []);
assert.deepEqual(runPseudo(st, { call: CALL }).output, ['COUNT_BASE returned 3']);
}
});
test('the run shows the declaration and its starting value as steps of the same line', () => {
const tr = runPseudo(studyOf(ONE), { call: CALL });
assert.deepEqual(tr.steps.filter((s) => s.line === 2).map((s) => s.text), ['N is INTEGER, with no value yet.', 'N becomes 7.']);
});
test('the Mojo is var x: T = value for the one-line form, and stays two lines for the two-line form', () => {
const one = pseudoToMojo(ONE, { call: CALL }).source;
assert.match(one, /^ {4}var n: Int = t\.count_codepoints\(\)$/m);
assert.match(one, /^ {4}var total: Int = 0$/m);
assert.doesNotMatch(one, /^ {4}n = /m);
assert.deepEqual(checkMojo(one).diagnostics.filter((d) => d.severity === 'error'), []);
const two = pseudoToMojo(TWO, { call: CALL }).source;
assert.match(two, /^ {4}var n: Int\n {4}n = t\.count_codepoints\(\)$/m);
});
test('a mistake in the form says what the form is', () => {
const msg = (src) => studyOf(src + '\n').diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join(' | ');
assert.match(msg('DECLARE A, B : INTEGER <- 0'), /names one variable/);
assert.match(msg('DECLARE A : INTEGER <-'), /nothing on the right of its arrow/);
assert.match(msg('DECLARE A <- 3'), /needs its type before a starting value/);
assert.match(msg('DECLARE A : INTEGER <- "x"'), /INTEGER values, and "x" is a STRING/);
});
+129
View File
@@ -0,0 +1,129 @@
// The reference is only trustworthy if its examples run through the real parsers.
import test from 'node:test';
import assert from 'node:assert/strict';
import { REFERENCE, PSEUDO_REF, LINGO_REF, SPECO_REF, ASM_REF, MATHO_REF, explain, SPECO_SYMBOLS } from '../dist/core/reference.js';
import { parseMatho } from '../dist/services/matho.js';
import { studyOf } from '../dist/services/pseudo.js';
import { readLingo } from '../dist/services/lingo.js';
import { parseSpeco, toAscii, toMath } from '../dist/services/speco.js';
import { parseAsm } from '../dist/services/asm.js';
import { KEYWORDS, BUILTINS } from '../dist/engines/pseudo/lang.js';
import { OPS } from '../dist/engines/asm86/interp.js';
test('every pseudocode statement example parses and checks', () => {
const sec = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-statements');
for (const e of sec.entries) {
const st = studyOf(e.example);
const errs = st.diagnostics.filter((d) => d.severity === 'error');
assert.equal(errs.length, 0, `${e.term}: ${errs.map((d) => `L${d.line} ${d.message}`).join(' | ')}`);
}
});
test('every keyword in the engine has a meaning in the reference, and vice versa', () => {
const sec = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-keywords');
const listed = new Set(sec.entries.map((e) => e.term));
for (const k of KEYWORDS) assert.ok(listed.has(k), `keyword ${k} is missing from the reference`);
for (const e of sec.entries) assert.ok(e.meaning.length > 10, `${e.term} has no meaning`);
for (const b of Object.keys(BUILTINS)) assert.ok(explain('pseudo', b), `built-in ${b} has no hover`);
});
test('every lingo sentence example reads (inside a minimal routine where needed)', () => {
const failures = [];
// kinds for every name the examples mention, so a fragment reads the same way it does on the page
const kinds = 'T is a text. P is a text. S is a text. w is a text. A is a whole number. Score is a whole number.';
const setup = [
'Start with Count at 0.', 'Start with Best at 0.', 'Start with Run at 0.', 'Start with Left at 3.', 'Start with window at "AC".',
'Start with an empty list called Starts.', 'Start with an empty tally called Counts.', 'Start with an empty text called R.',
];
const defs = "The complement of b is 'T' when b is 'A', and 'N' otherwise.";
for (const sec of LINGO_REF.sections.filter((s) => s.id.startsWith('lingo-forms'))) {
for (const e of sec.entries) {
if (!e.example) continue;
const ex = e.example.trim();
const examples = ex.includes(' ') && !/^To /.test(ex) && !/ when /.test(ex) ? ex.split(/\s{2,}/) : [ex];
for (const one of examples) {
let src;
const head = 'To test T, P, S, w, A, B and Score:';
const pre = setup.map((l) => ' ' + l).join('\n');
if (/^The answer is/.test(one)) src = `${head}\n${pre}\n Start with i at 0.\n ${one}\n\n${kinds}`;
else if (/^To /.test(one)) src = `${one}\n${pre}\n The answer is Count.\n\n${kinds}`;
else if (/ when /.test(one) || /^The .* is /.test(one)) src = `To test P, T, S and b:\n Start with i at 0.\n If b is 'A', add 1 to i.\n The answer is the length of T plus the length of P plus the length of S plus i.\n\n${one}${/[.:]$/.test(one) ? '' : '.'}\n\nP is a text. T is a text. S is a text. b is a letter.`;
else if (/^(For|Repeat)/.test(one)) src = `${head}\n${pre}\n${/^For (each|every) .*\bi\b/.test(one) ? '' : ' Start with i at 0.\n'} ${one}\n Add 1 to Count.\n The answer is Count.\n\n${kinds}`;
else if (/^If /.test(one) && /:$/.test(one)) src = `${head}\n${pre}\n ${one}\n Add 1 to Count.\n The answer is Count.\n\n${kinds}`;
else if (sec.title === 'Tests') src = `${head}\n${pre}\n For each position i of T:\n Set b to the letter of T at i.\n ${/^If /.test(one) ? one : `If ${one}, add 1 to Count.`}\n The answer is Count.\n\n${kinds}`;
else if (sec.title === 'Values') src = `${head}\n${pre}\n For each position i of T:\n Set X to ${one}.\n The answer is Count.\n\n${kinds}`;
else src = `${head}\n${pre}\n Start with i at 0.\n${/\bb\b/.test(one) && !/\bB\b/.test(one) ? ' Set b to the letter of T at i.\n' : ''} ${one}\n The answer is Count.\n\n${/complement/.test(one) ? defs + '\n\n' : ''}${kinds}${/^Swap/.test(one) ? ' B is a whole number.' : ''}`;
const r = readLingo(src);
const tolerated = ['pronoun', 'kind-clash', 'unset-read', 'borrowed-input', 'which-letter', 'never-read', 'unreachable'];
const errs = r.diagnostics.filter((d) => d.severity === 'error' && !tolerated.includes(d.id));
if (errs.length) failures.push(`${e.term} → "${one}": ${errs.map((d) => d.id + ': ' + d.message.slice(0, 100)).join(' | ')}`);
}
}
}
assert.deepEqual(failures, []);
});
test('every lingo ambiguity example raises exactly the question it documents', () => {
const sec = LINGO_REF.sections.find((s) => s.id === 'lingo-questions');
for (const e of sec.entries) {
const r = readLingo(e.example);
assert.ok(r.diagnostics.some((d) => d.id === e.term), `${e.term}: got ${r.diagnostics.map((d) => d.id).join(', ')}`);
}
});
test('every speco example parses, and the symbol table round-trips', () => {
const sec = SPECO_REF.sections.find((s) => s.id === 'speco-forms');
for (const e of sec.entries) {
const m = parseSpeco(e.example);
assert.ok(m.ok, `${e.term}: ${m.diagnostics.map((d) => d.message).join(' | ')}`);
}
for (const s of SPECO_SYMBOLS) {
if (s.math.length !== 1) continue;
assert.equal(toAscii(`A ${s.math} B`), `A ${s.ascii} B`);
assert.equal(toMath(`A ${s.ascii} B`), `A ${s.math} B`);
}
});
test('every instruction has a gloss and a hover; setup examples parse', () => {
for (const op of Object.keys(OPS)) assert.ok(explain('asm', op)?.body, `${op} has no gloss`);
for (const e of ASM_REF.sections.find((s) => s.id === 'asm-registers').entries) assert.ok(explain('asm', e.term), e.term);
assert.ok(parseAsm(' mov rax, [rbx]\n ret\n').ok);
assert.ok(REFERENCE.length === 5);
});
test('every pseudo idiom runs and prints what its note says', async () => {
const { runPseudo } = await import('../dist/services/pseudo.js');
const { KEYWORDS: KW, BUILTINS: BI, CONTEXTUAL } = await import('../dist/engines/pseudo/lang.js');
const sec = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-idioms');
assert.ok(sec.entries.length >= 8);
for (const e of sec.entries) {
const st = studyOf(e.example);
const errs = st.diagnostics.filter((d) => d.severity === 'error');
assert.equal(errs.length, 0, `${e.term}: ${errs.map((d) => `L${d.line} ${d.message}`).join(' | ')}`);
const tr = runPseudo(st, { input: [], maxSteps: 200000 });
assert.equal(tr.error, null, `${e.term}: ${tr.error?.message}`);
const out = tr.output.filter((l) => !/^\w+ returned /.test(l));
assert.deepEqual(out, [e.note], e.term);
}
const reserved = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-reserved');
const words = new Set(reserved.entries.map((e) => e.term));
for (const k of KW) assert.ok(words.has(k), `reserved list misses keyword ${k}`);
for (const k of Object.keys(CONTEXTUAL)) assert.ok(words.has(k), `reserved list misses ${k}`);
for (const k of Object.keys(BI)) assert.ok(words.has(k), `reserved list misses built-in ${k}`);
});
test('every matho example translates to pseudo that checks, and every refusal is refused', () => {
const forms = MATHO_REF.sections.find((s) => s.id === 'matho-forms');
for (const e of forms.entries) {
const r = parseMatho(e.example);
const errs = r.diagnostics.filter((d) => d.severity === 'error');
assert.ok(r.ok, `${e.term}: ${errs.map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | ')}\n${r.program.pseudo}`);
const st = studyOf(r.program.pseudo);
assert.ok(st.ok, `${e.term}: the pseudo does not check: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join(' | ')}`);
}
for (const src of ['y = \\int_0^1 x dx', 's = \\sum_{i=0}^{\\infty} x_i', 'y = \\lim_{x \\to 0} x', 'y = \\partial f', 'y = a \\pm b', 'a = b = c']) {
const r = parseMatho(src);
assert.equal(r.ok, false, `${src} should be refused`);
assert.ok(r.diagnostics.some((d) => d.severity === 'error' && /matho-(refused|pm|chain)/.test(d.id)), src);
}
});
+100
View File
@@ -0,0 +1,100 @@
// The Run view (core/runview.ts): the run top to bottom, one row per step, the variables the student follows as
// columns with their value after each step, and the lifecycle of the value each names. Checked against the
// interpreters' own traces for every exercise, in pseudo and in Mojo.
import test from 'node:test';
import assert from 'node:assert/strict';
import { Session } from '../dist/core/session.js';
import { PROBLEMS, stateForProblem, problemById } from '../dist/core/catalog.js';
import { runColumns, runRows, defaultFollow, valuesAt, rulerCells, gridCells } from '../dist/core/runview.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
const traceOf = (p, lang) => {
const st = stateForProblem(p); st.lang = lang;
const s = new Session(st, { area: lang === 'mojo' ? 'dev' : null, storageId: null });
return s.trace();
};
const RUNS = [];
for (const p of PROBLEMS) for (const lang of ['pseudo', 'mojo']) {
if (!(p.sources?.[lang] ?? '').trim()) continue;
const tr = traceOf(p, lang);
if (tr && tr.count) RUNS.push({ id: `${p.id} (${lang})`, tr });
}
test('every run with a trace has its variables as columns, and a suggestion to follow', () => {
assert.ok(RUNS.length >= 25, `the exercises give runs to check (${RUNS.length})`);
for (const { id, tr } of RUNS) {
const cols = runColumns(tr);
const keys = new Set(cols.map((c) => c.key));
assert.equal(keys.size, cols.length, `${id}: one column per routine and name`);
const follow = defaultFollow(cols);
assert.ok(follow.length <= 5 && follow.every((k) => keys.has(k)), `${id}: the suggestion is at most five of the run's variables`);
if (cols.some((c) => c.writes && c.name !== 'self' && c.name !== '_')) assert.ok(follow.length, `${id}: something is suggested when something changes`);
assert.ok(!follow.some((k) => k.endsWith('.self') || k.endsWith('._')), `${id}: never self or _`);
assert.equal(new Set(follow.map((k) => k.split('.')[0])).size <= 1, true, `${id}: the suggestion is one routine's variables`);
}
});
test('each row is one step, and each cell is the variable\'s value after that step', () => {
for (const { id, tr } of RUNS) {
const cols = runColumns(tr);
const follow = cols.map((c) => c.key).slice(0, 12);
const rows = runRows(tr, follow);
assert.equal(rows.length, tr.count, `${id}: a row per step`);
rows.forEach((r, i) => {
assert.equal(r.step, i);
assert.equal(r.line, tr.steps[i].line ?? null);
const after = valuesAt(tr, follow, i + 1);
for (const v of after) {
const c = r.cells[v.key];
assert.equal(c.text, v.inScope ? v.text : null, `${id} step ${i + 1} ${v.key}: the cell is the value after the step`);
if (c.wrote) assert.ok((tr.steps[i].writes ?? []).some((w) => w.name === v.key.split('.').slice(1).join('.')), `${id} step ${i + 1}: ${v.key} marked written only when the step writes it`);
}
});
// a window of the run is the same rows
const mid = Math.floor(tr.count / 2);
assert.deepEqual(runRows(tr, follow, mid, mid + 3).map((r) => r.step), [mid, mid + 1, mid + 2].filter((x) => x < tr.count));
// only the steps that change something: a subset, and every step that writes is kept
const few = runRows(tr, follow, 0, tr.count, { onlyWrites: true });
assert.ok(few.length <= rows.length);
for (let i = 0; i < tr.count; i++) if ((tr.steps[i].writes ?? []).length) assert.ok(few.some((r) => r.step === i), `${id}: step ${i + 1} writes, so it stays`);
// what each step printed adds up to the run's output
assert.deepEqual(rows.flatMap((r) => r.printed), tr.output.slice(0, tr.states[tr.count]?.outputLength ?? tr.output.length));
}
});
test('the lifecycle of the value a variable names is in that variable\'s column', () => {
const tr = traceOf(problemById('demo'), 'mojo');
const events = tr.steps.flatMap((s) => (s.life ?? []).filter((e) => e.kind !== 'named'));
assert.ok(events.length > 0, 'the demo makes, moves and destroys values');
const cols = runColumns(tr);
const all = cols.map((c) => c.key);
const rows = runRows(tr, all);
const marks = rows.flatMap((r) => Object.values(r.cells).flatMap((c) => c.life));
const others = rows.flatMap((r) => r.otherLife);
assert.ok(marks.length > 0, 'some events land on a named variable');
// every event is shown once for each place it belongs: a move is a value moved in and a value moved out
const expected = events.reduce((n, e) => n + (e.kind === 'moved' && e.from != null ? 2 : 1), 0);
const named = new Map();
for (const s of tr.steps) for (const e of s.life ?? []) if (e.kind === 'named') named.set(e.id, (named.get(e.id) ?? 0) + 1);
assert.ok(marks.length + others.length >= expected, `every event is shown (${marks.length} in columns, ${others.length} beside the step, ${expected} events)`);
for (const r of rows) for (const c of Object.values(r.cells)) for (const m of c.life) assert.match(m.kind, /^(made|copied|moved-in|moved-out|destroyed)$/);
// following no variable puts every event beside its step
const bare = runRows(tr, []);
assert.equal(bare.reduce((n, r) => n + r.otherLife.length, 0), expected);
});
test('strings and lists get a ruler, tables a grid', () => {
assert.deepEqual(rulerCells({ value: 'GAT', type: 'STRING' }), ['G', 'A', 'T']);
assert.equal(rulerCells({ value: 'G', type: 'CHAR' }), null, 'one character is not a ruler');
assert.deepEqual(rulerCells({ text: '"GAT"', value: { k: 'str', v: 'GAT' } }), ['G', 'A', 'T'], 'a Mojo String');
assert.deepEqual(rulerCells({ text: '[1, 2]', value: { k: 'list', items: [{ k: 'int', v: 1n }, { k: 'int', v: 2n }] } }), ['1', '2'], 'a Mojo List[Int]');
assert.equal(rulerCells({ value: 'x'.repeat(81) }), null, 'too long to draw');
const ed = traceOf(problemById('edit-distance'), 'pseudo');
const d = runColumns(ed).find((c) => c.name === 'D');
assert.ok(d, 'edit-distance has its table D');
const [v] = valuesAt(ed, [d.key], ed.count - 1);
assert.ok(v.grid && v.grid.length > 1 && v.grid[0].length > 1, 'D is drawn as a grid');
assert.ok(v.grid.flat().some((x) => /^\d+$/.test(x)), 'with its numbers in the cells');
assert.equal(gridCells({ value: [[1, 2], 3] }), null, 'not a table');
});
+68
View File
@@ -0,0 +1,68 @@
{
"count-base": [
"6-8 declare-then-assign",
"7-9 declare-then-assign",
"11-13 codepoint-loop",
"15 augmented-assign"
],
"count-base-proved": [
"6-8 declare-then-assign",
"7-9 declare-then-assign",
"11-12 codepoint-loop",
"13 augmented-assign"
],
"match": [
"6-10 declare-then-assign",
"7-11 declare-then-assign"
],
"match-proved": [
"8-12 declare-then-assign",
"9-13 declare-then-assign"
],
"match-off-by-one": [
"6-10 declare-then-assign",
"7-11 declare-then-assign"
],
"match-reset": [
"5-9 declare-then-assign",
"6-10 declare-then-assign",
"7-11 declare-then-assign"
],
"first-unknown": [
"7-8 declare-then-assign",
"11 augmented-assign"
],
"gc-fraction": [
"6-7 declare-then-assign",
"8-9 codepoint-loop",
"11 augmented-assign"
],
"hamming": [
"7-8 declare-then-assign",
"12 augmented-assign"
],
"best-position": [
"8-9 declare-then-assign"
],
"tally": [
"7-8 codepoint-loop"
],
"kmers": [],
"revcomp": [
"6-8 declare-then-assign",
"20 augmented-assign"
],
"binary-search": [
"6-9 declare-then-assign",
"7-10 declare-then-assign"
],
"edit-distance": [
"5-7 declare-then-assign",
"6-8 declare-then-assign"
],
"translate-codons": [
"11-18 declare-then-assign",
"27 augmented-assign",
"29 augmented-assign"
]
}
@@ -0,0 +1,19 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# The position of the highest quality score; the first one, if several tie.
def best_position(q: List[Int]) -> Int:
# REQUIRE LENGTH(Q) >= 1
# ENSURE 0 <= RESULT AND RESULT < LENGTH(Q)
# ENSURE FORALL K IN [0, LENGTH(Q)) : Q[K] <= Q[RESULT]
var best: Int
best = 0
# INVARIANT 0 <= Best AND Best < I
# INVARIANT FORALL K IN [0, I) : Q[K] <= Q[Best]
for i in range(1, len(q)):
if q[i] > q[best]:
best = i
return best
def main():
print(best_position(List[Int](12, 30, 25, 30, 8)))
@@ -0,0 +1,25 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# Is X in the sorted list L? Halve the range that could hold it each time.
def contains_sorted(l: List[Int], x: Int) -> Bool:
# REQUIRE FORALL K IN [0, LENGTH(L) - 1) : L[K] <= L[K + 1]
var lo: Int
var hi: Int
var mid: Int
lo = 0
hi = len(l)
# INVARIANT 0 <= Lo AND Lo <= Hi AND Hi <= LENGTH(L)
# DECREASES Hi - Lo
while lo < hi:
mid = (lo + hi) // 2
if l[mid] == x:
return True
elif l[mid] < x:
lo = mid + 1
else:
hi = mid
return False
def main():
print(contains_sorted(List[Int](2, 5, 8, 13, 21), 13))
@@ -0,0 +1,18 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# How many positions of T hold the base B, with its specification.
def count_base(b: String, t: String) -> Int:
# ENSURE RESULT == LENGTH({ K IN [0, LENGTH(T)) : T[K] == B })
var n: Int
var total: Int
n = t.count_codepoints()
total = 0
# INVARIANT Total == LENGTH({ K IN [0, I) : T[K] == B })
for i in range(n):
if String(t[codepoint=i]) == b:
total = total + 1
return total
def main():
print(count_base("A", "GATTACA"))
+21
View File
@@ -0,0 +1,21 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# How many positions of T hold the base B.
def count_base(b: String, t: String) -> Int:
# Set up
var n: Int
var total: Int
n = t.count_codepoints()
total = 0
# Visit each position
for i in range(n):
# Test this position
if String(t[codepoint=i]) == b:
# Update the running total
total = total + 1
# Report
return total
def main():
print(count_base("A", "GATTACA"))
@@ -0,0 +1,31 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# The fewest single-letter edits (substitute, insert, delete) that turn A into B.
def edit_distance(a: String, b: String) -> Int:
var n: Int
var m: Int
n = a.count_codepoints()
m = b.count_codepoints()
var d = List[List[Int]]()
for _ in range(n + 1):
var _row1 = List[Int]()
for _ in range(m + 1):
_row1.append(0)
d.append(_row1^)
for i in range(n + 1):
d[i][0] = i
for j in range(m + 1):
d[0][j] = j
for i in range(1, n + 1):
for j in range(1, m + 1):
var cost: Int
if String(a[codepoint=i - 1]) == String(b[codepoint=j - 1]):
cost = 0
else:
cost = 1
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost)
return d[n][m]
def main():
print(edit_distance("GATTACA", "GCATGCA"))
@@ -0,0 +1,16 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# The position of the first N in T, or LENGTH(T) when there is none.
def first_unknown(t: String) -> Int:
# ENSURE 0 <= RESULT AND RESULT <= LENGTH(T)
# ENSURE FORALL K IN [0, RESULT) : T[K] != 'N'
var i: Int
i = 0
# INVARIANT FORALL K IN [0, I) : T[K] != 'N'
while i < t.count_codepoints() and String(t[codepoint=i]) != "N":
i = i + 1
return i
def main():
print(first_unknown("GATNACA"))
@@ -0,0 +1,16 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# The fraction of T that is G or C.
def gc_fraction(t: String) -> Float64:
# REQUIRE LENGTH(T) > 0
var gc: Int
gc = 0
for _b_pos1 in range(t.count_codepoints()):
var b = String(t[codepoint=_b_pos1])
if b == "G" or b == "C":
gc = gc + 1
return Float64(gc) / Float64(t.count_codepoints())
def main():
print(gc_fraction("GATTACA"))
+17
View File
@@ -0,0 +1,17 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# How many positions differ between two reads of the same length.
def hamming(a: String, b: String) -> Int:
# REQUIRE LENGTH(A) == LENGTH(B)
# ENSURE RESULT == LENGTH({ K IN [0, LENGTH(A)) : A[K] != B[K] })
var differences: Int
differences = 0
# INVARIANT Differences == LENGTH({ K IN [0, I) : A[K] != B[K] })
for i in range(a.count_codepoints()):
if String(a[codepoint=i]) != String(b[codepoint=i]):
differences = differences + 1
return differences
def main():
print(hamming("GATTACA", "GACTATA"))
+21
View File
@@ -0,0 +1,21 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# How many times each word of K letters occurs in T.
def kmer_counts(t: String, k: Int) raises -> Dict[String, Int]:
# REQUIRE K >= 1
var counts = Dict[String, Int]()
var word: String
for i in range(t.count_codepoints() - k + 1):
var _piece2 = String("")
for _at1 in range(i, i + k):
_piece2 += String(t[codepoint=_at1])
word = _piece2
if word in counts:
counts[word] = counts[word] + 1
else:
counts[word] = 1
return counts.copy()
def main() raises:
print(kmer_counts("GATTACAGATT", 3))
@@ -0,0 +1,27 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# One bound is wrong. Every position where pattern P occurs exactly in text T.
def match_(p: String, t: String) -> List[Int]:
# Set up
var n: Int
var m: Int
var matched: Bool
var answer = List[Int]()
n = t.count_codepoints()
m = p.count_codepoints()
# Visit each legal start
for i in range(n):
# Test: does the pattern match here?
matched = True
for j in range(m):
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
matched = False
break
# Update
if matched:
answer.append(i)
return answer.copy()
def main():
print(match_("ATT", "GATTACA"))
@@ -0,0 +1,28 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# MATCH, with the specification from "Specifying the problem",
# and the two invariants that prove it.
def match_(p: String, t: String) -> List[Int]:
# REQUIRE LENGTH(P) >= 1
# ENSURE RESULT == SORTED({ S IN [0, LENGTH(T) - LENGTH(P) + 1) : FORALL K IN [0, LENGTH(P)) : T[S + K] == P[K] })
var n: Int
var m: Int
var matched: Bool
var answer = List[Int]()
n = t.count_codepoints()
m = p.count_codepoints()
# INVARIANT Answer == SORTED({ S IN [0, I) : FORALL K IN [0, M) : T[S + K] == P[K] })
for i in range(n - m + 1):
matched = True
# INVARIANT Matched == (FORALL K IN [0, J) : T[I + K] == P[K])
for j in range(m):
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
matched = False
break
if matched:
answer.append(i)
return answer.copy()
def main():
print(match_("ATT", "GATTACA"))
@@ -0,0 +1,23 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# Matched is reset in the wrong place.
def match_(p: String, t: String) -> List[Int]:
var n: Int
var m: Int
var matched: Bool
var answer = List[Int]()
n = t.count_codepoints()
m = p.count_codepoints()
matched = True
for i in range(n - m + 1):
for j in range(m):
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
matched = False
break
if matched:
answer.append(i)
return answer.copy()
def main():
print(match_("GC", "GATGCA"))
+27
View File
@@ -0,0 +1,27 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# Every position where pattern P occurs exactly in text T.
def match_(p: String, t: String) -> List[Int]:
# Set up
var n: Int
var m: Int
var matched: Bool
var answer = List[Int]()
n = t.count_codepoints()
m = p.count_codepoints()
# Visit each legal start
for i in range(n - m + 1):
# Test: does the pattern match here?
matched = True
for j in range(m):
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
matched = False
break
# Update
if matched:
answer.append(i)
return answer.copy()
def main():
print(match_("ATT", "GATTACA"))
+25
View File
@@ -0,0 +1,25 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# The reverse complement of a strand: read backwards, each base swapped for its partner.
def reverse_complement(t: String) -> String:
# ENSURE LENGTH(RESULT) == LENGTH(T)
var answer: String
var partner: String
answer = ""
for i in range(t.count_codepoints() - 1, -1, -1):
if String(t[codepoint=i]) == "A":
partner = "T"
elif String(t[codepoint=i]) == "T":
partner = "A"
elif String(t[codepoint=i]) == "G":
partner = "C"
elif String(t[codepoint=i]) == "C":
partner = "G"
else:
partner = "N"
answer = answer + partner
return answer
def main():
print(reverse_complement("GATTACA"))
+17
View File
@@ -0,0 +1,17 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
# How many times each base appears in T.
def tally_bases(t: String) raises -> Dict[String, Int]:
var tally = Dict[String, Int]()
var b: String
for i in range(t.count_codepoints()):
b = String(t[codepoint=i])
if b in tally:
tally[b] = tally[b] + 1
else:
tally[b] = 1 # first sighting of this base
return tally.copy()
def main() raises:
print(tally_bases("GATTACA"))
@@ -0,0 +1,34 @@
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
from std.collections import Set
# Translate a reading frame into amino acids, stopping at the first stop codon.
def stops() -> Set[String]:
return Set[String]("TAA", "TAG", "TGA")
def translate(t: String) raises -> String:
var table = Dict[String, String]()
var protein: String
var codon: String
table["ATG"] = "M"
table["GCC"] = "A"
table["AAA"] = "K"
table["TTT"] = "F"
table["GGC"] = "G"
protein = ""
for i in range(0, t.count_codepoints() // 3 * 3, 3):
var _piece2 = String("")
for _at1 in range(i, i + 3):
_piece2 += String(t[codepoint=_at1])
codon = _piece2
if codon in stops():
break
elif codon in table:
protein = protein + table[codon]
else:
protein = protein + "X"
return protein
def main() raises:
print(translate("ATGGCCAAATAGTTT"))
+104
View File
@@ -0,0 +1,104 @@
// What each translator can and cannot say, over every exercise, every corpus program, and the single-construct
// fixtures. The test writes docs/sync-coverage.md (deterministically, so a rerun changes nothing) and holds each
// translator to a floor that can only rise.
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 { programs, EQUATIONS } from './helpers/sync-programs.mjs';
import { studyOf } from '../dist/services/pseudo.js';
import { checkMojo } from '../dist/mojo/check.js';
import { pseudoToMojo } from '../dist/sync/pseudo-to-mojo.js';
import { pseudoToLingo } from '../dist/sync/pseudo-to-lingo.js';
import { pseudoToMatho } from '../dist/sync/pseudo-to-matho.js';
import { parseMatho } from '../dist/services/matho.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
const here = path.dirname(fileURLToPath(import.meta.url));
const docPath = path.join(here, '..', 'docs', 'sync-coverage.md');
/** Floors: programs each translator says completely. They only rise. */
const FLOOR = { mojo: 76, lingo: 31, matho: 3 };
const reason = (m) => m
.replace(/^English has no sentence for /, '').replace(/^matho has no form for /, '')
.replace(/, so the (lingo|matho) pane keeps its last good text\.?$/, '').replace(/ \(line \d+\)/g, '')
.replace(/^not translated: pseudo line \d+/, 'a line with no faithful Mojo spelling');
function tally(rows) {
const by = new Map();
for (const [id, why] of rows) { const e = by.get(why) ?? []; if (!e.includes(id)) e.push(id); by.set(why, e); }
return [...by.entries()].sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0]));
}
const list = (ids) => (ids.length > 4 ? `${ids.slice(0, 4).join(', ')}, and ${ids.length - 4} more` : ids.join(', '));
function measure() {
const all = programs().map((p) => ({ ...p, parses: studyOf(p.pseudo).ok }));
const ok = all.filter((p) => p.parses);
const rows = { mojo: { full: 0, gaps: [], checker: 0, no: [] }, lingo: { full: 0, no: [] }, matho: { full: 0, no: [] } };
for (const p of ok) {
const m = pseudoToMojo(p.pseudo, { call: p.call });
if ('unsupported' in m) rows.mojo.no.push([p.id, reason(m.unsupported[0].message)]);
else {
const gaps = m.notes.filter((n) => n.id === 'translate-gap');
const clean = !checkMojo(m.source).diagnostics.some((d) => d.severity === 'error');
if (gaps.length) for (const g of gaps) rows.mojo.gaps.push([p.id, reason(g.message)]);
else if (clean) rows.mojo.full++;
else rows.mojo.checker++;
}
const l = pseudoToLingo(p.pseudo);
if ('unsupported' in l) for (const d of l.unsupported) rows.lingo.no.push([p.id, reason(d.message)]); else rows.lingo.full++;
const t = pseudoToMatho(p.pseudo);
if ('unsupported' in t) rows.matho.no.push([p.id, reason(t.unsupported[0].message)]); else rows.matho.full++;
}
const eqs = Object.entries(EQUATIONS).map(([id, [src]]) => {
const r = pseudoToMatho(parseMatho(src).program.pseudo);
return [id, 'unsupported' in r ? reason(r.unsupported[0].message) : null];
});
return { all, ok, rows, eqs };
}
function report(x) {
const { all, ok, rows, eqs } = x;
const ex = all.filter((p) => p.id.startsWith('ex:')).length;
const fx = all.filter((p) => p.id.startsWith('fixture:')).length;
const out = [];
out.push('# Translation coverage', '',
'<!-- Written by tests/sync-coverage.test.mjs. Do not edit; rerun `npm test` to refresh. -->', '',
`Each translator in \`src/sync/\` is run over every exercise with pseudo (${ex}), every program in \`tests/corpus/\` (${all.length - ex - fx}), and ${fx} one-construct fixtures (\`tests/helpers/sync-programs.mjs\`). ${all.length - ok.length} of the ${all.length} have pseudo that does not read, by design (they teach the notation's error messages), and are left out below. That leaves **${ok.length} programs**.`, '',
'| From pseudo to | Says it completely | Says it with a gap or a checker error | Cannot say it |', '| --- | ---: | ---: | ---: |',
`| Mojo | ${rows.mojo.full} | ${new Set(rows.mojo.gaps.map((g) => g[0])).size + rows.mojo.checker} | ${rows.mojo.no.length} |`,
`| lingo | ${rows.lingo.full} | 0 | ${ok.length - rows.lingo.full} |`,
`| matho | ${rows.matho.full} | 0 | ${ok.length - rows.matho.full} |`, '',
'Mojo is a program-for-program translation that always produces something: a line with no faithful spelling becomes a visible `raise Error("not translated: pseudo line N")` and a warning. Lingo and matho must read back as the same program, so what they cannot say exactly they refuse, with the construct and its line, and the pane keeps its last good text.', '');
const section = (title, rowsIn, intro) => {
out.push(`## ${title}`, '', intro, '');
if (!rowsIn.length) { out.push('Nothing.', ''); return; }
out.push('| What it has no form for | Programs | Which |', '| --- | ---: | --- |');
for (const [why, ids] of tally(rowsIn)) out.push(`| ${why.replace(/\|/g, '/')} | ${ids.length} | ${list(ids)} |`);
out.push('');
};
section('Mojo: lines that become a visible gap', rows.mojo.gaps, 'These programs translate, with the listed lines left as a `raise Error`.');
section('Mojo: programs the translator refuses', rows.mojo.no, 'Pseudo the course itself rejects is not translated.');
section('lingo: what English has no sentence for', rows.lingo.no, 'English is read by `readSteps`, whose grammar has no sentence for these.');
section('matho: what is not an equation', rows.matho.no, 'An equation lowers to one `FUNCTION` that returns a value; a program with output, text, a loop that does more than add up, or steps outside a function is not one.');
out.push('## matho: the equation sets', '',
`Each set in \`tests/matho.test.mjs\` is lowered to pseudo by the matho reader, written back as equations by \`pseudoToMatho\`, and checked: ${eqs.filter((e) => !e[1]).length} of ${eqs.length} round-trip.`, '',
'| Equation set | Round-trips | Why not |', '| --- | :---: | --- |');
for (const [id, why] of eqs) out.push(`| ${id} | ${why ? 'no' : 'yes'} | ${why ?? ''} |`);
out.push('');
return out.join('\n');
}
test('the coverage report is written, and every translator is at or above its floor', () => {
const x = measure();
assert.ok(x.rows.mojo.full >= FLOOR.mojo, `Mojo says ${x.rows.mojo.full} programs completely, below ${FLOOR.mojo}`);
assert.ok(x.rows.lingo.full >= FLOOR.lingo, `lingo says ${x.rows.lingo.full} programs, below ${FLOOR.lingo}`);
assert.ok(x.rows.matho.full >= FLOOR.matho, `matho says ${x.rows.matho.full} programs, below ${FLOOR.matho}`);
const text = report(x);
fs.mkdirSync(path.dirname(docPath), { recursive: true });
if (!fs.existsSync(docPath) || fs.readFileSync(docPath, 'utf8') !== text) fs.writeFileSync(docPath, text);
assert.equal(report(measure()), text, 'the report is deterministic');
});
+112
View File
@@ -0,0 +1,112 @@
// Stage 5: pseudo -> lingo. What is written must read (readSteps accepts it), must read as the same
// routines, must behave like the original once lowered back to pseudo, and writing it again from that
// pseudo must give the same lingo (a fixed point, comments aside). A program with no English form is
// reported as unsupported with a reason; the translator never writes lingo the reader would reject.
import test from 'node:test';
import assert from 'node:assert/strict';
import { programs, stripComments } from './helpers/sync-programs.mjs';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
import { readLingo, lowerLingo } from '../dist/services/lingo.js';
import { pseudoToLingo } from '../dist/sync/pseudo-to-lingo.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
const SUPPORTED_AT_LEAST = 31; // raise as English learns more forms; counted below
const run = (pseudo, call, input) => {
const st = studyOf(pseudo);
assert.ok(st.ok, 'pseudo should study');
const t = runPseudo(st, { call, input, maxSteps: 200000 });
return { out: t.output.map((l) => l.replace(/^\w+ returned /, 'returned ')), err: t.error ? t.error.kind : null };
};
function judge(p) {
if (!studyOf(p.pseudo).ok) return { verdict: 'no-pseudo' };
const r = pseudoToLingo(p.pseudo);
if ('unsupported' in r) return { verdict: 'unsupported', r };
const parsed = readLingo(r.source);
if (!parsed.ok) return { verdict: 'unread', detail: parsed.diagnostics.map((d) => d.message).join(' | '), r };
const low = lowerLingo(parsed, 'pseudo');
if (!low) return { verdict: 'unlowered', r };
return { verdict: 'ok', r, low };
}
const verdicts = new Map();
test('every program: the lingo reads, lowers to pseudo that behaves like the original, and is a fixed point; or the program is reported unsupported', () => {
const problems = [];
let supported = 0;
for (const p of programs()) {
const j = judge(p);
verdicts.set(p.id, j);
if (j.verdict === 'no-pseudo' || j.verdict === 'unsupported') continue;
if (j.verdict !== 'ok') { problems.push(`${p.id}: ${j.verdict}: ${j.detail ?? ''}`); continue; }
supported++;
// same behavior: the call (or the input-driven main) gives the same output and the same kind of stop
const want = run(p.pseudo, p.call, p.input);
let got;
try { got = run(j.low.source, p.call, p.input); } catch (e) { problems.push(`${p.id}: lowered pseudo does not study: ${e.message}`); continue; }
if (JSON.stringify(want) !== JSON.stringify(got)) problems.push(`${p.id}: behavior differs: ${JSON.stringify(want).slice(0, 120)} vs ${JSON.stringify(got).slice(0, 120)}`);
// fixed point
const again = pseudoToLingo(j.low.source);
if ('unsupported' in again) { problems.push(`${p.id}: the lowered pseudo is no longer supported`); continue; }
if (stripComments(again.source) !== stripComments(j.r.source)) problems.push(`${p.id}: not a fixed point`);
}
assert.deepEqual(problems, []);
assert.ok(supported >= SUPPORTED_AT_LEAST, `${supported} programs have English, fewer than ${SUPPORTED_AT_LEAST}`);
});
test('a program with no English form is unsupported with a located reason, not a crash', () => {
let n = 0;
for (const p of programs()) {
const j = verdicts.get(p.id) ?? judge(p);
if (j.verdict !== 'unsupported') continue;
n++;
assert.ok(j.r.unsupported.length > 0, p.id);
for (const d of j.r.unsupported) {
assert.ok(Number.isInteger(d.line) && d.line >= 1, `${p.id}: line`);
assert.ok(d.message.length > 0, p.id);
}
}
assert.ok(n > 0);
});
test('the line map has one entry per output line and points inside the pseudo', () => {
for (const p of programs()) {
const j = verdicts.get(p.id) ?? judge(p);
if (j.verdict !== 'ok') continue;
const lines = j.r.source.replace(/\n$/, '').split('\n');
assert.equal(j.r.map.length, lines.length, p.id);
const n = p.pseudo.split('\n').length;
for (const m of j.r.map) assert.ok(m === null || (Number.isInteger(m) && m >= 1 && m <= n), `${p.id}: ${m}`);
}
});
test('the English is plain: a counting loop and a sum read the way a student would say them', () => {
const r = pseudoToLingo(`FUNCTION SUM_TO(N : INTEGER) RETURNS INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR I <- 1 TO N
Total <- Total + I
NEXT I
RETURN Total
ENDFUNCTION
`);
assert.ok(!('unsupported' in r), JSON.stringify(r));
assert.ok(readLingo(r.source).ok, r.source);
assert.ok(/Total/.test(r.source));
});
test('pseudo that does not parse is unsupported, and records have no English form', () => {
const bad = pseudoToLingo('FUNCTION F(\n');
assert.ok('unsupported' in bad && bad.unsupported.length > 0);
const rec = pseudoToLingo(`RECORD P
X : INTEGER
ENDRECORD
FUNCTION F(A : INTEGER) RETURNS INTEGER
DECLARE R : P
R.X <- A
RETURN R.X
ENDFUNCTION
`);
assert.ok('unsupported' in rec, 'records are left unsupported by design');
});
+234
View File
@@ -0,0 +1,234 @@
// Stage 6: pseudo -> matho. For the programs that are equations, the generated LaTeX must read, lower back to
// pseudo that behaves like the original, and be a fixed point; every other program is refused with the construct
// and its line. The equation sets are the ones tests/matho.test.mjs checks by hand-worked values.
import test from 'node:test';
import assert from 'node:assert/strict';
import { programs, EQUATIONS } from './helpers/sync-programs.mjs';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
import { parseMatho } from '../dist/services/matho.js';
import { pseudoToMatho } from '../dist/sync/pseudo-to-matho.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
/**
* Equations the reader lowers but this generator does not write back, and why. An equation leaves this list
* when it round-trips; the test fails if a listed equation does, so the list can only shrink.
*/
const KNOWN_GAPS = {
'matrix-row': 'the row index i is an input that only subscripts, and the reader recognizes that only for a left side written y_i, with the function name carrying the subscript',
'choose': 'the reader names the second factorial counter K2 (K is the input), and a loop counter has to be a single letter in matho',
};
const SUPPORTED_AT_LEAST = 24; // raise as matho learns more forms; counted below
const num = (line) => { const m = /returned (.*)$/.exec(line ?? ''); return m ? m[1] : null; };
function run(pseudo, call, input = []) {
const st = studyOf(pseudo);
assert.ok(st.ok, `pseudo should study: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join(' | ')}`);
const t = runPseudo(st, { call, input, maxSteps: 200000 });
return { value: t.error ? `error:${t.error.kind}` : num(t.output[t.output.length - 1]), out: t.error ? [] : t.output.slice(0, -1) };
}
const same = (a, b) => a.value === b.value || (!Number.isNaN(Number(a.value)) && !Number.isNaN(Number(b.value)) && Math.abs(Number(a.value) - Number(b.value)) <= 1e-9 * Math.max(1, Math.abs(Number(a.value)))) && JSON.stringify(a.out) === JSON.stringify(b.out);
const lowerPseudo = (latex) => { const r = parseMatho(latex); assert.ok(r.ok, `the generated equations should read: ${latex}\n${r.diagnostics.map((d) => d.message).join(' | ')}`); return r.program.pseudo; };
const results = new Map();
test('every equation set: the LaTeX reads, lowers to pseudo that behaves like the original, and is a fixed point; or it is a listed gap', () => {
const problems = [];
let ok = 0;
for (const [id, [src, call]] of Object.entries(EQUATIONS)) {
const first = parseMatho(src);
assert.ok(first.ok, `${id}: the fixture should lower`);
const pseudo = first.program.pseudo;
const m = pseudoToMatho(pseudo);
results.set(id, m);
const known = KNOWN_GAPS[id];
if ('unsupported' in m) {
if (!known) problems.push(`${id}: ${m.unsupported.map((d) => d.message).join(' | ')}`);
continue;
}
if (known) { problems.push(`${id}: now round-trips; take it off KNOWN_GAPS (${known})`); continue; }
ok++;
// reads, and same behavior: the original equations' pseudo and the pseudo of the written equations
let again;
try { again = lowerPseudo(m.source); } catch (e) { problems.push(`${id}: ${e.message}`); continue; }
const a = run(pseudo, call); const b = run(again, call);
if (!same(a, b)) problems.push(`${id}: behavior differs: ${JSON.stringify(a)} vs ${JSON.stringify(b)}`);
// fixed point
const m2 = pseudoToMatho(again);
if ('unsupported' in m2) { problems.push(`${id}: the lowered pseudo is no longer supported: ${m2.unsupported[0].message}`); continue; }
if (m2.source !== m.source) problems.push(`${id}: not a fixed point:\n${m.source}\nvs\n${m2.source}`);
}
assert.deepEqual(problems, []);
assert.ok(ok >= SUPPORTED_AT_LEAST, `${ok} equation sets round-trip, fewer than ${SUPPORTED_AT_LEAST}`);
});
test('every exercise and corpus program: accepted and checked the same way, or refused with a reason and a line', () => {
let accepted = 0;
for (const p of programs()) {
if (!studyOf(p.pseudo).ok) continue;
const m = pseudoToMatho(p.pseudo);
if ('unsupported' in m) {
assert.ok(m.unsupported.length > 0, p.id);
for (const d of m.unsupported) {
assert.ok(Number.isInteger(d.line) && d.line >= 1 && d.line <= p.pseudo.split('\n').length, `${p.id}: line ${d.line}`);
assert.ok(d.message.length > 20, p.id);
}
continue;
}
accepted++;
const again = lowerPseudo(m.source);
if (p.call) assert.ok(same(run(p.pseudo, p.call), run(again, p.call)), `${p.id}: behavior differs`);
const m2 = pseudoToMatho(again);
assert.ok(!('unsupported' in m2) && m2.source === m.source, `${p.id}: not a fixed point`);
}
assert.ok(accepted >= 3, `${accepted} exercise, corpus, and fixture programs are equations, fewer than 3`);
});
test('a program that is not an equation is refused with the construct and its line', () => {
const ask = (pseudo) => { const r = pseudoToMatho(pseudo); assert.ok('unsupported' in r, pseudo); return r.unsupported.map((d) => d.message).join(' | '); };
assert.match(ask(`FUNCTION COUNT_DOWN(N : INTEGER) RETURNS INTEGER
DECLARE K : INTEGER
K <- N
WHILE K > 0
K <- K - 1
ENDWHILE
RETURN K
ENDFUNCTION
`), /matho has no form for a WHILE loop \(line 4\)/);
assert.match(ask(`FUNCTION F(N : INTEGER) RETURNS INTEGER
OUTPUT N
RETURN N
ENDFUNCTION
`), /OUTPUT.*\(line 2\)/);
assert.match(ask(`FUNCTION SHOUT(T : STRING) RETURNS STRING
RETURN T
ENDFUNCTION
`), /(answers STRING|not a number).*\(line 1\)/);
assert.match(ask(`FUNCTION F(N : INTEGER) RETURNS INTEGER
IF N > 0 THEN
IF N > 1 THEN
RETURN 2
ELSE
RETURN 1
ENDIF
ELSE
RETURN 0
ENDIF
ENDFUNCTION
`), /nested IF.*\(line 3\)/);
assert.match(ask('OUTPUT 1 + 2\n'), /steps outside a function/);
assert.match(ask('FUNCTION F(\n'), /./);
});
test('names: SIGMA is \\sigma, X_BAR is \\bar{x}, a single letter is lower case, and a hint restores the student\'s own spelling', () => {
const pseudo = `FUNCTION X_BAR(N : INTEGER, X : LIST OF FLOAT) RETURNS FLOAT
DECLARE Total : FLOAT
Total <- 0.0
FOR I <- 0 TO N
Total <- Total + X[I]
NEXT I
RETURN 1 / N * Total
ENDFUNCTION
FUNCTION SIGMA(V : FLOAT) RETURNS FLOAT
RETURN V * V
ENDFUNCTION
`;
const plain = pseudoToMatho(pseudo);
assert.ok(!('unsupported' in plain), JSON.stringify(plain));
assert.match(plain.source, /^\\bar\{x\} = /m);
assert.match(plain.source, /^\\sigma = /m);
assert.match(plain.source, /x_\{i\}/);
const hinted = pseudoToMatho(pseudo, { names: { X_BAR: '\\overline{x}' } });
assert.ok(!('unsupported' in hinted), JSON.stringify(hinted));
assert.match(hinted.source, /^\\overline\{x\} = /m);
assert.equal(parseMatho(hinted.source).program.defs[0].name, 'X_BAR');
});
test('a hint that does not lower back to the function is not trusted', () => {
const r = pseudoToMatho(`FUNCTION SQUARE(V : FLOAT) RETURNS FLOAT
RETURN V * V
ENDFUNCTION
`, { names: { SQUARE: 'q' } });
assert.ok('unsupported' in r);
assert.match(r.unsupported[0].message, /SQUARE comes back as/);
});
test('the line map has one entry per output line and points inside the pseudo', () => {
for (const [id, [src]] of Object.entries(EQUATIONS)) {
const m = results.get(id) ?? pseudoToMatho(parseMatho(src).program.pseudo);
if ('unsupported' in m) continue;
const lines = m.source.replace(/\n$/, '').split('\n');
assert.equal(m.map.length, lines.length, id);
const n = parseMatho(src).program.pseudo.split('\n').length;
for (const x of m.map) assert.ok(x === null || (Number.isInteger(x) && x >= 1 && x <= n), `${id}: ${x}`);
}
});
test('a sum from 1 reads x_i for the pseudo\'s X[I - 1], and a sum from 0 reads x_i for X[I]', () => {
const from1 = pseudoToMatho(parseMatho(EQUATIONS['sum-from-one'][0]).program.pseudo);
assert.match(from1.source, /\\sum_\{i=1\}\^\{n\} x_\{i\}/);
const from0 = pseudoToMatho(parseMatho(EQUATIONS['mean'][0]).program.pseudo);
assert.match(from0.source, /\\sum_\{i=0\}\^\{n-1\} x_\{i\}/);
// L[I] in a loop the reader would shift is not an equation it can say
const r = pseudoToMatho(`FUNCTION S(N : INTEGER, L : LIST OF FLOAT) RETURNS FLOAT
DECLARE Total : FLOAT
Total <- 0.0
FOR I <- 1 TO N + 1
Total <- Total + L[I]
NEXT I
RETURN Total
ENDFUNCTION
`);
assert.ok('unsupported' in r);
assert.match(r.unsupported[0].message, /starts at 1/);
});
/** Pseudo a student wrote by hand (not lowered from matho) that is still an equation: the same three checks. */
const HAND = {
'whole-number-types': [`FUNCTION AREA(W : INTEGER, H : INTEGER) RETURNS INTEGER
RETURN W * H
ENDFUNCTION
`, 'AREA(3, 4)'],
'elseif-chain': [`FUNCTION SIGN(X : FLOAT) RETURNS INTEGER
IF X < 0 THEN
RETURN -1
ELSEIF X == 0 THEN
RETURN 0
ENDIF
RETURN 1
ENDFUNCTION
`, 'SIGN(-2.5)'],
'sum-of-squares-to-n': [`FUNCTION SQUARES(N : INTEGER) RETURNS INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR K <- 1 TO N + 1
Total <- Total + K * K
NEXT K
RETURN Total
ENDFUNCTION
`, 'SQUARES(4)'],
'mean-of-a-list': [`FUNCTION MEAN(L : LIST OF FLOAT) RETURNS FLOAT
DECLARE Total : FLOAT
Total <- 0.0
FOR I <- 0 TO LENGTH(L)
Total <- Total + L[I]
NEXT I
RETURN Total / LENGTH(L)
ENDFUNCTION
`, 'MEAN([1.0, 2.0, 6.0])'],
};
test('pseudo written by hand that is an equation: accepted when matho can say it, and held to the same three checks', () => {
const said = [];
for (const [id, [pseudo, call]] of Object.entries(HAND)) {
const m = pseudoToMatho(pseudo);
if ('unsupported' in m) { assert.equal(id, 'mean-of-a-list', `${id}: ${m.unsupported[0].message}`); continue; }
said.push(id);
const again = lowerPseudo(m.source);
assert.ok(same(run(pseudo, call), run(again, call)), `${id}: ${JSON.stringify(run(pseudo, call))} vs ${JSON.stringify(run(again, call))}`);
const m2 = pseudoToMatho(again);
assert.ok(!('unsupported' in m2) && m2.source === m.source, `${id}: not a fixed point`);
}
assert.deepEqual(said, ['whole-number-types', 'elseif-chain', 'sum-of-squares-to-n']);
});
+268
View File
@@ -0,0 +1,268 @@
// Stage 4: pseudo -> Mojo. The generated Mojo must be accepted by the checker, run in the interpreter, and
// print what the pseudo prints; its style is pinned by a snapshot per exercise; every standard-library name
// it writes is in the harvested index. Programs it cannot yet hold to that are listed below, each with its reason.
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 { EXERCISES } from '../dist/exercises/index.js';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
import { checkMojo } from '../dist/mojo/check.js';
import { runMojo } from '../dist/mojo/interp.js';
import { pseudoToMojo, GENERATED_HEADER, USED_STDLIB, USED_PRELUDE } from '../dist/sync/pseudo-to-mojo.js';
import { formatAsMojo, sameOutput } from '../dist/sync/compare.js';
import { MOJO_STD_EXPORTS } from '../dist/mojo/generated/stdlib.js';
import { MOJO_STD_STRUCTS } from '../dist/mojo/generated/types.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
const here = path.dirname(fileURLToPath(import.meta.url));
const corpusDir = path.join(here, 'corpus');
const snapDir = path.join(here, 'snapshots', 'sync-mojo');
/**
* Programs that do not (yet) get the full check, and why. A program leaves this list when it passes;
* the test fails if a listed program passes, so the list can only shrink.
* declined: the Mojo interpreter declines the construct (it declines what it has not been verified
* against godbolt on); the translation is still checked, and the run is not.
* differs: the notation and Mojo disagree by design, and a literal translation keeps Mojo's behavior.
* checker: the checker's error is the right answer for what the pseudo says.
*/
const KNOWN_GAPS = {
'ex:match-off-by-one': ['declined', 'the pseudo stops with an index error; the interpreter declines an out-of-range codepoint index'],
'32-stops.pseudo': ['declined', 'the pseudo stops with an index error; the interpreter declines an out-of-range codepoint index'],
'08-caesar.pseudo': ['declined', 'chr(...) is not run by the interpreter yet'],
'24-string-builtins.pseudo': ['declined', 'ord(...) is not run by the interpreter yet'],
'23-dice.pseudo': ['declined', 'random_si64 is not run by the interpreter yet'],
'42-numerics.pseudo': ['declined', 'sin and the other math functions are not run by the interpreter yet'],
'46-projectile.pseudo': ['declined', 'pi is not run by the interpreter yet'],
'47-binomial-and-ph.pseudo': ['declined', 'comb is not run by the interpreter yet'],
'33-stops-div.pseudo': ['differs', 'dividing by zero stops the notation; a Float64 division in Mojo gives nan'],
'44-parallel-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
'45-parallel-neighbour-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
'35-stops-unset.pseudo': ['checker', 'the pseudo reads a variable that was never given a value; the checker reports exactly that, as the compiler would'],
'36-stops-counter.pseudo': ['checker', 'the pseudo reads a FOR counter after its loop, where it has no value; the checker reports exactly that'],
};
/** Programs whose pseudo does not parse, by design. The translator must say so, not crash. */
const NO_PSEUDO = new Set(['ex:five-bugs', '37-static-errors.pseudo', '41-reserved-name.pseudo']);
const AGREE_AT_LEAST = 48; // raise as the interpreter learns more; counted below
function programs() {
const out = [];
for (const e of EXERCISES) if (e.sources.pseudo) out.push({ id: `ex:${e.id}`, name: e.id, pseudo: e.sources.pseudo, call: e.call ?? null, input: [] });
for (const f of fs.readdirSync(corpusDir).filter((x) => x.endsWith('.pseudo')).sort()) {
const src = fs.readFileSync(path.join(corpusDir, f), 'utf8');
out.push({ id: f, name: f, pseudo: src, call: null, input: [...src.matchAll(/^\/\/ INPUT: (.*)$/gm)].map((m) => m[1]) });
}
return out;
}
/** How one program fares: what the translator, the checker, the interpreter, and the pseudo run say. */
function judge(p) {
const st = studyOf(p.pseudo);
if (!st.ok) return { verdict: 'no-pseudo', result: pseudoToMojo(p.pseudo, { call: p.call }) };
const tr = runPseudo(st, { call: p.call, input: p.input, maxSteps: 200000 });
const result = pseudoToMojo(p.pseudo, { call: p.call });
assert.ok(!('unsupported' in result), `${p.id}: ${JSON.stringify(result.unsupported)}`);
const gaps = result.notes.filter((n) => n.id === 'translate-gap');
const checked = checkMojo(result.source);
const errors = checked.diagnostics.filter((d) => d.severity === 'error');
if (gaps.length) return { verdict: 'gap', detail: gaps.map((g) => `line ${g.line}: ${g.message}`).join('; '), result };
if (errors.length) return { verdict: 'checker', detail: errors.map((d) => `L${d.line} ${d.message}`).join(' | '), result };
const run = runMojo(checked.module, result.source, { input: p.input, maxSteps: 400000 });
if (run.refused) return { verdict: 'declined', detail: run.refused, result };
const stops = run.exitCode !== 0 || run.limited;
if (tr.error) return stops ? { verdict: 'agree', result } : { verdict: 'differs', detail: `pseudo stops (${tr.error.kind}); Mojo ran to the end and printed ${JSON.stringify(run.stdout.slice(0, 80))}`, result };
if (stops) return { verdict: 'differs', detail: `Mojo stopped (exit ${run.exitCode}); pseudo printed ${JSON.stringify(tr.output.slice(0, 2))}`, result };
const want = p.call && tr.raw.result !== undefined
? [...tr.raw.output, formatAsMojo(tr.raw.result)].join('\n')
: tr.raw.output.join('\n');
return sameOutput(want, run.stdout)
? { verdict: 'agree', result }
: { verdict: 'differs', detail: `pseudo printed ${JSON.stringify(want.slice(0, 100))}, Mojo printed ${JSON.stringify(run.stdout.slice(0, 100))}`, result };
}
const verdicts = new Map();
test('every exercise and corpus program: the generated Mojo checks, runs, and agrees with the pseudo, or is a listed gap', () => {
const unexpected = [];
let agree = 0;
for (const p of programs()) {
const j = judge(p);
verdicts.set(p.id, j);
if (j.verdict === 'no-pseudo') {
if (!NO_PSEUDO.has(p.id)) unexpected.push(`${p.id}: the pseudo does not parse, and the program is not listed in NO_PSEUDO`);
continue;
}
if (NO_PSEUDO.has(p.id)) { unexpected.push(`${p.id}: listed in NO_PSEUDO but its pseudo parses`); continue; }
const known = KNOWN_GAPS[p.id];
if (j.verdict === 'agree') {
agree++;
if (known) unexpected.push(`${p.id}: now agrees; take it off KNOWN_GAPS (${known[1]})`);
continue;
}
if (!known) { unexpected.push(`${p.id}: ${j.verdict}: ${j.detail}`); continue; }
if (known[0] !== j.verdict) unexpected.push(`${p.id}: listed as ${known[0]} but is ${j.verdict}: ${j.detail}`);
}
assert.deepEqual(unexpected, []);
assert.ok(agree >= AGREE_AT_LEAST, `${agree} programs agree, fewer than ${AGREE_AT_LEAST}`);
});
test('the pseudo that does not parse gets a reason, not a crash', () => {
for (const id of NO_PSEUDO) {
const p = programs().find((x) => x.id === id);
const r = pseudoToMojo(p.pseudo, { call: p.call });
assert.ok('unsupported' in r, id);
assert.ok(r.unsupported.length > 0 && r.unsupported.every((d) => d.severity === 'error'), id);
}
});
test('every exercise with pseudo has its generated Mojo pinned by a snapshot (UPDATE_SNAPSHOTS=1 rewrites them for review)', () => {
fs.mkdirSync(snapDir, { recursive: true });
const wanted = new Set();
for (const p of programs().filter((x) => x.id.startsWith('ex:'))) {
const r = pseudoToMojo(p.pseudo, { call: p.call });
if ('unsupported' in r) continue; // five-bugs: its pseudo has mistakes by design
const file = path.join(snapDir, `${p.name}.mojo`);
wanted.add(`${p.name}.mojo`);
if (process.env.UPDATE_SNAPSHOTS) fs.writeFileSync(file, r.source);
assert.ok(fs.existsSync(file), `no snapshot for ${p.name}; run with UPDATE_SNAPSHOTS=1 and review the new file`);
assert.equal(r.source, fs.readFileSync(file, 'utf8'), `the generated Mojo for ${p.name} changed; if the change is wanted, run with UPDATE_SNAPSHOTS=1 and review the diff`);
}
for (const f of fs.readdirSync(snapDir)) assert.ok(wanted.has(f), `snapshot ${f} belongs to no exercise`);
});
test('the translation is literal: the declare-then-assign, index-loop, and total = total + 1 habits are kept', () => {
const ex = EXERCISES.find((e) => e.id === 'count-base');
const r = pseudoToMojo(ex.sources.pseudo, { call: ex.call });
for (const line of [' var n: Int', ' var total: Int', ' n = t.count_codepoints()', ' total = 0', ' for i in range(n):', ' if String(t[codepoint=i]) == b:', ' total = total + 1', ' return total']) {
assert.ok(r.source.split('\n').includes(line), `missing: ${line}`);
}
assert.ok(!r.source.includes('+='), 'the translator never rewrites total = total + 1');
});
test('the translator does not import or mention the best-practices helper', () => {
const text = fs.readFileSync(path.join(here, '..', 'src', 'sync', 'pseudo-to-mojo.ts'), 'utf8');
assert.ok(!/practices/i.test(text), 'src/sync/pseudo-to-mojo.ts refers to the helper');
});
test('every generated source starts with the generated-code header and has a line map that fits', () => {
for (const p of programs()) {
const r = pseudoToMojo(p.pseudo, { call: p.call });
if ('unsupported' in r) continue;
const lines = r.source.replace(/\n$/, '').split('\n');
assert.equal(lines[0], GENERATED_HEADER, p.id);
assert.equal(r.map.length, lines.length, `${p.id}: one map entry per line`);
assert.equal(r.map[0], null, p.id);
const pseudoLines = p.pseudo.split('\n').length;
for (const m of r.map) assert.ok(m === null || (Number.isInteger(m) && m >= 1 && m <= pseudoLines), `${p.id}: map entry ${m}`);
}
});
test('the map links Mojo lines to the pseudo lines they came from', () => {
const ex = EXERCISES.find((e) => e.id === 'count-base');
const r = pseudoToMojo(ex.sources.pseudo, { call: ex.call });
const lines = r.source.split('\n');
const pseudo = ex.sources.pseudo.split('\n');
const at = lines.findIndex((l) => l.includes('total = total + 1'));
assert.ok(pseudo[r.map[at] - 1].includes('Total <- Total + 1'), pseudo[r.map[at] - 1]);
});
test('grounding: every standard-library name the translator writes is in the harvested index, with the module it imports from', () => {
const structs = MOJO_STD_STRUCTS;
const exported = (n) => MOJO_STD_EXPORTS[n] ?? (structs[n] ? 'std' : undefined);
for (const [name, mod] of Object.entries(USED_STDLIB)) assert.equal(MOJO_STD_EXPORTS[name], mod, `${name} should come from ${mod}`);
// names the index leaves out are named here, with why: Float64 is a SIMD alias, abs is exported from two modules
const NOT_INDEXED = { Float64: 'an alias of SIMD', abs: 'exported from two modules, so the index leaves it out', Int: 'a builtin type' };
for (const name of USED_PRELUDE) assert.ok(exported(name) || NOT_INDEXED[name], `${name} is not in the harvested index`);
});
test('every import in a generated source is one the translator knows, and no import is unused', () => {
for (const p of programs()) {
const r = pseudoToMojo(p.pseudo, { call: p.call });
if ('unsupported' in r) continue;
const body = r.source.split('\n').filter((l) => !l.startsWith('from ')).join('\n');
for (const m of r.source.matchAll(/^from (\S+) import (.+)$/gm)) {
for (const name of m[2].split(', ')) {
assert.equal(USED_STDLIB[name], m[1], `${p.id}: ${m[0]}`);
assert.ok(new RegExp(`\\b${name}\\b`).test(body), `${p.id}: ${name} is imported and never used`);
}
}
}
});
test('main is written from the call: literals follow the parameter types (TRUE, character literals, floats, lists)', () => {
const pseudo = `FUNCTION F(B : BOOL, C : CHAR, X : FLOAT, L : LIST OF INTEGER) RETURNS BOOL
RETURN B AND C == 'A' AND X > 0.5 AND LENGTH(L) > 1
ENDFUNCTION
`;
const r = pseudoToMojo(pseudo, { call: "F(TRUE, 'A', 1, [3, 4])" });
assert.ok(r.source.includes(' print(f(True, "A", 1.0, List[Int](3, 4)))'), r.source);
assert.equal(checkMojo(r.source).diagnostics.filter((d) => d.severity === 'error').length, 0);
const run = runMojo(checkMojo(r.source).module, r.source, {});
assert.equal(run.stdout, 'True\n');
});
test('main follows the pseudo\'s own lines, and a procedure call is not printed', () => {
const r = pseudoToMojo('PROCEDURE SHOW(N : INTEGER)\n OUTPUT N\nENDPROCEDURE\n', { call: 'SHOW(4)' });
assert.ok(r.source.includes('def main():\n show(4)\n'), r.source);
const none = pseudoToMojo('FUNCTION F(N : INTEGER) RETURNS INTEGER\n RETURN N\nENDFUNCTION\n');
assert.ok(!none.source.includes('def main'), 'with no call and no lines of its own there is nothing to run');
});
test('raises is inferred through dictionary reads and through callers, and nothing else raises', () => {
const pseudo = `FUNCTION GET(M : HASHMAP OF STRING : INTEGER, K : STRING) RETURNS INTEGER
RETURN M[K]
ENDFUNCTION
FUNCTION TWICE(M : HASHMAP OF STRING : INTEGER, K : STRING) RETURNS INTEGER
RETURN GET(M, K) * 2
ENDFUNCTION
FUNCTION PLAIN(N : INTEGER) RETURNS INTEGER
RETURN N + 1
ENDFUNCTION
`;
const r = pseudoToMojo(pseudo, { call: null });
assert.ok(r.source.includes('def get(m: Dict[String, Int], k: String) raises -> Int:'), r.source);
assert.ok(r.source.includes('def twice(m: Dict[String, Int], k: String) raises -> Int:'), r.source);
assert.ok(r.source.includes('def plain(n: Int) -> Int:'), r.source);
const withMain = pseudoToMojo(pseudo + 'OUTPUT PLAIN(1)\n');
assert.ok(withMain.source.includes('def main():'), 'main does not raise when nothing it calls does');
const raising = pseudoToMojo(pseudo + 'DECLARE M : HASHMAP OF STRING : INTEGER\nM["a"] <- 1\nOUTPUT TWICE(M, "a")\n');
assert.ok(raising.source.includes('def main() raises:'), raising.source);
});
test('a set needs its import, and the import comes from the harvested module', () => {
const r = pseudoToMojo('TYPE Bases = SET OF CHAR\nDEFINE Purines (\'A\', \'G\') : Bases\nOUTPUT \'A\' IN Purines\n');
assert.ok(r.source.includes('from std.collections import Set'), r.source);
assert.equal(checkMojo(r.source).diagnostics.filter((d) => d.severity === 'error').length, 0);
});
test('what has no faithful Mojo becomes a visible raise and a warning, never a guess', () => {
const r = pseudoToMojo('DECLARE N : FLOAT\nN <- 1.0\nN <- RANDN()\nOUTPUT N\n');
const gap = r.notes.find((n) => n.id === 'translate-gap');
assert.ok(gap && gap.severity === 'warning' && gap.line === 3, JSON.stringify(r.notes ?? r));
assert.ok(r.source.includes('raise Error("not translated: pseudo line 3")'), r.source);
assert.ok(r.source.includes('def main() raises:'), 'the routine that raises says so');
assert.equal(checkMojo(r.source).diagnostics.filter((d) => d.severity === 'error').length, 0, 'the result still checks');
});
test('float literals are not folded the way Mojo folds them: 0.1 + 0.2 stays a Float64 sum', () => {
const r = pseudoToMojo('OUTPUT 0.1 + 0.2 == 0.3\n');
assert.ok(r.source.includes('Float64(0.1) + 0.2 == 0.3'), r.source);
const run = runMojo(checkMojo(r.source).module, r.source, {});
assert.equal(run.stdout, 'FALSE\n');
});
test('quantifiers become loops written before the line that uses them, and agree with the pseudo', () => {
const src = fs.readFileSync(path.join(corpusDir, '27-quantifiers.pseudo'), 'utf8');
const r = pseudoToMojo(src);
assert.ok(/var _all\d+ = True/.test(r.source) && /var _any\d+ = False/.test(r.source) && /var _sum\d+ = 0/.test(r.source), r.source);
assert.equal(verdicts.get('27-quantifiers.pseudo')?.verdict, 'agree');
});
test('a quantifier inside a loop condition is a gap, not a wrong answer', () => {
const r = pseudoToMojo('DECLARE L : LIST OF INTEGER\nL <- [1, 2]\nWHILE EXISTS K IN [0, LENGTH(L)) : L[K] > 5\n L[0] <- 9\nENDWHILE\n');
assert.ok(r.notes.some((n) => n.id === 'translate-gap'), JSON.stringify(r.notes));
});
+150
View File
@@ -0,0 +1,150 @@
// Stage 7: speco as a lens on pseudo's contracts. extractSpec reads the contracts out of pseudo; weave in
// replace mode writes a speco into pseudo in place of what is there; reconcileSpec keeps the claims that have
// lost their routine or loop. The laws below make sync settle after one pass and never lose a contract.
import test from 'node:test';
import assert from 'node:assert/strict';
import { programs } from './helpers/sync-programs.mjs';
import { studyOf } from '../dist/services/pseudo.js';
import { parseSpec, weave, extractSpec, reconcileSpec } from '../dist/services/speco.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
const CONTRACT = /^\s*(REQUIRE|ENSURE|INVARIANT|DECREASES)\b/;
const withContracts = () => programs().filter((p) => p.pseudo.split('\n').some((l) => CONTRACT.test(l)) && studyOf(p.pseudo).ok);
const stripContracts = (src) => src.split('\n').filter((l) => !CONTRACT.test(l)).join('\n');
const COUNT = `FUNCTION COUNT_BASE(B : CHAR, T : STRING) RETURNS INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR I <- 0 TO LENGTH(T)
IF T[I] == B THEN
Total <- Total + 1
ENDIF
NEXT I
RETURN Total
ENDFUNCTION
`;
const SPEC = 'SPEC COUNT_BASE(B, T)\n ENSURE result = |{ K ∈ [0, |T|) : T[K] = B }|\n LOOP FOR I\n INVARIANT Total = |{ K ∈ [0, I) : T[K] = B }|';
test('there are programs with contracts to hold the laws to', () => {
const ids = withContracts().map((p) => p.id);
assert.ok(ids.includes('ex:count-base-proved') && ids.includes('ex:match-proved'), ids.join(', '));
assert.ok(ids.length >= 10, `${ids.length} programs with contracts`);
});
test('replace mode: what the speco says replaces what the routine and the selected loops said, and insert mode stays as it was', () => {
const old = weave(COUNT, parseSpec('SPEC COUNT_BASE(B, T)\n REQUIRE LENGTH(T) >= 0\n ENSURE RESULT >= 0\n LOOP FOR I\n INVARIANT Total >= 0\n DECREASES LENGTH(T) - I')).source;
assert.match(old, /REQUIRE LENGTH\(T\) >= 0/);
const w = weave(old, parseSpec(SPEC), { replace: true });
assert.deepEqual(w.diagnostics, []);
assert.ok(!/REQUIRE|DECREASES|RESULT >= 0|Total >= 0/.test(w.source), w.source);
assert.match(w.source, /FUNCTION COUNT_BASE[^\n]*\n\s+ENSURE RESULT == LENGTH\(\{ K IN \[0, LENGTH\(T\)\) : T\[K\] == B \}\)\n/);
assert.match(w.source, /FOR I <- 0 TO LENGTH\(T\)\n\s+INVARIANT Total == LENGTH/);
assert.ok(studyOf(w.source).ok);
// insert mode keeps what is there and adds the new claims
const kept = weave(old, parseSpec(SPEC));
assert.match(kept.source, /REQUIRE LENGTH\(T\) >= 0/);
assert.match(kept.source, /DECREASES LENGTH\(T\) - I/);
assert.match(kept.source, /ENSURE RESULT == LENGTH/);
});
test('replace mode: routines and loops the spec does not name are left alone, and a unit with no claims clears its routine', () => {
const two = `${COUNT}
FUNCTION OTHER(N : INTEGER) RETURNS INTEGER
REQUIRE N >= 0
RETURN N
ENDFUNCTION
`;
const w = weave(two, parseSpec('SPEC COUNT_BASE(B, T)\n ENSURE RESULT >= 0'), { replace: true });
assert.match(w.source, /REQUIRE N >= 0/);
const cleared = weave(w.source, parseSpec('SPEC COUNT_BASE(B, T)\n REQUIRE TRUE'), { replace: true });
assert.ok(!/ENSURE RESULT >= 0/.test(cleared.source));
assert.match(cleared.source, /REQUIRE TRUE/);
});
test('replace mode is idempotent, and its map names the speco line each written line came from', () => {
const spec = parseSpec(SPEC);
const once = weave(COUNT, spec, { replace: true });
const twice = weave(once.source, spec, { replace: true });
assert.equal(twice.source, once.source);
assert.equal(once.map.length, 2);
const lines = once.source.split('\n');
for (const m of once.map) assert.ok(/ENSURE|INVARIANT/.test(lines[m.to - 1]) && m.from >= 1);
});
test('lens law: extract(weave(P, extract(P))) === extract(P), for every program with contracts', () => {
const bad = [];
for (const p of withContracts()) {
const spec = extractSpec(p.pseudo);
const w = weave(p.pseudo, parseSpec(spec), { replace: true });
if (w.diagnostics.length) { bad.push(`${p.id}: ${w.diagnostics[0].message}`); continue; }
if (!studyOf(w.source).ok) { bad.push(`${p.id}: the woven pseudo does not read`); continue; }
if (extractSpec(w.source) !== spec) bad.push(`${p.id}: extract(weave(P, extract(P))) differs`);
const again = weave(w.source, parseSpec(spec), { replace: true });
if (again.source !== w.source) bad.push(`${p.id}: weaving twice changes the pseudo`);
}
assert.deepEqual(bad, []);
});
test('preservation: a spec woven into pseudo with no contracts, then extracted, comes back, for every program with contracts', () => {
const bad = [];
for (const p of withContracts()) {
const spec = extractSpec(p.pseudo);
const bare = stripContracts(p.pseudo);
const w = weave(bare, parseSpec(spec), { replace: true });
if (w.diagnostics.length) { bad.push(`${p.id}: ${w.diagnostics[0].message}`); continue; }
if (extractSpec(w.source) !== spec) bad.push(`${p.id}: the spec did not come back`);
// and the contracts it restored are the ones the program had, up to spelling
const norm = (t) => t.split('\n').filter((l) => CONTRACT.test(l)).map((l) => l.trim().replace(/\s+/g, '')).join('|');
if (norm(w.source) !== norm(p.pseudo)) bad.push(`${p.id}: the contracts written back differ from the original's`);
}
assert.deepEqual(bad, []);
});
test('orphans: a spec whose routine is gone is kept, says why, and is reported on its line', () => {
const prev = `${extractSpec(weave(COUNT, parseSpec(SPEC)).source)}\n\nSPEC GONE(N)\n REQUIRE N >= 0\n`;
const r = reconcileSpec(COUNT.replace('COUNT_BASE', 'COUNT_BASE'), prev);
assert.match(r.source, /\/\/ kept: the pseudocode has no FUNCTION or PROCEDURE called GONE\nSPEC GONE\(N\)\n REQUIRE N ≥ 0/);
assert.equal(r.diagnostics.length, 1);
assert.equal(r.diagnostics[0].id, 'spec-orphan');
assert.equal(r.source.split('\n')[r.diagnostics[0].line - 1], 'SPEC GONE(N)');
// the pseudo's own contracts (none here) are the spec's: COUNT_BASE has none, so its claims were deleted there
assert.ok(!/SPEC COUNT_BASE/.test(r.source));
// a fixed point
assert.equal(reconcileSpec(COUNT, r.source).source, r.source);
});
test('orphans: a spec for a loop that is gone stays in its unit, and a routine that comes back takes its claims again', () => {
const woven = weave(COUNT, parseSpec(SPEC)).source;
const prev = extractSpec(woven);
// the FOR loop becomes a WHILE loop: the invariant has no loop to be about
const whileVersion = woven.replace(/FOR I <- 0 TO LENGTH\(T\)\n\s+INVARIANT[^\n]*\n/, 'WHILE FALSE\n').replace(' NEXT I', ' ENDWHILE');
const r = reconcileSpec(whileVersion, prev);
assert.match(r.source, /ENSURE result/);
assert.match(r.source, /\/\/ kept: COUNT_BASE has no loop matching "LOOP FOR I"\n\s+LOOP FOR I\n\s+INVARIANT Total/);
assert.equal(r.diagnostics.length, 1);
assert.equal(r.source.split('\n')[r.diagnostics[0].line - 1].trim(), 'LOOP FOR I');
assert.equal(reconcileSpec(whileVersion, r.source).source, r.source);
// the FOR loop returns, with no invariant yet: the kept claim is its again
const back = reconcileSpec(stripContracts(woven), r.source);
assert.ok(!/kept:/.test(back.source), back.source);
assert.match(back.source, /LOOP FOR I\n\s+INVARIANT Total/);
assert.equal(back.diagnostics.length, 0);
// a routine that was away and is back, with no contracts yet, takes its kept spec again
const gone = reconcileSpec('FUNCTION OTHER(N : INTEGER) RETURNS INTEGER\n RETURN N\nENDFUNCTION\n', prev);
assert.match(gone.source, /kept: the pseudocode has no FUNCTION or PROCEDURE called COUNT_BASE/);
const returned = reconcileSpec(stripContracts(woven), gone.source);
assert.ok(!/kept:/.test(returned.source));
assert.match(returned.source, /^SPEC COUNT_BASE\(B, T\)\n\s+ENSURE result/);
// but a contract deleted in the pseudo, from a spec that was not waiting, is gone from the spec too
assert.ok(!/SPEC COUNT_BASE/.test(reconcileSpec(stripContracts(woven), prev).source));
});
test('a loop the spec names that the routine does not have is reported on the LOOP line of the speco', () => {
const w = weave(COUNT, parseSpec('SPEC COUNT_BASE(B, T)\n ENSURE RESULT >= 0\n\n LOOP WHILE\n INVARIANT Total >= 0'));
const d = w.diagnostics.find((x) => x.id === 'no-loop');
assert.ok(d);
assert.equal(d.line, 4);
// the spec's own text is not touched by weaving: it is the caller's to keep
assert.match(w.source, /ENSURE RESULT >= 0/);
});