Add first VM unit test, refactor testing logic
All checks were successful
per-push tests / build (push) Successful in 56s
per-push tests / test-utility (push) Successful in 1m0s
per-push tests / test-frontend (push) Successful in 1m10s
per-push tests / test-backend (push) Successful in 1m31s
per-push tests / timed-decomposer-parse (push) Successful in 1m47s

This is in its own commit because it greatly affects the testing
logic in the previous commit.

Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
Ava Apples Affine 2025-07-30 00:01:07 +00:00
parent 609e65a8db
commit d0c2625874

View file

@ -588,120 +588,9 @@ impl VM {
#[cfg(test)]
mod tests {
use core::array;
use super::*;
use crate::instr;
const ISA_TESTS: [Option<Vec<(VM, TestResult)>>; instr::TOTAL_INSTRUCTIONS] = [
// TRAP
None,
// BIND
None,
// UNBIND
None,
// BOUND
None,
// PUSH
None,
// POP
None,
// ENTER
None,
// EXIT
None,
// LOAD
None,
// DUPL
None,
// CLEAR
None,
// NOP
None,
// HALT
None,
// PANIC
None,
// JMP
None,
// JMPIF
None,
// EQ
None,
// LT
None,
// GT
None,
// LTE
None,
// GTE
None,
// BOOL_NOT
None,
// BOOL_AND
None,
// BOOL_OR
None,
// BYTE_AND
None,
// BYTE_OR
None,
// XOR
None,
// BYTE_NOT
None,
// ADD
None,
// SUB
None,
// MUL
None,
// FDIV
None,
// IDIV
None,
// POW
None,
// MODULO
None,
// REM
None,
// INC
None,
// DEC
None,
// CTON
None,
// NTOC
None,
// NTOI
None,
// NTOE
None,
// CONST
None,
// MKVEC
None,
// MKBVEC
None,
// INDEX
None,
// LENGTH
None,
// SUBSL
None,
// INSER
None,
// CONS
None,
// CAR
None,
// CDR
None,
// CONCAT
None,
// S_APPEND
None,
];
use crate::instr as i;
use crate::util::{Program, Instruction, Operand};
fn has_stack(state: &VM, idx: usize, target: Gc<Datum>) -> bool {
*(state.stack[idx]) == *target
@ -763,9 +652,34 @@ mod tests {
}
}
fn add_trap_tests(tests: &mut [Option<Vec<(VM, TestResult)>>]) {
tests[i::TRAP.0 as usize] = Some(vec![
// call a trap function and check its output
(VM::from(Program(vec![
Instruction(i::TRAP, vec![Operand(Address::Numer, 0)])
])).with_traps(Some(vec![
Arc::from(|state: &mut VM| {
state.expr = Datum::Bool(true).into()
})
])).to_owned(),
TestResult{
expr: Some(Datum::Bool(true).into()),
stack: vec![],
syms: vec![],
errr: None,
}),
]);
}
#[test]
fn run_isa_tests() {
for i in &ISA_TESTS.to_vec() {
let mut isa_tests: [Option<Vec<(VM, TestResult)>>; i::TOTAL_INSTRUCTIONS] =
array::from_fn(|_| None);
add_trap_tests(&mut isa_tests);
for i in &isa_tests.to_vec() {
let Some(test_case) = i else {
assert!(false); // dont let untested instructions happen
return