use qubit_function::{
ArcFunction,
ArcPredicate,
BoxFunction,
BoxPredicate,
Function,
FunctionOnce,
Predicate,
RcFunction,
RcPredicate,
};
#[test]
fn test_function_trait_apply() {
let double = |x: &i32| x * 2;
assert_eq!(double.apply(&21), 42);
assert_eq!(double.apply(&0), 0);
assert_eq!(double.apply(&-10), -20);
}
#[test]
fn test_box_function_new_allows_non_static_t() {
fn run<'a>(value: &'a str) -> usize {
let func: BoxFunction<&'a str, usize> =
BoxFunction::new(|x: &&'a str| x.len());
func.apply(&value)
}
let text = String::from("hello");
assert_eq!(run(text.as_str()), 5);
}
#[test]
fn test_box_function_new_allows_non_static_r() {
fn run<'a>(value: &'a str) -> &'a str {
let func: BoxFunction<&'a str, &'a str> =
BoxFunction::new(|x: &&'a str| *x);
func.apply(&value)
}
let text = String::from("qubit");
assert_eq!(run(text.as_str()), "qubit");
}
#[test]
fn test_rc_function_new_allows_non_static_t() {
fn run<'a>(value: &'a str) -> usize {
let func: RcFunction<&'a str, usize> =
RcFunction::new(|x: &&'a str| x.len());
func.apply(&value)
}
let text = String::from("hello");
assert_eq!(run(text.as_str()), 5);
}
#[test]
fn test_arc_function_new_allows_non_static_t() {
fn run<'a>(value: &'a str) -> usize {
let func: ArcFunction<&'a str, usize> =
ArcFunction::new(|x: &&'a str| x.len() + 1);
func.apply(&value)
}
let text = String::from("hello");
assert_eq!(run(text.as_str()), 6);
}
#[test]
fn test_rc_function_new_allows_non_static_r() {
fn run<'a>(value: &'a str) -> &'a str {
let func: RcFunction<&'a str, &'a str> =
RcFunction::new(|x: &&'a str| *x);
func.apply(&value)
}
let text = String::from("qubit");
assert_eq!(run(text.as_str()), "qubit");
}
#[test]
fn test_box_function_new() {
let double = BoxFunction::new(|x: &i32| x * 2);
assert_eq!(double.apply(&21), 42);
assert_eq!(double.apply(&0), 0);
assert_eq!(double.apply(&-5), -10);
}
#[test]
fn test_box_function_identity() {
let identity = BoxFunction::<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_function_constant() {
let constant = BoxFunction::constant("hello");
assert_eq!(constant.apply(&123), "hello");
assert_eq!(constant.apply(&456), "hello");
assert_eq!(constant.apply(&0), "hello");
}
#[test]
fn test_box_function_apply() {
let add_one = BoxFunction::new(|x: &i32| x + 1);
assert_eq!(add_one.apply(&41), 42);
assert_eq!(add_one.apply(&0), 1);
assert_eq!(add_one.apply(&-1), 0);
}
#[test]
fn test_box_function_and_then() {
let double = BoxFunction::new(|x: &i32| x * 2);
let to_string = BoxFunction::new(|x: &i32| x.to_string());
let composed = double.and_then(to_string);
assert_eq!(composed.apply(&21), "42");
assert_eq!(composed.apply(&0), "0");
assert_eq!(composed.apply(&-5), "-10");
}
#[test]
fn test_box_function_and_then_with_closure() {
let double = BoxFunction::new(|x: &i32| x * 2);
let composed = double.and_then(|x: &i32| x + 10);
assert_eq!(composed.apply(&16), 42);
}
#[test]
fn test_box_function_when_or_else() {
let double = BoxFunction::new(|x: &i32| x * 2);
let identity = BoxFunction::<i32, i32>::identity();
let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
assert_eq!(conditional.apply(&5), 10); assert_eq!(conditional.apply(&-5), -5); assert_eq!(conditional.apply(&0), 0); }
#[test]
fn test_box_function_when_with_closure() {
let double = BoxFunction::new(|x: &i32| x * 2);
let conditional = double.when(|x: &i32| *x >= 10).or_else(|x: &i32| *x);
assert_eq!(conditional.apply(&15), 30); assert_eq!(conditional.apply(&5), 5); }
#[test]
fn test_box_function_when_with_predicate() {
let double = BoxFunction::new(|x: &i32| x * 2);
let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
let conditional = double.when(is_positive).or_else(|x: &i32| -(*x));
assert_eq!(conditional.apply(&5), 10); assert_eq!(conditional.apply(&-5), 5); }
#[test]
fn test_box_function_once_impl_apply() {
let double = BoxFunction::new(|x: &i32| x * 2);
let result = double.apply(&21);
assert_eq!(result, 42);
}
#[test]
fn test_arc_function_new() {
let double = ArcFunction::new(|x: &i32| x * 2);
assert_eq!(double.apply(&21), 42);
assert_eq!(double.apply(&0), 0);
assert_eq!(double.apply(&-5), -10);
}
#[test]
fn test_arc_function_identity() {
let identity = ArcFunction::<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_function_constant() {
let constant = ArcFunction::constant("hello");
assert_eq!(constant.apply(&123), "hello");
assert_eq!(constant.apply(&456), "hello");
}
#[test]
fn test_arc_function_once_impl_apply() {
let add_one = ArcFunction::new(|x: &i32| x + 1);
assert_eq!(add_one.apply(&41), 42);
assert_eq!(add_one.apply(&0), 1);
}
#[test]
fn test_arc_function_clone() {
let double = ArcFunction::new(|x: &i32| x * 2);
let cloned = double.clone();
assert_eq!(double.apply(&21), 42);
assert_eq!(cloned.apply(&21), 42);
}
#[test]
fn test_arc_function_and_then() {
let double = ArcFunction::new(|x: &i32| x * 2);
let to_string = ArcFunction::new(|x: &i32| x.to_string());
let composed = double.and_then(to_string);
assert_eq!(composed.apply(&21), "42");
assert_eq!(double.apply(&10), 20);
}
#[test]
fn test_arc_function_and_then_with_clone() {
let double = ArcFunction::new(|x: &i32| x * 2);
let to_string = ArcFunction::new(|x: &i32| x.to_string());
let composed = double.and_then(to_string.clone());
assert_eq!(composed.apply(&21), "42");
assert_eq!(to_string.apply(&5), "5");
}
#[test]
fn test_arc_function_when_or_else() {
let double = ArcFunction::new(|x: &i32| x * 2);
let identity = ArcFunction::<i32, i32>::identity();
let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
let conditional_clone = conditional.clone();
assert_eq!(conditional.apply(&5), 10);
assert_eq!(conditional_clone.apply(&-5), -5);
}
#[test]
fn test_arc_function_when_with_predicate() {
let double = ArcFunction::new(|x: &i32| x * 2);
let is_positive = ArcPredicate::new(|x: &i32| *x > 0);
let conditional = double
.when(is_positive.clone())
.or_else(ArcFunction::identity());
assert_eq!(conditional.apply(&5), 10);
assert!(is_positive.test(&3));
}
#[test]
fn test_arc_function_send_sync() {
let double = ArcFunction::new(|x: &i32| x * 2);
let double_clone = double.clone();
let handle = std::thread::spawn(move || double_clone.apply(&21));
assert_eq!(handle.join().expect("thread should not panic"), 42);
assert_eq!(double.apply(&10), 20);
}
#[test]
fn test_rc_function_new() {
let double = RcFunction::new(|x: &i32| x * 2);
assert_eq!(double.apply(&21), 42);
assert_eq!(double.apply(&0), 0);
assert_eq!(double.apply(&-5), -10);
}
#[test]
fn test_rc_function_identity() {
let identity = RcFunction::<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_function_constant() {
let constant = RcFunction::constant("hello");
assert_eq!(constant.apply(&123), "hello");
assert_eq!(constant.apply(&456), "hello");
}
#[test]
fn test_rc_function_once_impl_apply() {
let add_one = RcFunction::new(|x: &i32| x + 1);
assert_eq!(add_one.apply(&41), 42);
assert_eq!(add_one.apply(&0), 1);
}
#[test]
fn test_rc_function_clone() {
let double = RcFunction::new(|x: &i32| x * 2);
let cloned = double.clone();
assert_eq!(double.apply(&21), 42);
assert_eq!(cloned.apply(&21), 42);
}
#[test]
fn test_rc_function_and_then() {
let double = RcFunction::new(|x: &i32| x * 2);
let to_string = RcFunction::new(|x: &i32| x.to_string());
let composed = double.and_then(to_string);
assert_eq!(composed.apply(&21), "42");
assert_eq!(double.apply(&10), 20);
}
#[test]
fn test_rc_function_and_then_with_clone() {
let double = RcFunction::new(|x: &i32| x * 2);
let to_string = RcFunction::new(|x: &i32| x.to_string());
let composed = double.and_then(to_string.clone());
assert_eq!(composed.apply(&21), "42");
assert_eq!(to_string.apply(&5), "5");
}
#[test]
fn test_rc_function_when_or_else() {
let double = RcFunction::new(|x: &i32| x * 2);
let identity = RcFunction::<i32, i32>::identity();
let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
let conditional_clone = conditional.clone();
assert_eq!(conditional.apply(&5), 10);
assert_eq!(conditional_clone.apply(&-5), -5);
}
#[test]
fn test_rc_function_when_with_predicate() {
let double = RcFunction::new(|x: &i32| x * 2);
let is_positive = RcPredicate::new(|x: &i32| *x > 0);
let conditional =
double
.when(is_positive.clone())
.or_else(RcFunction::<i32, i32>::identity());
assert_eq!(conditional.apply(&5), 10);
assert!(is_positive.test(&3));
}
#[test]
fn test_function_with_zero() {
let double = BoxFunction::new(|x: &i32| x * 2);
assert_eq!(double.apply(&0), 0);
}
#[test]
fn test_function_with_negative() {
let double = BoxFunction::new(|x: &i32| x * 2);
assert_eq!(double.apply(&-42), -84);
}
#[test]
fn test_function_with_max_value() {
let identity = BoxFunction::<i32, i32>::identity();
assert_eq!(identity.apply(&i32::MAX), i32::MAX);
}
#[test]
fn test_function_with_min_value() {
let identity = BoxFunction::<i32, i32>::identity();
assert_eq!(identity.apply(&i32::MIN), i32::MIN);
}