use qubit_function::{
ArcMutator,
BoxMutator,
Mutator,
MutatorOnce,
RcMutator,
};
struct TestMutator {
multiplier: i32,
}
impl TestMutator {
fn new(multiplier: i32) -> Self {
TestMutator { multiplier }
}
}
impl Mutator<i32> for TestMutator {
fn apply(&self, input: &mut i32) {
*input *= self.multiplier;
}
}
impl Clone for TestMutator {
fn clone(&self) -> Self {
TestMutator {
multiplier: self.multiplier,
}
}
}
#[cfg(test)]
mod test_unified_interface {
use super::{
ArcMutator,
BoxMutator,
Mutator,
RcMutator,
};
fn apply_mutator<C: Mutator<i32>>(mutator: &C, value: i32) -> i32 {
let mut val = value;
mutator.apply(&mut val);
val
}
#[test]
fn test_with_box_mutator() {
let mutator = BoxMutator::new(|x: &mut i32| *x *= 2);
assert_eq!(apply_mutator(&mutator, 5), 10);
}
#[test]
fn test_with_arc_mutator() {
let mutator = ArcMutator::new(|x: &mut i32| *x *= 2);
assert_eq!(apply_mutator(&mutator, 5), 10);
}
#[test]
fn test_with_rc_mutator() {
let mutator = RcMutator::new(|x: &mut i32| *x *= 2);
assert_eq!(apply_mutator(&mutator, 5), 10);
}
#[test]
fn test_with_closure() {
let closure = |x: &mut i32| *x *= 2;
assert_eq!(apply_mutator(&closure, 5), 10);
}
}