lunar_wave/src/tests.rs

224 lines
5.1 KiB
Rust
Raw Normal View History

use crate::state::{
2023-09-25 00:47:17 +00:00
Block,
Chunk,
Instruction as Inst,
State,
};
2023-09-25 00:47:17 +00:00
#[test]
fn bools () {
/*
local function bool_to_x (b)
if b then
return 99
else
return 98
end
end
local x = bool_to_x (not not arg [1])
print (x)
return x
*/
let chunk = Chunk {
blocks: vec! [
Block {
instructions: vec! [
Inst::VarArgPrep (0),
Inst::Closure (0, 0),
Inst::Move (1, 0),
Inst::LoadFalse (2),
Inst::Call (1, 2, 1),
Inst::Move (1, 0),
Inst::LoadTrue (2),
Inst::Call (1, 2, 1),
Inst::Move (1, 0),
Inst::GetTabUp (2, 0, 0),
Inst::GetI (2, 2, 1),
Inst::Not (2, 2),
Inst::Not (2, 2),
Inst::Call (1, 2, 2),
Inst::GetTabUp (2, 0, 1),
Inst::Move (3, 1),
Inst::Call (2, 2, 1),
Inst::Return (1, 2, 1, false),
Inst::Return (2, 1, 1, false),
2023-09-25 00:47:17 +00:00
],
constants: vec! [
"arg".into (),
"print".into (),
],
upvalue_count: 1,
2023-09-25 00:47:17 +00:00
},
Block {
instructions: vec! [
Inst::Test (0, 0),
Inst::Jmp (3),
Inst::LoadI (1, 99),
Inst::Return1 (1),
Inst::Jmp (2),
Inst::LoadI (1, 98),
Inst::Return1 (1),
Inst::Return0,
],
constants: vec! [],
upvalue_count: 0,
2023-09-25 00:47:17 +00:00
},
],
};
for (arg, expected) in [
(vec! ["_exe_name"], vec! [98.into ()]),
(vec! ["_exe_name", "asdf"], vec! [99.into ()]),
] {
let mut vm = State::default ();
let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ()));
let actual = vm.execute_chunk (&chunk, &upvalues);
assert_eq! (actual, expected);
}
}
#[test]
fn closure () {
let bytecode = include_bytes! ("../test_vectors/closure.luac");
let mut rdr = std::io::Cursor::new (bytecode);
let file = crate::loader::parse_chunk (&mut rdr).unwrap ();
for (arg, expected) in [
// Run the same test twice so clippy won't complain about a vec of 1 element
(vec! ["_exe_name"], vec! [23.into ()]),
(vec! ["_exe_name"], vec! [23.into ()]),
] {
let mut vm = State::default ();
let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ()));
let actual = vm.execute_chunk (&file, &upvalues);
assert_eq! (actual, expected);
}
}
#[test]
fn floats () {
/*
local a = 0.5
local b = 3
local x = a + b
print (x)
return x
*/
2023-09-25 00:47:17 +00:00
let block = Block {
instructions: vec! [
Inst::VarArgPrep (0),
Inst::LoadK (0, 0),
Inst::LoadI (1, 3),
Inst::Add (2, 0, 1),
Inst::MmBin (0, 1, 6),
Inst::GetTabUp (3, 0, 1),
Inst::Move (4, 2),
Inst::Call (3, 2, 1),
Inst::Return (2, 2, 1, false),
Inst::Return (3, 1, 1, false),
],
constants: vec! [
0.5.into (),
"print".into (),
],
upvalue_count: 1,
};
2023-09-25 00:47:17 +00:00
let chunk = Chunk {
blocks: vec! [block],
};
for (arg, expected) in [
(vec! ["_exe_name"], vec! [3.5.into ()]),
2023-09-24 22:42:56 +00:00
(vec! ["_exe_name", " "], vec! [3.5.into ()]),
] {
let mut vm = State::default ();
let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ()));
let actual = vm.execute_chunk (&chunk, &upvalues);
assert_eq! (actual, expected);
}
}
#[test]
fn fma () {
let bytecode = include_bytes! ("../test_vectors/fma.luac");
let mut rdr = std::io::Cursor::new (bytecode);
let file = crate::loader::parse_chunk (&mut rdr).unwrap ();
assert_eq! (file.blocks.len (), 4);
for (arg, expected) in [
(vec! ["_exe_name"], vec! [122.into ()]),
(vec! ["_exe_name"], vec! [122.into ()]),
] {
let mut vm = State::default ();
let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ()));
let actual = vm.execute_chunk (&file, &upvalues);
assert_eq! (actual, expected);
}
}
#[test]
fn is_93 () {
let source = include_bytes! ("../test_vectors/is_93.lua");
assert_eq! (&blake3::hash (source).to_hex (), "a058869ed5142cbc8ccb2372991ef8b37657af38dc3f5e34814a7274b14d1e52");
2023-09-26 18:33:59 +00:00
let bytecode = crate::loader::compile_bytecode (source.to_vec ());
assert_eq! (&blake3::hash (&bytecode).to_hex (), "b59ce3e054500beb109c247e784cc4a0ff09ac42f8086dc5cdbf711bce1ba071");
let mut rdr = std::io::Cursor::new (bytecode);
let file = crate::loader::parse_chunk (&mut rdr).unwrap ();
for (arg, expected) in [
(vec! ["_exe_name"], vec! [1.into ()]),
(vec! ["_exe_name", "93"], vec! [0.into ()]),
(vec! ["_exe_name", "94"], vec! [1.into ()]),
] {
let mut vm = State::default ();
let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ()));
let actual = vm.execute_chunk (&file, &upvalues);
assert_eq! (actual, expected);
}
}
#[test]
fn value_size () {
// Per https://www.lua.org/doc/jucs05.pdf,
// "The Implementation of Lua 5.0",
//
// Lua's tagged union values are 12-16 bytes on a 32-bit system
// with 64-bit floats
//
// It is very nice if LunarWaveVM is the same or better.
// There is some exploratory things in this test, too
use std::{
mem::size_of,
rc::Rc,
};
assert! (size_of::<Box <()>> () <= 8);
assert! (size_of::<std::rc::Rc <()>> () <= 8);
pub enum Value {
Nil,
Boolean (bool),
Float (f64),
String (Rc <String>),
}
assert_eq! (size_of::<Value> (), 16);
assert_eq! (size_of::<crate::state::Value> (), 16);
}