def testMax(self): """Test using max() with PokerRank""" ranks = [ PokerRank.pair(Rank.QUEEN, kickers = Cards.fromString("JC 8D 4S")), PokerRank.straight(Rank.TEN), PokerRank.trips(Rank.SEVEN, kickers = Cards.fromString("AD JC")), ] max_rank = max(ranks) self.assertEqual(max_rank.getType(), PokerRank.STRAIGHT)
def testRemoveDuplicateRranks(self): """Test removeDuplicateRanks() method""" # Two 4's and one ace should be removed cards = Cards.fromString("8D 4C AH 9D 4H KC 4S AC") pruned_cards = cards.removeDuplicateRanks() self.assertIsNotNone(pruned_cards) self.assertEqual(len(pruned_cards), 5) # Nothing should be removed cards = Cards.fromString("8D 4C AH 9D 5H KC 3S JC") pruned_cards = cards.removeDuplicateRanks() self.assertIsNotNone(pruned_cards) self.assertEqual(len(pruned_cards), 8)
def testGetEightOrLower(self): """Test getEightOrLower() method""" cards = Cards.fromString("AC TH 7D 8S QD") low_cards = cards.getEightOrLower() self.assertIsNotNone(low_cards) self.assertEqual(len(low_cards), 3) self.assertListEqual(low_cards, Cards.fromString("AC 7D 8S")) cards = Cards.fromString("AC 5H 7D 8S 2D") low_cards = cards.getEightOrLower() self.assertIsNotNone(low_cards) self.assertEqual(len(low_cards), 5) self.assertListEqual(low_cards, Cards.fromString("AC 5H 7D 8S 2D")) cards = Cards.fromString("JC TH 9D KS QD") low_cards = cards.getEightOrLower() self.assertIsNotNone(low_cards) self.assertEqual(len(low_cards), 0)
def testHandToSuitedBitFields(self): """Test _handToSuitedBitFields() method.""" cards = Cards.fromString("QH 7C 4S KH 6D 7S AC") bitfield = self.ranker._handToBitField(cards) bitfields = self.ranker._handToSuitedBitFields(cards) expectedClubs = BitField() expectedClubs += Rank.SEVEN expectedClubs += Rank.ACE self.assertEqual(bitfields[Suit.CLUBS], expectedClubs, "Clubs: %s != %s" % (bitfields[Suit.CLUBS], expectedClubs)) expectedDiamonds = BitField() expectedDiamonds += Rank.SIX self.assertEqual(bitfields[Suit.DIAMONDS], expectedDiamonds, "DIAMONDS: %s != %s" % (bitfields[Suit.DIAMONDS], expectedDiamonds)) expectedHearts = BitField() expectedHearts += Rank.QUEEN expectedHearts += Rank.KING self.assertEqual(bitfields[Suit.HEARTS], expectedHearts, "HEARTS: %s != %s" % (bitfields[Suit.HEARTS], expectedHearts)) expectedSpades = BitField() expectedSpades += Rank.FOUR expectedSpades += Rank.SEVEN self.assertEqual(bitfields[Suit.SPADES], expectedSpades, "SPADES: %s != %s" % (bitfields[Suit.SPADES], expectedSpades))
def testHoldEmStartingHandRanker2(self): """Test basic ranking (pair).""" ranker = HoldEm.StartingHandRanker() rank = ranker.rankHand(Cards.fromString("5C 5D")) self.assertIsNotNone(rank) self.assertIsInstance(rank, PokerRank) self.assertNotEqual(rank, 0) self.assertEqual(rank.getType(), PokerRank.PAIR) self.assertEqual(rank.getPrimaryCardRank(), Rank.FIVE) kickers = rank.getKickerRanks() self.assertEqual(len(kickers), 0)
def testHoldEmStartingHandRanker(self): """Test basic ranking (high card).""" ranker = HoldEm.StartingHandRanker() rank = ranker.rankHand(Cards.fromString("2C 8D")) self.assertIsNotNone(rank) self.assertIsInstance(rank, PokerRank) self.assertNotEqual(rank, 0) self.assertEqual(rank.getType(), PokerRank.HIGH_CARD) self.assertEqual(rank.getPrimaryCardRank(), Rank.EIGHT) kickers = rank.getKickerRanks() self.assertEqual(len(kickers), 1) self.assertEqual(kickers[0], Rank.TWO)
def testCombinations(self): """Test basic hand combinatins.""" cards = Cards.fromString("8C 9D 7C 6S AH") self.assert_iterator(cards.combinations(2), count=10, assert_item_function=lambda i: len(i)==2) self.assert_iterator(cards.combinations(5), count=1, assert_item_function=lambda i: len(i)==5) cards.addCardsFromString("AS 2C") self.assert_iterator(cards.combinations(5), count=21, assert_item_function=lambda i: len(i)==5)
def testHandToBitField(self): """Test _handToBitField() method.""" cards = Cards.fromString("JC 8D 4S KH 9D 3C") bitfield = self.ranker._handToBitField(cards) expectedBitfield = BitField() expectedBitfield += Rank.JACK expectedBitfield += Rank.EIGHT expectedBitfield += Rank.FOUR expectedBitfield += Rank.KING expectedBitfield += Rank.NINE expectedBitfield += Rank.THREE self.assertEqual(bitfield, expectedBitfield, "%s != %s" % (bitfield, expectedBitfield))
def testHasStraight(self): """Test hasStriaght() method.""" tests = [ # cards as string, expected rank ("KC JS AC QS TH", Rank.ACE), ("KC JS 8C QS TH", None), ("KC JS AC QS TH 9S", Rank.ACE), ("KC JS QS TH 9S", Rank.KING), ("4S 3D AC 2S 5H", Rank.FIVE), ("8S 6D 5H 7S 4H", Rank.EIGHT), ("2D KH AC QD JH", None) ] for cards, rank in tests: bitfield = self.ranker._handToBitField(Cards.fromString(cards)) self.assertEqual(self.ranker._hasStraight(bitfield), rank)
def testCombinations(self): """Test basic hand combinatins.""" cards = Cards.fromString("8C 9D 7C 6S AH") count = 0 for combs in cards.combinations(2): count += 1 self.assertEquals(len(combs), 2) self.assertEquals(count, 10) count = 0 for combs in cards.combinations(5): count += 1 self.assertEquals(len(combs), 5) self.assertEquals(count, 1) cards.addCardsFromString("AS 2C") count = 0 for combs in cards.combinations(5): count += 1 self.assertEquals(len(combs), 5) self.assertEquals(count, 21)
def testKickerSort(self): """Test sorting of kickers.""" kickers = Cards.fromString("8C KS AH") rank = PokerRank.pair(Rank.QUEEN, kickers) kickerRanks = rank.getKickerRanks() self.assertEqual(kickerRanks[0], Rank.ACE, "kickerRanks[0] == %s != A" % kickerRanks[0]) self.assertEqual(kickerRanks[1], Rank.KING, "kickerRanks[1] == %s != K" % kickerRanks[1]) self.assertEqual(kickerRanks[2], Rank.EIGHT, "kickerRanks[2] == %s != 8" % kickerRanks[2]) # Now test with Ace low kickers.makeAcesLow() rank = PokerRank.pair(Rank.QUEEN, kickers) kickerRanks = rank.getKickerRanks() self.assertEqual(kickerRanks[0], Rank.KING, "kickerRanks[0] == %s != K" % kickerRanks[0]) self.assertEqual(kickerRanks[1], Rank.EIGHT, "kickerRanks[1] == %s != 8" % kickerRanks[1]) self.assertEqual(kickerRanks[2], Rank.ACE_LOW, "kickerRanks[2] == %s != A" % kickerRanks[2])
def testCreation(self): """Test basic creation of PokerRankBase.""" kickers = Cards.fromString("JC 8D 4S") rank = PokerRank(PokerRank.PAIR, primaryCard=Rank.KING, kickers=kickers) self.assertIsNotNone(rank) self.assertNotEqual(rank, 0) type = rank.getType() self.assertEqual(type, PokerRank.PAIR, "rank = (%s) %d != PAIR" % (str(type), type)) primaryRank = rank.getPrimaryCardRank() self.assertEqual(primaryRank, Rank.KING, "primary rank = %s (%d) != KING" % (str(primaryRank), primaryRank)) secondaryRank = rank.getSecondaryCardRank() # Manual test here, otherwise string creation fails on sucess if secondaryRank is not None: self.fail("rank = %s (%d) != None" % (str(secondaryRank), secondaryRank)) kickerRanks = rank.getKickerRanks() for i, card in enumerate(kickers): self.assertEqual(card.rank, kickerRanks[i])
def testSuitedBitfieldToRankBitFields(self): """Test _suitedBitfieldsToRankBitfields() method.""" cards = Cards.fromString("QH QC QS QD TC TH TD 2S 2C 2D 8H 8S 4H 4D 3S AH KS") bitfields = self.ranker._handToSuitedBitFields(cards) (singletonsBitField, pairsBitField, tripsBitField, quadsBitField) = self.ranker._suitedBitFieldsToRankedBitFields(bitfields) self.assertTrue(quadsBitField.testBit(Rank.QUEEN)) self.assertTrue(tripsBitField.testBit(Rank.TEN)) self.assertTrue(tripsBitField.testBit(Rank.TWO)) self.assertTrue(pairsBitField.testBit(Rank.EIGHT)) self.assertTrue(pairsBitField.testBit(Rank.FOUR)) self.assertTrue(singletonsBitField.testBit(Rank.THREE)) self.assertTrue(singletonsBitField.testBit(Rank.ACE)) self.assertTrue(singletonsBitField.testBit(Rank.KING)) # Check an arbitrary group of bits that shouldn't be set self.assertFalse(quadsBitField.testBit(Rank.JACK)) self.assertFalse(tripsBitField.testBit(Rank.KING)) self.assertFalse(tripsBitField.testBit(Rank.QUEEN)) self.assertFalse(pairsBitField.testBit(Rank.SEVEN)) self.assertFalse(pairsBitField.testBit(Rank.ACE)) self.assertFalse(singletonsBitField.testBit(Rank.QUEEN)) self.assertFalse(singletonsBitField.testBit(Rank.SIX)) self.assertFalse(singletonsBitField.testBit(Rank.FOUR))