Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Add test cases of untested functions for BTreeSet #96034

Merged
merged 2 commits into from
Apr 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions library/alloc/src/collections/btree/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,42 @@ fn test_is_subset() {
assert_eq!(is_subset(&[99, 100], &large), false);
}

#[test]
fn test_is_superset() {
fn is_superset(a: &[i32], b: &[i32]) -> bool {
let set_a = BTreeSet::from_iter(a.iter());
let set_b = BTreeSet::from_iter(b.iter());
set_a.is_superset(&set_b)
}

assert_eq!(is_superset(&[], &[]), true);
assert_eq!(is_superset(&[], &[1, 2]), false);
assert_eq!(is_superset(&[0], &[1, 2]), false);
assert_eq!(is_superset(&[1], &[1, 2]), false);
assert_eq!(is_superset(&[4], &[1, 2]), false);
assert_eq!(is_superset(&[1, 4], &[1, 2]), false);
assert_eq!(is_superset(&[1, 2], &[1, 2]), true);
assert_eq!(is_superset(&[1, 2, 3], &[1, 3]), true);
assert_eq!(is_superset(&[1, 2, 3], &[]), true);
assert_eq!(is_superset(&[-1, 1, 2, 3], &[-1, 3]), true);

if cfg!(miri) {
// Miri is too slow
return;
}

let large = Vec::from_iter(0..100);
assert_eq!(is_superset(&[], &large), false);
assert_eq!(is_superset(&large, &[]), true);
assert_eq!(is_superset(&large, &[1]), true);
assert_eq!(is_superset(&large, &[50, 99]), true);
assert_eq!(is_superset(&large, &[100]), false);
assert_eq!(is_superset(&large, &[0, 99]), true);
assert_eq!(is_superset(&[-1], &large), false);
assert_eq!(is_superset(&[0], &large), false);
assert_eq!(is_superset(&[99, 100], &large), false);
}

#[test]
fn test_retain() {
let mut set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
Expand Down Expand Up @@ -391,6 +427,26 @@ fn test_clear() {
x.clear();
assert!(x.is_empty());
}
#[test]
fn test_remove() {
let mut x = BTreeSet::new();
assert!(x.is_empty());

x.insert(1);
x.insert(2);
x.insert(3);
x.insert(4);

assert_eq!(x.remove(&2), true);
assert_eq!(x.remove(&0), false);
assert_eq!(x.remove(&5), false);
assert_eq!(x.remove(&1), true);
assert_eq!(x.remove(&2), false);
assert_eq!(x.remove(&3), true);
assert_eq!(x.remove(&4), true);
assert_eq!(x.remove(&4), false);
assert!(x.is_empty());
}

#[test]
fn test_zip() {
Expand Down