From 36fc109d91b9216d443e693e6b947341d93628c3 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 17 May 2024 21:23:56 +0200 Subject: [PATCH] add more unit tests --- servicepoint2/src/command.rs | 51 ++++++++++++++++++++------------- servicepoint2/src/pixel_grid.rs | 49 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/servicepoint2/src/command.rs b/servicepoint2/src/command.rs index 8d7c34d..0d46cff 100644 --- a/servicepoint2/src/command.rs +++ b/servicepoint2/src/command.rs @@ -702,25 +702,36 @@ mod tests { Err(TryFromPacketError::UnexpectedPayloadSize(1, 2))); } - /* TODO unexpected payload size + #[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)); + } - /// 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_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))); + } } diff --git a/servicepoint2/src/pixel_grid.rs b/servicepoint2/src/pixel_grid.rs index 0ee4894..39b1b08 100644 --- a/servicepoint2/src/pixel_grid.rs +++ b/servicepoint2/src/pixel_grid.rs @@ -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 = grid.into(); + + let mut grid = PixelGrid::load(8, 3, &data); + assert_eq!(grid.mut_data_ref(), [0xAA, 0x55, 0xAA]); + } +} \ No newline at end of file