uxn/tests/vm_tests.rs

716 lines
18 KiB
Rust
Raw Normal View History

2022-12-24 03:41:44 +00:00
use uxn::isa::Icode;
use uxn::vm::*;
#[test]
fn test_brk() {
let mut vm = Uxn::of1(&[Icode::BRK]);
assert_eq!(vm.step(), Err(UxnError::Break));
}
#[test]
fn test_lit() {
let mut vm = Uxn::of1(&[Icode::LIT, 0x01, Icode::BRK]);
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x01));
}
#[test]
fn test_lit2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0xFF, 0xAA,
Icode::BRK, // BRK
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().peek2(), Ok(0xFFAA));
}
#[test]
fn test_litr() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::RETURN, 0xAA,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.rst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_inc() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0xFF, 0xAA,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().peek2(), Ok(0xFFAA));
}
#[test]
fn test_pop1() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xFF,
Icode::LIT, 0xAA,
Icode::POP,
Icode::LIT, 0xBB,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().peek2(), Ok(0xFFBB));
}
#[test]
fn test_pop2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xFF,
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::POP | Icode::SHORT,
Icode::LIT, 0xCC,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().peek2(), Ok(0xFFCC));
}
#[test]
fn test_popk() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xFF,
Icode::POP | Icode::KEEP,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xFF));
}
#[test]
fn test_nip() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::NIP,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xBB));
}
#[test]
fn test_nip2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0xAA, 0x00,
Icode::LIT | Icode::SHORT, 0xBB, 0x00,
Icode::NIP | Icode::SHORT,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().peek2(), Ok(0xBB00));
}
#[test]
fn test_nipk() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::NIP | Icode::KEEP,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().get1(1), Ok(0xBB));
assert_eq!(vm.wst.borrow().get1(2), Ok(0xBB));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xBB));
}
#[test]
fn test_swp() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::SWP,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xBB));
assert_eq!(vm.wst.borrow().get1(1), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_swp2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0xAA, 0x00,
Icode::LIT | Icode::SHORT, 0xBB, 0x00,
Icode::SWP | Icode::SHORT,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get2(0), Ok(0xBB00));
assert_eq!(vm.wst.borrow().get2(2), Ok(0xAA00));
assert_eq!(vm.wst.borrow().peek2(), Ok(0xAA00));
}
#[test]
fn test_swpk() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::SWP | Icode::KEEP,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().get1(1), Ok(0xBB));
assert_eq!(vm.wst.borrow().get1(2), Ok(0xBB));
assert_eq!(vm.wst.borrow().get1(3), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_rot() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::LIT, 0xCC,
Icode::ROT,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xBB));
assert_eq!(vm.wst.borrow().get1(1), Ok(0xCC));
assert_eq!(vm.wst.borrow().get1(2), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_rot_cycle() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::LIT, 0xCC,
Icode::ROT,
Icode::ROT,
Icode::ROT,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().get1(1), Ok(0xBB));
assert_eq!(vm.wst.borrow().get1(2), Ok(0xCC));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xCC));
}
#[test]
fn test_dup() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::DUP,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().get1(1), Ok(0xBB));
assert_eq!(vm.wst.borrow().get1(2), Ok(0xBB));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xBB));
}
#[test]
fn test_ovr() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::OVR,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().get1(1), Ok(0xBB));
assert_eq!(vm.wst.borrow().get1(2), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_eql_neg() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::EQL,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x00));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x00));
}
#[test]
fn test_eql_pos() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xAA,
Icode::EQL,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x01));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x01));
}
#[test]
fn test_neq_neg() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xAA,
Icode::NEQ,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x00));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x00));
}
#[test]
fn test_neq_pos() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0xBB,
Icode::NEQ,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x01));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x01));
}
#[test]
fn test_gth_neg() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x00,
Icode::LIT, 0x01,
Icode::GTH,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x00));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x00));
}
#[test]
fn test_gth_pos() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x01,
Icode::LIT, 0x00,
Icode::GTH,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x01));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x01));
}
#[test]
fn test_lth_neg() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x01,
Icode::LIT, 0x00,
Icode::LTH,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x00));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x00));
}
#[test]
fn test_lth_pos() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x00,
Icode::LIT, 0x01,
Icode::LTH,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x01));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x01));
}
#[test]
fn test_jmprp() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xAA,
Icode::LIT, 0x03,
Icode::JMP,
Icode::POP,
Icode::LIT, 0xFF,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_jmpa() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0x01, 0x10, Icode::JMP | Icode::SHORT,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
Icode::LIT, 0xAA,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_jmprn() {
// Negative relative jup
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0x01, 0x10, Icode::JMP | Icode::SHORT,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// Negative relative jump target
Icode::LIT, 0xAA, Icode::BRK, 0x00,
// Absolute jump target
Icode::LIT, -7i8 as u8, Icode::JMP,
// Wrong arm
Icode::LIT, 0xFF,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_jmpc_true() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x01,
Icode::LIT, 0x03,
Icode::JCN,
// Fall through
Icode::LIT, 0xFF, Icode::BRK,
// Target
Icode::LIT, 0xAA, Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
}
#[test]
fn test_jmpc_false() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x00,
Icode::LIT, 0x03,
Icode::JCN,
// Fall through
Icode::LIT, 0xFF, Icode::BRK,
// Target
Icode::LIT, 0xAA, Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xFF));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xFF));
}
#[test]
fn test_jsr() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x05, Icode::JSR, 0x00,
// Fall through
0x00, 0x00, 0x00, 0x00,
// Target
Icode::LIT, 0xAA, Icode::BRK, 0x00,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
assert_eq!(vm.rst.borrow().get2(0), Ok(0x0103));
assert_eq!(vm.rst.borrow().peek2(), Ok(0x0103));
}
#[test]
fn test_jsra() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0x01, 0x08, Icode::JSR | Icode::SHORT,
// Fall through
0x00, 0x00, 0x00, 0x00,
// Target
Icode::LIT, 0xAA, Icode::BRK, 0x00,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0xAA));
assert_eq!(vm.wst.borrow().peek1(), Ok(0xAA));
assert_eq!(vm.rst.borrow().get2(0), Ok(0x0104));
assert_eq!(vm.rst.borrow().peek2(), Ok(0x0104));
}
#[test]
fn test_sth() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0x01, 0x08, Icode::STH,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().get1(0), Ok(0x01));
assert_eq!(vm.wst.borrow().peek1(), Ok(0x01));
assert_eq!(vm.rst.borrow().get1(0), Ok(0x08));
assert_eq!(vm.rst.borrow().peek1(), Ok(0x08));
}
#[test]
fn test_sth2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0x01, 0x08, Icode::STH | Icode::SHORT,
Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.rst.borrow().get2(0), Ok(0x0108));
assert_eq!(vm.rst.borrow().peek2(), Ok(0x0108));
}
#[test]
fn test_ldz() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x0b, Icode::LDZ, Icode::BRK,
]);
vm.sta1(0x000b, 0xFF).unwrap();
}
// Make sure the write took
assert_eq!(vm.lda1(0x00b).unwrap(), 0xFF);
// Run the example
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 1);
assert_eq!(vm.wst.borrow().peek1(), Ok(0xFF));
}
#[test]
fn test_ldz2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x0b, Icode::LDZ | Icode::SHORT, Icode::BRK,
]);
vm.sta2(0x000b, 0xFFEE).unwrap();
}
// Make sure the write took
assert_eq!(vm.lda1(0x00b).unwrap(), 0xFF);
// Run the example
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 2);
assert_eq!(vm.wst.borrow().peek2(), Ok(0xFFEE));
}
#[test]
fn test_stz() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xFF, Icode::LIT, 0x0b,
Icode::STZ, Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 0);
assert_eq!(vm.lda1(0x000b), Ok(0xFF));
}
#[test]
fn test_stz2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xFF, Icode::LIT, 0x0b,
Icode::STZ, Icode::BRK,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 0);
assert_eq!(vm.lda1(0x000b), Ok(0xFF));
}
#[test]
fn test_ldr() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x01, Icode::LDR, Icode::BRK,
0xFF,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 1);
assert_eq!(vm.wst.borrow().peek1(), Ok(0xFF));
}
#[test]
fn test_ldr2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0x01, Icode::LDR | Icode::SHORT, Icode::BRK,
0xFF, 0xEE
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 2);
assert_eq!(vm.wst.borrow().peek2(), Ok(0xFFEE));
}
#[test]
fn test_str() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xEE, Icode::LIT, 0x03,
Icode::STR, Icode::BRK, 0x00, 0x00,
0xFF, // Will be overwritten
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 0);
assert_eq!(vm.lda1(0x0108), Ok(0xEE));
}
#[test]
fn test_str2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0xEE, 0xDD, Icode::LIT,
0x02, Icode::STR | Icode::SHORT, Icode::BRK, 0x00,
0xFF, 0xFF // Will be overwritten
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 0);
assert_eq!(vm.lda2(0x0108), Ok(0xEEDD));
}
#[test]
fn test_lda() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0x01, 0x08, Icode::LDA,
Icode::BRK, 0x00, 0x00, 0x00,
0xEE,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 1);
assert_eq!(vm.wst.borrow().peek1(), Ok(0xEE));
}
#[test]
fn test_lda2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0x01, 0x08, Icode::LDA | Icode::SHORT,
Icode::BRK, 0x00, 0x00, 0x00,
0xEE, 0xDD,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 2);
assert_eq!(vm.wst.borrow().peek2(), Ok(0xEEDD));
}
#[test]
fn test_sta() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT, 0xFF, Icode::NOP, Icode::NOP,
Icode::LIT | Icode::SHORT, 0x01, 0x0c, Icode::NOP,
Icode::STA, Icode::BRK, 0x00, 0x00,
0x00,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 0);
assert_eq!(vm.lda1(0x010c), Ok(0xFF));
}
#[test]
fn test_sta2() {
nofmt::pls! {
let mut vm = Uxn::of1(&[
Icode::LIT | Icode::SHORT, 0xFF, 0xEE, Icode::NOP,
Icode::LIT | Icode::SHORT, 0x01, 0x0c, Icode::NOP,
Icode::STA | Icode::SHORT, Icode::BRK, Icode::NOP,
0x00, 0x00,
]);
}
assert_eq!(vm.run(64), Err(UxnError::Break));
assert_eq!(vm.wst.borrow().idx(), 0);
assert_eq!(vm.lda2(0x010c), Ok(0xFFEE));
}