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)]
mod tests {
use super::*;
use crate::card::{make_decks, Rank};
use crate::{
card::{make_decks, Rank},
modes::tests::test_non_reflexivity,
};
#[test]
fn new() {
// Two jokers can never be a pair.
assert_eq!(Pair::new(Card::make_joker(), Card::make_joker()), None);
for rank in Rank::iter_all() {
for c1 in rank.cards() {
for c2 in rank.cards() {
// Iterate over all ranks
for r1 in Rank::iter_all() {
// 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.
let pair = {
let pair = Pair::new(c1, c2);
@@ -139,52 +156,16 @@ mod tests {
assert!(matches!(pair.0, Card::Joker(_)));
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);
}
}
}
}
}
fn exhaustive_pairs_deck() -> impl Iterator<Item = Pair> {
// A rank has 4 cards. There are a total of 10 proper pairs that can be
// 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> {
// A deck has 2 jokers => 18 pairs per rank.
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
make_decks(1).flat_map(|c1| {
make_decks(1).filter_map(move |c2| Pair::new(c1, c2))
})
}
#[test]
@@ -203,32 +184,31 @@ mod tests {
// exclusively one of the pairs is improper - pairs that are
// improper should be sorted less than the proper one.
(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()
}
(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()
}
_ => false,
}
}
for p1 in &exhaustive_pairs_deck() {
for p2 in &exhaustive_pairs_deck() {
for p1 in exhaustive_pairs_deck() {
for p2 in exhaustive_pairs_deck() {
// TEST: For any two pair we expect them to have the
// `expected_ordering_relation`.
assert!(expected_ordering_relation(p1, p2));
assert!(expected_ordering_relation(&p1, &p2));
}
}
}
#[test]
fn footstool() {
let pairs = exhaustive_pairs_deck();
for p1 in &pairs {
for p2 in &pairs {
let (p1_on_p2, p2_on_p1) = test_non_reflexivity(p1, p2);
for p1 in exhaustive_pairs_deck() {
for p2 in exhaustive_pairs_deck() {
let (p1_on_p2, p2_on_p1) = test_non_reflexivity(&p1, &p2);
let (hc1_on_hc2, hc2_on_hc1) = {
let [high_card_1, high_card_2] =
[p1.1, p2.1].map(Single::new).map(|x| {