Add slice unit tests

This commit is contained in:
Rico Riedel 2022-08-05 19:07:34 +02:00
parent 726f482687
commit dd5b215aaf
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18

View File

@ -1,21 +1,26 @@
use crate::pattern::*; use crate::pattern::*;
use crate::Vector; use crate::Vector;
#[derive(derive_more::Constructor)]
pub struct SliceFactory { pub struct SliceFactory {
child: Box<dyn PatternFactory>, child: Box<dyn PatternFactory>,
width: f32, slices: u8,
rest: f32,
} }
#[derive(derive_more::Constructor)]
pub struct Slice { pub struct Slice {
child: Box<dyn Pattern>, child: Box<dyn Pattern>,
width: f32, width: f32,
rest: f32, rest: f32,
} }
impl SliceFactory { impl PatternFactory for SliceFactory {
pub fn new(child: Box<dyn PatternFactory>, slices: u8) -> Self { fn create(&self, config: &Config) -> Box<dyn Pattern> {
Box::new(Slice::new(self.child.create(config), self.slices))
}
}
impl Slice {
pub fn new(child: Box<dyn Pattern>, slices: u8) -> Self {
let width = 1.0 / slices as f32; let width = 1.0 / slices as f32;
let rest = 1.0 - width; let rest = 1.0 - width;
@ -23,14 +28,91 @@ impl SliceFactory {
} }
} }
impl PatternFactory for SliceFactory {
fn create(&self, config: &Config) -> Box<dyn Pattern> {
Box::new(Slice::new(self.child.create(config), self.width, self.rest))
}
}
impl Pattern for Slice { impl Pattern for Slice {
fn sample(&self, pos: Vector) -> f32 { fn sample(&self, pos: Vector) -> f32 {
(self.child.sample(pos) - self.rest) / self.width (self.child.sample(pos) - self.rest) / self.width
} }
} }
#[cfg(test)]
mod test {
use super::*;
use crate::MockPatternFactory;
use mockall::predicate::eq;
#[test]
fn create_config_correct() {
let config = Config {
size: Vector::new(7.0, 3.0),
step: 0.2,
};
let mut child = MockPatternFactory::new();
child
.expect_create()
.with(eq(config))
.once()
.returning(|_| Box::new(MockPattern::new()));
SliceFactory::new(Box::new(child), 4).create(&config);
}
#[test]
fn sample_starts_with_one() {
let mut child = MockPatternFactory::new();
child.expect_create().returning(|_| {
let mut sampler = MockPattern::new();
sampler.expect_sample().return_const(1.0);
Box::new(sampler)
});
let sampler = SliceFactory::new(Box::new(child), 4).create(&Config::default());
assert_eq!(1.0, sampler.sample(Vector::default()));
}
#[test]
fn sample_ends_with_zero() {
let mut child = MockPatternFactory::new();
child.expect_create().returning(|_| {
let mut sampler = MockPattern::new();
sampler.expect_sample().return_const(0.75);
Box::new(sampler)
});
let sampler = SliceFactory::new(Box::new(child), 4).create(&Config::default());
assert_eq!(0.0, sampler.sample(Vector::default()));
}
#[test]
fn sample_values_beyond_end_are_negative() {
let mut child = MockPatternFactory::new();
child.expect_create().returning(|_| {
let mut sampler = MockPattern::new();
sampler.expect_sample().return_const(0.5);
Box::new(sampler)
});
let sampler = SliceFactory::new(Box::new(child), 4).create(&Config::default());
assert!(sampler.sample(Vector::default()) < 0.0);
}
#[test]
fn sample_pos_correct() {
let mut child = MockPatternFactory::new();
child.expect_create().once().returning(|_| {
let mut sampler = MockPattern::new();
sampler
.expect_sample()
.with(eq(Vector::new(3.0, 5.0)))
.once()
.return_const(0.0);
Box::new(sampler)
});
let sampler = SliceFactory::new(Box::new(child), 3).create(&Config::default());
sampler.sample(Vector::new(3.0, 5.0));
}
}