-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #78903 - ssomers:btree_order_chaos_testing, r=Mark-Si…
…mulacrum BTreeMap: test chaotic ordering & other bits & bobs r? `@Mark-Simulacrum`
- Loading branch information
Showing
3 changed files
with
201 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
library/alloc/src/collections/btree/map/tests/ord_chaos.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::cell::Cell; | ||
use std::cmp::Ordering::{self, *}; | ||
use std::ptr; | ||
|
||
#[derive(Debug)] | ||
pub enum Cyclic3 { | ||
A, | ||
B, | ||
C, | ||
} | ||
use Cyclic3::*; | ||
|
||
impl PartialOrd for Cyclic3 { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for Cyclic3 { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
match (self, other) { | ||
(A, A) | (B, B) | (C, C) => Equal, | ||
(A, B) | (B, C) | (C, A) => Less, | ||
(A, C) | (B, A) | (C, B) => Greater, | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for Cyclic3 { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.cmp(&other) == Equal | ||
} | ||
} | ||
|
||
impl Eq for Cyclic3 {} | ||
|
||
#[derive(Debug)] | ||
pub struct Governor { | ||
flipped: Cell<bool>, | ||
} | ||
|
||
impl Governor { | ||
pub fn new() -> Self { | ||
Governor { flipped: Cell::new(false) } | ||
} | ||
|
||
pub fn flip(&self) { | ||
self.flipped.set(!self.flipped.get()); | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Governed<'a, T>(pub T, pub &'a Governor); | ||
|
||
impl<T: Ord> PartialOrd for Governed<'_, T> { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl<T: Ord> Ord for Governed<'_, T> { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
assert!(ptr::eq(self.1, other.1)); | ||
let ord = self.0.cmp(&other.0); | ||
if self.1.flipped.get() { ord.reverse() } else { ord } | ||
} | ||
} | ||
|
||
impl<T: PartialEq> PartialEq for Governed<'_, T> { | ||
fn eq(&self, other: &Self) -> bool { | ||
assert!(ptr::eq(self.1, other.1)); | ||
self.0.eq(&other.0) | ||
} | ||
} | ||
|
||
impl<T: Eq> Eq for Governed<'_, T> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters