Fix boundaries

This commit is contained in:
Nicolas 2022-04-09 18:30:13 +02:00
parent 35ca4b2605
commit 71e531a276
3 changed files with 38 additions and 6 deletions

View File

@ -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()
}

View File

@ -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]
}

View File

@ -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 { .. }));
}
}