use qubit_function::{
BoxConsumerOnce,
ConsumerOnce,
};
use std::sync::{
Arc,
Mutex,
};
#[cfg(test)]
mod closure_tests {
use super::{
Arc,
ConsumerOnce,
Mutex,
};
#[test]
fn test_closure_accept() {
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let closure = move |x: &i32| {
l.lock().expect("mutex should not be poisoned").push(*x * 2);
};
closure.accept(&5);
assert_eq!(
*log.lock().expect("mutex should not be poisoned"),
vec![10]
);
}
#[test]
fn test_closure_and_then() {
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let chained = qubit_function::BoxConsumerOnce::new(move |x: &i32| {
l1.lock()
.expect("mutex should not be poisoned")
.push(*x * 2);
})
.and_then(move |x: &i32| {
l2.lock()
.expect("mutex should not be poisoned")
.push(*x + 10);
});
chained.accept(&5);
assert_eq!(
*log.lock().expect("mutex should not be poisoned"),
vec![10, 15]
);
}
#[test]
fn test_closure_multi_step_chaining() {
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let l3 = log.clone();
let chained = qubit_function::BoxConsumerOnce::new(move |x: &i32| {
l1.lock()
.expect("mutex should not be poisoned")
.push(*x * 2);
})
.and_then(move |x: &i32| {
l2.lock()
.expect("mutex should not be poisoned")
.push(*x + 10);
})
.and_then(move |x: &i32| {
l3.lock()
.expect("mutex should not be poisoned")
.push(*x / 2);
});
chained.accept(&5);
assert_eq!(
*log.lock().expect("mutex should not be poisoned"),
vec![10, 15, 2]
);
}
}