//! Invalid queries are faults, never panics. use rechner::card::Card; use rechner::equity::monte_carlo::estimate; use rechner::equity::query::Query; use rechner::equity::villain_range::VillainRange; use rechner::fault::Fault; use rechner::rng::Rng; use rechner::variant::{rules, Variant}; fn cs(text: &str) -> Vec { 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 = (0..10).map(|_| VillainRange::Any).collect(); assert!(matches!(fault(Variant::Plo5, "Ah Kh Qh Jh Th", "", &ten_any, 10), Fault::Exhausted(_))); let thirty: Vec = (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 = (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 = (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()); }