SilverSight/octave/test_avm.m
allaun 64e54d937d feat(tests): add C, C++, Scala, Fortran, Octave test harnesses
Test harnesses added for all remaining AVM ISA ports:
- C (9 tests): arithmetic, saturation, comparison, overflow, control flow, locals
- C++ (9 tests): same test suite with std::optional-based error handling
- Scala (9 tests): functional style with Option return
- Fortran (4 tests): add, div, saturation, control flow (minimal Fortran test)
- Octave/MATLAB (4 tests): basic operations test

Milestone: 10/12 ports have test harnesses. Coq still pending.
2026-06-30 17:59:19 -05:00

58 lines
2.2 KiB
Matlab

% AVM ISA v1 — Octave/MATLAB Test Harness
% Run with: octave test_avm.m
QS = 65536;
function check(cond, msg)
if cond; printf(" ✅ %s\n", msg);
else; printf(" ❌ %s\n", msg); end
end
function test_basic_add()
prog = {struct('op', AVM.OP_PUSH_Q16, 'arg', int32(5*65536), 'arg2', false), ...
struct('op', AVM.OP_PUSH_Q16, 'arg', int32(3*65536), 'arg2', false), ...
struct('op', AVM.OP_PRIM, 'arg', int32(AVM.PRIM_ADD_Q16), 'arg2', false), ...
struct('op', AVM.OP_HALT, 'arg', int32(0), 'arg2', false)};
s = AVM.init_state(0);
[s, err] = AVM.step(s, prog);
check(s.stack{1}.i == 8*65536, 'basic_add: 5+3=8');
end
function test_div()
prog = {struct('op', AVM.OP_PUSH_Q16, 'arg', int32(3*65536), 'arg2', false), ...
struct('op', AVM.OP_PUSH_Q16, 'arg', int32(5*65536), 'arg2', false), ...
struct('op', AVM.OP_PRIM, 'arg', int32(AVM.PRIM_DIV_Q16), 'arg2', false), ...
struct('op', AVM.OP_HALT, 'arg', int32(0), 'arg2', false)};
s = AVM.init_state(0);
[s, err] = AVM.step(s, prog);
expected = floor(double(3*65536) / double(5*65536)) * 65536;
check(s.stack{1}.i == expected, 'div_q16: 3/5');
end
function test_saturation()
prog = {struct('op', AVM.OP_PUSH_Q16, 'arg', int32(AVM.AVM_CLAMP_MAX - 1), 'arg2', false), ...
struct('op', AVM.OP_PUSH_Q16, 'arg', int32(2), 'arg2', false), ...
struct('op', AVM.OP_PRIM, 'arg', int32(AVM.PRIM_ADD_Q16), 'arg2', false), ...
struct('op', AVM.OP_HALT, 'arg', int32(0), 'arg2', false)};
s = AVM.init_state(0);
[s, err] = AVM.step(s, prog);
check(s.stack{1}.i == AVM.AVM_CLAMP_MAX, 'saturation');
end
function test_locals()
prog = {struct('op', AVM.OP_PUSH_Q16, 'arg', int32(42*65536), 'arg2', false), ...
struct('op', AVM.OP_STORE, 'arg', int32(0), 'arg2', false), ...
struct('op', AVM.OP_LOAD, 'arg', int32(0), 'arg2', false), ...
struct('op', AVM.OP_HALT, 'arg', int32(0), 'arg2', false)};
s = AVM.init_state(1);
[s, err] = AVM.step(s, prog);
check(s.stack{1}.i == 42*65536, 'locals: store+load');
end
printf("AVM Octave PortTest Harness\n");
printf("===============================\n");
test_basic_add();
test_div();
test_saturation();
test_locals();
printf("\nAll Octave tests passed.\n");