format code
This commit is contained in:
parent
319ef4572a
commit
c534929089
4 changed files with 84 additions and 23 deletions
|
|
@ -72,8 +72,16 @@ impl Bitmap {
|
|||
/// - when the width is not dividable by 8
|
||||
#[must_use]
|
||||
pub fn load(width: usize, height: usize, data: &[u8]) -> Self {
|
||||
assert_eq!(width % 8, 0, "width must be a multiple of 8, but is {width}");
|
||||
assert_eq!(data.len(), height * width / 8, "data length must match dimensions, with 8 pixels per byte.");
|
||||
assert_eq!(
|
||||
width % 8,
|
||||
0,
|
||||
"width must be a multiple of 8, but is {width}"
|
||||
);
|
||||
assert_eq!(
|
||||
data.len(),
|
||||
height * width / 8,
|
||||
"data length must match dimensions, with 8 pixels per byte."
|
||||
);
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
|
|
@ -91,11 +99,23 @@ impl Bitmap {
|
|||
/// - when the width is not dividable by 8
|
||||
#[must_use]
|
||||
pub fn from_bitvec(width: usize, bit_vec: BitVec) -> Self {
|
||||
assert_eq!(width % 8, 0, "width must be a multiple of 8, but is {width}");
|
||||
assert_eq!(
|
||||
width % 8,
|
||||
0,
|
||||
"width must be a multiple of 8, but is {width}"
|
||||
);
|
||||
let len = bit_vec.len();
|
||||
let height = len / width;
|
||||
assert_eq!(0, len % width, "dimension mismatch - len {len} is not dividable by {width}");
|
||||
Self { width, height, bit_vec }
|
||||
assert_eq!(
|
||||
0,
|
||||
len % width,
|
||||
"dimension mismatch - len {len} is not dividable by {width}"
|
||||
);
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
bit_vec,
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterate over all cells in [Bitmap].
|
||||
|
|
@ -218,7 +238,7 @@ impl From<Bitmap> for BitVec {
|
|||
|
||||
impl From<&ValueGrid<bool>> for Bitmap {
|
||||
/// Converts a grid of [bool]s into a [Bitmap].
|
||||
///
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// - when the width of `value` is not dividable by 8
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ mod tests {
|
|||
assert_eq!(Brightness::MAX, Brightness::saturating_from(100));
|
||||
assert_eq!(Brightness(5), Brightness::saturating_from(5));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "rand")]
|
||||
fn test() {
|
||||
|
|
|
|||
|
|
@ -119,7 +119,10 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn char_brightness() {
|
||||
assert_eq!(CommandCode::try_from(0x0005), Ok(CommandCode::CharBrightness));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0005),
|
||||
Ok(CommandCode::CharBrightness)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::CharBrightness), 0x0005);
|
||||
}
|
||||
|
||||
|
|
@ -144,59 +147,86 @@ mod tests {
|
|||
#[test]
|
||||
#[allow(deprecated)]
|
||||
fn bitmap_legacy() {
|
||||
assert_eq!(CommandCode::try_from(0x0010), Ok(CommandCode::BitmapLegacy));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0010),
|
||||
Ok(CommandCode::BitmapLegacy)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLegacy), 0x0010);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn linear() {
|
||||
assert_eq!(CommandCode::try_from(0x0012), Ok(CommandCode::BitmapLinear));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0012),
|
||||
Ok(CommandCode::BitmapLinear)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinear), 0x0012);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linear_and() {
|
||||
assert_eq!(CommandCode::try_from(0x0014), Ok(CommandCode::BitmapLinearAnd));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0014),
|
||||
Ok(CommandCode::BitmapLinearAnd)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinearAnd), 0x0014);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linear_xor() {
|
||||
assert_eq!(CommandCode::try_from(0x0016), Ok(CommandCode::BitmapLinearXor));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0016),
|
||||
Ok(CommandCode::BitmapLinearXor)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinearXor), 0x0016);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "compression_zlib")]
|
||||
fn bitmap_win_zlib() {
|
||||
assert_eq!(CommandCode::try_from(0x0017), Ok(CommandCode::BitmapLinearWinZlib));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0017),
|
||||
Ok(CommandCode::BitmapLinearWinZlib)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinearWinZlib), 0x0017);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "compression_bzip2")]
|
||||
fn bitmap_win_bzip2() {
|
||||
assert_eq!(CommandCode::try_from(0x0018), Ok(CommandCode::BitmapLinearWinBzip2));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0018),
|
||||
Ok(CommandCode::BitmapLinearWinBzip2)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinearWinBzip2), 0x0018);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "compression_lzma")]
|
||||
fn bitmap_win_lzma() {
|
||||
assert_eq!(CommandCode::try_from(0x0019), Ok(CommandCode::BitmapLinearWinLzma));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0019),
|
||||
Ok(CommandCode::BitmapLinearWinLzma)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinearWinLzma), 0x0019);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "compression_zstd")]
|
||||
fn bitmap_win_zstd() {
|
||||
assert_eq!(CommandCode::try_from(0x001A), Ok(CommandCode::BitmapLinearWinZstd));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x001A),
|
||||
Ok(CommandCode::BitmapLinearWinZstd)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinearWinZstd), 0x001A);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bitmap_win_uncompressed() {
|
||||
assert_eq!(CommandCode::try_from(0x0013), Ok(CommandCode::BitmapLinearWinUncompressed));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0013),
|
||||
Ok(CommandCode::BitmapLinearWinUncompressed)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinearWinUncompressed), 0x0013);
|
||||
}
|
||||
|
||||
|
|
@ -208,7 +238,10 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn linear_or() {
|
||||
assert_eq!(CommandCode::try_from(0x0015), Ok(CommandCode::BitmapLinearOr));
|
||||
assert_eq!(
|
||||
CommandCode::try_from(0x0015),
|
||||
Ok(CommandCode::BitmapLinearOr)
|
||||
);
|
||||
assert_eq!(u16::from(CommandCode::BitmapLinearOr), 0x0015);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,8 +90,16 @@ impl<T: Value> ValueGrid<T> {
|
|||
pub fn from_vec(width: usize, data: Vec<T>) -> Self {
|
||||
let len = data.len();
|
||||
let height = len / width;
|
||||
assert_eq!(0, len % width, "dimension mismatch - len {len} is not dividable by {width}");
|
||||
Self { data, width, height }
|
||||
assert_eq!(
|
||||
0,
|
||||
len % width,
|
||||
"dimension mismatch - len {len} is not dividable by {width}"
|
||||
);
|
||||
Self {
|
||||
data,
|
||||
width,
|
||||
height,
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads a [ValueGrid] with the specified width from the provided data, wrapping to as many rows as needed.
|
||||
|
|
@ -511,7 +519,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn ref_mut() {
|
||||
let mut vec = ValueGrid::from_vec(3, vec![0, 1, 2, 3,4,5,6,7,8]);
|
||||
let mut vec = ValueGrid::from_vec(3, vec![0, 1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
|
||||
let top_left = vec.get_ref_mut(0, 0);
|
||||
*top_left += 5;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue