From 4117e8c2d328a7615c2dc294d939b610d4e22dca Mon Sep 17 00:00:00 2001 From: Keita Nonaka Date: Thu, 14 Apr 2022 23:40:05 -0700 Subject: [PATCH 1/4] test: add pop_first() pop_last() test cases for BTreeSet --- .../alloc/src/collections/btree/map/tests.rs | 86 +++++++++++++++++-- 1 file changed, 77 insertions(+), 9 deletions(-) diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index cc986e93698b7..acc49799a73fc 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -791,7 +791,11 @@ fn test_range_finding_ill_order_in_range_ord() { impl Ord for EvilTwin { fn cmp(&self, other: &Self) -> Ordering { let ord = self.0.cmp(&other.0); - if COMPARES.fetch_add(1, SeqCst) > 0 { ord.reverse() } else { ord } + if COMPARES.fetch_add(1, SeqCst) > 0 { + ord.reverse() + } else { + ord + } } } @@ -943,13 +947,12 @@ mod test_drain_filter { fn mutating_and_keeping() { let pairs = (0..3).map(|i| (i, i)); let mut map = BTreeMap::from_iter(pairs); - assert!( - map.drain_filter(|_, v| { + assert!(map + .drain_filter(|_, v| { *v += 6; false }) - .eq(iter::empty()) - ); + .eq(iter::empty())); assert!(map.keys().copied().eq(0..3)); assert!(map.values().copied().eq(6..9)); map.check(); @@ -960,13 +963,12 @@ mod test_drain_filter { fn mutating_and_removing() { let pairs = (0..3).map(|i| (i, i)); let mut map = BTreeMap::from_iter(pairs); - assert!( - map.drain_filter(|_, v| { + assert!(map + .drain_filter(|_, v| { *v += 6; true }) - .eq((0..3).map(|i| (i, i + 6))) - ); + .eq((0..3).map(|i| (i, i + 6)))); assert!(map.is_empty()); map.check(); } @@ -1878,6 +1880,72 @@ fn test_first_last_entry() { a.check(); } +#[test] +fn test_pop_first_last() { + let mut map = BTreeMap::new(); + assert_eq!(map.pop_first(), None); + assert_eq!(map.pop_last(), None); + + map.insert(1, 10); + map.insert(2, 20); + map.insert(3, 30); + map.insert(4, 40); + + assert_eq!(map.len(), 4); + + let (key, val) = map.pop_first().unwrap(); + assert_eq!(key, 1); + assert_eq!(val, 10); + assert_eq!(map.len(), 3); + + let (key, val) = map.pop_first().unwrap(); + assert_eq!(key, 2); + assert_eq!(val, 20); + assert_eq!(map.len(), 2); + let (key, val) = map.pop_last().unwrap(); + assert_eq!(key, 4); + assert_eq!(val, 40); + assert_eq!(map.len(), 1); + + map.insert(5, 50); + map.insert(6, 60); + assert_eq!(map.len(), 3); + + let (key, val) = map.pop_first().unwrap(); + assert_eq!(key, 3); + assert_eq!(val, 30); + assert_eq!(map.len(), 2); + + let (key, val) = map.pop_last().unwrap(); + assert_eq!(key, 6); + assert_eq!(val, 60); + assert_eq!(map.len(), 1); + + let (key, val) = map.pop_last().unwrap(); + assert_eq!(key, 5); + assert_eq!(val, 50); + assert_eq!(map.len(), 0); + + assert_eq!(map.pop_first(), None); + assert_eq!(map.pop_last(), None); + + map.insert(7, 70); + map.insert(8, 80); + + let (key, val) = map.pop_last().unwrap(); + assert_eq!(key, 8); + assert_eq!(val, 80); + assert_eq!(map.len(), 1); + + let (key, val) = map.pop_last().unwrap(); + assert_eq!(key, 7); + assert_eq!(val, 70); + assert_eq!(map.len(), 0); + + assert_eq!(map.pop_first(), None); + assert_eq!(map.pop_last(), None); +} + #[test] fn test_insert_into_full_height_0() { let size = node::CAPACITY; From e1626020d3dc5b598f514ef9648e7e5a62f456ba Mon Sep 17 00:00:00 2001 From: Keita Nonaka Date: Fri, 15 Apr 2022 00:04:03 -0700 Subject: [PATCH 2/4] test: add get_key_value() test cases for BTreeSet --- .../alloc/src/collections/btree/map/tests.rs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index acc49799a73fc..d14f2cd034e44 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -1946,6 +1946,30 @@ fn test_pop_first_last() { assert_eq!(map.pop_last(), None); } +#[test] +fn test_get_key_value() { + let mut map = BTreeMap::new(); + + assert!(map.is_empty()); + assert_eq!(map.get_key_value(&1), None); + assert_eq!(map.get_key_value(&2), None); + + map.insert(1, 10); + map.insert(2, 20); + map.insert(3, 30); + + assert_eq!(map.len(), 3); + assert_eq!(map.get_key_value(&1), Some((&1, &10))); + assert_eq!(map.get_key_value(&3), Some((&3, &30))); + assert_eq!(map.get_key_value(&4), None); + + map.remove(&3); + + assert_eq!(map.len(), 2); + assert_eq!(map.get_key_value(&3), None); + assert_eq!(map.get_key_value(&2), Some((&2, &20))); +} + #[test] fn test_insert_into_full_height_0() { let size = node::CAPACITY; From 3f2f4a35ed89ed02b7192bf3b35b02a4654bb2f0 Mon Sep 17 00:00:00 2001 From: Keita Nonaka Date: Fri, 15 Apr 2022 01:12:00 -0700 Subject: [PATCH 3/4] test: add try_insert() test cases for BTreeSet --- library/alloc/src/collections/btree/map/tests.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index d14f2cd034e44..b9db98ffae96a 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -1996,6 +1996,21 @@ fn test_insert_into_full_height_1() { } } +#[test] +fn test_try_insert() { + let mut map = BTreeMap::new(); + + assert!(map.is_empty()); + + assert_eq!(map.try_insert(1, 10).unwrap(), &10); + assert_eq!(map.try_insert(2, 20).unwrap(), &20); + + let err = map.try_insert(2, 200).unwrap_err(); + assert_eq!(err.entry.key(), &2); + assert_eq!(err.entry.get(), &20); + assert_eq!(err.value, 200); +} + macro_rules! create_append_test { ($name:ident, $len:expr) => { #[test] From 3f46ba6028ccbe1fa63d7423c77d5ccbb8abdbc4 Mon Sep 17 00:00:00 2001 From: Keita Nonaka Date: Fri, 15 Apr 2022 01:30:05 -0700 Subject: [PATCH 4/4] chore: formatting --- .../alloc/src/collections/btree/map/tests.rs | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index b9db98ffae96a..47ba1777ae903 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -791,11 +791,7 @@ fn test_range_finding_ill_order_in_range_ord() { impl Ord for EvilTwin { fn cmp(&self, other: &Self) -> Ordering { let ord = self.0.cmp(&other.0); - if COMPARES.fetch_add(1, SeqCst) > 0 { - ord.reverse() - } else { - ord - } + if COMPARES.fetch_add(1, SeqCst) > 0 { ord.reverse() } else { ord } } } @@ -947,12 +943,13 @@ mod test_drain_filter { fn mutating_and_keeping() { let pairs = (0..3).map(|i| (i, i)); let mut map = BTreeMap::from_iter(pairs); - assert!(map - .drain_filter(|_, v| { + assert!( + map.drain_filter(|_, v| { *v += 6; false }) - .eq(iter::empty())); + .eq(iter::empty()) + ); assert!(map.keys().copied().eq(0..3)); assert!(map.values().copied().eq(6..9)); map.check(); @@ -963,12 +960,13 @@ mod test_drain_filter { fn mutating_and_removing() { let pairs = (0..3).map(|i| (i, i)); let mut map = BTreeMap::from_iter(pairs); - assert!(map - .drain_filter(|_, v| { + assert!( + map.drain_filter(|_, v| { *v += 6; true }) - .eq((0..3).map(|i| (i, i + 6)))); + .eq((0..3).map(|i| (i, i + 6))) + ); assert!(map.is_empty()); map.check(); }