add tests

This commit is contained in:
Vinzenz Schroeter 2024-10-12 18:38:43 +02:00
parent 3d47b41106
commit 5f8f691464
2 changed files with 33 additions and 3 deletions

View file

@ -77,8 +77,8 @@ impl From<BrightnessGrid> for Vec<u8> {
} }
} }
impl From<BrightnessGrid> for PrimitiveGrid<u8> { impl From<&BrightnessGrid> for PrimitiveGrid<u8> {
fn from(value: PrimitiveGrid<Brightness>) -> Self { fn from(value: &PrimitiveGrid<Brightness>) -> Self {
let u8s = value let u8s = value
.iter() .iter()
.map(|brightness| (*brightness).into()) .map(|brightness| (*brightness).into())
@ -109,3 +109,33 @@ impl Distribution<Brightness> for Standard {
Brightness(rng.gen_range(Brightness::MIN.0..=Brightness::MAX.0)) Brightness(rng.gen_range(Brightness::MIN.0..=Brightness::MAX.0))
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::DataRef;
#[test]
fn brightness_from_u8() {
assert_eq!(Err(100), Brightness::try_from(100));
assert_eq!(Ok(Brightness(1)), Brightness::try_from(1))
}
#[test]
#[cfg(feature = "rand")]
fn rand_brightness() {
let mut rng = rand::thread_rng();
for _ in 0..100 {
let _: Brightness = rng.gen();
}
}
#[test]
fn to_u8_grid() {
let mut grid = BrightnessGrid::new(2, 2);
grid.set(1, 0, Brightness::MIN);
grid.set(0, 1, Brightness::MAX);
let actual = PrimitiveGrid::from(&grid);
assert_eq!(actual.data_ref(), &[11, 0, 11, 11]);
}
}

View file

@ -331,7 +331,7 @@ mod tests {
payload: vec![42u8; 23], payload: vec![42u8; 23],
}; };
let data: Vec<u8> = p.into(); let data: Vec<u8> = p.into();
let p = Packet::try_from(&*data).unwrap(); let p = Packet::try_from(data).unwrap();
assert_eq!( assert_eq!(
p, p,
Packet { Packet {