Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTreeMap: test all borrowing interfaces and test more chaotic order behavior #81191

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion library/alloc/src/collections/btree/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ fn test_range_backwards_4() {

#[test]
#[should_panic]
fn test_range_backwards_5() {
fn test_range_finding_ill_order_in_map() {
let mut map = BTreeMap::new();
map.insert(Cyclic3::B, ());
// Lacking static_assert, call `range` conditionally, to emphasise that
Expand All @@ -788,6 +788,47 @@ fn test_range_backwards_5() {
}
}

#[test]
#[should_panic]
fn test_range_finding_ill_order_in_range_ord() {
// Has proper order the first time asked, then flips around.
struct EvilTwin(i32);

impl PartialOrd for EvilTwin {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

static COMPARES: AtomicUsize = AtomicUsize::new(0);
impl Ord for EvilTwin {
fn cmp(&self, other: &Self) -> Ordering {
let ord = self.0.cmp(&other.0);
if COMPARES.fetch_add(1, SeqCst) > 0 { ord.reverse() } else { ord }
}
}

impl PartialEq for EvilTwin {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}

impl Eq for EvilTwin {}

#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct CompositeKey(i32, EvilTwin);

impl Borrow<EvilTwin> for CompositeKey {
fn borrow(&self) -> &EvilTwin {
&self.1
}
}

let map = (0..12).map(|i| (CompositeKey(i, EvilTwin(i)), ())).collect::<BTreeMap<_, _>>();
map.range(EvilTwin(5)..=EvilTwin(7));
}

#[test]
fn test_range_1000() {
// Miri is too slow
Expand Down Expand Up @@ -1222,6 +1263,51 @@ fn test_borrow() {
map.insert(Rc::new(0), 1);
assert_eq!(map[&0], 1);
}

#[allow(dead_code)]
fn get<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.get(t);
}

#[allow(dead_code)]
fn get_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
v.get_mut(t);
}

#[allow(dead_code)]
fn get_key_value<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.get_key_value(t);
}

#[allow(dead_code)]
fn contains_key<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.contains_key(t);
}

#[allow(dead_code)]
fn range<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: T) {
v.range(t..);
}

#[allow(dead_code)]
fn range_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: T) {
v.range_mut(t..);
}

#[allow(dead_code)]
fn remove<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
v.remove(t);
}

#[allow(dead_code)]
fn remove_entry<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
v.remove_entry(t);
}

#[allow(dead_code)]
fn split_off<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
v.split_off(t);
}
}

#[test]
Expand Down