open_pql/functions/
rate_hi_hand.rs

1use super::*;
2
3#[pqlfn(arg, rtn, eval)]
4pub fn rate_hi_hand(
5    text: &str,
6    game: PQLGame,
7) -> Result<PQLHiRating, RuntimeError> {
8    let c64 = to_five_cards(text)?;
9
10    Ok(match game {
11        PQLGame::Holdem | PQLGame::Omaha => eval_holdem7(c64),
12        PQLGame::ShortDeck => eval_shortdeck7(c64),
13    })
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19    use crate::*;
20
21    #[quickcheck]
22    fn test_rate_hi_hand(hbg: HandBoardGame) -> TestResult {
23        let cards = hbg.board.to_vec();
24        let c64 = Card64::from(hbg.board);
25
26        let s = cards.iter().map(std::string::ToString::to_string).join("");
27
28        let rating = match hbg.game {
29            PQLGame::Holdem | PQLGame::Omaha => eval_holdem7(c64),
30            PQLGame::ShortDeck => eval_shortdeck7(c64),
31        };
32
33        TestResult::from_bool(rating == rate_hi_hand(&s, hbg.game).unwrap())
34    }
35
36    #[test]
37    fn test_rate_hi_hand_error() {
38        let g = PQLGame::Holdem;
39
40        assert!(rate_hi_hand(" As Ks Qs Js Ts ", g).is_ok());
41
42        let s = "AsKsQsJsTs";
43        for i in 0..s.len() {
44            assert!(to_five_cards(&s[0..i]).is_err());
45        }
46
47        assert![matches!(
48            rate_hi_hand(" A Ks Qs Js Ts ", g),
49            Err(RuntimeError::RateHiHandParseFailed(_))
50        )];
51        assert![matches!(
52            rate_hi_hand(" sA Ks Qs Js Ts ", g),
53            Err(RuntimeError::RateHiHandParseFailed(_))
54        )];
55
56        assert![matches!(
57            rate_hi_hand("AsKsQsJsTs9s", g),
58            Err(RuntimeError::RateHiHandExpectedFiveCards(_))
59        )];
60
61        assert![matches!(
62            rate_hi_hand("AsAsAsAsAs", g),
63            Err(RuntimeError::RateHiHandExpectedFiveCards(_))
64        )];
65    }
66}