#![allow(unused_assignments)]
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{
Arc,
Mutex,
};
use qubit_function::{
ArcPredicate,
ArcStatefulFunction,
BoxPredicate,
BoxStatefulFunction,
FunctionOnce,
RcPredicate,
RcStatefulFunction,
StatefulFunction,
};
#[test]
fn test_stateful_function_trait_apply() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
assert_eq!(func.clone().apply(&10), 10);
assert_eq!(func.clone().apply(&10), 11);
assert_eq!(func.apply(&10), 12);
}
#[test]
fn test_stateful_function_trait_into_box() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
let mut boxed = StatefulFunction::into_box(func);
assert_eq!(boxed.apply(&10), 10);
assert_eq!(boxed.apply(&10), 11);
}
#[test]
fn test_stateful_function_trait_into_rc() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
let mut rc = func.into_rc();
assert_eq!(rc.apply(&10), 10);
assert_eq!(rc.apply(&10), 11);
}
#[test]
fn test_stateful_function_trait_into_arc() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
};
let mut arc = func.into_arc();
assert_eq!(arc.apply(&10), 10);
assert_eq!(arc.apply(&10), 11);
}
#[test]
fn test_stateful_function_trait_into_fn() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
let mut closure = StatefulFunction::into_fn(func);
assert_eq!(closure(&10), 10);
assert_eq!(closure(&10), 11);
}
#[test]
fn test_stateful_function_trait_to_box() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
let mut boxed = StatefulFunction::to_box(&func);
assert_eq!(boxed.apply(&10), 10);
assert_eq!(boxed.apply(&10), 11);
}
#[test]
fn test_stateful_function_trait_to_rc() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
let mut rc = func.to_rc();
assert_eq!(rc.apply(&10), 10);
assert_eq!(rc.apply(&10), 11);
}
#[test]
fn test_stateful_function_trait_to_arc() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
};
let mut arc = func.to_arc();
assert_eq!(arc.apply(&10), 10);
assert_eq!(arc.apply(&10), 11);
}
#[test]
fn test_stateful_function_trait_to_fn() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
let mut closure = StatefulFunction::to_fn(&func);
assert_eq!(closure(&10), 10);
assert_eq!(closure(&10), 11);
}
#[test]
fn test_stateful_function_trait_into_once() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
let once_func = func.into_once();
assert_eq!(once_func.apply(&10), 10);
}
#[test]
fn test_stateful_function_trait_to_once() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
};
let once_func = func.to_once();
assert_eq!(once_func.apply(&10), 10);
}
#[test]
fn test_box_stateful_function_new() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = BoxStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
assert_eq!(func.apply(&10), 10);
assert_eq!(func.apply(&10), 11);
assert_eq!(func.apply(&10), 12);
}
#[test]
fn test_box_stateful_function_identity() {
let mut identity = BoxStatefulFunction::<i32, i32>::identity();
assert_eq!(identity.apply(&42), 42);
assert_eq!(identity.apply(&0), 0);
assert_eq!(identity.apply(&-100), -100);
}
#[test]
fn test_box_stateful_function_constant() {
let mut constant = BoxStatefulFunction::constant("hello");
assert_eq!(constant.apply(&123), "hello");
assert_eq!(constant.apply(&456), "hello");
assert_eq!(constant.apply(&0), "hello");
}
#[test]
fn test_box_stateful_function_apply() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = BoxStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x * current
});
assert_eq!(func.apply(&10), 0);
assert_eq!(func.apply(&10), 10);
assert_eq!(func.apply(&10), 20);
}
#[test]
fn test_box_stateful_function_and_then() {
let mut counter1 = 0;
let func1 = BoxStatefulFunction::new(move |x: &i32| {
counter1 += 1;
x + counter1
});
let mut counter2 = 0;
let func2 = BoxStatefulFunction::new(move |x: &i32| {
counter2 += 1;
x * counter2
});
let mut composed = func1.and_then(func2);
assert_eq!(composed.apply(&10), 11); assert_eq!(composed.apply(&10), 24); }
#[test]
fn test_box_stateful_function_when_or_else() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = BoxStatefulFunction::new(move |x: &i32| {
*counter_clone.borrow_mut() += 1;
x * 2
})
.when(|x: &i32| *x > 10)
.or_else(|x: &i32| x + 1);
assert_eq!(func.apply(&15), 30); assert_eq!(func.apply(&5), 6); assert_eq!(*counter.borrow(), 1); }
#[test]
fn test_box_stateful_function_when_with_predicate() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = BoxStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x * current
})
.when(BoxPredicate::new(|x: &i32| *x > 0))
.or_else(|x: &i32| -(*x));
assert_eq!(func.apply(&10), 0); assert_eq!(func.apply(&-5), 5); }
#[test]
fn test_box_stateful_function_into_box() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = BoxStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut boxed = func.into_box();
assert_eq!(boxed.apply(&10), 10);
}
#[test]
fn test_box_stateful_function_into_rc() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = BoxStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut rc = func.into_rc();
assert_eq!(rc.apply(&10), 10);
}
#[test]
fn test_box_stateful_function_into_fn() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = BoxStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut closure = func.into_fn();
assert_eq!(closure(&10), 10);
}
#[test]
fn test_arc_stateful_function_new() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let mut func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
assert_eq!(func.apply(&10), 10);
assert_eq!(func.apply(&10), 11);
assert_eq!(func.apply(&10), 12);
}
#[test]
fn test_arc_stateful_function_identity() {
let mut identity = ArcStatefulFunction::<i32, i32>::identity();
assert_eq!(identity.apply(&42), 42);
assert_eq!(identity.apply(&0), 0);
assert_eq!(identity.apply(&-100), -100);
}
#[test]
fn test_arc_stateful_function_constant() {
let mut constant = ArcStatefulFunction::constant("hello");
assert_eq!(constant.apply(&123), "hello");
assert_eq!(constant.apply(&456), "hello");
}
#[test]
fn test_arc_stateful_function_apply() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let mut func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x * *current;
*current += 1;
result
});
assert_eq!(func.apply(&10), 0);
assert_eq!(func.apply(&10), 10);
}
#[test]
fn test_arc_stateful_function_clone() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut func_clone = func.clone();
assert_eq!(func_clone.apply(&10), 10);
assert_eq!(func_clone.apply(&10), 11);
}
#[test]
fn test_arc_stateful_function_and_then() {
let mut counter1 = 0;
let func1 = ArcStatefulFunction::new(move |x: &i32| {
counter1 += 1;
x + counter1
});
let mut counter2 = 0;
let func2 = ArcStatefulFunction::new(move |x: &i32| {
counter2 += 1;
x * counter2
});
let mut composed = func1.and_then(func2);
assert_eq!(composed.apply(&10), 11); assert_eq!(composed.apply(&10), 24); }
#[test]
fn test_arc_stateful_function_when_or_else() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let mut func = ArcStatefulFunction::new(move |x: &i32| {
*counter_clone.lock().unwrap() += 1;
x * 2
})
.when(|x: &i32| *x > 10)
.or_else(|x: &i32| x + 1);
assert_eq!(func.apply(&15), 30); assert_eq!(func.apply(&5), 6); assert_eq!(*counter.lock().unwrap(), 1); }
#[test]
fn test_arc_stateful_function_when_with_predicate() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let mut func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x * *current;
*current += 1;
result
})
.when(ArcPredicate::new(|x: &i32| *x > 0))
.or_else(|x: &i32| -(*x));
assert_eq!(func.apply(&10), 0); assert_eq!(func.apply(&-5), 5); }
#[test]
fn test_arc_stateful_function_into_box() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut boxed = func.into_box();
assert_eq!(boxed.apply(&10), 10);
}
#[test]
fn test_arc_stateful_function_into_rc() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut rc = func.into_rc();
assert_eq!(rc.apply(&10), 10);
}
#[test]
fn test_arc_stateful_function_into_arc() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut arc = func.into_arc();
assert_eq!(arc.apply(&10), 10);
}
#[test]
fn test_arc_stateful_function_into_fn() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut closure = func.into_fn();
assert_eq!(closure(&10), 10);
}
#[test]
fn test_arc_stateful_function_to_box() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut boxed = func.to_box();
assert_eq!(boxed.apply(&10), 10);
assert_eq!(func.clone().apply(&10), 11);
}
#[test]
fn test_arc_stateful_function_to_rc() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut rc = func.to_rc();
assert_eq!(rc.apply(&10), 10);
assert_eq!(func.clone().apply(&10), 11);
}
#[test]
fn test_arc_stateful_function_to_arc() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut arc = func.to_arc();
assert_eq!(arc.apply(&10), 10);
assert_eq!(func.clone().apply(&10), 11);
}
#[test]
fn test_arc_stateful_function_to_fn() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut closure = func.to_fn();
assert_eq!(closure(&10), 10);
assert_eq!(func.clone().apply(&10), 11);
}
#[test]
fn test_arc_stateful_function_thread_safety() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let func = ArcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.lock().unwrap();
let result = x + *current;
*current += 1;
result
});
let mut func_clone = func.clone();
let handle = std::thread::spawn(move || func_clone.apply(&10));
assert_eq!(handle.join().unwrap(), 10);
}
#[test]
fn test_rc_stateful_function_new() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
assert_eq!(func.apply(&10), 10);
assert_eq!(func.apply(&10), 11);
assert_eq!(func.apply(&10), 12);
}
#[test]
fn test_rc_stateful_function_identity() {
let mut identity = RcStatefulFunction::<i32, i32>::identity();
assert_eq!(identity.apply(&42), 42);
assert_eq!(identity.apply(&0), 0);
assert_eq!(identity.apply(&-100), -100);
}
#[test]
fn test_rc_stateful_function_constant() {
let mut constant = RcStatefulFunction::constant("hello");
assert_eq!(constant.apply(&123), "hello");
assert_eq!(constant.apply(&456), "hello");
}
#[test]
fn test_rc_stateful_function_apply() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x * current
});
assert_eq!(func.apply(&10), 0);
assert_eq!(func.apply(&10), 10);
}
#[test]
fn test_rc_stateful_function_clone() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut func_clone = func.clone();
assert_eq!(func_clone.apply(&10), 10);
assert_eq!(func_clone.apply(&10), 11);
}
#[test]
fn test_rc_stateful_function_and_then() {
let mut counter1 = 0;
let func1 = RcStatefulFunction::new(move |x: &i32| {
counter1 += 1;
x + counter1
});
let mut counter2 = 0;
let func2 = RcStatefulFunction::new(move |x: &i32| {
counter2 += 1;
x * counter2
});
let mut composed = func1.and_then(func2);
assert_eq!(composed.apply(&10), 11); assert_eq!(composed.apply(&10), 24); }
#[test]
fn test_rc_stateful_function_when_or_else() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = RcStatefulFunction::new(move |x: &i32| {
*counter_clone.borrow_mut() += 1;
x * 2
})
.when(|x: &i32| *x > 10)
.or_else(|x: &i32| x + 1);
assert_eq!(func.apply(&15), 30); assert_eq!(func.apply(&5), 6); assert_eq!(*counter.borrow(), 1); }
#[test]
fn test_rc_stateful_function_when_with_predicate() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = RcStatefulFunction::new(move |x: &i32| {
let mut current = counter_clone.borrow_mut();
let result = x * *current;
*current += 1;
result
})
.when(RcPredicate::new(|x: &i32| *x > 0))
.or_else(|x: &i32| -(*x));
assert_eq!(func.apply(&10), 0); assert_eq!(func.apply(&-5), 5); }
#[test]
fn test_rc_stateful_function_into_box() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut boxed = func.into_box();
assert_eq!(boxed.apply(&10), 10);
}
#[test]
fn test_rc_stateful_function_into_rc() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut rc = func.into_rc();
assert_eq!(rc.apply(&10), 10);
}
#[test]
fn test_rc_stateful_function_into_fn() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut closure = func.into_fn();
assert_eq!(closure(&10), 10);
}
#[test]
fn test_rc_stateful_function_to_box() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut boxed = func.to_box();
assert_eq!(boxed.apply(&10), 10);
assert_eq!(func.apply(&5), 6); }
#[test]
fn test_rc_stateful_function_to_rc() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
let mut rc = func.to_rc();
assert_eq!(rc.apply(&10), 10);
assert_eq!(func.apply(&5), 6); }
#[test]
fn test_rc_stateful_function_to_fn() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = RcStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
{
let mut closure = func.to_fn();
assert_eq!(closure(&10), 10);
}
assert_eq!(func.apply(&5), 6); }
#[test]
fn test_stateful_function_with_zero() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = BoxStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x + current
});
assert_eq!(func.apply(&0), 0);
assert_eq!(func.apply(&0), 1);
}
#[test]
fn test_stateful_function_with_negative() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let mut func = BoxStatefulFunction::new(move |x: &i32| {
let current = *counter_clone.borrow();
*counter_clone.borrow_mut() += 1;
x * current
});
assert_eq!(func.apply(&-10), 0);
assert_eq!(func.apply(&-10), -10);
}
#[test]
fn test_stateful_function_accumulator() {
let mut sum = 0;
let mut func = BoxStatefulFunction::new(move |x: &i32| {
sum += *x;
sum
});
assert_eq!(func.apply(&1), 1);
assert_eq!(func.apply(&2), 3);
assert_eq!(func.apply(&3), 6);
assert_eq!(func.apply(&4), 10);
}
#[test]
fn test_stateful_function_with_string() {
let mut buffer = String::new();
let mut func = BoxStatefulFunction::new(move |s: &String| {
buffer.push_str(s);
buffer.clone()
});
assert_eq!(func.apply(&String::from("Hello")), "Hello");
assert_eq!(func.apply(&String::from(" ")), "Hello ");
assert_eq!(func.apply(&String::from("World")), "Hello World");
}
#[test]
fn test_stateful_function_with_vec() {
let mut history = Vec::new();
let mut func = BoxStatefulFunction::new(move |x: &i32| {
history.push(*x);
history.len()
});
assert_eq!(func.apply(&1), 1);
assert_eq!(func.apply(&2), 2);
assert_eq!(func.apply(&3), 3);
}
#[test]
fn test_stateful_function_counter() {
let mut count = 0;
let mut func = BoxStatefulFunction::new(move |_x: &i32| {
count += 1;
count
});
assert_eq!(func.apply(&0), 1);
assert_eq!(func.apply(&0), 2);
assert_eq!(func.apply(&0), 3);
}
#[test]
fn test_stateful_function_toggle() {
let mut toggle = false;
let mut func = BoxStatefulFunction::new(move |x: &i32| {
toggle = !toggle;
if toggle {
*x
} else {
-*x
}
});
assert_eq!(func.apply(&5), 5);
assert_eq!(func.apply(&5), -5);
assert_eq!(func.apply(&5), 5);
}
#[test]
fn test_fn_stateful_function_ops_and_then() {
use qubit_function::FnStatefulFunctionOps;
let mut counter1 = 0;
let func1 = move |x: &i32| {
counter1 += 1;
x + counter1
};
let mut counter2 = 0;
let func2 = move |x: &i32| {
counter2 += 1;
x * counter2
};
let mut composed = func1.and_then(func2);
assert_eq!(composed.apply(&10), 11);
}
#[test]
fn test_fn_stateful_function_ops_when() {
use qubit_function::FnStatefulFunctionOps;
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let func = move |x: &i32| {
*counter_clone.borrow_mut() += 1;
x * 2
};
let mut conditional = func.when(|x: &i32| *x > 0).or_else(|x: &i32| -(*x));
assert_eq!(conditional.apply(&5), 10); assert_eq!(conditional.apply(&-5), 5); assert_eq!(*counter.borrow(), 1); }
#[test]
fn test_stateful_function_with_multiple_state() {
let mut count = 0;
let mut sum = 0;
let mut func = BoxStatefulFunction::new(move |x: &i32| {
count += 1;
sum += *x;
(count, sum)
});
assert_eq!(func.apply(&10), (1, 10));
assert_eq!(func.apply(&20), (2, 30));
assert_eq!(func.apply(&30), (3, 60));
}
#[test]
fn test_stateful_function_with_option_state() {
let mut last_value: Option<i32> = None;
let mut func = BoxStatefulFunction::new(move |x: &i32| {
let result = last_value.unwrap_or(0) + *x;
last_value = Some(*x);
result
});
assert_eq!(func.apply(&10), 10);
assert_eq!(func.apply(&20), 30);
assert_eq!(func.apply(&30), 50);
}
#[derive(Clone)]
struct CustomStatefulFunction {
multiplier: i32,
}
unsafe impl Send for CustomStatefulFunction {}
unsafe impl Sync for CustomStatefulFunction {}
impl StatefulFunction<i32, i32> for CustomStatefulFunction {
fn apply(&mut self, input: &i32) -> i32 {
self.multiplier += 1;
input * self.multiplier
}
}
#[test]
fn test_custom_stateful_function_into_box() {
let custom = CustomStatefulFunction { multiplier: 0 };
let mut boxed = custom.into_box();
assert_eq!(boxed.apply(&10), 10); assert_eq!(boxed.apply(&10), 20); assert_eq!(boxed.apply(&10), 30); }
#[test]
fn test_custom_stateful_function_into_rc() {
let custom = CustomStatefulFunction { multiplier: 0 };
let mut rc = custom.into_rc();
assert_eq!(rc.apply(&10), 10); assert_eq!(rc.apply(&10), 20); assert_eq!(rc.apply(&10), 30); }
#[test]
fn test_custom_stateful_function_into_arc() {
let custom = CustomStatefulFunction { multiplier: 0 };
let mut arc = custom.into_arc();
assert_eq!(arc.apply(&10), 10); assert_eq!(arc.apply(&10), 20); assert_eq!(arc.apply(&10), 30); }
#[test]
fn test_custom_stateful_function_into_fn() {
let custom = CustomStatefulFunction { multiplier: 0 };
let mut func = custom.into_fn();
assert_eq!(func(&10), 10); assert_eq!(func(&10), 20); assert_eq!(func(&10), 30); }
#[test]
fn test_custom_stateful_function_to_box() {
let custom = CustomStatefulFunction { multiplier: 0 };
let mut boxed = custom.to_box();
assert_eq!(boxed.apply(&10), 10); assert_eq!(boxed.apply(&10), 20); let mut custom_clone = custom.clone();
assert_eq!(custom_clone.apply(&10), 10); }
#[test]
fn test_custom_stateful_function_to_rc() {
let custom = CustomStatefulFunction { multiplier: 0 };
let mut rc = custom.to_rc();
assert_eq!(rc.apply(&10), 10); assert_eq!(rc.apply(&10), 20); let mut custom_clone = custom.clone();
assert_eq!(custom_clone.apply(&10), 10); }
#[test]
fn test_custom_stateful_function_to_arc() {
let custom = CustomStatefulFunction { multiplier: 0 };
let mut arc = custom.to_arc();
assert_eq!(arc.apply(&10), 10); assert_eq!(arc.apply(&10), 20); let mut custom_clone = custom.clone();
assert_eq!(custom_clone.apply(&10), 10); }
#[test]
fn test_custom_stateful_function_to_fn() {
let custom = CustomStatefulFunction { multiplier: 0 };
let mut func = custom.to_fn();
assert_eq!(func(&10), 10); assert_eq!(func(&10), 20); let mut custom_clone = custom.clone();
assert_eq!(custom_clone.apply(&10), 10); }
#[test]
fn test_arc_conditional_stateful_function_clone() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
let conditional = ArcStatefulFunction::new(move |x: &i32| {
*counter_clone.lock().unwrap() += 1;
x * 2
})
.when(|x: &i32| *x > 10);
let clone1 = conditional.clone();
let clone2 = conditional.clone();
let mut func = conditional.or_else(|x: &i32| x + 1);
let mut func_clone1 = clone1.or_else(|x: &i32| x + 1);
let mut func_clone2 = clone2.or_else(|x: &i32| x + 1);
assert_eq!(func.apply(&15), 30); assert_eq!(*counter.lock().unwrap(), 1);
assert_eq!(func_clone1.apply(&20), 40); assert_eq!(*counter.lock().unwrap(), 2);
assert_eq!(func_clone2.apply(&5), 6); assert_eq!(*counter.lock().unwrap(), 2);
assert_eq!(func_clone2.apply(&12), 24); assert_eq!(*counter.lock().unwrap(), 3);
}
#[test]
fn test_rc_conditional_stateful_function_clone() {
let counter = Rc::new(RefCell::new(0));
let counter_clone = counter.clone();
let conditional = RcStatefulFunction::new(move |x: &i32| {
*counter_clone.borrow_mut() += 1;
x * 2
})
.when(|x: &i32| *x > 10);
let clone1 = conditional.clone();
let clone2 = conditional.clone();
let mut func = conditional.or_else(|x: &i32| x + 1);
let mut func_clone1 = clone1.or_else(|x: &i32| x + 1);
let mut func_clone2 = clone2.or_else(|x: &i32| x + 1);
assert_eq!(func.apply(&15), 30); assert_eq!(*counter.borrow(), 1);
assert_eq!(func_clone1.apply(&20), 40); assert_eq!(*counter.borrow(), 2);
assert_eq!(func_clone2.apply(&5), 6); assert_eq!(*counter.borrow(), 2);
assert_eq!(func_clone2.apply(&12), 24); assert_eq!(*counter.borrow(), 3);
}
#[allow(unused_variables)]
#[test]
fn test_box_stateful_function_debug_display() {
let mut double = BoxStatefulFunction::new(move |x: &i32| x * 2);
let _result1 = double.apply(&5);
let debug_str = format!("{:?}", double);
assert!(debug_str.contains("BoxStatefulFunction"));
assert!(debug_str.contains("name"));
assert!(debug_str.contains("function"));
let display_str = format!("{}", double);
assert_eq!(display_str, "BoxStatefulFunction");
let mut named_double = BoxStatefulFunction::new_with_name("stateful_double", |x: &i32| x * 2);
let _result2 = named_double.apply(&3);
let named_debug_str = format!("{:?}", named_double);
assert!(named_debug_str.contains("BoxStatefulFunction"));
assert!(named_debug_str.contains("name"));
assert!(named_debug_str.contains("function"));
let named_display_str = format!("{}", named_double);
assert_eq!(named_display_str, "BoxStatefulFunction(stateful_double)");
}
#[allow(unused_variables)]
#[test]
fn test_rc_stateful_function_debug_display() {
let mut double = RcStatefulFunction::new(move |x: &i32| x * 2);
let _result1 = double.apply(&5);
let debug_str = format!("{:?}", double);
assert!(debug_str.contains("RcStatefulFunction"));
assert!(debug_str.contains("name"));
assert!(debug_str.contains("function"));
let display_str = format!("{}", double);
assert_eq!(display_str, "RcStatefulFunction");
let mut named_double = RcStatefulFunction::new_with_name("rc_stateful_double", |x: &i32| x * 2);
let _result2 = named_double.apply(&3);
let named_debug_str = format!("{:?}", named_double);
assert!(named_debug_str.contains("RcStatefulFunction"));
assert!(named_debug_str.contains("name"));
assert!(named_debug_str.contains("function"));
let named_display_str = format!("{}", named_double);
assert_eq!(named_display_str, "RcStatefulFunction(rc_stateful_double)");
}
#[allow(unused_variables)]
#[test]
fn test_arc_stateful_function_debug_display() {
let mut double = ArcStatefulFunction::new(move |x: &i32| x * 2);
let _result1 = double.apply(&5);
let debug_str = format!("{:?}", double);
assert!(debug_str.contains("ArcStatefulFunction"));
assert!(debug_str.contains("name"));
assert!(debug_str.contains("function"));
let display_str = format!("{}", double);
assert_eq!(display_str, "ArcStatefulFunction");
let mut counter2 = 0;
let mut named_double =
ArcStatefulFunction::new_with_name("arc_stateful_double", move |x: &i32| {
counter2 += 1;
x * 2
});
let _result2 = named_double.apply(&3);
let named_debug_str = format!("{:?}", named_double);
assert!(named_debug_str.contains("ArcStatefulFunction"));
assert!(named_debug_str.contains("name"));
assert!(named_debug_str.contains("function"));
let named_display_str = format!("{}", named_double);
assert_eq!(
named_display_str,
"ArcStatefulFunction(arc_stateful_double)"
);
}
#[allow(unused_variables)]
#[test]
fn test_box_stateful_function_name_methods() {
let mut double = BoxStatefulFunction::new_with_name("box_stateful_func", move |x: &i32| x * 2);
assert_eq!(double.name(), Some("box_stateful_func"));
double.set_name("modified_box_stateful");
assert_eq!(double.name(), Some("modified_box_stateful"));
assert_eq!(double.apply(&5), 10);
}
#[allow(unused_variables)]
#[test]
fn test_rc_stateful_function_name_methods() {
let mut double = RcStatefulFunction::new_with_name("rc_stateful_func", move |x: &i32| x * 2);
assert_eq!(double.name(), Some("rc_stateful_func"));
double.set_name("modified_rc_stateful");
assert_eq!(double.name(), Some("modified_rc_stateful"));
assert_eq!(double.apply(&5), 10);
let mut cloned = double.clone();
assert_eq!(cloned.name(), Some("modified_rc_stateful"));
assert_eq!(cloned.apply(&3), 6);
}
#[allow(unused_variables)]
#[test]
fn test_arc_stateful_function_name_methods() {
let mut double = ArcStatefulFunction::new_with_name("arc_stateful_func", move |x: &i32| x * 2);
assert_eq!(double.name(), Some("arc_stateful_func"));
double.set_name("modified_arc_stateful");
assert_eq!(double.name(), Some("modified_arc_stateful"));
assert_eq!(double.apply(&5), 10);
let mut cloned = double.clone();
assert_eq!(cloned.name(), Some("modified_arc_stateful"));
assert_eq!(cloned.apply(&3), 6);
}
#[allow(unused_variables)]
#[test]
fn test_box_conditional_stateful_function_debug_display() {
let mut double = BoxStatefulFunction::new(move |x: &i32| x * 2);
let _result1 = double.apply(&5);
let conditional = double.when(|x: &i32| *x > 0);
let debug_str = format!("{:?}", conditional);
assert!(debug_str.contains("BoxConditionalStatefulFunction"));
assert!(debug_str.contains("name"));
assert!(debug_str.contains("function"));
assert!(debug_str.contains("predicate"));
let display_str = format!("{}", conditional);
assert!(display_str.starts_with("BoxConditionalStatefulFunction("));
assert!(display_str.contains("BoxStatefulFunction"));
assert!(display_str.contains("BoxPredicate"));
assert!(display_str.ends_with(")"));
let mut counter2 = 0;
let mut named_double = BoxStatefulFunction::new_with_name("stateful_double", move |x: &i32| {
counter2 += 1;
x * 2
});
let _result2 = named_double.apply(&3);
let named_conditional = named_double.when(|x: &i32| *x % 2 == 0);
let named_debug_str = format!("{:?}", named_conditional);
assert!(named_debug_str.contains("BoxConditionalStatefulFunction"));
assert!(named_debug_str.contains("name"));
assert!(named_debug_str.contains("function"));
assert!(named_debug_str.contains("predicate"));
let named_display_str = format!("{}", named_conditional);
assert!(named_display_str.starts_with("BoxConditionalStatefulFunction("));
assert!(named_display_str.contains("BoxStatefulFunction(stateful_double)"));
assert!(named_display_str.contains("BoxPredicate"));
assert!(named_display_str.ends_with(")"));
}
#[allow(unused_variables)]
#[test]
fn test_rc_conditional_stateful_function_debug_display() {
let mut double = RcStatefulFunction::new(move |x: &i32| x * 2);
let _result1 = double.apply(&5);
let conditional = double.when(|x: &i32| *x > 0);
let debug_str = format!("{:?}", conditional);
assert!(debug_str.contains("RcConditionalStatefulFunction"));
assert!(debug_str.contains("name"));
assert!(debug_str.contains("function"));
assert!(debug_str.contains("predicate"));
let display_str = format!("{}", conditional);
assert!(display_str.starts_with("RcConditionalStatefulFunction("));
assert!(display_str.contains("RcStatefulFunction"));
assert!(display_str.contains("RcPredicate"));
assert!(display_str.ends_with(")"));
let mut counter2 = 0;
let mut named_double =
RcStatefulFunction::new_with_name("rc_stateful_double", move |x: &i32| {
counter2 += 1;
x * 2
});
let _result2 = named_double.apply(&3);
let named_conditional = named_double.when(|x: &i32| *x % 2 == 0);
let named_debug_str = format!("{:?}", named_conditional);
assert!(named_debug_str.contains("RcConditionalStatefulFunction"));
assert!(named_debug_str.contains("name"));
assert!(named_debug_str.contains("function"));
assert!(named_debug_str.contains("predicate"));
let named_display_str = format!("{}", named_conditional);
assert!(named_display_str.starts_with("RcConditionalStatefulFunction("));
assert!(named_display_str.contains("RcStatefulFunction(rc_stateful_double)"));
assert!(named_display_str.contains("RcPredicate"));
assert!(named_display_str.ends_with(")"));
}
#[allow(unused_variables)]
#[test]
fn test_arc_conditional_stateful_function_debug_display() {
let mut double = ArcStatefulFunction::new(move |x: &i32| x * 2);
let _result1 = double.apply(&5);
let conditional = double.when(|x: &i32| *x > 0);
let debug_str = format!("{:?}", conditional);
assert!(debug_str.contains("ArcConditionalStatefulFunction"));
assert!(debug_str.contains("name"));
assert!(debug_str.contains("function"));
assert!(debug_str.contains("predicate"));
let display_str = format!("{}", conditional);
assert!(display_str.starts_with("ArcConditionalStatefulFunction("));
assert!(display_str.contains("ArcStatefulFunction"));
assert!(display_str.contains("ArcPredicate"));
assert!(display_str.ends_with(")"));
let mut counter2 = 0;
let mut named_double =
ArcStatefulFunction::new_with_name("arc_stateful_double", move |x: &i32| {
counter2 += 1;
x * 2
});
let _result2 = named_double.apply(&3);
let named_conditional = named_double.when(|x: &i32| *x % 2 == 0);
let named_debug_str = format!("{:?}", named_conditional);
assert!(named_debug_str.contains("ArcConditionalStatefulFunction"));
assert!(named_debug_str.contains("name"));
assert!(named_debug_str.contains("function"));
assert!(named_debug_str.contains("predicate"));
let named_display_str = format!("{}", named_conditional);
assert!(named_display_str.starts_with("ArcConditionalStatefulFunction("));
assert!(named_display_str.contains("ArcStatefulFunction(arc_stateful_double)"));
assert!(named_display_str.contains("ArcPredicate"));
assert!(named_display_str.ends_with(")"));
}
#[cfg(test)]
mod test_stateful_function_trait_default_methods {
use super::*;
use qubit_function::FunctionOnce;
use std::sync::atomic::{
AtomicUsize,
Ordering,
};
#[test]
fn test_custom_stateful_function_into_once() {
let counter = Arc::new(AtomicUsize::new(0));
struct MyStatefulFunction {
counter: Arc<AtomicUsize>,
}
impl StatefulFunction<i32, i32> for MyStatefulFunction {
fn apply(&mut self, value: &i32) -> i32 {
self.counter.fetch_add(1, Ordering::SeqCst);
*value * 2
}
}
let my_func = MyStatefulFunction {
counter: counter.clone(),
};
let once_func = my_func.into_once();
let result = once_func.apply(&5);
assert_eq!(result, 10);
assert_eq!(counter.load(Ordering::SeqCst), 1);
}
#[test]
fn test_custom_stateful_function_to_once() {
let counter = Arc::new(AtomicUsize::new(0));
#[derive(Clone)]
struct MyStatefulFunction {
counter: Arc<AtomicUsize>,
}
impl StatefulFunction<i32, i32> for MyStatefulFunction {
fn apply(&mut self, value: &i32) -> i32 {
self.counter.fetch_add(1, Ordering::SeqCst);
*value * 2
}
}
let mut my_func = MyStatefulFunction {
counter: counter.clone(),
};
let once_func = my_func.to_once();
let result = once_func.apply(&5);
assert_eq!(result, 10);
assert_eq!(counter.load(Ordering::SeqCst), 1);
let result2 = my_func.apply(&3);
assert_eq!(result2, 6);
assert_eq!(counter.load(Ordering::SeqCst), 2);
}
}