Skip to content

Commit 92c2bda

Browse files
committed
[std][BTree] Create tests for keys which can be == without being identical
1 parent 8c32e31 commit 92c2bda

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::fmt::Debug;
1111
use crate::rc::Rc;
1212
use crate::string::{String, ToString};
1313
use crate::testing::crash_test::{CrashTestDummy, Panic};
14-
use crate::testing::ord_chaos::{Cyclic3, Governed, Governor};
14+
use crate::testing::ord_chaos::{Cyclic3, Governed, Governor, IdBased};
1515
use crate::testing::rng::DeterministicRng;
1616

1717
// Minimum number of elements to insert, to guarantee a tree with 2 levels,
@@ -2587,3 +2587,31 @@ fn cursor_peek_prev_agrees_with_cursor_mut() {
25872587
let prev = cursor.peek_prev();
25882588
assert_matches!(prev, Some((&3, _)));
25892589
}
2590+
2591+
#[test]
2592+
fn test_id_based_insert() {
2593+
let mut lhs = BTreeMap::new();
2594+
let mut rhs = BTreeMap::new();
2595+
2596+
lhs.insert(IdBased { id: 0, name: "lhs_k".to_string() }, "lhs_v".to_string());
2597+
rhs.insert(IdBased { id: 0, name: "rhs_k".to_string() }, "rhs_v".to_string());
2598+
2599+
for (k, v) in rhs.into_iter() {
2600+
lhs.insert(k, v);
2601+
}
2602+
2603+
assert_eq!(lhs.pop_first().unwrap().0.name, "lhs_k".to_string());
2604+
}
2605+
2606+
#[test]
2607+
fn test_id_based_append() {
2608+
let mut lhs = BTreeMap::new();
2609+
let mut rhs = BTreeMap::new();
2610+
2611+
lhs.insert(IdBased { id: 0, name: "lhs_k".to_string() }, "lhs_v".to_string());
2612+
rhs.insert(IdBased { id: 0, name: "rhs_k".to_string() }, "rhs_v".to_string());
2613+
2614+
lhs.append(&mut rhs);
2615+
2616+
assert_eq!(lhs.pop_first().unwrap().0.name, "lhs_k".to_string());
2617+
}

library/alloctests/testing/ord_chaos.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::cell::Cell;
22
use std::cmp::Ordering::{self, *};
33
use std::ptr;
44

5+
use crate::string::String;
6+
57
// Minimal type with an `Ord` implementation violating transitivity.
68
#[derive(Debug)]
79
pub(crate) enum Cyclic3 {
@@ -79,3 +81,31 @@ impl<T: PartialEq> PartialEq for Governed<'_, T> {
7981
}
8082

8183
impl<T: Eq> Eq for Governed<'_, T> {}
84+
85+
// Comparison based only on the ID, the name is ignored.
86+
#[derive(Debug)]
87+
pub(crate) struct IdBased {
88+
pub id: u32,
89+
#[allow(dead_code)]
90+
pub name: String,
91+
}
92+
93+
impl PartialEq for IdBased {
94+
fn eq(&self, other: &Self) -> bool {
95+
self.id == other.id
96+
}
97+
}
98+
99+
impl Eq for IdBased {}
100+
101+
impl PartialOrd for IdBased {
102+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
103+
Some(self.cmp(other))
104+
}
105+
}
106+
107+
impl Ord for IdBased {
108+
fn cmp(&self, other: &Self) -> Ordering {
109+
self.id.cmp(&other.id)
110+
}
111+
}

0 commit comments

Comments
 (0)