From ab66a6a33e5d4940594a90f2a9934e4480789eba Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 17 May 2024 18:36:18 +0200 Subject: [PATCH] unit tests for round trip command and packet, fix bugs and add needed derivations --- servicepoint2/src/bit_vec.rs | 2 +- servicepoint2/src/byte_grid.rs | 2 +- servicepoint2/src/command.rs | 63 ++++++++++++++++++++++++--- servicepoint2/src/command_code.rs | 4 ++ servicepoint2/src/compression_code.rs | 2 +- servicepoint2/src/packet.rs | 17 +++++++- servicepoint2/src/pixel_grid.rs | 2 +- 7 files changed, 81 insertions(+), 11 deletions(-) diff --git a/servicepoint2/src/bit_vec.rs b/servicepoint2/src/bit_vec.rs index 4313287..46c7e5e 100644 --- a/servicepoint2/src/bit_vec.rs +++ b/servicepoint2/src/bit_vec.rs @@ -1,5 +1,5 @@ /// A vector of bits -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct BitVec { data: Vec, } diff --git a/servicepoint2/src/byte_grid.rs b/servicepoint2/src/byte_grid.rs index cc27db5..a54dd3a 100644 --- a/servicepoint2/src/byte_grid.rs +++ b/servicepoint2/src/byte_grid.rs @@ -1,5 +1,5 @@ /// A 2D grid of bytes -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct ByteGrid { /// Size in the x-axis pub width: usize, diff --git a/servicepoint2/src/command.rs b/servicepoint2/src/command.rs index 353a2a6..0e89440 100644 --- a/servicepoint2/src/command.rs +++ b/servicepoint2/src/command.rs @@ -1,11 +1,11 @@ -use crate::command_code::CommandCode; -use crate::compression::{into_compressed, into_decompressed}; use crate::{ BitVec, ByteGrid, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE, }; +use crate::command_code::CommandCode; +use crate::compression::{into_compressed, into_decompressed}; /// An origin marks the top left position of a window sent to the display. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Origin(pub u16, pub u16); impl Origin { @@ -25,7 +25,7 @@ pub type Offset = u16; pub type Brightness = u8; /// A command to send to the display. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum Command { /// Set all pixels to the off state Clear, @@ -290,7 +290,7 @@ fn packet_into_bitmap_win( pixel_h as usize, &payload, ), - CompressionCode::Uncompressed, + compression, )) } @@ -523,3 +523,56 @@ pub mod c_api { _ = Box::from_raw(ptr); } } + +#[cfg(test)] +mod tests { + use crate::{BitVec, ByteGrid, Command, CompressionCode, Origin, Packet, PixelGrid}; + + fn round_trip(original: Command) { + let packet: Packet = original.clone().into(); + let copy: Command = match Command::try_from(packet) { + Ok(command) => command, + Err(err) => panic!("could not reload {original:?}: {err:?}"), + }; + assert_eq!(copy, original); + } + + #[test] + fn round_trip_clear() { round_trip(Command::Clear); } + + #[test] + fn round_trip_hard_reset() { round_trip(Command::HardReset); } + + #[test] + fn round_trip_fade_out() { round_trip(Command::FadeOut); } + + #[test] + fn round_trip_brightness() { round_trip(Command::Brightness(6)); } + + #[test] + #[allow(deprecated)] + fn round_trip_bitmap_legacy() { round_trip(Command::BitmapLegacy); } + + #[test] + fn round_trip_char_brightness() { + round_trip(Command::CharBrightness(Origin(5, 2), ByteGrid::new(7, 5))); + } + + #[test] + fn round_trip_cp437_data() { + round_trip(Command::Cp437Data(Origin(5, 2), ByteGrid::new(7, 5))); + } + + #[test] + fn round_trip_bitmap_linear() { + let codes = [CompressionCode::Uncompressed, CompressionCode::Lzma, + CompressionCode::Bzip2, CompressionCode::Zlib, CompressionCode::Zstd]; + for compression in codes { + round_trip(Command::BitmapLinear(23, BitVec::new(40), compression)); + round_trip(Command::BitmapLinearAnd(23, BitVec::new(40), compression)); + round_trip(Command::BitmapLinearOr(23, BitVec::new(40), compression)); + round_trip(Command::BitmapLinearXor(23, BitVec::new(40), compression)); + round_trip(Command::BitmapLinearWin(Origin(0, 0), PixelGrid::max_sized(), compression)); + } + } +} \ No newline at end of file diff --git a/servicepoint2/src/command_code.rs b/servicepoint2/src/command_code.rs index dff6a57..eb482c1 100644 --- a/servicepoint2/src/command_code.rs +++ b/servicepoint2/src/command_code.rs @@ -51,6 +51,10 @@ impl TryFrom for CommandCode { value if value == BitmapLinearAnd as u16 => Ok(BitmapLinearAnd), value if value == BitmapLinearOr as u16 => Ok(BitmapLinearOr), value if value == BitmapLinearXor as u16 => Ok(BitmapLinearXor), + value if value == BitmapLinearWinZstd as u16 => Ok(BitmapLinearWinZstd), + value if value == BitmapLinearWinLzma as u16 => Ok(BitmapLinearWinLzma), + value if value == BitmapLinearWinZlib as u16 => Ok(BitmapLinearWinZlib), + value if value == BitmapLinearWinBzip2 as u16 => Ok(BitmapLinearWinBzip2), _ => Err(()), } } diff --git a/servicepoint2/src/compression_code.rs b/servicepoint2/src/compression_code.rs index 631054f..180f87f 100644 --- a/servicepoint2/src/compression_code.rs +++ b/servicepoint2/src/compression_code.rs @@ -2,7 +2,7 @@ use CompressionCode::*; /// Specifies the kind of compression to use. Availability depends on features. #[repr(u16)] -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum CompressionCode { Uncompressed = 0x0, #[cfg(feature = "compression_zlib")] diff --git a/servicepoint2/src/packet.rs b/servicepoint2/src/packet.rs index bafedd3..8af146a 100644 --- a/servicepoint2/src/packet.rs +++ b/servicepoint2/src/packet.rs @@ -1,14 +1,14 @@ use std::mem::size_of; /// A raw header. Should probably not be used directly. -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct Header(pub u16, pub u16, pub u16, pub u16, pub u16); /// The raw payload. Should probably not be used directly. pub type Payload = Vec; /// The raw packet. Should probably not be used directly. -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct Packet(pub Header, pub Payload); impl From for Vec { @@ -97,3 +97,16 @@ mod c_api { _ = Box::from_raw(this) } } + +#[cfg(test)] +mod tests { + use crate::{Header, Packet}; + + #[test] + fn round_trip() { + let p = Packet(Header(0,1,2,3,4), vec![42u8; 23]); + let data: Vec = p.into(); + let p = Packet::try_from(&*data).unwrap(); + assert_eq!(p, Packet(Header(0,1,2,3,4), vec![42u8; 23])); + } +} \ No newline at end of file diff --git a/servicepoint2/src/pixel_grid.rs b/servicepoint2/src/pixel_grid.rs index f5a03e9..2ab8595 100644 --- a/servicepoint2/src/pixel_grid.rs +++ b/servicepoint2/src/pixel_grid.rs @@ -1,7 +1,7 @@ use crate::{BitVec, PIXEL_HEIGHT, PIXEL_WIDTH}; /// A grid of pixels stored in packed bytes. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct PixelGrid { /// the width in pixels pub width: usize,