#![expect(clippy::unwrap_used, reason = "Tests use panicking operations for brevity and clarity")]
use {
fp_library::{
brands::*,
functions::*,
types::*,
},
std::{
sync::{
Arc,
Mutex,
},
thread,
},
};
#[test]
fn test_arc_ptr_basic() {
let ptr = send_ref_counted_pointer_new::<ArcBrand, _>(42);
assert_eq!(*ptr, 42);
let clone = ptr.clone();
assert_eq!(*clone, 42);
}
#[test]
fn test_arc_fn_basic() {
let f = send_lift_fn_new::<ArcFnBrand, _, _>(|x: i32| x + 1);
assert_eq!(f(1), 2);
let clone = f.clone();
assert_eq!(clone(1), 2);
}
#[test]
fn test_arc_fn_thread_safety() {
let f = send_lift_fn_new::<ArcFnBrand, _, _>(|x: i32| x + 1);
let handle = thread::spawn(move || f(10));
assert_eq!(handle.join().unwrap(), 11);
}
#[test]
fn test_arc_memo_basic() {
let memo = ArcLazy::new(|| 42);
assert_eq!(*memo.evaluate(), 42);
}
#[test]
fn test_arc_memo_shared_memoization() {
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
let memo = ArcLazy::new(move || {
let mut guard = counter_clone.lock().unwrap();
*guard += 1;
42
});
let memo_clone = memo.clone();
assert_eq!(*counter.lock().unwrap(), 0);
assert_eq!(*memo.evaluate(), 42);
assert_eq!(*counter.lock().unwrap(), 1);
assert_eq!(*memo_clone.evaluate(), 42);
assert_eq!(*counter.lock().unwrap(), 1);
}
#[test]
fn test_arc_memo_thread_safety() {
let memo = ArcLazy::new(|| 42);
let memo_clone = memo.clone();
let handle = thread::spawn(move || *memo_clone.evaluate());
assert_eq!(handle.join().unwrap(), 42);
assert_eq!(*memo.evaluate(), 42);
}
#[test]
fn test_rc_memo_basic() {
let memo = RcLazy::new(|| 42);
assert_eq!(*memo.evaluate(), 42);
}
#[test]
fn test_rc_memo_shared_memoization() {
use std::{
cell::RefCell,
rc::Rc,
};
let counter = Rc::new(RefCell::new(0));
let counter_clone = counter.clone();
let memo = RcLazy::new(move || {
*counter_clone.borrow_mut() += 1;
42
});
let memo_clone = memo.clone();
assert_eq!(*counter.borrow(), 0);
assert_eq!(*memo.evaluate(), 42);
assert_eq!(*counter.borrow(), 1);
assert_eq!(*memo_clone.evaluate(), 42);
assert_eq!(*counter.borrow(), 1);
}