120 lines
3.8 KiB
Rust
120 lines
3.8 KiB
Rust
//! Invalid input becomes a `Fault`, never a panic. Same checks for `read` and `glance`.
|
|
use pokerfreak::card::Card;
|
|
use pokerfreak::fault::Fault;
|
|
use pokerfreak::read::glance::glance;
|
|
use pokerfreak::read::hand_read::read;
|
|
use pokerfreak::variant::{rules, Variant};
|
|
|
|
fn cs(text: &str) -> Vec<Card> {
|
|
Card::parse_list(text).expect("cards")
|
|
}
|
|
|
|
fn both(variant: Variant, hole: &str, board: &str) -> (Result<(), Fault>, Result<(), Fault>) {
|
|
let r = rules(variant);
|
|
(
|
|
read(&r, &cs(hole), &cs(board)).map(|_| ()),
|
|
glance(&r, &cs(hole), &cs(board)).map(|_| ()),
|
|
)
|
|
}
|
|
|
|
fn invalid(result: &Result<(), Fault>) -> bool {
|
|
matches!(result, Err(Fault::Invalid { .. }))
|
|
}
|
|
|
|
#[test]
|
|
fn board_with_two_cards_is_invalid() {
|
|
let (a, b) = both(Variant::Nlhe, "Ah Kd", "7c 2d");
|
|
assert!(invalid(&a) && invalid(&b));
|
|
}
|
|
|
|
#[test]
|
|
fn empty_board_is_invalid() {
|
|
let (a, b) = both(Variant::Nlhe, "Ah Kd", "");
|
|
assert!(invalid(&a) && invalid(&b));
|
|
}
|
|
|
|
#[test]
|
|
fn board_with_six_cards_is_invalid() {
|
|
let (a, b) = both(Variant::Nlhe, "Ah Kd", "7c 2d 3s 4h 9c Td");
|
|
assert!(invalid(&a) && invalid(&b));
|
|
}
|
|
|
|
#[test]
|
|
fn holdem_with_one_or_three_hole_cards_is_invalid() {
|
|
for hole in ["Ah", "Ah Kd Qs", ""] {
|
|
let (a, b) = both(Variant::Nlhe, hole, "7c 2d 3s");
|
|
assert!(invalid(&a) && invalid(&b), "{hole}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn plo5_needs_exactly_five_hole_cards() {
|
|
for hole in ["Ah Kd Qs Jc", "Ah Kd Qs Jc Th 9h"] {
|
|
let (a, b) = both(Variant::Plo5, hole, "7c 2d 3s");
|
|
assert!(invalid(&a) && invalid(&b), "{hole}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn holdem_hand_in_plo5_is_invalid() {
|
|
let (a, b) = both(Variant::Plo5, "Ah Kd", "7c 2d 3s");
|
|
assert!(invalid(&a) && invalid(&b));
|
|
}
|
|
|
|
#[test]
|
|
fn duplicate_between_hole_and_board_is_inconsistent() {
|
|
let (a, b) = both(Variant::Nlhe, "Ah Kd", "Ah 2d 3s");
|
|
assert!(matches!(a, Err(Fault::Inconsistent(_))));
|
|
assert!(matches!(b, Err(Fault::Inconsistent(_))));
|
|
}
|
|
|
|
#[test]
|
|
fn duplicate_inside_hole_or_board_is_inconsistent() {
|
|
let (a, b) = both(Variant::Nlhe, "Ah Ah", "7c 2d 3s");
|
|
assert!(matches!(a, Err(Fault::Inconsistent(_))) && matches!(b, Err(Fault::Inconsistent(_))));
|
|
let (a, b) = both(Variant::Nlhe, "Ah Kd", "7c 7c 3s");
|
|
assert!(matches!(a, Err(Fault::Inconsistent(_))) && matches!(b, Err(Fault::Inconsistent(_))));
|
|
}
|
|
|
|
#[test]
|
|
fn card_below_the_short_deck_is_invalid() {
|
|
let (a, b) = both(Variant::ShortDeck, "Ah 5d", "7c Kd 9s");
|
|
assert!(invalid(&a) && invalid(&b));
|
|
let (a, b) = both(Variant::ShortDeck, "Ah Kd", "7c 2d 9s");
|
|
assert!(invalid(&a) && invalid(&b));
|
|
}
|
|
|
|
#[test]
|
|
fn six_is_the_lowest_short_deck_card() {
|
|
let (a, b) = both(Variant::ShortDeck, "Ah 6d", "7c Kd 9s");
|
|
assert!(a.is_ok() && b.is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn valid_inputs_pass_in_every_variant() {
|
|
assert!(both(Variant::Nlhe, "Ah Kd", "7c 2d 3s").0.is_ok());
|
|
assert!(both(Variant::Plo5, "Ah Kd Qs Jc 9h", "7c 2d 3s 4h 8d").0.is_ok());
|
|
assert!(both(Variant::KPoker, "Ah Kd", "7c 2d 3s 4h").0.is_ok());
|
|
assert!(both(Variant::Aof, "Ah Kd", "7c 2d 3s").0.is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn arbitrary_card_lists_never_panic() {
|
|
use pokerfreak::card::deck;
|
|
use pokerfreak::rng::Rng;
|
|
let mut rng = Rng::seeded(99);
|
|
for variant in Variant::ALL {
|
|
let r = rules(variant);
|
|
for i in 0..300usize {
|
|
let (hole_len, board_len) = (i % 8, (i / 8) % 8);
|
|
// Duplicates and short-deck-foreign cards are possible on purpose: draw from the full deck twice.
|
|
let (cards, next) = deck::draw(&deck::full(), hole_len + board_len, rng).expect("draw");
|
|
rng = next;
|
|
let (hole, board) = cards.split_at(hole_len);
|
|
let a = read(&r, hole, board);
|
|
let b = glance(&r, hole, board);
|
|
assert_eq!(a.is_ok(), b.is_ok(), "{variant:?} {hole:?} {board:?}");
|
|
}
|
|
}
|
|
}
|