diff --git a/agb-image-converter/Cargo.toml b/agb-image-converter/Cargo.toml index 15dd5d66..ce55029c 100644 --- a/agb-image-converter/Cargo.toml +++ b/agb-image-converter/Cargo.toml @@ -20,3 +20,6 @@ proc-macro2 = "1" quote = "1" asefile = "0.3.8" fontdue = "0.9" + +[dev-dependencies] +quickcheck = "1" diff --git a/agb-image-converter/src/colour.rs b/agb-image-converter/src/colour.rs index 26c64f6b..cba2a831 100644 --- a/agb-image-converter/src/colour.rs +++ b/agb-image-converter/src/colour.rs @@ -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> { + Box::new( + vec![ + Colour::from_rgb(0, 0, 0, 0), + Colour::from_rgb(self.r, self.g, self.b, 0), + self.clone(), + ] + .into_iter(), + ) + } +} diff --git a/agb-image-converter/src/palette16.rs b/agb-image-converter/src/palette16.rs index 03ae3a57..ea3d8128 100644 --- a/agb-image-converter/src/palette16.rs +++ b/agb-image-converter/src/palette16.rs @@ -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) -> () { + 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 + } + } +}