add more unit tests

This commit is contained in:
Vinzenz Schroeter 2024-05-17 21:02:50 +02:00
parent 8426698b9f
commit c61c267b02
8 changed files with 167 additions and 27 deletions

View file

@ -1,4 +1,5 @@
use clap::Parser; use clap::Parser;
use servicepoint2::{ByteGrid, Command, Connection, Origin}; use servicepoint2::{ByteGrid, Command, Connection, Origin};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -43,6 +44,6 @@ fn main() {
} }
connection connection
.send(Command::Cp437Data(Origin::top_left(), chars).into()) .send(Command::Cp437Data(Origin(0, 0), chars).into())
.unwrap(); .unwrap();
} }

View file

@ -25,7 +25,7 @@ fn main() {
connection connection
.send( .send(
Command::BitmapLinearWin( Command::BitmapLinearWin(
Origin::top_left(), Origin(0, 0),
field.clone(), field.clone(),
CompressionCode::Bzip2, CompressionCode::Bzip2,
) )

View file

@ -4,8 +4,8 @@ use std::time::Duration;
use clap::Parser; use clap::Parser;
use servicepoint2::{ use servicepoint2::{
Command, CompressionCode, Connection, Origin, PixelGrid, PIXEL_HEIGHT, Command, CompressionCode, Connection, Origin, PIXEL_HEIGHT, PIXEL_WIDTH,
PIXEL_WIDTH, PixelGrid,
}; };
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -29,7 +29,7 @@ fn main() {
connection connection
.send( .send(
Command::BitmapLinearWin( Command::BitmapLinearWin(
Origin::top_left(), Origin(0, 0),
pixels.clone(), pixels.clone(),
CompressionCode::Lzma, CompressionCode::Lzma,
) )

View file

@ -3,11 +3,11 @@ use std::time::Duration;
use clap::Parser; use clap::Parser;
use rand::Rng; use rand::Rng;
use servicepoint2::Command::{BitmapLinearWin, Brightness, CharBrightness};
use servicepoint2::{ use servicepoint2::{
ByteGrid, CompressionCode, Connection, Origin, PixelGrid, TILE_HEIGHT, ByteGrid, CompressionCode, Connection, Origin, PixelGrid, TILE_HEIGHT,
TILE_WIDTH, TILE_WIDTH,
}; };
use servicepoint2::Command::{BitmapLinearWin, Brightness, CharBrightness};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct Cli { struct Cli {
@ -32,7 +32,7 @@ fn main() {
filled_grid.fill(true); filled_grid.fill(true);
let command = BitmapLinearWin( let command = BitmapLinearWin(
Origin::top_left(), Origin(0, 0),
filled_grid, filled_grid,
CompressionCode::Lzma, CompressionCode::Lzma,
); );

View file

@ -285,4 +285,19 @@ mod tests {
let vec = BitVec::new(0); let vec = BitVec::new(0);
assert!(vec.is_empty()); assert!(vec.is_empty());
} }
#[test]
fn get_returns_old() {
let mut vec = BitVec::new(8);
assert_eq!(vec.set(1, true), false);
assert_eq!(vec.set(1, true), true);
assert_eq!(vec.set(1, false), true);
assert_eq!(vec.set(1, false), false);
}
#[test]
fn debug_print() {
let vec = BitVec::new(8 * 3);
format!("{vec:?}");
}
} }

View file

@ -1,19 +1,13 @@
use crate::command_code::CommandCode;
use crate::compression::{into_compressed, into_decompressed};
use crate::{ use crate::{
BitVec, ByteGrid, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE, 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. /// An origin marks the top left position of a window sent to the display.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct Origin(pub u16, pub u16); pub struct Origin(pub u16, pub u16);
impl Origin {
pub fn top_left() -> Self {
Self(0, 0)
}
}
/// Size defines the width and height of a window /// Size defines the width and height of a window
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Size(pub u16, pub u16); pub struct Size(pub u16, pub u16);
@ -164,6 +158,7 @@ impl From<Command> for Packet {
#[derive(Debug)] #[derive(Debug)]
/// Err values for `Command::try_from`. /// Err values for `Command::try_from`.
#[derive(PartialEq)]
pub enum TryFromPacketError { pub enum TryFromPacketError {
/// the contained command code does not correspond to a known command /// the contained command code does not correspond to a known command
InvalidCommand(u16), InvalidCommand(u16),
@ -526,9 +521,9 @@ pub mod c_api {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{BitVec, ByteGrid, Command, CompressionCode, Header, Origin, Packet, PixelGrid};
BitVec, ByteGrid, Command, CompressionCode, Origin, Packet, PixelGrid, use crate::command::TryFromPacketError;
}; use crate::command_code::CommandCode;
fn round_trip(original: Command) { fn round_trip(original: Command) {
let packet: Packet = original.clone().into(); let packet: Packet = original.clone().into();
@ -539,6 +534,16 @@ mod tests {
assert_eq!(copy, original); assert_eq!(copy, original);
} }
fn all_compressions() -> [CompressionCode; 5] {
[
CompressionCode::Uncompressed,
CompressionCode::Lzma,
CompressionCode::Bzip2,
CompressionCode::Zlib,
CompressionCode::Zstd,
]
}
#[test] #[test]
fn round_trip_clear() { fn round_trip_clear() {
round_trip(Command::Clear); round_trip(Command::Clear);
@ -577,14 +582,7 @@ mod tests {
#[test] #[test]
fn round_trip_bitmap_linear() { fn round_trip_bitmap_linear() {
let codes = [ for compression in all_compressions() {
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::BitmapLinear(23, BitVec::new(40), compression));
round_trip(Command::BitmapLinearAnd( round_trip(Command::BitmapLinearAnd(
23, 23,
@ -608,4 +606,121 @@ mod tests {
)); ));
} }
} }
#[test]
fn error_invalid_command() {
let p = Packet(Header(0xFF, 0x00, 0x00, 0x00, 0x00), vec!());
let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::InvalidCommand(0xFF))))
}
#[test]
fn error_extraneous_header_values_clear() {
let p = Packet(Header(CommandCode::Clear.into(), 0x05, 0x00, 0x00, 0x00), vec!());
let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues)))
}
#[test]
fn error_extraneous_header_values_brightness() {
let p = Packet(Header(CommandCode::Brightness.into(), 0x00, 0x13, 0x37, 0x00), vec!(5));
let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues)))
}
#[test]
fn error_extraneous_header_hard_reset() {
let p = Packet(Header(CommandCode::HardReset.into(), 0x00, 0x00, 0x00, 0x01), vec!());
let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues)))
}
#[test]
fn error_extraneous_header_fade_out() {
let p = Packet(Header(CommandCode::FadeOut.into(), 0x10, 0x00, 0x00, 0x01), vec!());
let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues)))
}
#[test]
fn error_unexpected_payload() {
let p = Packet(Header(CommandCode::FadeOut.into(), 0x00, 0x00, 0x00, 0x00), vec!(5, 7));
let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::UnexpectedPayloadSize(0, 2))))
}
#[test]
fn error_decompression_failed_win() {
for compression in all_compressions() {
let p: Packet = Command::BitmapLinearWin(Origin(16, 8), PixelGrid::new(8, 8), compression).into();
let Packet(header, mut payload) = p;
// mangle it
for i in 0..payload.len() {
payload[i] -= payload[i] / 2;
}
let p = Packet(header, payload);
let result = Command::try_from(p);
if compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
} else {
assert!(matches!(result, Ok(_)));
}
}
}
#[test]
fn error_decompression_failed_and() {
for compression in all_compressions() {
let p: Packet = Command::BitmapLinearAnd(0, BitVec::new(8), compression).into();
let Packet(header, mut payload) = p;
// mangle it
for i in 0..payload.len() {
payload[i] -= payload[i] / 2;
}
let p = Packet(header, payload);
let result = Command::try_from(p);
if compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
} else {
assert!(matches!(result, Ok(_)));
}
}
}
#[test]
fn unexpected_payload_size_brightness() {
assert_eq!(
Command::try_from(Packet(Header(CommandCode::Brightness.into(), 0, 0, 0, 0), vec!())),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 0)));
assert_eq!(
Command::try_from(Packet(Header(CommandCode::Brightness.into(), 0, 0, 0, 0), vec!(0, 0))),
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),
*/
} }

View file

@ -48,7 +48,12 @@ pub(crate) fn into_decompressed(
} }
} }
#[cfg(feature = "compression_lzma")] #[cfg(feature = "compression_lzma")]
CompressionCode::Lzma => Some(lzma::decompress(&payload).unwrap()), CompressionCode::Lzma => {
match lzma::decompress(&payload) {
Err(_) => None,
Ok(decompressed) => Some(decompressed),
}
}
#[cfg(feature = "compression_zstd")] #[cfg(feature = "compression_zstd")]
CompressionCode::Zstd => { CompressionCode::Zstd => {
let mut decoder = match ZstdDecoder::new(&*payload) { let mut decoder = match ZstdDecoder::new(&*payload) {

4
shell.nix Normal file
View file

@ -0,0 +1,4 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
nativeBuildInputs = with pkgs.buildPackages; [ rustup pkg-config xe lzma cargo-tarpaulin ];
}