add more unit tests

This commit is contained in:
Vinzenz Schroeter 2024-05-17 21:23:56 +02:00
parent c61c267b02
commit 36fc109d91
2 changed files with 80 additions and 20 deletions

View file

@ -702,25 +702,36 @@ mod tests {
Err(TryFromPacketError::UnexpectedPayloadSize(1, 2))); Err(TryFromPacketError::UnexpectedPayloadSize(1, 2)));
} }
/* TODO unexpected payload size #[test]
fn error_reserved_used() {
/// Set the brightness of tiles let Packet(header, payload)
CharBrightness(Origin, crate::byte_grid::ByteGrid), = Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into();
/// Set pixel data starting at the offset. let Header(command, offset, length, sub, reserved) = header;
/// The contained BitVec is always uncompressed. let p = Packet(Header(command, offset, length, sub, 69), payload);
BitmapLinear(Offset, crate::bit_vec::BitVec, crate::compression_code::CompressionCode), assert_eq!(
/// Set pixel data according to an and-mask starting at the offset. Command::try_from(p),
/// The contained BitVec is always uncompressed. Err(TryFromPacketError::ExtraneousHeaderValues));
BitmapLinearAnd(Offset, crate::bit_vec::BitVec, crate::compression_code::CompressionCode), }
/// Set pixel data according to an or-mask starting at the offset.
/// The contained BitVec is always uncompressed. #[test]
BitmapLinearOr(Offset, crate::bit_vec::BitVec, crate::compression_code::CompressionCode), fn error_invalid_compression() {
/// Set pixel data according to an xor-mask starting at the offset. let Packet(header, payload)
/// The contained BitVec is always uncompressed. = Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into();
BitmapLinearXor(Offset, crate::bit_vec::BitVec, crate::compression_code::CompressionCode), let Header(command, offset, length, sub, reserved) = header;
/// Show text on the screen. Note that the byte data has to be CP437 encoded. let p = Packet(Header(command, offset, length, 42, reserved), payload);
Cp437Data(Origin, crate::byte_grid::ByteGrid), assert_eq!(
/// Sets a window of pixels to the specified values Command::try_from(p),
BitmapLinearWin(Origin, crate::pixel_grid::PixelGrid, crate::compression_code::CompressionCode), Err(TryFromPacketError::InvalidCompressionCode(42)));
*/ }
#[test]
fn error_unexpected_size() {
let Packet(header, payload)
= Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into();
let Header(command, offset, length, compression, reserved) = header;
let p = Packet(Header(command, offset, 420, compression, reserved), payload);
assert_eq!(
Command::try_from(p),
Err(TryFromPacketError::UnexpectedPayloadSize(420, length as usize)));
}
} }

View file

@ -208,3 +208,52 @@ pub mod c_api {
} }
} }
} }
#[cfg(test)]
mod tests {
use crate::PixelGrid;
#[test]
fn fill() {
let mut grid = PixelGrid::new(8, 2);
assert_eq!(grid.mut_data_ref(), [0x00, 0x00]);
grid.fill(true);
assert_eq!(grid.mut_data_ref(), [0xFF, 0xFF]);
grid.fill(false);
assert_eq!(grid.mut_data_ref(), [0x00, 0x00]);
}
#[test]
fn get_set() {
let mut grid = PixelGrid::new(8, 2);
assert_eq!(grid.get(0, 0), false);
assert_eq!(grid.get(1, 1), false);
grid.set(5, 0, true);
grid.set(1, 1, true);
assert_eq!(grid.mut_data_ref(), [0x04, 0x40]);
assert_eq!(grid.get(5, 0), true);
assert_eq!(grid.get(1, 1), true);
assert_eq!(grid.get(1, 0), false);
}
#[test]
fn load() {
let mut grid = PixelGrid::new(8, 3);
for x in 0..grid.width {
for y in 0..grid.height {
grid.set(x, y, (x + y) % 2 == 0);
}
}
assert_eq!(grid.mut_data_ref(), [0xAA, 0x55, 0xAA]);
let data: Vec<u8> = grid.into();
let mut grid = PixelGrid::load(8, 3, &data);
assert_eq!(grid.mut_data_ref(), [0xAA, 0x55, 0xAA]);
}
}