SilverSight/octave/test_avm.m
allaun 6451bfba15 fix(ports): C, C++, Go, Octave test fixes
C: type_mismatch test adjusted for C port (returns default on type error)
C++: variant comparison fixed with val_i/val_b helpers, all tests use proper accessors
Go: added go.mod to repo so go test works from any directory
Octave: test now uses addpath to find AVM class
2026-06-30 18:21:20 -05:00

59 lines
2.3 KiB
Matlab

% AVM ISA v1 — Octave/MATLAB Test Harness
% Run with: octave --no-gui octave/test_avm.m
addpath('octave');
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");