mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 00:01:34 +11:00
Add a really basic quickcheck
This commit is contained in:
parent
d6bc6f49b4
commit
fb186809a9
|
@ -20,3 +20,6 @@ proc-macro2 = "1"
|
|||
quote = "1"
|
||||
asefile = "0.3.8"
|
||||
fontdue = "0.9"
|
||||
|
||||
[dev-dependencies]
|
||||
quickcheck = "1"
|
||||
|
|
|
@ -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(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue