modes:{pair,triple}:tests: refinement

This commit is contained in:
2026-04-14 19:07:48 +01:00
committed by oreodave
parent 5d3e83d79c
commit 0bab322f3a
2 changed files with 60 additions and 37 deletions

View File

@@ -130,7 +130,7 @@ mod tests {
// TEST: Any card with one joker can be made into a valid pair. // TEST: Any card with one joker can be made into a valid pair.
let c2 = Card::make_joker(); let c2 = Card::make_joker();
let pair = Pair::new(c1, c2); let pair = Pair::new(c1, c2);
assert!(pair.is_some(), "Expected ({c1}, {c2}) to be a valid pair",); assert_ne!(pair, None, "Expected ({c1}, {c2}) to be a valid pair");
let pair = pair.unwrap(); let pair = pair.unwrap();
@@ -157,8 +157,8 @@ mod tests {
// TEST: Two cards of similar rank make a valid pair. // TEST: Two cards of similar rank make a valid pair.
let pair = { let pair = {
let pair = Pair::new(c1, c2); let pair = Pair::new(c1, c2);
assert!( assert_ne!(
pair.is_some(), pair, None,
"Expected {c1} and {c2} to form a valid pair." "Expected {c1} and {c2} to form a valid pair."
); );
pair.unwrap() pair.unwrap()

View File

@@ -111,7 +111,10 @@ impl Hash for Triple {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::card::{PlayingCard, Rank}; use crate::{
card::{PlayingCard, Rank},
zipcartesian::ZipCartesianExt,
};
use super::*; use super::*;
@@ -129,8 +132,8 @@ mod tests {
for card in PlayingCard::iter_all(0).map(Card::PlayingCard) { for card in PlayingCard::iter_all(0).map(Card::PlayingCard) {
let trip = Triple::new(card, joker, joker); let trip = Triple::new(card, joker, joker);
// TEST: Any card with two jokers is a triple // TEST: Any card with two jokers is a triple
assert!( assert_ne!(
trip.is_some(), trip, None,
"Expected ({card}, {joker}, {joker}) to make a triple" "Expected ({card}, {joker}, {joker}) to make a triple"
); );
let trip = trip.unwrap(); let trip = trip.unwrap();
@@ -147,45 +150,65 @@ mod tests {
); );
} }
for rank in Rank::iter_all() { // Iterate over all pairs of cards with similar ranks
for (c1, c2) in rank.cards().zip(rank.cards()) { for (c1, c2) in
let trip = Triple::new(c1, c2, joker); Rank::iter_all().flat_map(|r| r.cards().zip_cartesian(r.cards()))
// TEST: Any two similar rank cards with 1 joker are a {
// Triple. let trip = Triple::new(c1, c2, joker);
assert_ne!( // TEST: Any two similar rank cards with 1 joker are a
trip, None, // Triple.
"Expected ({c1}, {c2}, Joker) to make a Triple" assert_ne!(
); trip, None,
"Expected ({c1}, {c2}, Joker) to make a Triple"
);
let trip = trip.unwrap(); let trip = trip.unwrap();
// TEST: Triples formed with 1 joker are improper. // TEST: Triples formed with 1 joker are improper.
assert!(trip.is_improper(), "Expected {trip} to be improper"); assert!(trip.is_improper(), "Expected {trip} to be improper");
// TEST: 1 joker triples have Triple::0 as the joker. // TEST: 1 joker triples have Triple::0 as the joker.
assert!( assert!(
matches!(trip.0, Card::Joker(_)), matches!(trip.0, Card::Joker(_)),
"Expected {} to be a joker", "Expected {} to be a joker",
trip.0 trip.0
); );
let [c1, c2] = ordered([c1, c2]); let [c1, c2] = ordered([c1, c2]);
// TEST: Expect Triple::1 and Triple::2 to follow ordering // TEST: Expect Triple::1 and Triple::2 to follow ordering
// of c1 and c2. // of c1 and c2.
assert_eq!( assert_eq!(
[trip.1, trip.2], [trip.1, trip.2],
[c1, c2], [c1, c2],
"Expected {} = min({c1}, {c2}) and {} = max({c1}, {c2})", "Expected {} = min({c1}, {c2}) and {} = max({c1}, {c2})",
trip.1, trip.1,
trip.2, trip.2,
); );
}
} }
for (c1, (c2, c3)) in PlayingCard::iter_all(0) // Iterate over all pairs of cards with differing ranks
.zip(PlayingCard::iter_all(0).zip(PlayingCard::iter_all(0))) for (c1, c2) in Rank::iter_all()
.flat_map(|r1| {
Rank::iter_all()
.filter(move |&r2| r2 != r1)
.map(move |r2| (r1, r2))
})
.flat_map(|(r1, r2)| r1.cards().zip_cartesian(r2.cards()))
{ {
// TEST: Cannot make a triple out of 1 joker and two different rank
// cards
let trip = Triple::new(c1, c2, joker);
assert_eq!(
trip, None,
"Expected ({c1}, {c2}, {joker}) to never make a triple."
);
}
// Iterate over all triples of cards (regardless of rank)
for (c1, (c2, c3)) in PlayingCard::iter_all(0).zip_cartesian(
PlayingCard::iter_all(0).zip_cartesian(PlayingCard::iter_all(0)),
) {
let [c1, c2, c3] = [c1, c2, c3].map(Card::PlayingCard); let [c1, c2, c3] = [c1, c2, c3].map(Card::PlayingCard);
let trip = Triple::new(c1, c2, c3); let trip = Triple::new(c1, c2, c3);