Skip to content

Commit 4547ebb

Browse files
authored
Rollup merge of #76983 - ssomers:btree_extra_test, r=Mark-Simulacrum
BTreeMap: extra testing & fixed comments r? @Mark-Simulacrum
2 parents 4b362bb + 37ec045 commit 4547ebb

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ impl<'a, K: 'a, V: 'a> BTreeMap<K, V> {
8888
let min_len = if is_root { 1 } else { node::MIN_LEN };
8989
assert!(node.len() >= min_len, "{} < {}", node.len(), min_len);
9090

91+
for idx in 0..=node.len() {
92+
let edge = unsafe { node::Handle::new_edge(node, idx) };
93+
assert!(edge.descend().ascend().ok().unwrap() == edge);
94+
}
95+
9196
internal_length += node.len();
9297
}
9398
Position::InternalKV(kv) => {
@@ -1846,3 +1851,17 @@ fn test_into_values() {
18461851
assert!(values.contains(&'b'));
18471852
assert!(values.contains(&'c'));
18481853
}
1854+
1855+
#[test]
1856+
fn test_insert_remove_intertwined() {
1857+
let loops = if cfg!(miri) { 100 } else { 1_000_000 };
1858+
let mut map = BTreeMap::new();
1859+
let mut i = 1;
1860+
for _ in 0..loops {
1861+
i = (i + 421) & 0xFF;
1862+
map.insert(i, i);
1863+
map.remove(&(0xFF - i));
1864+
}
1865+
1866+
map.check();
1867+
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,8 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
613613
}
614614

615615
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
616-
/// Adds a key/value pair and an edge to go to the right of that pair to
617-
/// the end of the node.
616+
/// Adds a key/value pair, and an edge to go to the right of that pair,
617+
/// to the end of the node.
618618
pub fn push(&mut self, key: K, val: V, edge: Root<K, V>) {
619619
assert!(edge.height == self.height - 1);
620620

@@ -630,8 +630,8 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
630630
}
631631
}
632632

633-
/// Adds a key/value pair and an edge to go to the left of that pair to
634-
/// the beginning of the node.
633+
/// Adds a key/value pair, and an edge to go to the left of that pair,
634+
/// to the beginning of the node.
635635
pub fn push_front(&mut self, key: K, val: V, edge: Root<K, V>) {
636636
assert!(edge.height == self.height - 1);
637637
assert!(self.len() < CAPACITY);
@@ -1152,7 +1152,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
11521152
///
11531153
/// - The node is truncated to only contain the key/value pairs to the right of
11541154
/// this handle.
1155-
/// - The key and value pointed to by this handle and extracted.
1155+
/// - The key and value pointed to by this handle are extracted.
11561156
/// - All the key/value pairs to the right of this handle are put into a newly
11571157
/// allocated node.
11581158
pub fn split(mut self) -> (NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, K, V, Root<K, V>) {
@@ -1196,7 +1196,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
11961196
///
11971197
/// - The node is truncated to only contain the edges and key/value pairs to the
11981198
/// right of this handle.
1199-
/// - The key and value pointed to by this handle and extracted.
1199+
/// - The key and value pointed to by this handle are extracted.
12001200
/// - All the edges and key/value pairs to the right of this handle are put into
12011201
/// a newly allocated node.
12021202
pub fn split(mut self) -> (NodeRef<marker::Mut<'a>, K, V, marker::Internal>, K, V, Root<K, V>) {

0 commit comments

Comments
 (0)