add tests, fix bug
Some checks failed
Rust / build (pull_request) Failing after 1m4s

This commit is contained in:
Vinzenz Schroeter 2025-03-08 18:13:51 +01:00
parent 18db901fb5
commit 28f0bd5903
9 changed files with 99 additions and 49 deletions

View file

@ -141,17 +141,18 @@ impl BitmapCommand {
Some(decompressed) => decompressed,
};
let bitmap = Bitmap::load(
tile_w as usize * TILE_SIZE,
pixel_h as usize,
&payload,
)?;
Ok(Self {
origin: Origin::new(
tiles_x as usize * TILE_SIZE,
pixels_y as usize,
),
bitmap: Bitmap::load(
tile_w as usize * TILE_SIZE,
pixel_h as usize,
&payload,
)
.unwrap(),
bitmap,
compression,
})
}
@ -159,9 +160,9 @@ impl BitmapCommand {
#[cfg(test)]
mod tests {
use super::*;
use crate::command_code::CommandCode;
use crate::*;
use super::*;
#[test]
fn command_code() {
@ -185,7 +186,7 @@ mod tests {
let p: Packet = commands::BitmapCommand {
origin: Origin::new(16, 8),
bitmap: Bitmap::new(8, 8).unwrap(),
compression,
compression: *compression,
}
.into();
@ -201,7 +202,7 @@ mod tests {
let p = Packet { header, payload };
let result = TypedCommand::try_from(p);
if compression != CompressionCode::Uncompressed {
if *compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
} else {
assert!(result.is_ok());

View file

@ -167,7 +167,7 @@ mod tests {
BitVecCommand {
offset: 23,
bitvec: BitVec::repeat(false, 40),
compression,
compression: *compression,
operation,
}
.into(),
@ -177,7 +177,7 @@ mod tests {
BitmapCommand {
origin: Origin::ZERO,
bitmap: Bitmap::max_sized(),
compression,
compression: *compression,
}
.into(),
);
@ -190,7 +190,7 @@ mod tests {
let p: Packet = commands::BitVecCommand {
offset: 0,
bitvec: BitVec::repeat(false, 8),
compression,
compression: *compression,
operation: BinaryOperation::Overwrite,
}
.into();
@ -206,7 +206,7 @@ mod tests {
let p = Packet { header, payload };
let result = TypedCommand::try_from(p);
if compression != CompressionCode::Uncompressed {
if *compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
} else {
// when not compressing, there is no way to detect corrupted data

View file

@ -38,7 +38,8 @@ impl TryFrom<Packet> for BrightnessGridCommand {
payload,
} = packet;
let grid = ByteGrid::load(width as usize, height as usize, &payload).unwrap();
let grid =
ByteGrid::load(width as usize, height as usize, &payload).unwrap();
let grid = match BrightnessGrid::try_from(grid) {
Ok(grid) => grid,
Err(val) => return Err(TryFromPacketError::InvalidBrightness(val)),

View file

@ -46,12 +46,12 @@ mod tests {
use crate::Header;
#[test]
fn round_trip_clear() {
fn round_trip() {
crate::commands::tests::round_trip(ClearCommand.into());
}
#[test]
fn error_extraneous_header_values_clear() {
fn extraneous_header_values() {
let p = Packet {
header: Header {
command_code: CommandCode::Clear.into(),
@ -68,4 +68,24 @@ mod tests {
Err(TryFromPacketError::ExtraneousHeaderValues)
))
}
#[test]
fn invalid_command_code() {
let p = Packet {
header: Header {
command_code: CommandCode::HardReset.into(),
a: 0x00,
b: 0x00,
c: 0x00,
d: 0x00,
},
payload: vec![],
};
assert_eq!(
Err(TryFromPacketError::InvalidCommand(
CommandCode::HardReset.into()
)),
ClearCommand::try_from(p)
);
}
}

View file

@ -1,7 +1,7 @@
use crate::{
command_code::CommandCode, BitVecCommand, BitmapCommand, BrightnessCommand,
BrightnessGridCommand, CharGridCommand, ClearCommand, Cp437GridCommand,
FadeOutCommand, HardResetCommand, Header, Packet,
FadeOutCommand, HardResetCommand, Header, LoadBitmapError, Packet,
};
/// This enum contains all commands provided by the library.
@ -60,6 +60,8 @@ pub enum TryFromPacketError {
/// Some provided text was not valid UTF-8.
#[error(transparent)]
InvalidUtf8(#[from] std::string::FromUtf8Error),
#[error(transparent)]
LoadBitmapFailed(#[from] LoadBitmapError),
}
impl TryFrom<Packet> for TypedCommand {

View file

@ -41,7 +41,7 @@ pub enum CompressionCode {
impl CompressionCode {
/// All available compression codes (depending on features).
pub const ALL: [CompressionCode; 5] = [
pub const ALL: &'static [CompressionCode] = &[
Self::Uncompressed,
#[cfg(feature = "compression_zlib")]
Self::Zlib,

View file

@ -397,7 +397,8 @@ mod tests {
8,
1,
&[true, false, true, false, true, false, true, false],
).unwrap();
)
.unwrap();
let converted = Bitmap::try_from(&original).unwrap();
let reconverted = ValueGrid::from(&converted);
assert_eq!(original, reconverted);
@ -405,26 +406,38 @@ mod tests {
#[test]
fn load_invalid_width() {
let data = BitVec::repeat(false, 7*3).into_vec();
assert_eq!(Bitmap::load(7, 3, &data), Err(LoadBitmapError::InvalidWidth))
let data = BitVec::repeat(false, 7 * 3).into_vec();
assert_eq!(
Bitmap::load(7, 3, &data),
Err(LoadBitmapError::InvalidWidth)
)
}
#[test]
fn load_invalid_size() {
let data = BitVec::repeat(false, 8*4).into_vec();
assert_eq!(Bitmap::load(8, 3, &data), Err(LoadBitmapError::InvalidDataSize))
let data = BitVec::repeat(false, 8 * 4).into_vec();
assert_eq!(
Bitmap::load(8, 3, &data),
Err(LoadBitmapError::InvalidDataSize)
)
}
#[test]
fn from_vec_invalid_width() {
let data = BitVec::repeat(false, 7*3);
assert_eq!(Bitmap::from_bitvec(7, data), Err(LoadBitmapError::InvalidWidth))
let data = BitVec::repeat(false, 7 * 3);
assert_eq!(
Bitmap::from_bitvec(7, data),
Err(LoadBitmapError::InvalidWidth)
)
}
#[test]
fn from_vec_invalid_size() {
let data = BitVec::repeat(false, 7*4);
assert_eq!(Bitmap::from_bitvec(8, data), Err(LoadBitmapError::InvalidDataSize))
let data = BitVec::repeat(false, 7 * 4);
assert_eq!(
Bitmap::from_bitvec(8, data),
Err(LoadBitmapError::InvalidDataSize)
)
}
#[test]
@ -435,6 +448,6 @@ mod tests {
#[test]
fn new_invalid_width() {
assert_eq!(Bitmap::new(7,2), None)
assert_eq!(Bitmap::new(7, 2), None)
}
}

View file

@ -21,7 +21,9 @@ pub type BrightnessGrid = ValueGrid<Brightness>;
impl BrightnessGrid {
/// Like [Self::load], but ignoring any out-of-range brightness values
pub fn saturating_load(width: usize, height: usize, data: &[u8]) -> Self {
ValueGrid::load(width, height, data).unwrap().map(Brightness::saturating_from)
ValueGrid::load(width, height, data)
.unwrap()
.map(Brightness::saturating_from)
}
}
@ -52,11 +54,10 @@ impl TryFrom<ByteGrid> for BrightnessGrid {
.iter()
.map(|b| Brightness::try_from(*b))
.collect::<Result<Vec<_>, _>>()?;
Ok(BrightnessGrid::load(
value.width(),
value.height(),
&brightnesses,
).unwrap())
Ok(
BrightnessGrid::load(value.width(), value.height(), &brightnesses)
.unwrap(),
)
}
}
@ -85,7 +86,8 @@ mod tests {
Brightness::MIN,
Brightness::MAX
]
).unwrap(),
)
.unwrap(),
BrightnessGrid::saturating_load(2, 2, &[255u8, 23, 0, 42])
);
}

View file

@ -101,18 +101,6 @@ impl<T: Value> ValueGrid<T> {
})
}
/// Loads a [ValueGrid] with the specified width from the provided data, wrapping to as many rows as needed.
///
/// returns: [ValueGrid] that contains a copy of the provided data or [TryLoadValueGridError].
///
#[must_use]
pub fn wrap(
width: usize,
data: &[T],
) -> Option<Self> {
Self::from_vec(width, data.to_vec())
}
/// Iterate over all cells in [ValueGrid].
///
/// Order is equivalent to the following loop:
@ -406,7 +394,7 @@ impl<'t, T: Value> Iterator for EnumerateGrid<'t, T> {
}
let result =
Some((self.row, self.column, self.grid.get(self.row, self.column)));
Some((self.column, self.row, self.grid.get(self.column, self.row)));
self.column += 1;
if self.column == self.grid.width {
self.column = 0;
@ -520,7 +508,8 @@ mod tests {
#[test]
fn ref_mut() {
let mut vec = ValueGrid::from_vec(3, vec![0, 1, 2, 3, 4, 5, 6, 7, 8]).unwrap();
let mut vec =
ValueGrid::from_vec(3, vec![0, 1, 2, 3, 4, 5, 6, 7, 8]).unwrap();
let top_left = vec.get_ref_mut(0, 0);
*top_left += 5;
@ -596,4 +585,26 @@ mod tests {
let grid = ValueGrid::from_vec(4, vec![0, 1, 2, 3, 4, 5]);
assert_eq!(grid, None);
}
#[test]
fn load_invalid_size() {
assert_eq!(ValueGrid::load(2, 2, &[1, 2, 3]), None);
}
#[test]
fn enumerate() {
let grid = ValueGrid::load(2, 3, &[0, 1, 2, 3, 4, 5]).unwrap();
let values = grid.enumerate().collect::<Vec<_>>();
assert_eq!(
values,
vec![
(0, 0, 0),
(1, 0, 1),
(0, 1, 2),
(1, 1, 3),
(0, 2, 4),
(1, 2, 5)
]
);
}
}