unit tests for round trip command and packet, fix bugs and add needed derivations

This commit is contained in:
Vinzenz Schroeter 2024-05-17 18:36:18 +02:00
parent 483a5057fe
commit ab66a6a33e
7 changed files with 81 additions and 11 deletions

View file

@ -1,5 +1,5 @@
/// A vector of bits
#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub struct BitVec {
data: Vec<u8>,
}

View file

@ -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,

View file

@ -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));
}
}
}

View file

@ -51,6 +51,10 @@ impl TryFrom<u16> 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(()),
}
}

View file

@ -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")]

View file

@ -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<u8>;
/// The raw packet. Should probably not be used directly.
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct Packet(pub Header, pub Payload);
impl From<Packet> for Vec<u8> {
@ -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<u8> = p.into();
let p = Packet::try_from(&*data).unwrap();
assert_eq!(p, Packet(Header(0,1,2,3,4), vec![42u8; 23]));
}
}

View file

@ -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,