use cmporder::{cmp_order, partial_cmp_order};
use rand::seq::SliceRandom;
#[test]
fn test_cmporder() {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ABC {
a: u8,
b: u8,
}
impl Ord for ABC {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
cmp_order!(self.a.cmp(&other.a), self.b.cmp(&other.b))
}
}
impl PartialOrd for ABC {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
let abc1 = ABC { a: 10, b: 10 };
let abc2 = ABC { a: 20, b: 10 };
let abc3 = ABC { a: 10, b: 20 };
let list = vec![abc1, abc2, abc3];
let mut rng = rand::rng();
let mut list = list.clone();
list.shuffle(&mut rng);
list.sort();
assert_eq!(list, vec![abc1, abc3, abc2]);
}
#[test]
fn test_partial_cmporder() {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ABC {
a: u8,
b: u8,
}
impl PartialOrd for ABC {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
partial_cmp_order!(self.a.partial_cmp(&other.a), self.b.partial_cmp(&other.b))
}
}
impl Ord for ABC {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap_or(std::cmp::Ordering::Equal)
}
}
let abc1 = ABC { a: 20, b: 20 };
let abc2 = ABC { a: 20, b: 10 };
let abc3 = ABC { a: 10, b: 20 };
let list = vec![abc1, abc2, abc3];
let mut rng = rand::rng();
let mut list = list.clone();
list.shuffle(&mut rng);
list.sort();
assert_eq!(list, vec![abc3, abc2, abc1]);
}