% 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 Port — Test Harness\n"); printf("===============================\n"); test_basic_add(); test_div(); test_saturation(); test_locals(); printf("\nAll Octave tests passed.\n");