From 71e531a276315e2e781a2143c3ecbc874ee3a901 Mon Sep 17 00:00:00 2001 From: Nicolas <> Date: Sat, 9 Apr 2022 18:30:13 +0200 Subject: [PATCH] Fix boundaries --- src/char.rs | 4 ++-- src/color.rs | 4 ++-- src/sampler.rs | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/char.rs b/src/char.rs index 979c066..aa35c21 100644 --- a/src/char.rs +++ b/src/char.rs @@ -23,9 +23,9 @@ impl SimpleCharSampler { impl CharSampler for SimpleCharSampler { fn sample(&self, level: f32) -> char { - let index = level * self.chars.chars().count() as f32; + assert!(0.0 <= level && level < 1.0); - assert!(index >= 0.0); + let index = level * self.chars.chars().count() as f32; self.chars.chars().nth(index as usize).unwrap() } diff --git a/src/color.rs b/src/color.rs index 9830614..5fa4b19 100644 --- a/src/color.rs +++ b/src/color.rs @@ -22,9 +22,9 @@ impl SimpleColorSampler { impl ColorSampler for SimpleColorSampler { fn sample(&self, fill: f32) -> Color { - let index = self.values.len() as f32 * fill; + assert!(0.0 <= fill && fill < 1.0); - assert!(index >= 0.0); + let index = self.values.len() as f32 * fill; self.values[index as usize] } diff --git a/src/sampler.rs b/src/sampler.rs index 12eccfd..806065d 100644 --- a/src/sampler.rs +++ b/src/sampler.rs @@ -37,9 +37,9 @@ impl Sampler for ComposedSampler { fn sample(&self, step: f32, pos: Vector) -> Sample { let level = self.animation.sample(step, pos); - if level > 1.0 { + if level >= 1.0 { Sample::Keep - } else if level > 0.0 { + } else if level >= 0.0 { let char = self.char.sample(level); let fill = self.fill.sample(level, pos); let color = self.color.sample(fill); @@ -104,4 +104,36 @@ mod test { assert!(matches!(sampler.sample(0.7, Vector::new(0.3, 0.1)), Sample::Clear)); } + + #[test] + fn sample_almost_draw() { + let mut anim = Box::new(MockAnimation::new()); + let fill = Box::new(MockFillMode::new()); + let color = Box::new(MockColorSampler::new()); + let char = Box::new(MockCharSampler::new()); + + anim.expect_sample().return_const(1.0); + + let sampler = ComposedSampler::new(anim, fill, color, char); + + assert!(matches!(sampler.sample(0.7, Vector::new(0.3, 0.1)), Sample::Keep)); + } + + + #[test] + fn sample_almost_clear() { + let mut anim = Box::new(MockAnimation::new()); + let mut fill = Box::new(MockFillMode::new()); + let mut color = Box::new(MockColorSampler::new()); + let mut char = Box::new(MockCharSampler::new()); + + anim.expect_sample().return_const(0.0); + fill.expect_sample().return_const(0.8); + color.expect_sample().return_const(Color::Blue); + char.expect_sample().return_const('a'); + + let sampler = ComposedSampler::new(anim, fill, color, char); + + assert!(matches!(sampler.sample(0.7, Vector::new(0.3, 0.1)), Sample::Draw { .. })); + } } \ No newline at end of file