diff --git a/tests/integration.rs b/tests/integration.rs index bf3d7e5..6b0eaee 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -231,6 +231,25 @@ mod lock_set { let m: Mutex = Mutex::new(0); let _set = LockSet::new((&m, &m)); } + + #[test] + fn three_tuple() { + let mut a: Mutex = Mutex::new(1); + let mut b: Mutex = Mutex::new(2); + let mut c: Mutex = Mutex::new(3); + + let set = LockSet::new((&a, &b, &c)); + lock_scope(|key| { + let ((mut ga, mut gb, mut gc), _key) = key.lock(&set); + *ga += 10; + *gb += 10; + *gc += 10; + }); + + assert_eq!(*a.get_mut(), 11); + assert_eq!(*b.get_mut(), 12); + assert_eq!(*c.get_mut(), 13); + } } mod cross_level { @@ -527,6 +546,13 @@ mod locksmith { let _smith = surelock::locksmith::Locksmith::create_unlimited(); let _smith2 = surelock::locksmith::Locksmith::create_unlimited(); } + + #[test] + #[serial] + fn remaining_none_when_unlimited() { + let smith = surelock::locksmith::Locksmith::unlimited().unwrap(); + assert_eq!(smith.remaining(), None); + } } mod new_higher { @@ -914,6 +940,116 @@ mod smart_pointers { }); assert_eq!(sum, 6); } + + #[test] + fn rc_tuple_lockset() { + let a: Rc> = Rc::new(Mutex::new(10)); + let b: Rc> = Rc::new(Mutex::new(20)); + + let set = LockSet::new((&a, &b)); + lock_scope(|key| { + let ((ga, gb), _key) = key.lock(&set); + assert_eq!(*ga + *gb, 30); + }); + } + + #[test] + fn box_tuple_lockset() { + let a: Box> = Box::new(Mutex::new(10)); + let b: Box> = Box::new(Mutex::new(20)); + + let set = LockSet::new((&a, &b)); + lock_scope(|key| { + let ((ga, gb), _key) = key.lock(&set); + assert_eq!(*ga + *gb, 30); + }); + } + + #[test] + fn mixed_rc_bare_tuple_lock_with() { + let rc: Rc> = Rc::new(Mutex::new(10)); + let bare: Mutex = Mutex::new(20); + + let sum = lock_scope(|key| { + let (s, _key) = key.lock_with(&(&rc, &bare), |(ga, gb)| *ga + *gb); + s + }); + assert_eq!(sum, 30); + } + + #[test] + fn mixed_arc_rc_tuple_lock_with() { + let arc: Arc> = Arc::new(Mutex::new(10)); + let rc: Rc> = Rc::new(Mutex::new(20)); + + let sum = lock_scope(|key| { + let (s, _key) = key.lock_with(&(&arc, &rc), |(ga, gb)| *ga + *gb); + s + }); + assert_eq!(sum, 30); + } + + #[test] + fn mixed_box_arc_tuple_lockset() { + let bx: Box> = Box::new(Mutex::new(10)); + let arc: Arc> = Arc::new(Mutex::new(20)); + + let set = LockSet::new((&bx, &arc)); + lock_scope(|key| { + let ((ga, gb), _key) = key.lock(&set); + assert_eq!(*ga + *gb, 30); + }); + } + + #[test] + fn arc_multi_level_lockset() { + let low: Arc> = Arc::new(Mutex::new(10)); + let high: Arc>> = Arc::new(Mutex::new(20)); + + let set = LockSet::new((&low, &high)); + lock_scope(|key| { + let ((gl, gh), _key) = key.lock(&set); + assert_eq!(*gl + *gh, 30); + }); + } + + #[test] + fn box_mutex_lock_with() { + let boxed: Box> = Box::new(Mutex::new(42)); + + let val = lock_scope(|key| { + let (v, _key) = key.lock_with(&boxed, |g| *g); + v + }); + + assert_eq!(val, 42); + } + + #[test] + fn new_higher_arc_arc_tuple() { + let a: Arc> = Arc::new(Mutex::new(1)); + let b: Arc> = Arc::new(Mutex::new(2)); + + let child = Mutex::new_higher(3u32, (&a, &b)); + + lock_scope(|key| { + let (v, _key) = key.lock_with(&child, |g| *g); + assert_eq!(v, 3); + }); + } + + #[test] + fn new_higher_rc_bare_tuple() { + let rc: Rc> = Rc::new(Mutex::new(1)); + let bare: Mutex = Mutex::new(2); + + let child = Mutex::new_higher(3u32, (&rc, &bare)); + + lock_scope(|key| { + let (v, _key) = key.lock_with(&child, |g| *g); + assert_eq!(v, 3); + }); + } } mod raw_mutex_try_lock {