move tests to its own modules

This commit is contained in:
Nilton Constantino
2026-01-29 15:58:14 +00:00
parent 9de23475f6
commit 4174d01a43
27 changed files with 1494 additions and 1503 deletions
@@ -121,3 +121,87 @@ fn lower_type(ty: &ir_core::Type) -> ir::Type {
ir_core::Type::Function { .. } => ir::Type::Function,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ir_core;
use crate::ir_core::*;
use crate::ir::*;
#[test]
fn test_full_lowering() {
let mut const_pool = ConstPool::new();
const_pool.insert(ConstantValue::Int(100)); // ConstId(0)
let program = Program {
const_pool,
modules: vec![ir_core::Module {
name: "test_mod".to_string(),
functions: vec![ir_core::Function {
id: FunctionId(1),
name: "main".to_string(),
params: vec![],
return_type: ir_core::Type::Void,
blocks: vec![
Block {
id: 0,
instrs: vec![
Instr::PushConst(ConstId(0)),
Instr::Call(FunctionId(2), 1),
],
terminator: Terminator::Jump(1),
},
Block {
id: 1,
instrs: vec![
Instr::Syscall(42),
],
terminator: Terminator::Return,
},
],
}],
}],
};
let vm_module = lower_program(&program).expect("Lowering failed");
assert_eq!(vm_module.name, "test_mod");
let func = &vm_module.functions[0];
assert_eq!(func.name, "main");
assert_eq!(func.body.len(), 7);
match &func.body[0].kind {
InstrKind::Label(Label(l)) => assert_eq!(l, "block_0"),
_ => panic!("Expected label block_0"),
}
match &func.body[1].kind {
InstrKind::PushConst(id) => assert_eq!(id.0, 0),
_ => panic!("Expected PushConst 0"),
}
match &func.body[2].kind {
InstrKind::Call { func_id, arg_count } => {
assert_eq!(func_id.0, 2);
assert_eq!(*arg_count, 1);
}
_ => panic!("Expected Call"),
}
match &func.body[3].kind {
InstrKind::Jmp(Label(l)) => assert_eq!(l, "block_1"),
_ => panic!("Expected Jmp block_1"),
}
match &func.body[4].kind {
InstrKind::Label(Label(l)) => assert_eq!(l, "block_1"),
_ => panic!("Expected label block_1"),
}
match &func.body[5].kind {
InstrKind::Syscall(id) => assert_eq!(*id, 42),
_ => panic!("Expected Syscall 42"),
}
match &func.body[6].kind {
InstrKind::Ret => (),
_ => panic!("Expected Ret"),
}
}
}