cfd16-assembler 0.7.1

An assembler backend for the CFD-16 ISA.
Documentation
use std::{fs, path::PathBuf};

use crate::{assemble, error::AssemErrorKind, parse::BinFormat};

#[test]
fn compile_divrem_raw() {
    let prog = "../../programs/divrem.s";

    let correct = vec![
        0x00, 0x4a, 0x11, 0xc8, 0xfd, 0x1d, 0x21, 0xd2, 0x00, 0x51, 0x02, 0x50, 0x00, 0x88,
    ];

    let result = match assemble(PathBuf::from(prog), BinFormat::Raw) {
        Ok(r) => r,
        Err(e) => {
            panic!("Program '{}' didn't assemble... Reason: {}", prog, e);
        }
    };

    assert_eq!(correct, result);
}

#[test]
fn compile_fibonacci_raw() {
    let prog = "../../programs/fibonacci.s";

    let correct = vec![
        0x0c, 0x49, 0x00, 0x59, 0x00, 0x29, 0x05, 0x48, 0x00, 0x48, 0x00, 0x98, 0x00, 0x53, 0x01,
        0x48, 0x32, 0xcb, 0x01, 0x1d, 0x01, 0x49, 0x00, 0x88, 0x00, 0x52, 0x01, 0xd0, 0x31, 0xcb,
        0xfb, 0x1a, 0x02, 0x51, 0x00, 0x88,
    ];

    let result = match assemble(PathBuf::from(prog), BinFormat::Raw) {
        Ok(r) => r,
        Err(e) => {
            panic!("Program '{}' didn't assemble... Reason: {}", prog, e);
        }
    };

    assert_eq!(correct, result);
}

#[test]
fn compile_nonexec_instr() {
    let prog = PathBuf::from("/tmp/prog.s");

    let assembly = "
        mov %b, illegal
        jsr %b
        mov %a, 2 ; branch slot
    
    illegal:
        movr %r, %a
        ret
    ";

    match fs::write(&prog, assembly) {
        Ok(_) => {}
        Err(e) => panic!("Unable to write to file... Reason: {}", e),
    };

    match assemble(prog, BinFormat::Raw) {
        Ok(_) => panic!("Program should not have assembled."),
        Err(e) => assert_eq!(e.kind, AssemErrorKind::NonExecableInstr),
    }
}