modes:pair:tests: refactor for cleanliness

This commit is contained in:
2026-04-03 21:29:35 +01:00
committed by oreodave
parent 3cdfde07fe
commit f088915a60

View File

@@ -99,16 +99,33 @@ impl PartialOrd for Pair {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::card::{make_decks, Rank}; use crate::{
card::{make_decks, Rank},
modes::tests::test_non_reflexivity,
};
#[test] #[test]
fn new() { fn new() {
// Two jokers can never be a pair. // Two jokers can never be a pair.
assert_eq!(Pair::new(Card::make_joker(), Card::make_joker()), None); assert_eq!(Pair::new(Card::make_joker(), Card::make_joker()), None);
for rank in Rank::iter_all() { // Iterate over all ranks
for c1 in rank.cards() { for r1 in Rank::iter_all() {
for c2 in rank.cards() { // Iterate over all cards in a rank
for c1 in r1.cards() {
// Iterate over all ranks that aren't r1
for r2 in Rank::iter_all().filter(|&r2| r2 != r1) {
// TEST: Cannot compose a pair made from two different
// ranked cards.
let all_cards = r2.cards();
let all_pairs =
all_cards.filter_map(|c2| Pair::new(c1, c2));
assert_eq!(all_pairs.count(), 0);
}
// Iterate over all cards that are of the same rank as r1.
for c2 in r1.cards() {
// TEST: Pairs are composed of two similar rank cards. // TEST: Pairs are composed of two similar rank cards.
let pair = { let pair = {
let pair = Pair::new(c1, c2); let pair = Pair::new(c1, c2);
@@ -139,52 +156,16 @@ mod tests {
assert!(matches!(pair.0, Card::Joker(_))); assert!(matches!(pair.0, Card::Joker(_)));
assert!(matches!(pair.1, Card::PlayingCard(_))); assert!(matches!(pair.1, Card::PlayingCard(_)));
} }
for opposing_rank in Rank::iter_all() {
if rank == opposing_rank {
continue;
}
assert!(rank != opposing_rank);
// TEST: Two playing cards of differing rank can never be a pair.
for r1 in rank.cards() {
for r2 in opposing_rank.cards() {
assert_eq!(Pair::new(r1, r2), None);
}
}
}
} }
} }
// A rank has 4 cards. There are a total of 10 proper pairs that can be fn exhaustive_pairs_deck() -> impl Iterator<Item = Pair> {
// made out of 4 cards. With n jokers, add 4n improper pairs to the // A rank has 4 cards. There are a total of 10 proper pairs that can be
// total count of pairs for a single rank. // made out of 4 cards. With n jokers, add 4n improper pairs to the
// total count of pairs for a single rank.
fn exhaustive_pairs_deck() -> Vec<Pair> { make_decks(1).flat_map(|c1| {
// A deck has 2 jokers => 18 pairs per rank. make_decks(1).filter_map(move |c2| Pair::new(c1, c2))
let mut pairs = Vec::with_capacity(13 * 18); })
for c1 in make_decks(1) {
for c2 in make_decks(2) {
if let Some(p) = Pair::new(c1, c2) {
pairs.push(p);
}
}
}
pairs
}
fn exhaustive_pairs_rank(r: Rank) -> Vec<Pair> {
// We're only looking at one joker here, so 14 pairs in a rank.
let mut pairs = Vec::with_capacity(14);
for c1 in r.cards() {
for c2 in r.cards() {
pairs.push(Pair::new(c1, c2).unwrap());
}
pairs.push(Pair::new(c1, Card::make_joker()).unwrap());
}
pairs
} }
#[test] #[test]
@@ -203,32 +184,31 @@ mod tests {
// exclusively one of the pairs is improper - pairs that are // exclusively one of the pairs is improper - pairs that are
// improper should be sorted less than the proper one. // improper should be sorted less than the proper one.
(Ordering::Less, Ordering::Equal) => { (Ordering::Less, Ordering::Equal) => {
// p1 has a joker, p2 doesn't => p2 is better than p1 // p1 has a joker, p2 doesn't => p1 < p2
p1.is_improper() && p2.is_proper() p1.is_improper() && p2.is_proper()
} }
(Ordering::Greater, Ordering::Equal) => { (Ordering::Greater, Ordering::Equal) => {
// p2 has a joker, p1 doesn't => p1 is better than p2 // p2 has a joker, p1 doesn't => p1 > p2
p2.is_improper() && p1.is_proper() p2.is_improper() && p1.is_proper()
} }
_ => false, _ => false,
} }
} }
for p1 in &exhaustive_pairs_deck() { for p1 in exhaustive_pairs_deck() {
for p2 in &exhaustive_pairs_deck() { for p2 in exhaustive_pairs_deck() {
// TEST: For any two pair we expect them to have the // TEST: For any two pair we expect them to have the
// `expected_ordering_relation`. // `expected_ordering_relation`.
assert!(expected_ordering_relation(p1, p2)); assert!(expected_ordering_relation(&p1, &p2));
} }
} }
} }
#[test] #[test]
fn footstool() { fn footstool() {
let pairs = exhaustive_pairs_deck(); for p1 in exhaustive_pairs_deck() {
for p1 in &pairs { for p2 in exhaustive_pairs_deck() {
for p2 in &pairs { let (p1_on_p2, p2_on_p1) = test_non_reflexivity(&p1, &p2);
let (p1_on_p2, p2_on_p1) = test_non_reflexivity(p1, p2);
let (hc1_on_hc2, hc2_on_hc1) = { let (hc1_on_hc2, hc2_on_hc1) = {
let [high_card_1, high_card_2] = let [high_card_1, high_card_2] =
[p1.1, p2.1].map(Single::new).map(|x| { [p1.1, p2.1].map(Single::new).map(|x| {