#![allow(clippy::unwrap_used)]
use decasm::{Instruction, Opcode};
use strum::IntoEnumIterator;

#[test]
fn instructions() {
    for mut instruction in Instruction::iter() {
        // Resolve clash between ambiguous opcodes 0x0000
        if instruction == Instruction::CallMachineCode(ux::u12::new(0x000)) {
            instruction = Instruction::CallMachineCode(ux::u12::new(0x001));
        }
        let opcode = Opcode::from(instruction);
        assert_eq!(instruction, Instruction::try_from(opcode).unwrap());
    }
}

#[test]
fn opcodes() {
    for opcode in 0x0000..=0xFFFF {
        // Resolve clash between ambiguous opcodes 0x00B0
        if (0x00B0..=0x00BF).contains(&opcode) {
            continue;
        }
        let opcode = Opcode::Opcode(opcode);
        if let Ok(instruction) = Instruction::try_from(opcode) {
            assert_eq!(opcode, Opcode::from(instruction));
        }
    }
}

#[test]
fn long_opcode() {
    assert_eq!(
        Opcode::LongOpcode(0xF000_0000),
        Opcode::from(Instruction::SetIndexLong(0x0000))
    );
    assert_eq!(
        Instruction::try_from(Opcode::LongOpcode(0xF000_0000)).unwrap(),
        Instruction::SetIndexLong(0x0000)
    );
}
