def test_deal(self): ''' Test that deal() works as specified ''' # Case 1: 5 hands, 5 cards per hand d1 = hw5_cards_ec2.Deck() list_hands = d1.deal(num_hands=5, hand_size=5) self.assertEqual(type(list_hands[0]), hw5_cards_ec2.Hand) # Check if the element is Hand self.assertEqual(len(list_hands), 5) # Check the number of hands self.assertEqual(len(list_hands[0].init_cards), 5) # Check the hand size self.assertEqual(len(d1.cards), 52 - 5 * 5) # Check the number of cards in the deck # Case 2: 5 hands, all of the cards are dealt (i.e. hand_size=-1) d2 = hw5_cards_ec2.Deck() list_hands = d2.deal(num_hands=5, hand_size=-1) self.assertEqual(type(list_hands[0]), hw5_cards_ec2.Hand) # Check if the element is Hand self.assertEqual(len(list_hands), 5) # Check the number of hands list_hand_size = [len(hand.init_cards) for hand in list_hands] correct_list_hand_size = [11, 11, 10, 10, 10] self.assertTrue( list_hand_size == correct_list_hand_size) # Check the hand size self.assertEqual(len(d2.cards), 0) # Check the number of cards in the deck
def test_remove_pairs(self): ''' Test that remove_pairs() works as specified ''' # Case 1: 13 pairs & 0 unpaired cards d1 = hw5_cards_ec2.Deck() h1 = hw5_cards_ec2.Hand(d1.deal_hand(13 * 2)) h1.remove_pairs() self.assertEqual(len(h1.init_cards), 0) # Case 2: 13 pairs & 6 unpaired cards (7 pairs & 6 three of a kind) d2 = hw5_cards_ec2.Deck() h2 = hw5_cards_ec2.Hand(d2.deal_hand(13 * 2 + 6)) h2.remove_pairs() self.assertEqual(len(h2.init_cards), 6) # Case 3: no pairs & 7 unpaired cards d2 = hw5_cards_ec2.Deck() h2 = hw5_cards_ec2.Hand(d2.deal_hand(7)) h2.remove_pairs() self.assertEqual(len(h2.init_cards), 7) # Case 4: 7 four of a kind & 6 three of a kind d3 = hw5_cards_ec2.Deck() h3 = hw5_cards_ec2.Hand(d3.deal_hand(13 * 3 + 7)) h3.remove_pairs() self.assertEqual(len(h3.init_cards), 6)
def test_deal(self): #Test when there are enough cards to deal num_hands = 5 num_cards = 8 deck = hw5_cards_ec2.Deck() handlist = deck.deal(num_hands, num_cards) self.assertEqual(len(handlist), 5) self.assertIsInstance(handlist[0], hw5_cards_ec2.Hand) self.assertEqual(len(handlist[-1].cards), 8) #Test when there are not enough cards to distribute everyone num_hands_2 = 8 num_cards_2 = 7 deck2 = hw5_cards_ec2.Deck() handlist2 = deck2.deal(num_hands_2, num_cards_2) self.assertEqual(len(handlist2), 8) self.assertIsInstance(handlist2[-1], hw5_cards_ec2.Hand) self.assertEqual(len(handlist2[0].cards), 7) self.assertEqual(len(handlist2[-1].cards), 6) self.assertEqual(len(handlist2[4].cards), 6) #Test when there num_cards == -1 num_hands_3 = 8 num_cards_3 = -1 deck3 = hw5_cards_ec2.Deck() handlist3 = deck3.deal(num_hands_3, num_cards_3) self.assertEqual(len(handlist3), 8) self.assertIsInstance(handlist3[-1], hw5_cards_ec2.Hand) self.assertEqual(len(handlist3[0].cards), 7) self.assertEqual(len(handlist3[-1].cards), 6) self.assertEqual(len(handlist3[4].cards), 6)
def test_q4(self): ''' 1. fill in your test method for question 4: Test that if you create a eck instance, it will have 52 cards in its cards instance variable 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(len(hw5_cards_ec2.Deck().cards), 52) return len(hw5_cards_ec2.Deck().cards), 52
def test_q5(self): ''' 1. fill in your test method for question 5: Test that if you invoke the deal_card method on a deck, it will return a card instance. 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.assertIsInstance(hw5_cards_ec2.Deck().deal_card(), hw5_cards_ec2.Card) return hw5_cards_ec2.Deck().deal_card(), hw5_cards_ec2.Card
def test_deal(self): deck = hw5_cards_ec2.Deck() # test (1st situation): number of hands larger than number of cards in deck hands_test_1 = deck.deal(400,-1) for hand in hands_test_1: self.assertEqual(len(hand.init_card), 1) # test (2nd situation): the second parameter is set to -1 hands_test_2 = deck.deal(20,-1) self.assertEqual(len(hands_test_2), 20) # test the number of hands self.assertEqual(len(hands_test_2[11].init_card), 3) # up to 12th hands will get 3 cards self.assertEqual(len(hands_test_2[12].init_card), 2) # from 13th, get 2 cards # test (3rd situation): # of hands X # of cards per hand <= 52 # sub_test_1: empty the deck hands_test_3 = deck.deal(4, 13) self.assertEqual(len(hands_test_3), 4) # test number of hands self.assertEqual(len(hands_test_3[3].init_card), 13) # sub_test_2: do not empty the deck hands_test_3 = deck.deal(6, 6) self.assertEqual(len(hands_test_3), 6) # test number of hands self.assertEqual(len(hands_test_3[0].init_card), 6) # test number of cards per hand self.assertEqual(len(hands_test_3[1].init_card), 6) self.assertEqual(len(hands_test_3[2].init_card), 6) self.assertEqual(len(hands_test_3[3].init_card), 6) self.assertEqual(len(hands_test_3[4].init_card), 6) self.assertEqual(len(hands_test_3[5].init_card), 6) # test (4th situation): # of hands X # of cards per hand > 52 hands_test_4 = deck.deal(3,18) self.assertEqual(len(hands_test_4), 3) self.assertEqual(len(hands_test_4[0].init_card), 18) # test number of cards per hand self.assertEqual(len(hands_test_4[1].init_card), 17) self.assertEqual(len(hands_test_4[2].init_card), 17)
def test_6_deal_deck(self): '''test on deal parts of the cards ''' d1 = hw5_cards_ec2.Deck() hand_list = d1.deal(6, 5) self.assertEqual(len(hand_list), 6) self.assertEqual(len(hand_list[0]), 5) self.assertEqual(len(d1.cards), 22)
def test_4_deal_deck(self): '''test on deal all to 4 hands(exact division) ''' d1 = hw5_cards_ec2.Deck() hand_list = d1.deal(4, -1) self.assertEqual(len(hand_list), 4) self.assertEqual(len(hand_list[0]), 13) self.assertEqual(len(d1.cards), 0)
def testDeal(self): a_deck = hw5_cards_ec2.Deck() num_hand = 3 num_card = -1 three_hands = a_deck.deal(num_card=num_card, num_hand=num_hand) self.assertEqual(len(a_deck.cards), 0) self.assertEqual(len(three_hands[0].init_card), 18) self.assertEqual(len(three_hands[1].init_card), 17) a_deck = hw5_cards_ec2.Deck() num_hand = 3 num_card = 7 three_hands = a_deck.deal(num_card=num_card, num_hand=num_hand) self.assertEqual(len(a_deck.cards), 31) self.assertEqual(len(three_hands[0].init_card), 7) self.assertEqual(len(three_hands[1].init_card), 7) self.assertEqual(len(three_hands[2].init_card), 7)
def test2(self): d1 = hw5_cards_ec2.Deck() # create a deck for test num_cards_per_hand is not -1 d1.shuffle() # shuffle the deck d2 = hw5_cards_ec2.Deck() # create a deck for test num_cards_per_hand is -1 d2.shuffle() # shuffle the deck num_hands = random.randint(1,51) # test num_cards_per_hand is not -1 num_cards = random.randint(1,52//num_hands) hand_list = d1.deal(num_hands, num_cards) for i in hand_list: self.assertEqual(len(i.cards),num_cards) # test num_cards_per_hand is -1 num_cards = 52//num_hands hand_list = d2.deal(num_hands) for i in hand_list: self.assertTrue(len(i.cards) in [num_cards, num_cards+1])
def test_q10(self): ''' basic deal test, test that deal function in deck outputs list of hands with the correct size ''' deck = hw5_cards_ec2.Deck() list_of_hands = deck.deal(num_of_hands=4, cards_per_hand=5) num_of_hands = len(list_of_hands) self.assertEqual(num_of_hands, 4) for hand in list_of_hands: self.assertEqual(len(hand), 5)
def testDeal(self): number_of_hands = [4, 5, 7, 10, 50] size_of_hands = [7, 4, -1, -1, 2] for x in range(4): d1 = hw5_cards_ec2.Deck() hands = d1.deal(number_of_hands[x], size_of_hands[x]) card_total = len(d1.cards) for hand in hands: card_total += len(hand.cards) self.assertEqual(card_total, 52)
def test1(self): d = hw5_cards_ec2.Deck() # create a deck d.shuffle() # shuffle the deck hand_size = random.randint(1,51) # generate a random hand size hand = d.deal_hand(hand_size) # create a temp hand list from deck also update the deck h = hw5_cards_ec2.Hand(hand) # create hand object h.remove_pairs() # remove pairs in this hand card_list = [i.rank for i in h.cards] # create a list of rank for rank in card_list: self.assertFalse(card_list.count(rank)==2) self.assertFalse(card_list.count(rank)==4)
def test_5_deal_deck(self): '''test on deal all to 5 hands(has remainder) ''' d1 = hw5_cards_ec2.Deck() hand_list = d1.deal(5, -1) self.assertEqual(len(hand_list), 5) self.assertEqual(len(hand_list[0]), 11) self.assertEqual(len(hand_list[1]), 11) self.assertEqual(len(hand_list[2]), 10) self.assertEqual(len(hand_list[3]), 10) self.assertEqual(len(hand_list[4]), 10) self.assertEqual(len(d1.cards), 0)
def test_q9(self): ''' test that when calling remove_pairs function with a 30-card-hand, the pairs are removed. But if three cards of the same rank is present, only two of them are removed. ''' deck = hw5_cards_ec2.Deck() hand = deck.deal_hand(30) hand = hw5_cards_ec2.remove_pairs(hand) hand_set = set() for c in hand: s = c.suit_name[0] r = c.rank_name[0] hand_set.add(tuple([r, s])) self.assertEqual(hand_set, set({('Q', 'S'), ('J', 'S'), ('1', 'S'), ('K', 'S')}))
def test_draw(self): ''' Test that draw( ) works as specified ''' d = hw5_cards_ec2.Deck() # Initialize a deck h = hw5_cards_ec2.Hand([]) # Initialize a hand with no cards h.draw(d) # Draw a card from the deck self.assertEqual(len(h.init_cards), 1) # Check if one card is added self.assertEqual( len(d.cards), 51 ) # Check if the number of cards in the deck has decreased by one (side effect) self.assertTrue( str(h.init_cards[0]) not in [str(card) for card in d.cards ]) # Check if the drawed card is not in the deck
def test_q11(self): ''' more comlicated deal test, test that deal function in deck outputs list of hands with the correct size, when the hand sizes are uneven ''' deck = hw5_cards_ec2.Deck() list_of_hands = deck.deal(num_of_hands=5, cards_per_hand=-1) num_of_hands = len(list_of_hands) self.assertEqual(num_of_hands, 5) longer_hand_count = 0 regular_hand_count = 0 for hand in list_of_hands: if len(hand) == 10: regular_hand_count += 1 elif len(hand) == 11: longer_hand_count += 1 self.assertEqual(longer_hand_count, 2) self.assertEqual(regular_hand_count, 3)
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_q6(self): ''' 1. fill in your test method for question 6: Test that if you invoke the deal_card method on a deck, the deck has one fewer cards in it afterwards. 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.deal_card() self.assertEqual(len(deck.cards), 51) return len(deck.cards), 51
def test_q7(self): ''' 1. fill in your test method for question 7: Test that if you invoke the replace_card method, the deck has one more card in it afterwards. (Please note that you want to use deal_card function first to remove a card from the deck and then add the same card back in) 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() card = deck.deal_card() deck.replace_card(card) self.assertEqual(len(deck.cards), 52) return len(deck.cards), 52
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)