Skip to content

Commit

Permalink
Rollup merge of #110234 - marc0246:btree-insert-after-fix, r=cuviper
Browse files Browse the repository at this point in the history
Fix btree `CursorMut::insert_after` check

Fixes a check inside `BTreeMap`'s `CursorMut::insert_after`, where it would peek the previous element to check whether the inserted key is below the next one, instead of peeking the next element.
  • Loading branch information
matthiaskrgr committed Apr 13, 2023
2 parents 6f1500a + cdd9829 commit 209e6d9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3183,7 +3183,7 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMut<'a, K, V, A> {
panic!("key must be ordered above the current element");
}
}
if let Some((next, _)) = self.peek_prev() {
if let Some((next, _)) = self.peek_next() {
if &key >= next {
panic!("key must be ordered below the next element");
}
Expand Down
64 changes: 64 additions & 0 deletions library/alloc/src/collections/btree/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,3 +2385,67 @@ fn test_cursor_mut() {
assert_eq!(cur.key(), Some(&4));
assert_eq!(map, BTreeMap::from([(0, '?'), (1, 'a'), (3, 'c'), (4, 'd')]));
}

#[should_panic(expected = "key must be ordered above the previous element")]
#[test]
fn test_cursor_mut_insert_before_1() {
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
let mut cur = map.upper_bound_mut(Bound::Included(&2));
cur.insert_before(0, 'd');
}

#[should_panic(expected = "key must be ordered above the previous element")]
#[test]
fn test_cursor_mut_insert_before_2() {
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
let mut cur = map.upper_bound_mut(Bound::Included(&2));
cur.insert_before(1, 'd');
}

#[should_panic(expected = "key must be ordered below the current element")]
#[test]
fn test_cursor_mut_insert_before_3() {
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
let mut cur = map.upper_bound_mut(Bound::Included(&2));
cur.insert_before(2, 'd');
}

#[should_panic(expected = "key must be ordered below the current element")]
#[test]
fn test_cursor_mut_insert_before_4() {
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
let mut cur = map.upper_bound_mut(Bound::Included(&2));
cur.insert_before(3, 'd');
}

#[should_panic(expected = "key must be ordered above the current element")]
#[test]
fn test_cursor_mut_insert_after_1() {
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
let mut cur = map.upper_bound_mut(Bound::Included(&2));
cur.insert_after(1, 'd');
}

#[should_panic(expected = "key must be ordered above the current element")]
#[test]
fn test_cursor_mut_insert_after_2() {
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
let mut cur = map.upper_bound_mut(Bound::Included(&2));
cur.insert_after(2, 'd');
}

#[should_panic(expected = "key must be ordered below the next element")]
#[test]
fn test_cursor_mut_insert_after_3() {
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
let mut cur = map.upper_bound_mut(Bound::Included(&2));
cur.insert_after(3, 'd');
}

#[should_panic(expected = "key must be ordered below the next element")]
#[test]
fn test_cursor_mut_insert_after_4() {
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
let mut cur = map.upper_bound_mut(Bound::Included(&2));
cur.insert_after(4, 'd');
}

0 comments on commit 209e6d9

Please sign in to comment.