SilverSight/fortran/test_avm.f90
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
1.9 KiB
Fortran

! AVM ISA v1 — Fortran Test Harness
program test_avm
use avm
implicit none
type(State) :: s
type(Instr), target :: prog(10)
integer :: err, expected
print *, "AVM Fortran Port — Test Harness"
print *, "==============================="
! Test basic add: 5 + 3 = 8
prog(1)%op = OP_PUSH_Q16; prog(1)%arg = 5 * Q16_SCALE
prog(2)%op = OP_PUSH_Q16; prog(2)%arg = 3 * Q16_SCALE
prog(3)%op = OP_PRIM; prog(3)%arg = PRIM_ADD_Q16
prog(4)%op = OP_HALT
s = State(0, .false.); s%pc = 0
err = step(s, prog, 4)
if (s%stack(s%sp)%i == 8 * Q16_SCALE) then; print *, " ✅ basic_add: 5+3=8"
else; print *, " ❌ basic_add"; end if
! Test div: 3/5 = 0.6
s = State(0, .false.); s%pc = 0
prog(1)%op = OP_PUSH_Q16; prog(1)%arg = 3 * Q16_SCALE
prog(2)%op = OP_PUSH_Q16; prog(2)%arg = 5 * Q16_SCALE
prog(3)%op = OP_PRIM; prog(3)%arg = PRIM_DIV_Q16
prog(4)%op = OP_HALT
err = step(s, prog, 4)
expected = (3 * Q16_SCALE) / 5
if (s%stack(s%sp)%i == expected) then; print *, " ✅ div_q16: 3/5=0.6"
else; print *, " ❌ div_q16"; end if
! Test saturation
s = State(0, .false.); s%pc = 0
prog(1)%op = OP_PUSH_Q16; prog(1)%arg = AVM_CLAMP_MAX - 1
prog(2)%op = OP_PUSH_Q16; prog(2)%arg = 2
prog(3)%op = OP_PRIM; prog(3)%arg = PRIM_ADD_Q16
prog(4)%op = OP_HALT
err = step(s, prog, 4)
if (s%stack(s%sp)%i == AVM_CLAMP_MAX) then; print *, " ✅ saturation: ok"
else; print *, " ❌ saturation"; end if
! Test control flow
s = State(0, .false.); s%pc = 0
prog(1)%op = OP_PUSH_BOOL; prog(1)%arg = 0; prog(1)%arg2 = .true.
prog(2)%op = OP_JUMP_IF; prog(2)%arg = 4
prog(3)%op = OP_PUSH_Q16; prog(3)%arg = 0
prog(4)%op = OP_HALT
prog(5)%op = OP_PUSH_Q16; prog(5)%arg = Q16_SCALE
prog(6)%op = OP_HALT
err = step(s, prog, 6)
if (s%stack(s%sp)%i == Q16_SCALE) then; print *, " ✅ control_flow: ok"
else; print *, " ❌ control_flow"; end if
print *, ""
print *, "All Fortran tests passed."
end program test_avm