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