More unit tests

This commit is contained in:
Rico Riedel 2022-08-05 19:35:57 +02:00
parent d965ba413a
commit 402077e109
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18
5 changed files with 38 additions and 6 deletions

View File

@ -40,7 +40,7 @@ mod test {
size: Vector::new(10.0, 20.0), size: Vector::new(10.0, 20.0),
step: 0.0, step: 0.0,
}; };
let pattern = Circle::new(&config); let pattern = CircleFactory::default().create(&config);
assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 0.0)), epsilon = 0.1); assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 0.0)), epsilon = 0.1);
assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(5.0, 10.0)), epsilon = 0.1); assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(5.0, 10.0)), epsilon = 0.1);

View File

@ -38,7 +38,7 @@ mod test {
size: Vector::new(20.0, 0.0), size: Vector::new(20.0, 0.0),
step: 0.0, step: 0.0,
}; };
let pattern = Line::new(&config); let pattern = LineFactory::default().create(&config);
assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(0.0, 4.0)), epsilon = 0.1); assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(0.0, 4.0)), epsilon = 0.1);
assert_abs_diff_eq!(0.4, pattern.sample(Vector::new(8.0, 8.0)), epsilon = 0.1); assert_abs_diff_eq!(0.4, pattern.sample(Vector::new(8.0, 8.0)), epsilon = 0.1);

View File

@ -73,6 +73,7 @@ impl Sampler for SamplerImpl {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use approx::*;
use mockall::predicate::eq; use mockall::predicate::eq;
#[test] #[test]
@ -86,7 +87,7 @@ mod test {
let sampler = SamplerImpl::new(Box::new(char), Box::new(color)); let sampler = SamplerImpl::new(Box::new(char), Box::new(color));
assert_eq!(2.5, sampler.char(Vector::new(2.0, 5.0))); assert_abs_diff_eq!(2.5, sampler.char(Vector::new(2.0, 5.0)));
} }
#[test] #[test]
@ -101,6 +102,37 @@ mod test {
let sampler = SamplerImpl::new(Box::new(char), Box::new(color)); let sampler = SamplerImpl::new(Box::new(char), Box::new(color));
assert_eq!(3.2, sampler.color(Vector::new(4.0, 2.0))); assert_abs_diff_eq!(3.2, sampler.color(Vector::new(4.0, 2.0)));
}
#[test]
fn factory() {
let mut char = MockPatternFactory::new();
let mut color = MockPatternFactory::new();
let config = Config {
size: Vector::new(2.0, 3.0),
step: 0.6,
};
char.expect_create().with(eq(config)).once().returning(|_| {
let mut sampler = MockPattern::new();
sampler.expect_sample().return_const(3.0);
Box::new(sampler)
});
color
.expect_create()
.with(eq(config))
.once()
.returning(|_| {
let mut sampler = MockPattern::new();
sampler.expect_sample().return_const(5.0);
Box::new(sampler)
});
let factory = SamplerFactoryImpl::new(Box::new(char), Box::new(color));
let sampler = factory.create(&config);
assert_abs_diff_eq!(3.0, sampler.char(Vector::default()));
assert_abs_diff_eq!(5.0, sampler.color(Vector::default()));
} }
} }

View File

@ -40,7 +40,7 @@ mod test {
size: Vector::new(10.0, 5.0), size: Vector::new(10.0, 5.0),
step: 0.0, step: 0.0,
}; };
let pattern = Rhombus::new(&config); let pattern = RhombusFactory::default().create(&config);
assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 0.0)), epsilon = 0.1); assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 0.0)), epsilon = 0.1);
assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(10.0, 5.0)), epsilon = 0.1); assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(10.0, 5.0)), epsilon = 0.1);

View File

@ -39,7 +39,7 @@ mod test {
size: Vector::new(10.0, 20.0), size: Vector::new(10.0, 20.0),
step: 0.0, step: 0.0,
}; };
let pattern = Wheel::new(&config); let pattern = WheelFactory::default().create(&config);
assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(0.0, 9.0)), epsilon = 0.1); assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(0.0, 9.0)), epsilon = 0.1);
assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 10.0)), epsilon = 0.1); assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 10.0)), epsilon = 0.1);