Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Oct 26, 2023
1 parent 348b42f commit d93534f
Show file tree
Hide file tree
Showing 2 changed files with 360 additions and 0 deletions.
221 changes: 221 additions & 0 deletions src/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,224 @@ fn iter_default() {
assert_default::<ValuesMut<'static, K, V>>();
assert_default::<IntoValues<K, V>>();
}

#[test]
fn test_binary_search_by() {
// addaped from stds test for binary_search
let b: IndexMap<_, i32> = []
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(0));

let b: IndexMap<_, i32> = [4]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by(|_, x| x.cmp(&3)), Err(0));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Ok(0));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(1));

let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(4));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(4));

let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by(|_, x| x.cmp(&9)), Err(6));

let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(5));

let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(5));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0));

let b: IndexMap<_, i32> = [1, 3, 3, 3, 7]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&1)), Ok(0));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&2)), Err(1));
assert!(match b.binary_search_by(|_, x| x.cmp(&3)) {
Ok(1..=3) => true,
_ => false,
});
assert!(match b.binary_search_by(|_, x| x.cmp(&3)) {
Ok(1..=3) => true,
_ => false,
});
assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Err(4));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(4));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Err(4));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Ok(4));
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Err(5));
}

#[test]
fn test_binary_search_by_key() {
// addaped from stds test for binary_search
let b: IndexMap<_, i32> = []
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(0));

let b: IndexMap<_, i32> = [4]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by_key(&3, |_, &x| x), Err(0));
assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Ok(0));
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(1));

let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3));
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3));
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(4));
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(4));

let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by_key(&9, |_, &x| x), Err(6));

let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3));
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3));
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(5));

let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(5));
assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0));

let b: IndexMap<_, i32> = [1, 3, 3, 3, 7]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0));
assert_eq!(b.binary_search_by_key(&1, |_, &x| x), Ok(0));
assert_eq!(b.binary_search_by_key(&2, |_, &x| x), Err(1));
assert!(match b.binary_search_by_key(&3, |_, &x| x) {
Ok(1..=3) => true,
_ => false,
});
assert!(match b.binary_search_by_key(&3, |_, &x| x) {
Ok(1..=3) => true,
_ => false,
});
assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Err(4));
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(4));
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Err(4));
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Ok(4));
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Err(5));
}

#[test]
fn test_partition_point() {
// addaped from stds test for partition_point
let b: IndexMap<_, i32> = []
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.partition_point(|_, &x| x < 5), 0);

let b: IndexMap<_, i32> = [4]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.partition_point(|_, &x| x < 3), 0);
assert_eq!(b.partition_point(|_, &x| x < 4), 0);
assert_eq!(b.partition_point(|_, &x| x < 5), 1);

let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.partition_point(|_, &x| x < 5), 3);
assert_eq!(b.partition_point(|_, &x| x < 6), 3);
assert_eq!(b.partition_point(|_, &x| x < 7), 4);
assert_eq!(b.partition_point(|_, &x| x < 8), 4);

let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.partition_point(|_, &x| x < 9), 6);

let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.partition_point(|_, &x| x < 6), 3);
assert_eq!(b.partition_point(|_, &x| x < 5), 3);
assert_eq!(b.partition_point(|_, &x| x < 8), 5);

let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.partition_point(|_, &x| x < 7), 5);
assert_eq!(b.partition_point(|_, &x| x < 0), 0);

let b: IndexMap<_, i32> = [1, 3, 3, 3, 7]
.into_iter()
.enumerate()
.map(|(i, x)| (i + 100, x))
.collect();
assert_eq!(b.partition_point(|_, &x| x < 0), 0);
assert_eq!(b.partition_point(|_, &x| x < 1), 0);
assert_eq!(b.partition_point(|_, &x| x < 2), 1);
assert_eq!(b.partition_point(|_, &x| x < 3), 1);
assert_eq!(b.partition_point(|_, &x| x < 4), 4);
assert_eq!(b.partition_point(|_, &x| x < 5), 4);
assert_eq!(b.partition_point(|_, &x| x < 6), 4);
assert_eq!(b.partition_point(|_, &x| x < 7), 4);
assert_eq!(b.partition_point(|_, &x| x < 8), 5);
}
139 changes: 139 additions & 0 deletions src/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,142 @@ fn iter_default() {
assert_default::<Iter<'static, Item>>();
assert_default::<IntoIter<Item>>();
}

#[test]
fn test_binary_search_by() {
// addaped from stds test for binary_search
let b: IndexSet<i32> = [].into();
assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(0));

let b: IndexSet<i32> = [4].into();
assert_eq!(b.binary_search_by(|x| x.cmp(&3)), Err(0));
assert_eq!(b.binary_search_by(|x| x.cmp(&4)), Ok(0));
assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(1));

let b: IndexSet<i32> = [1, 2, 4, 6, 8, 9].into();
assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(3));
assert_eq!(b.binary_search_by(|x| x.cmp(&6)), Ok(3));
assert_eq!(b.binary_search_by(|x| x.cmp(&7)), Err(4));
assert_eq!(b.binary_search_by(|x| x.cmp(&8)), Ok(4));

let b: IndexSet<i32> = [1, 2, 4, 5, 6, 8].into();
assert_eq!(b.binary_search_by(|x| x.cmp(&9)), Err(6));

let b: IndexSet<i32> = [1, 2, 4, 6, 7, 8, 9].into();
assert_eq!(b.binary_search_by(|x| x.cmp(&6)), Ok(3));
assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(3));
assert_eq!(b.binary_search_by(|x| x.cmp(&8)), Ok(5));

let b: IndexSet<i32> = [1, 2, 4, 5, 6, 8, 9].into();
assert_eq!(b.binary_search_by(|x| x.cmp(&7)), Err(5));
assert_eq!(b.binary_search_by(|x| x.cmp(&0)), Err(0));

let b: IndexSet<i32> = [1, 3, 3, 3, 7].into();
assert_eq!(b.binary_search_by(|x| x.cmp(&0)), Err(0));
assert_eq!(b.binary_search_by(|x| x.cmp(&1)), Ok(0));
assert_eq!(b.binary_search_by(|x| x.cmp(&2)), Err(1));
// diff from std as set the duplicates keys
assert!(match b.binary_search_by(|x| x.cmp(&3)) {
Ok(1..=2) => true,
_ => false,
});
assert!(match b.binary_search_by(|x| x.cmp(&3)) {
Ok(1..=2) => true,
_ => false,
});
assert_eq!(b.binary_search_by(|x| x.cmp(&4)), Err(2));
assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(2));
assert_eq!(b.binary_search_by(|x| x.cmp(&6)), Err(2));
assert_eq!(b.binary_search_by(|x| x.cmp(&7)), Ok(2));
assert_eq!(b.binary_search_by(|x| x.cmp(&8)), Err(3));
}

#[test]
fn test_binary_search_by_key() {
// addaped from stds test for binary_search
let b: IndexSet<i32> = [].into();
assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(0));

let b: IndexSet<i32> = [4].into();
assert_eq!(b.binary_search_by_key(&3, |&x| x), Err(0));
assert_eq!(b.binary_search_by_key(&4, |&x| x), Ok(0));
assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(1));

let b: IndexSet<i32> = [1, 2, 4, 6, 8, 9].into();
assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(3));
assert_eq!(b.binary_search_by_key(&6, |&x| x), Ok(3));
assert_eq!(b.binary_search_by_key(&7, |&x| x), Err(4));
assert_eq!(b.binary_search_by_key(&8, |&x| x), Ok(4));

let b: IndexSet<i32> = [1, 2, 4, 5, 6, 8].into();
assert_eq!(b.binary_search_by_key(&9, |&x| x), Err(6));

let b: IndexSet<i32> = [1, 2, 4, 6, 7, 8, 9].into();
assert_eq!(b.binary_search_by_key(&6, |&x| x), Ok(3));
assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(3));
assert_eq!(b.binary_search_by_key(&8, |&x| x), Ok(5));

let b: IndexSet<i32> = [1, 2, 4, 5, 6, 8, 9].into();
assert_eq!(b.binary_search_by_key(&7, |&x| x), Err(5));
assert_eq!(b.binary_search_by_key(&0, |&x| x), Err(0));

let b: IndexSet<i32> = [1, 3, 3, 3, 7].into();
assert_eq!(b.binary_search_by_key(&0, |&x| x), Err(0));
assert_eq!(b.binary_search_by_key(&1, |&x| x), Ok(0));
assert_eq!(b.binary_search_by_key(&2, |&x| x), Err(1));
// diff from std as set the duplicates keys
assert!(match b.binary_search_by_key(&3, |&x| x) {
Ok(1..=2) => true,
_ => false,
});
assert!(match b.binary_search_by_key(&3, |&x| x) {
Ok(1..=2) => true,
_ => false,
});
assert_eq!(b.binary_search_by_key(&4, |&x| x), Err(2));
assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(2));
assert_eq!(b.binary_search_by_key(&6, |&x| x), Err(2));
assert_eq!(b.binary_search_by_key(&7, |&x| x), Ok(2));
assert_eq!(b.binary_search_by_key(&8, |&x| x), Err(3));
}

#[test]
fn test_partition_point() {
// addaped from stds test for partition_point
let b: IndexSet<i32> = [].into();
assert_eq!(b.partition_point(|&x| x < 5), 0);

let b: IndexSet<_> = [4].into();
assert_eq!(b.partition_point(|&x| x < 3), 0);
assert_eq!(b.partition_point(|&x| x < 4), 0);
assert_eq!(b.partition_point(|&x| x < 5), 1);

let b: IndexSet<_> = [1, 2, 4, 6, 8, 9].into();
assert_eq!(b.partition_point(|&x| x < 5), 3);
assert_eq!(b.partition_point(|&x| x < 6), 3);
assert_eq!(b.partition_point(|&x| x < 7), 4);
assert_eq!(b.partition_point(|&x| x < 8), 4);

let b: IndexSet<_> = [1, 2, 4, 5, 6, 8].into();
assert_eq!(b.partition_point(|&x| x < 9), 6);

let b: IndexSet<_> = [1, 2, 4, 6, 7, 8, 9].into();
assert_eq!(b.partition_point(|&x| x < 6), 3);
assert_eq!(b.partition_point(|&x| x < 5), 3);
assert_eq!(b.partition_point(|&x| x < 8), 5);

let b: IndexSet<_> = [1, 2, 4, 5, 6, 8, 9].into();
assert_eq!(b.partition_point(|&x| x < 7), 5);
assert_eq!(b.partition_point(|&x| x < 0), 0);

let b: IndexSet<_> = [1, 3, 3, 3, 7].into();
assert_eq!(b.partition_point(|&x| x < 0), 0);
assert_eq!(b.partition_point(|&x| x < 1), 0);
assert_eq!(b.partition_point(|&x| x < 2), 1);
assert_eq!(b.partition_point(|&x| x < 3), 1);
assert_eq!(b.partition_point(|&x| x < 4), 2); // diff from std as set the duplicates keys
assert_eq!(b.partition_point(|&x| x < 5), 2);
assert_eq!(b.partition_point(|&x| x < 6), 2);
assert_eq!(b.partition_point(|&x| x < 7), 2);
assert_eq!(b.partition_point(|&x| x < 8), 3);
}

0 comments on commit d93534f

Please sign in to comment.