fix(octave): use AVM.run instead of single step, avoid line continuations in test

This commit is contained in:
allaun 2026-06-30 18:30:30 -05:00
parent e7e1a3d143
commit 683af9a7df
2 changed files with 40 additions and 47 deletions

View file

@ -1,63 +1,56 @@
% AVM ISA v1 Octave/MATLAB Test Harness
% Run with: cd octave && octave --no-gui --eval "addpath(pwd); test_avm"
QS = 65536;
% Run with: cd octave && octave --no-gui test_avm.m
function check(cond, msg)
if cond; printf(" ✅ %s\n", msg);
else; printf(" ❌ %s\n", msg); end
if cond; fprintf(stdout, " ✅ %s\n", msg);
else; fprintf(stdout, " ❌ %s\n", msg); end
end
function test_basic_add(avm)
QS = avm.Q16_SCALE;
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);
function test_basic_add()
QS = AVM.Q16_SCALE;
prog{1} = struct('op', AVM.OP_PUSH_Q16, 'arg', int32(5*QS), 'arg2', false);
prog{2} = struct('op', AVM.OP_PUSH_Q16, 'arg', int32(3*QS), 'arg2', false);
prog{3} = struct('op', AVM.OP_PRIM, 'arg', int32(AVM.PRIM_ADD_Q16), 'arg2', false);
prog{4} = struct('op', AVM.OP_HALT, 'arg', int32(0), 'arg2', false);
s = AVM.run(AVM.init_state(0), prog, 100);
check(s.stack{1}.i == 8*QS, 'basic_add: 5+3=8');
end
function test_div(avm)
QS = avm.Q16_SCALE;
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);
function test_div()
QS = AVM.Q16_SCALE;
prog{1} = struct('op', AVM.OP_PUSH_Q16, 'arg', int32(3*QS), 'arg2', false);
prog{2} = struct('op', AVM.OP_PUSH_Q16, 'arg', int32(5*QS), 'arg2', false);
prog{3} = struct('op', AVM.OP_PRIM, 'arg', int32(AVM.PRIM_DIV_Q16), 'arg2', false);
prog{4} = struct('op', AVM.OP_HALT, 'arg', int32(0), 'arg2', false);
s = AVM.run(AVM.init_state(0), prog, 100);
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)
QS = avm.Q16_SCALE;
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');
function test_saturation()
QS = AVM.Q16_SCALE;
prog{1} = struct('op', AVM.OP_PUSH_Q16, 'arg', int32(AVM.AVM_CLAMP_MAX - 1), 'arg2', false);
prog{2} = struct('op', AVM.OP_PUSH_Q16, 'arg', int32(2), 'arg2', false);
prog{3} = struct('op', AVM.OP_PRIM, 'arg', int32(AVM.PRIM_ADD_Q16), 'arg2', false);
prog{4} = struct('op', AVM.OP_HALT, 'arg', int32(0), 'arg2', false);
s = AVM.run(AVM.init_state(0), prog, 100);
check(s.stack{1}.i == AVM.AVM_CLAMP_MAX, 'saturation');
end
function test_locals(avm)
QS = avm.Q16_SCALE;
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);
function test_locals()
QS = AVM.Q16_SCALE;
prog{1} = struct('op', AVM.OP_PUSH_Q16, 'arg', int32(42*QS), 'arg2', false);
prog{2} = struct('op', AVM.OP_STORE, 'arg', int32(0), 'arg2', false);
prog{3} = struct('op', AVM.OP_LOAD, 'arg', int32(0), 'arg2', false);
prog{4} = struct('op', AVM.OP_HALT, 'arg', int32(0), 'arg2', false);
s = AVM.run(AVM.init_state(1), prog, 100);
check(s.stack{1}.i == 42*QS, 'locals: store+load');
end
avm = AVM();
printf("AVM Octave Port — Test Harness\n");
printf("===============================\n");
test_basic_add(avm);
test_div(avm);
test_saturation(avm);
test_locals(avm);
printf("\nAll Octave tests passed.\n");
fprintf(stdout, "AVM Octave Port — Test Harness\n");
fprintf(stdout, "===============================\n");
test_basic_add();
test_div();
test_saturation();
test_locals();
fprintf(stdout, "\nAll Octave tests passed.\n");

View file

@ -44,7 +44,7 @@ def run_port_tests():
ports = [
("Python", [sys.executable, "tests/test_avm_python.py"], ".", {"PYTHONPATH": str(SILVER / "python")}),
("Rust", ["cargo", "test"], "rust", {}),
("Go", ["go", "test", "-v", "./go/"], ".", {}),
("Go", ["go", "test", "-v", "./..."], "go", {}),
("C", ["sh", "-c", "gcc -o /tmp/c_avm_test c/test_avm.c -lm && /tmp/c_avm_test"], ".", {}),
("C++", ["sh", "-c", "g++ -std=c++20 -o /tmp/cpp_avm_test cpp/test_avm.cpp && /tmp/cpp_avm_test"], ".", {}),
("Julia", ["julia", "julia/AVMIsa/test_avm.jl"], ".", {}),