Files
pokerfreak/tests/equity_faults.rs
T
2026-10-28 11:48:44 +01:00

84 lines
3.8 KiB
Rust

//! Invalid queries are faults, never panics.
use pokerfreak::card::Card;
use pokerfreak::equity::monte_carlo::estimate;
use pokerfreak::equity::query::Query;
use pokerfreak::equity::villain_range::VillainRange;
use pokerfreak::fault::Fault;
use pokerfreak::rng::Rng;
use pokerfreak::variant::{rules, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
fn fault(variant: Variant, hero: &str, board: &str, villains: &[VillainRange], trials: u32) -> Fault {
let (hero, board) = (cs(hero), cs(board));
let query = Query { rules: rules(variant), hero: &hero, board: &board, villains, trials };
estimate(&query, Rng::seeded(1)).expect_err("expected a fault")
}
const ANY: [VillainRange; 1] = [VillainRange::Any];
#[test]
fn wrong_hero_card_count_is_invalid() {
assert!(matches!(fault(Variant::Nlhe, "Ah Kh Qh", "", &ANY, 10), Fault::Invalid { .. }));
assert!(matches!(fault(Variant::Nlhe, "Ah", "", &ANY, 10), Fault::Invalid { .. }));
assert!(matches!(fault(Variant::Plo5, "Ah Kh Qh Jh", "", &ANY, 10), Fault::Invalid { .. }));
assert!(matches!(fault(Variant::Nlhe, "", "", &ANY, 10), Fault::Invalid { .. }));
}
#[test]
fn wrong_exact_villain_card_count_is_invalid() {
let villains = [VillainRange::Exact(cs("2c 3c 4c"))];
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "", &villains, 10), Fault::Invalid { .. }));
}
#[test]
fn duplicate_cards_are_inconsistent() {
assert!(matches!(fault(Variant::Nlhe, "Ah Ah", "", &ANY, 10), Fault::Inconsistent(_)));
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "Ah 2c 3d", &ANY, 10), Fault::Inconsistent(_)));
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "2c 2c 3d", &ANY, 10), Fault::Inconsistent(_)));
}
#[test]
fn exact_villain_clashing_with_dead_cards_is_inconsistent() {
let hero_clash = [VillainRange::Exact(cs("Ah 2c"))];
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "", &hero_clash, 10), Fault::Inconsistent(_)));
let board_clash = [VillainRange::Exact(cs("2c 3c"))];
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "2c 7d 8d", &board_clash, 10), Fault::Inconsistent(_)));
let twins = [VillainRange::Exact(cs("2c 3c")), VillainRange::Exact(cs("3c 4c"))];
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "", &twins, 10), Fault::Inconsistent(_)));
}
#[test]
fn board_sizes_other_than_0_3_4_5_are_invalid() {
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "2c 3d 4h 5s 6c 7d", &ANY, 10), Fault::Invalid { .. }));
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "2c", &ANY, 10), Fault::Invalid { .. }));
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "2c 3d", &ANY, 10), Fault::Invalid { .. }));
}
#[test]
fn zero_trials_is_invalid() {
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "", &ANY, 0), Fault::Invalid { .. }));
}
#[test]
fn deck_exhaustion_is_reported() {
let ten_any: Vec<VillainRange> = (0..10).map(|_| VillainRange::Any).collect();
assert!(matches!(fault(Variant::Plo5, "Ah Kh Qh Jh Th", "", &ten_any, 10), Fault::Exhausted(_)));
let thirty: Vec<VillainRange> = (0..30).map(|_| VillainRange::Any).collect();
assert!(matches!(fault(Variant::Nlhe, "Ah Kh", "", &thirty, 10), Fault::Exhausted(_)));
}
#[test]
fn exactly_enough_cards_is_fine() {
// 5 hero + 9 * 5 villains = 50, board needs 5 more but only 2 remain: exhausted.
let nine: Vec<VillainRange> = (0..9).map(|_| VillainRange::Any).collect();
assert!(matches!(fault(Variant::Plo5, "Ah Kh Qh Jh Th", "", &nine, 10), Fault::Exhausted(_)));
// 5 hero + 8 * 5 villains = 45, 7 left for a 5-card board.
let eight: Vec<VillainRange> = (0..8).map(|_| VillainRange::Any).collect();
let (hero, board) = (cs("Ah Kh Qh Jh Th"), cs(""));
let query = Query { rules: rules(Variant::Plo5), hero: &hero, board: &board, villains: &eight, trials: 20 };
assert!(estimate(&query, Rng::seeded(1)).is_ok());
}