Skip to content

Commit d29ff8a

Browse files
committed
Add tests for get_disjoint_mut
1 parent 9135f88 commit d29ff8a

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

tests/slab.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
use slab::*;
44

5-
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
5+
use std::{
6+
iter::FromIterator,
7+
panic::{catch_unwind, resume_unwind, AssertUnwindSafe},
8+
};
69

710
#[test]
811
fn insert_get_remove_one() {
@@ -730,3 +733,37 @@ fn clone_from() {
730733
assert_eq!(iter2.next(), None);
731734
assert!(slab2.capacity() >= 10);
732735
}
736+
737+
#[test]
738+
fn get_disjoint_mut() {
739+
let mut slab = Slab::from_iter((0..5).enumerate());
740+
slab.remove(1);
741+
slab.remove(3);
742+
743+
assert_eq!(slab.get_disjoint_mut([]), Ok([]));
744+
745+
assert_eq!(
746+
slab.get_disjoint_mut([4, 2, 0]).unwrap().map(|x| *x),
747+
[4, 2, 0]
748+
);
749+
750+
assert_eq!(
751+
slab.get_disjoint_mut([42, 2, 1, 2]),
752+
Err(GetDisjointMutError::OverlappingIndices)
753+
);
754+
755+
assert_eq!(
756+
slab.get_disjoint_mut([1, 5]),
757+
Err(GetDisjointMutError::IndexVacant)
758+
);
759+
760+
assert_eq!(
761+
slab.get_disjoint_mut([5, 1]),
762+
Err(GetDisjointMutError::IndexOutOfBounds)
763+
);
764+
765+
let [a, b] = slab.get_disjoint_mut([0, 4]).unwrap();
766+
(*a, *b) = (*b, *a);
767+
assert_eq!(slab[0], 4);
768+
assert_eq!(slab[4], 0);
769+
}

0 commit comments

Comments
 (0)