Files
pokerfreak/tests/hand_five_cards.rs
T
mmintel 04d5251acf Welt ohne fremden Namen: Pruefordner vollstaendig mitkopieren, Engine nur im eigenen Baum
Die Welt nannte 'pokerfreak' nur noch in zwei Harness-Zeilen - der Rest waren veraltete
Rust-Pruefungen im Pruefordner, die der Aufbau nie auffrischte.
2026-11-01 23:37:11 +01:00

118 lines
3.7 KiB
Rust

//! Five-card evaluation: categories, kickers, wheel, ordering.
//! Category ranking order: standard poker hand ranking (Wikipedia "List of poker hands").
use rechner::card::Card;
use rechner::hand::category::Category;
use rechner::hand::five::evaluate;
use rechner::hand::strength::Strength;
fn s(text: &str) -> Strength {
let cards = Card::parse_list(text).expect("cards");
let five: [Card; 5] = cards.try_into().expect("five cards");
evaluate(&five)
}
#[test]
fn recognises_every_category() {
assert_eq!(s("Ah Kd 9c 5s 3d").category, Category::HighCard);
assert_eq!(s("Ah Ad 9c 5s 3d").category, Category::Pair);
assert_eq!(s("Ah Ad 9c 9s 3d").category, Category::TwoPair);
assert_eq!(s("Ah Ad Ac 9s 3d").category, Category::ThreeOfAKind);
assert_eq!(s("5h 6d 7c 8s 9d").category, Category::Straight);
assert_eq!(s("Ah 9h 7h 5h 3h").category, Category::Flush);
assert_eq!(s("Ah Ad Ac 9s 9d").category, Category::FullHouse);
assert_eq!(s("Ah Ad Ac As 3d").category, Category::FourOfAKind);
assert_eq!(s("5h 6h 7h 8h 9h").category, Category::StraightFlush);
}
#[test]
fn categories_are_totally_ordered_in_poker_order() {
let ladder = [
s("Ah Kd 9c 5s 3d"),
s("2h 2d 9c 5s 3d"),
s("2h 2d 3c 3s 4d"),
s("2h 2d 2c 5s 3d"),
s("2h 3d 4c 5s 6d"),
s("2h 4h 7h 9h Jh"),
s("2h 2d 2c 5s 5d"),
s("2h 2d 2c 2s 3d"),
s("2h 3h 4h 5h 6h"),
];
assert!(ladder.windows(2).all(|w| w[0] < w[1]));
}
#[test]
fn wheel_is_the_lowest_straight_and_ace_plays_low() {
let wheel = s("Ah 2d 3c 4s 5d");
assert_eq!(wheel.category, Category::Straight);
assert!(wheel < s("2h 3d 4c 5s 6d"));
assert!(wheel > s("Ah Ad Ac Ks Qd"));
}
#[test]
fn broadway_is_the_highest_straight_and_no_wraparound() {
assert!(s("Th Jd Qc Ks Ad") > s("9h Td Jc Qs Kd"));
assert_eq!(s("Qh Kd Ac 2s 3d").category, Category::HighCard);
assert_eq!(s("Kh Ad 2c 3s 4d").category, Category::HighCard);
}
#[test]
fn steel_wheel_is_a_straight_flush_below_a_six_high_one() {
let steel = s("Ah 2h 3h 4h 5h");
assert_eq!(steel.category, Category::StraightFlush);
assert!(steel < s("2h 3h 4h 5h 6h"));
assert!(s("Th Jh Qh Kh Ah") > s("9h Th Jh Qh Kh"));
}
#[test]
fn kickers_decide_within_a_category() {
assert!(s("Ah Ad Kc 5s 3d") > s("Ah Ad Qc Js Td"));
assert!(s("Ah Ad Kc 5s 3d") > s("Ah Ad Kc 4s 3d"));
assert!(s("Ah Ad Kc Qs Jd") > s("Kh Kd Ac Qs Jd"));
assert!(s("Ah Kd Qc Js 9d") > s("Ah Kd Qc Js 8d"));
}
#[test]
fn two_pair_compares_high_pair_then_low_pair_then_kicker() {
assert!(s("Ah Ad 3c 3s 2d") > s("Kh Kd Qc Qs Jd"));
assert!(s("Kh Kd Qc Qs 2d") > s("Kh Kd Jc Js Ad"));
assert!(s("Kh Kd Qc Qs Ad") > s("Kh Kd Qc Qs 2d"));
}
#[test]
fn full_house_compares_trips_before_pair() {
assert!(s("3h 3d 3c 2s 2d") > s("2h 2d 2c As Ad"));
assert!(s("3h 3d 3c As Ad") > s("3h 3d 3c Ks Kd"));
}
#[test]
fn quads_compare_rank_then_kicker() {
assert!(s("3h 3d 3c 3s 2d") > s("2h 2d 2c 2s Ad"));
assert!(s("3h 3d 3c 3s Ad") > s("3h 3d 3c 3s Kd"));
}
#[test]
fn flush_compares_card_by_card() {
assert!(s("Ah Kh 9h 5h 3h") > s("Ah Kh 9h 5h 2h"));
assert!(s("Ah Kh 9h 5h 3h") > s("Qh Jh Th 9h 7h"));
}
#[test]
fn order_of_cards_and_suits_do_not_matter() {
assert_eq!(s("Ah Ad 9c 9s 3d"), s("3c 9d Ad 9h As"));
assert_eq!(s("Ah Kd 9c 5s 3d"), s("3h 5d 9s Kc Ac"));
assert_eq!(s("5h 6d 7c 8s 9d"), s("9c 8d 7s 6h 5c"));
}
#[test]
fn identical_ranks_in_different_suits_tie() {
assert_eq!(s("Ah Kd 9c 5s 3d"), s("Ac Kh 9s 5d 3c"));
}
#[test]
fn weakest_and_strongest_hands_bound_everything() {
let worst = s("7h 5d 4c 3s 2d");
let best = s("Th Jh Qh Kh Ah");
assert!(worst < best);
assert_eq!(worst.category, Category::HighCard);
}