Add a really basic quickcheck

This commit is contained in:
Gwilym Inzani 2024-09-25 10:47:17 +01:00
parent d6bc6f49b4
commit fb186809a9
3 changed files with 59 additions and 0 deletions

View file

@ -20,3 +20,6 @@ proc-macro2 = "1"
quote = "1"
asefile = "0.3.8"
fontdue = "0.9"
[dev-dependencies]
quickcheck = "1"

View file

@ -38,3 +38,26 @@ impl FromStr for Colour {
Ok(Colour::from_rgb(r, g, b, 255))
}
}
#[cfg(test)]
impl quickcheck::Arbitrary for Colour {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Self::from_rgb(
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
)
}
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Box::new(
vec![
Colour::from_rgb(0, 0, 0, 0),
Colour::from_rgb(self.r, self.g, self.b, 0),
self.clone(),
]
.into_iter(),
)
}
}

View file

@ -210,3 +210,36 @@ impl Palette16Optimiser {
}
}
}
#[cfg(test)]
mod test {
use quickcheck::{quickcheck, Arbitrary};
use super::*;
quickcheck! {
fn less_than_256_colours_always_fits(palettes: Vec<Palette16>) -> () {
let mut optimiser = Palette16Optimiser::new(None);
for palette in palettes.into_iter().take(16) {
optimiser.add_palette(palette);
}
optimiser.optimise_palettes();
}
}
impl Arbitrary for Palette16 {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let mut palette = Palette16::new();
let size: usize = Arbitrary::arbitrary(g);
let size = size.rem_euclid(16);
for _ in 0..size {
palette.add_colour(Arbitrary::arbitrary(g));
}
palette
}
}
}