mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-22 16:06:38 +00:00
Add slice unit tests
This commit is contained in:
parent
726f482687
commit
dd5b215aaf
@ -1,21 +1,26 @@
|
||||
use crate::pattern::*;
|
||||
use crate::Vector;
|
||||
|
||||
#[derive(derive_more::Constructor)]
|
||||
pub struct SliceFactory {
|
||||
child: Box<dyn PatternFactory>,
|
||||
width: f32,
|
||||
rest: f32,
|
||||
slices: u8,
|
||||
}
|
||||
|
||||
#[derive(derive_more::Constructor)]
|
||||
pub struct Slice {
|
||||
child: Box<dyn Pattern>,
|
||||
width: f32,
|
||||
rest: f32,
|
||||
}
|
||||
|
||||
impl SliceFactory {
|
||||
pub fn new(child: Box<dyn PatternFactory>, slices: u8) -> Self {
|
||||
impl PatternFactory for SliceFactory {
|
||||
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 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 {
|
||||
fn sample(&self, pos: Vector) -> f32 {
|
||||
(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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user