def test_1_remove_card(self): '''basic test on remove only one pair ''' c1 = hw5_cards_ec2.Card(0, 1) c2 = hw5_cards_ec2.Card(0, 2) c3 = hw5_cards_ec2.Card(1, 2) hand1 = [c1, c2, c3] h1 = hw5_cards_ec2.Hand(hand1) h1.remove_pairs() self.assertEqual(h1.init_cards[0], c1)
def testPairs(self): """Give a variety of 5-card hands, check the number of cards in each hand after pairs are removed """ c1 = hw5_cards_ec2.Card(0, 4) c2 = hw5_cards_ec2.Card(1, 4) c3 = hw5_cards_ec2.Card(2, 4) c4 = hw5_cards_ec2.Card(3, 4) c5 = hw5_cards_ec2.Card(0, 5) c6 = hw5_cards_ec2.Card(1, 5) c7 = hw5_cards_ec2.Card(3, 6) c8 = hw5_cards_ec2.Card(2, 7) c9 = hw5_cards_ec2.Card(3, 8) two_pair = [c1, c2, c7, c6, c5] full_house = [c1, c2, c3, c6, c5] #Check if there is three of a kind no_pair = [c1, c5, c7, c8, c9] four_of_a_kind = [c1, c2, c3, c4, c5] h1 = hw5_cards_ec2.Hand(two_pair) h2 = hw5_cards_ec2.Hand(full_house) h3 = hw5_cards_ec2.Hand(no_pair) h4 = hw5_cards_ec2.Hand(four_of_a_kind) h1.remove_pairs() h2.remove_pairs() h3.remove_pairs() h4.remove_pairs() self.assertEqual(len(h1.cards), 1) self.assertEqual(len(h2.cards), 1) self.assertEqual(len(h3.cards), 5) self.assertEqual(len(h4.cards), 1)
def test_3_remove_card(self): '''test on remove two pairs ''' c1 = hw5_cards_ec2.Card(0, 1) c2 = hw5_cards_ec2.Card(0, 2) c3 = hw5_cards_ec2.Card(1, 2) c4 = hw5_cards_ec2.Card(2, 3) c5 = hw5_cards_ec2.Card(3, 3) hand1 = [c1, c2, c3, c4, c5] hand2 = [c1] h1 = hw5_cards_ec2.Hand(hand1) h1.remove_pairs() self.assertEqual(h1.init_cards[0], c1)
def test_q2(self): ''' 1. fill in your test method for question 1: Test that if you create a card instance with suit 1, its suit_name will be "Clubs" 2. remove the pass command 3. uncomment the return command and 3b. change X, Y to the values from your assert statement ### please note: normally unit test methods do not have return statements. But returning will allow for unit testing of your unit test, and allow you to check your answer with the autograder. This is optional today. ''' self.assertEqual(hw5_cards_ec2.Card(1, 12).suit_name, 'Clubs') return hw5_cards_ec2.Card(1, 12).suit_name, 'Clubs'
def test_2_remove_card(self): '''test on remove a pair from 4 ''' c1 = hw5_cards_ec2.Card(0, 1) c2 = hw5_cards_ec2.Card(0, 2) c3 = hw5_cards_ec2.Card(1, 2) c4 = hw5_cards_ec2.Card(2, 2) c5 = hw5_cards_ec2.Card(3, 2) hand1 = [c1, c2, c3, c4, c5] hand2 = [c1, c2, c3] h1 = hw5_cards_ec2.Hand(hand1) h1.remove_pairs() self.assertEqual(h1.init_cards[0], c1) self.assertEqual(h1.init_cards[1], c2) self.assertEqual(h1.init_cards[2], c3)
def test_q3(self): ''' 1. fill in your test method for question 3: Test that if you invoke the __str__ method of a card instance that is created with suit=3, rank=13, it returns the string "King of Spades" 2. remove the pass command 3. uncomment the return command and 3b. change X, Y to the values from your assert statement ### please note: normally unit test methods do not have return statements. But returning will allow for unit testing of your unit test, and allow you to check your answer with the autograder. This is optional today. ''' self.assertEqual(hw5_cards_ec2.Card(3, 13).__str__(), 'King of Spades') return hw5_cards_ec2.Card(3, 13).__str__(), 'King of Spades'
def testremovePairs(self): c1 = hw5_cards_ec2.Card(suit=0, rank=2) c2 = hw5_cards_ec2.Card(suit=1, rank=2) init_list = [c1, c2] #2, 2 hand1 = hw5_cards_ec2.Hand(init_list) hand1.remove_pairs() self.assertEqual(len(hand1.init_card), 0) c3 = hw5_cards_ec2.Card(suit=2, rank=2) init_list = [c1, c2, c3] #2, 2, 2 hand2 = hw5_cards_ec2.Hand(init_list) hand2.remove_pairs() self.assertEqual(len(hand2.init_card), 1) c4 = hw5_cards_ec2.Card(suit=3, rank=2) c5 = hw5_cards_ec2.Card(suit=3, rank=5) c6 = hw5_cards_ec2.Card(suit=1, rank=5) c7 = hw5_cards_ec2.Card(suit=2, rank=5) init_list = [c1, c2, c3, c4, c5, c6, c7] # 2, 2, 2, 2, 5, 5, 5 hand2 = hw5_cards_ec2.Hand(init_list) hand2.remove_pairs() self.assertEqual(len(hand2.init_card), 1)
def test_construct_Card(self): c1 = hw5_cards_ec2.Card(0, 2) c2 = hw5_cards_ec2.Card(1, 1) self.assertEqual(c1.suit, 0) self.assertEqual(c1.suit_name, "Diamonds") self.assertEqual(c1.rank, 2) self.assertEqual(c1.rank_name, "2") self.assertIsInstance(c1.suit, int) self.assertIsInstance(c1.suit_name, str) self.assertIsInstance(c1.rank, int) self.assertIsInstance(c1.rank_name, str) self.assertEqual(c2.suit, 1) self.assertEqual(c2.suit_name, "Clubs") self.assertEqual(c2.rank, 1) self.assertEqual(c2.rank_name, "Ace")
def test_initialize_Hand(self): ''' Test that a hand is initialized properly ''' c1 = hw5_cards_ec2.Card(0, 2) c2 = hw5_cards_ec2.Card(1, 1) c3 = hw5_cards_ec2.Card(2, 12) c4 = hw5_cards_ec2.Card(3, 6) c5 = hw5_cards_ec2.Card(0, 13) h = hw5_cards_ec2.Hand([c1, c2, c3, c4, c5]) # Initialize a hand with five cards self.assertIsInstance(h.init_cards, list) # check the type of the Instance Attribute self.assertEqual(len(h.init_cards), 5) # check the number of cards self.assertEqual(type(h.init_cards[0]), hw5_cards_ec2.Card) # check the type of the card self.assertEqual(str(h.init_cards[1]), str(c2)) # check if the card corresponds to the input
def test_remove_pairs(self): # construct hand_1, instance of Hand cards_already_in_hands = \ [hw5_cards_ec2.Card(0, 1), hw5_cards_ec2.Card(1, 4), hw5_cards_ec2.Card(3, 11), hw5_cards_ec2.Card(3, 12)] hand_1 = hw5_cards_ec2.Hand(cards_already_in_hands) ## now there are no pairs len_before_remove_pairs = len(hand_1.init_card) str_list_before = [] for card in hand_1.init_card: str_list_before.append(card.__str__()) hand_1.remove_pairs() len_after_remove_pairs = len(hand_1.init_card) str_list_after = [] for card in hand_1.init_card: str_list_after.append(card.__str__()) self.assertEqual(len_before_remove_pairs, len_after_remove_pairs) # make sure equal length for element in str_list_after: self.assertTrue(element in str_list_before) # make sure str_list_before contains str_list_after for element in str_list_after: self.assertTrue(element in str_list_before) # make sure str_list_after contains str_list_before # thus the two list are the same ## then we add pairs to hand_1 hand_1.add_card(hw5_cards_ec2.Card(1, 1)) hand_1.add_card(hw5_cards_ec2.Card(2, 1)) # we have 3 Aces now hand_1.add_card(hw5_cards_ec2.Card(0, 12)) hand_1.add_card(hw5_cards_ec2.Card(1, 12)) hand_1.add_card(hw5_cards_ec2.Card(2, 12)) # we have 4 Queens now hand_1.add_card(hw5_cards_ec2.Card(1, 11)) # we have 2 Jacks now # now the cards in hand_1 in 10, with 4 pairs: 4 Q, 2 J and 2 A # remove pairs random.seed(10) hand_1.remove_pairs() # now we will only have "4 of Clubs" and one of the Aces in hand # test we only have two cards self.assertEqual(len(hand_1.init_card), 2) # test the two cards are exactly "4 of Club" and one of the Aces str_card_in_hand = [] for card in hand_1.init_card: str_card_in_hand.append(card.__str__()) ace_str_list = ['Ace of Diamonds', 'Ace of Clubs', 'Ace of Hearts'] random.seed(10) ace_st_remains = ace_str_list[random.randint(0,2)] self.assertTrue('4 of Clubs' in str_card_in_hand) self.assertTrue(ace_st_remains in str_card_in_hand)
def test_q8(self): ''' 1. fill in your test method for question 8: Test that if you invoke the replace_card method with a card that is already in the deck, the deck size is not affected.(The function must silently ignore it if you try to add a card that’s already in the deck) 2. remove the pass command 3. uncomment the return command and 3b. change X, Y to the values from your assert statement ### please note: normally unit test methods do not have return statements. But returning will allow for unit testing of your unit test, and allow you to check your answer with the autograder. This is optional today. ''' deck = hw5_cards_ec2.Deck() deck.replace_card(hw5_cards_ec2.Card(0, 1)) self.assertEqual(len(deck.cards), 52) return len(deck.cards), 52
def test_remove_pairs(self): # Queen of Spades, King of Diamonds, 6 of Hearts, 5 of Clubs, 3 of Spades, 3 of Hearts, 5 of Hearts # Card is not init_cards = [ hw5_cards_ec2.Card(3, 12), hw5_cards_ec2.Card(0, 13), hw5_cards_ec2.Card(2, 6), hw5_cards_ec2.Card(1, 5), hw5_cards_ec2.Card(3, 3), hw5_cards_ec2.Card(2, 3), hw5_cards_ec2.Card(2, 5) ] hand = hw5_cards_ec2.Hand(init_cards) length0 = len(hand.cards) card_str_0 = [card.__str__() for card in hand.cards] hand.remove_pairs() length1 = len(hand.cards) card_str_1 = [card.__str__() for card in hand.cards] # card length is shortened self.assertEqual(length0 - 4, length1, msg="Test if the pairs are removed") self.assertIn("5 of Clubs", card_str_0) self.assertIn("5 of Hearts", card_str_0) self.assertIn("3 of Spades", card_str_0) self.assertIn("3 of Hearts", card_str_0) # remove the correct card self.assertTrue("5 of Clubs" not in card_str_1) self.assertTrue("5 of Hearts" not in card_str_1) self.assertTrue("3 of Spades" not in card_str_1) self.assertTrue("3 of Hearts" not in card_str_1) # nothing to remove when there are no pairs hand.remove_pairs() length2 = len(hand.cards) self.assertEqual( length1, length2, msg= "Test if the length is equal when there is no pairs to be removed")
def testAddAndRemove(self): ''' Test that add_card( ) and remove_card( ) behave as specified. ''' c1 = hw5_cards_ec2.Card(0, 2) c2 = hw5_cards_ec2.Card(1, 1) c3 = hw5_cards_ec2.Card(2, 12) c4 = hw5_cards_ec2.Card(3, 6) c5 = hw5_cards_ec2.Card(0, 13) c6 = hw5_cards_ec2.Card(1, 3) list_init_cards = [c1, c2, c3, c4, c5] h1 = hw5_cards_ec2.Hand( list_init_cards) # Initialize a hand with five cards h2 = hw5_cards_ec2.Hand( list_init_cards) # Initialize a hand with five cards num_init_cards = len(h1.init_cards) # Test add_card() h1.add_card(c3) # Add the card that is already in the hand self.assertEqual( len(h1.init_cards), num_init_cards) # Check if the number of cards does not change h1.add_card(c6) # Add a new card self.assertEqual( len(h1.init_cards), num_init_cards + 1) # Check if the number of cards has increased by one self.assertTrue(str(c6) in [str(card) for card in h1.init_cards ]) # Check if the new card is in the hand # Test remove_card() h2.remove_card(c6) # Remove a card that is not in the hand self.assertEqual( len(h2.init_cards), num_init_cards) # Check if the number of cards does not change removed_card = h2.remove_card( c4) # Remove the card that is in the hand self.assertEqual( len(h2.init_cards), num_init_cards - 1) # Check if the number of cards has decreased by one self.assertEqual(str(removed_card), str(c4)) # Check if the removed card is correct
import hw5_cards_ec2 c1 = hw5_cards_ec2.Card(1, 1) c2 = hw5_cards_ec2.Card(2, 2) print(c1) print(c2) h1 = hw5_cards_ec2.Hand([c1, c2]) d1 = hw5_cards_ec2.Deck() hands = d1.deal(5, 12) print(len(d1.cards)) for x in hands: print("HanD!") for card in x.cards: print(card)