Skip to content

Commit de91e57

Browse files
committed
Add MappedMutexGuard tests.
1 parent 982c0cf commit de91e57

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

library/std/src/sync/mutex/tests.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::sync::atomic::{AtomicUsize, Ordering};
22
use crate::sync::mpsc::channel;
3-
use crate::sync::{Arc, Condvar, Mutex};
3+
use crate::sync::{Arc, Condvar, Mutex, MutexGuard, MappedMutexGuard};
44
use crate::thread;
55

66
struct Packet<T>(Arc<(Mutex<T>, Condvar)>);
@@ -188,6 +188,21 @@ fn test_mutex_arc_poison() {
188188
assert!(arc.is_poisoned());
189189
}
190190

191+
#[test]
192+
fn test_mutex_arc_poison_mapped() {
193+
let arc = Arc::new(Mutex::new(1));
194+
assert!(!arc.is_poisoned());
195+
let arc2 = arc.clone();
196+
let _ = thread::spawn(move || {
197+
let lock = arc2.lock().unwrap();
198+
let lock = MutexGuard::map(lock, |val| val);
199+
assert_eq!(*lock, 2); // deliberate assertion failure to poison the mutex
200+
})
201+
.join();
202+
assert!(arc.lock().is_err());
203+
assert!(arc.is_poisoned());
204+
}
205+
191206
#[test]
192207
fn test_mutex_arc_nested() {
193208
// Tests nested mutexes and access
@@ -236,3 +251,17 @@ fn test_mutex_unsized() {
236251
let comp: &[i32] = &[4, 2, 5];
237252
assert_eq!(&*mutex.lock().unwrap(), comp);
238253
}
254+
255+
256+
#[test]
257+
fn test_mapping_mapped_guard() {
258+
let arr = [0; 4];
259+
let mut lock = Mutex::new(arr);
260+
let guard = lock.lock().unwrap();
261+
let guard = MutexGuard::map(guard, |arr| &mut arr[..2]);
262+
let mut guard = MappedMutexGuard::map(guard, |slice| &mut slice[1..]);
263+
assert_eq!(guard.len(), 1);
264+
guard[0] = 42;
265+
drop(guard);
266+
assert_eq!(*lock.get_mut().unwrap(), [0, 42, 0, 0]);
267+
}

0 commit comments

Comments
 (0)