trevors_chip8_toy/gemma/tests/util_tests.rs
2025-05-28 11:32:09 -04:00

218 lines
5.6 KiB
Rust

use std::collections::BTreeMap;
use gemma::chip8::util::InstructionUtil;
#[test]
fn byte_to_bools() {
let data_set: BTreeMap<u8, [bool; 8]> = BTreeMap::from(
[
(0x00, [false, false, false, false, false, false, false, false]),
(0x0f, [true, true, true, true, false, false, false, false]),
(0xaa, [false, true, false, true, false, true, false, true]),
(0xf0, [false, false, false, false, true, true, true, true]),
(0xff, [true, true, true, true, true, true, true, true]),
]
);
for (key, value) in data_set.iter() {
let result_array = InstructionUtil::byte_to_bools(*key);
for i in 0..8 {
assert_eq!(result_array[i], value[i]);
}
}
}
#[test]
fn bools_to_byte() {
let data_set: BTreeMap<u8, [bool; 8]> = BTreeMap::from(
[
(0x00, [false, false, false, false, false, false, false, false]),
(0x0f, [true, true, true, true, false, false, false, false]),
(0xaa, [false, true, false, true, false, true, false, true]),
(0xf0, [false, false, false, false, true, true, true, true]),
(0xff, [true, true, true, true, true, true, true, true]),
]
);
for (key, value) in data_set.iter() {
let result = InstructionUtil::bools_to_byte(*value);
assert_eq!(result, *key);
}
}
#[test]
fn split_bytes() {
let data_set: BTreeMap<u16, (u8, u8)> = BTreeMap::from(
[
(0xff00, (0xff, 0x00)),
(0x00ff, (0x00, 0xff)),
(0xabcd, (0xab, 0xcd)),
(0xcdab, (0xcd, 0xab))
]
);
for (key, value) in data_set.iter() {
assert_eq!(InstructionUtil::split_bytes(*key), *value);
}
}
#[test]
fn join_bytes() {
let data_set: BTreeMap<u16, (u8, u8)> = BTreeMap::from(
[
(0xff00, (0xff, 0x00)),
(0x00ff, (0x00, 0xff)),
(0xabcd, (0xab, 0xcd)),
(0xcdab, (0xcd, 0xab))
]
);
for (key, (low, high )) in data_set.iter() {
assert_eq!(InstructionUtil::join_bytes(*low, *high), *key);
}
}
#[test]
fn swap_endian() {
let data_set: BTreeMap<u16, u16> = BTreeMap::from(
[
(0xabcd, 0xcdab),
(0x00ff, 0xff00),
(0xff00, 0x00ff),
(0xffff, 0xffff),
(0x0000, 0x0000)
]
);
for (key, value) in data_set.iter() {
assert_eq!(InstructionUtil::swap_endian(*key), *value);
}
}
#[test]
fn split_bytes_swap_endian() {
let data_set: BTreeMap<u16, (u8, u8)> = BTreeMap::from([
(0x0000, (0x00, 0x00)),
(0xffff, (0xff, 0xff)),
(0xff00, (0x00, 0xff)),
(0x00ff, (0xff, 0x00)),
(0xabcd, (0xcd, 0xab))
]);
for (key, result) in data_set.iter() {
assert_eq!(InstructionUtil::split_bytes_swap_endian(*key), *result)
}
}
#[test]
fn join_bytes_swap_endian() {
let data_set: BTreeMap<u16, (u8, u8)> = BTreeMap::from([
(0xffff, (0xff, 0xff)),
(0x0000, (0x00, 0x00)),
(0xff00, (0x00, 0xff)),
(0x00ff, (0xff, 0x00)),
(0xabcd, (0xcd, 0xab))
]);
for (key, (low, high)) in data_set.iter() {
assert_eq!(InstructionUtil::join_bytes_swap_endian(*low, *high), *key);
}
}
#[test]
fn read_address_from_instruction() {
let data_set: BTreeMap<u16, u16> = BTreeMap::from([
(0xabcd, 0x0bcd),
(0xffff, 0x0fff),
(0x0000, 0x0000),
(0x8123, 0x0123)
]) ;
for (key, value) in data_set.iter() {
assert_eq!(InstructionUtil::read_addr_from_instruction(*key), *value);
}
}
#[test]
fn read_nibble_from_instruction() {
let data_set: BTreeMap<u16, u8> = BTreeMap::from([
(0xffff, 0x0f),
(0x0000, 0x00),
(0xabcd, 0x0d),
]);
for(key, value) in data_set.iter() {
assert_eq!(InstructionUtil::read_nibble_from_instruction(*key), *value);
}
}
#[test]
fn read_x_from_instruction() {
let data_set: BTreeMap<u16, u8> = BTreeMap::from([
(0xffff, 0x0f),
(0x0000, 0x00),
(0x0a00, 0x0a),
(0x0b00, 0x0b)
]);
for (key, value) in data_set.iter() {
assert_eq!(InstructionUtil::read_x_from_instruction(*key), *value);
}
}
#[test]
fn read_y_from_instruction() {
let data_set: BTreeMap<u16, u8> = BTreeMap::from([
(0xffff, 0x0f),
(0x0000, 0x00),
(0x00a0, 0x0a),
(0x00b0, 0x0b)
]);
for (key, value) in data_set.iter() {
assert_eq!(InstructionUtil::read_y_from_instruction(*key), *value);
}
}
#[test]
fn read_byte_from_instruction() {
let data_set: BTreeMap<u16, u8> = BTreeMap::from([
(0xffff, 0xff),
(0x0000, 0x00),
(0xabcd, 0xcd),
(0xcdab, 0xab)
]);
for (key, value) in data_set.iter() {
assert_eq!(InstructionUtil::read_byte_from_instruction(*key), *value);
}
}
#[test]
fn read_upper_byte_lower_nibble() {
let data_set: BTreeMap<u16, u8> = BTreeMap::from([
(0x0000, 0x00),
(0xabcd, 0x0b),
(0xffff, 0x0f),
(0xcdab, 0x0d)
]);
for (key, value) in data_set.iter() {
assert_eq!(InstructionUtil::read_upper_byte_lower_nibble(*key), *value);
}
}
#[test]
fn read_bits_from_instruction() {
let data_set: BTreeMap<u16, (u8, u8, u16)> = BTreeMap::from([
(0x0000, (0, 7, 0x00)),
(0x0000, (8, 15, 0x00)),
(0xffff, (0, 7, 0xff)),
(0xffff, (8, 15, 0xff)),
]);
for (key, (start, end, value)) in data_set.iter() {
let result = InstructionUtil::read_bits_from_instruction(*key, *start, *end);
assert_eq!(result, *value);
}
}