From 53dce72dcab77a91ee28036176601a1ffee5c91e Mon Sep 17 00:00:00 2001 From: Gianluigi <1568018+davassi@users.noreply.github.com> Date: Sat, 16 Sep 2023 18:22:28 +0200 Subject: [PATCH] Implemented Macros --- src/card.rs | 11 ++++++----- src/match_evaluator.rs | 17 +++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/card.rs b/src/card.rs index 85d154f..6b9f714 100644 --- a/src/card.rs +++ b/src/card.rs @@ -170,7 +170,7 @@ impl Hand { &self.hand } - /// An handy constructor for tests + /// An handy constructor for tests and macros /// pub fn new(hand: [Card; 5]) -> Hand { Hand { hand } @@ -270,10 +270,11 @@ macro_rules! newcard { }; } -macro_rules! newhand { +#[macro_export] +macro_rules! hand { ($c:expr,$c1:expr,$c2:expr,$c3:expr,$c4:expr) => { - [newcard![$c],newcard![$c1],newcard![$c2],newcard![$c3],newcard![$c4]] + Hand::new([newcard![$c],newcard![$c1],newcard![$c2],newcard![$c3],newcard![$c4]]) }; } @@ -299,7 +300,7 @@ mod tests { #[test] fn test_try_from_valid_hand() { - let hand = newhand!["Ad","Kd","Qd","Jd","10d"]; - assert_eq!(hand[0], Card::new(14, Suit::Diamonds)); + let hand = hand!["Ad","Kd","Qd","Jd","10d"]; + assert_eq!(hand.hand[0], Card::new(14, Suit::Diamonds)); } } \ No newline at end of file diff --git a/src/match_evaluator.rs b/src/match_evaluator.rs index 29e720a..b3a82c9 100644 --- a/src/match_evaluator.rs +++ b/src/match_evaluator.rs @@ -40,24 +40,21 @@ impl MatchHandEvaluator { } } - +macro_rules! assert_rank { + ($hand:expr, $rank:expr) => { + assert_eq!(MatchHandEvaluator::slow_eval(&mut $hand), $rank); + }; +} #[cfg(test)] mod test { use super::MatchHandEvaluator; use crate::card::{Card, Hand, Rank, Suit, self}; use crate::newcard; + use crate::hand; #[test] fn rank_royal_flush() { - let cards = [ - newcard!["Ah"], - newcard!["Kh"], - newcard!["Jh"], - newcard!["Qh"], - newcard!["10h"], - ]; - let mut hand = Hand::new(cards); - assert_eq!(MatchHandEvaluator::slow_eval(&mut hand), Rank::RoyalFlush); + assert_rank!(hand!["Ad","Kd","Qd","Jd","10d"], Rank::RoyalFlush); } }