Skip to content

Commit

Permalink
Rollup merge of #96034 - Gumichocopengin8:test/btree-set, r=Dylan-DPC
Browse files Browse the repository at this point in the history
[test] Add test cases of untested functions for BTreeSet

- add [`is_superset()`](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html#method.is_superset) and [`remove()`](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html#method.remove) test cases for BTreeSet since these functions has no test cases.
  • Loading branch information
Dylan-DPC authored Apr 15, 2022
2 parents 937b0a0 + 50c339e commit 224afad
Showing 1 changed file with 56 additions and 0 deletions.
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

0 comments on commit 224afad

Please sign in to comment.