SilverSight/octave/test_avm.m
allaun 48ee7f7fdb fix(c,octave): C stack overflow test fuel, Octave instance methods
C: stack_overflow test now uses AVM_MAX_STACK+10 fuel
Octave: test creates AVM() instance and calls methods on it
2026-06-30 18:26:10 -05:00

59 lines
2.3 KiB
Matlab

% AVM ISA v1 — Octave/MATLAB Test Harness
% Run with: cd octave && octave --no-gui --eval "addpath(pwd); test_avm"
QS = 65536;
function check(cond, msg)
if cond; printf(" ✅ %s\n", msg);
else; printf(" ❌ %s\n", msg); end
end
function test_basic_add(avm)
prog = {struct('op', avm.OP_PUSH_Q16, 'arg', int32(5*QS), 'arg2', false), ...
struct('op', avm.OP_PUSH_Q16, 'arg', int32(3*QS), '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*QS, 'basic_add: 5+3=8');
end
function test_div(avm)
prog = {struct('op', avm.OP_PUSH_Q16, 'arg', int32(3*QS), 'arg2', false), ...
struct('op', avm.OP_PUSH_Q16, 'arg', int32(5*QS), '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 = idivide(int32(3*QS), int32(5*QS), 'floor') * int32(QS);
check(s.stack{1}.i == expected, 'div_q16: 3/5');
end
function test_saturation(avm)
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(avm)
prog = {struct('op', avm.OP_PUSH_Q16, 'arg', int32(42*QS), '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*QS, 'locals: store+load');
end
avm = AVM();
printf("AVM Octave PortTest Harness\n");
printf("===============================\n");
test_basic_add(avm);
test_div(avm);
test_saturation(avm);
test_locals(avm);
printf("\nAll Octave tests passed.\n");