Skip to content

Commit 209e6d9

Browse files
authored
Rollup merge of #110234 - marc0246:btree-insert-after-fix, r=cuviper
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.
2 parents 6f1500a + cdd9829 commit 209e6d9

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

library/alloc/src/collections/btree/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3183,7 +3183,7 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMut<'a, K, V, A> {
31833183
panic!("key must be ordered above the current element");
31843184
}
31853185
}
3186-
if let Some((next, _)) = self.peek_prev() {
3186+
if let Some((next, _)) = self.peek_next() {
31873187
if &key >= next {
31883188
panic!("key must be ordered below the next element");
31893189
}

library/alloc/src/collections/btree/map/tests.rs

+64
Original file line numberDiff line numberDiff line change
@@ -2385,3 +2385,67 @@ fn test_cursor_mut() {
23852385
assert_eq!(cur.key(), Some(&4));
23862386
assert_eq!(map, BTreeMap::from([(0, '?'), (1, 'a'), (3, 'c'), (4, 'd')]));
23872387
}
2388+
2389+
#[should_panic(expected = "key must be ordered above the previous element")]
2390+
#[test]
2391+
fn test_cursor_mut_insert_before_1() {
2392+
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
2393+
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2394+
cur.insert_before(0, 'd');
2395+
}
2396+
2397+
#[should_panic(expected = "key must be ordered above the previous element")]
2398+
#[test]
2399+
fn test_cursor_mut_insert_before_2() {
2400+
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
2401+
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2402+
cur.insert_before(1, 'd');
2403+
}
2404+
2405+
#[should_panic(expected = "key must be ordered below the current element")]
2406+
#[test]
2407+
fn test_cursor_mut_insert_before_3() {
2408+
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
2409+
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2410+
cur.insert_before(2, 'd');
2411+
}
2412+
2413+
#[should_panic(expected = "key must be ordered below the current element")]
2414+
#[test]
2415+
fn test_cursor_mut_insert_before_4() {
2416+
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
2417+
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2418+
cur.insert_before(3, 'd');
2419+
}
2420+
2421+
#[should_panic(expected = "key must be ordered above the current element")]
2422+
#[test]
2423+
fn test_cursor_mut_insert_after_1() {
2424+
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
2425+
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2426+
cur.insert_after(1, 'd');
2427+
}
2428+
2429+
#[should_panic(expected = "key must be ordered above the current element")]
2430+
#[test]
2431+
fn test_cursor_mut_insert_after_2() {
2432+
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
2433+
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2434+
cur.insert_after(2, 'd');
2435+
}
2436+
2437+
#[should_panic(expected = "key must be ordered below the next element")]
2438+
#[test]
2439+
fn test_cursor_mut_insert_after_3() {
2440+
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
2441+
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2442+
cur.insert_after(3, 'd');
2443+
}
2444+
2445+
#[should_panic(expected = "key must be ordered below the next element")]
2446+
#[test]
2447+
fn test_cursor_mut_insert_after_4() {
2448+
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
2449+
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2450+
cur.insert_after(4, 'd');
2451+
}

0 commit comments

Comments
 (0)