[std][BTree] Create tests for keys which can be == without being identical

This commit is contained in:
tinnamchoi 2025-08-20 03:38:05 +08:00
parent 8c32e313cc
commit 92c2bda423
No known key found for this signature in database
2 changed files with 59 additions and 1 deletions

View file

@ -11,7 +11,7 @@ use crate::fmt::Debug;
use crate::rc::Rc;
use crate::string::{String, ToString};
use crate::testing::crash_test::{CrashTestDummy, Panic};
use crate::testing::ord_chaos::{Cyclic3, Governed, Governor};
use crate::testing::ord_chaos::{Cyclic3, Governed, Governor, IdBased};
use crate::testing::rng::DeterministicRng;
// 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() {
let prev = cursor.peek_prev();
assert_matches!(prev, Some((&3, _)));
}
#[test]
fn test_id_based_insert() {
let mut lhs = BTreeMap::new();
let mut rhs = BTreeMap::new();
lhs.insert(IdBased { id: 0, name: "lhs_k".to_string() }, "lhs_v".to_string());
rhs.insert(IdBased { id: 0, name: "rhs_k".to_string() }, "rhs_v".to_string());
for (k, v) in rhs.into_iter() {
lhs.insert(k, v);
}
assert_eq!(lhs.pop_first().unwrap().0.name, "lhs_k".to_string());
}
#[test]
fn test_id_based_append() {
let mut lhs = BTreeMap::new();
let mut rhs = BTreeMap::new();
lhs.insert(IdBased { id: 0, name: "lhs_k".to_string() }, "lhs_v".to_string());
rhs.insert(IdBased { id: 0, name: "rhs_k".to_string() }, "rhs_v".to_string());
lhs.append(&mut rhs);
assert_eq!(lhs.pop_first().unwrap().0.name, "lhs_k".to_string());
}

View file

@ -2,6 +2,8 @@ use std::cell::Cell;
use std::cmp::Ordering::{self, *};
use std::ptr;
use crate::string::String;
// Minimal type with an `Ord` implementation violating transitivity.
#[derive(Debug)]
pub(crate) enum Cyclic3 {
@ -79,3 +81,31 @@ impl<T: PartialEq> PartialEq for Governed<'_, T> {
}
impl<T: Eq> Eq for Governed<'_, T> {}
// Comparison based only on the ID, the name is ignored.
#[derive(Debug)]
pub(crate) struct IdBased {
pub id: u32,
#[allow(dead_code)]
pub name: String,
}
impl PartialEq for IdBased {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for IdBased {}
impl PartialOrd for IdBased {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for IdBased {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}