Skip to content

Instantly share code, notes, and snippets.

@usaturn
Created July 4, 2012 09:35
Show Gist options
  • Save usaturn/3046367 to your computer and use it in GitHub Desktop.
Save usaturn/3046367 to your computer and use it in GitHub Desktop.
poker.pyのテストコード
# -*- coding:utf-8 -*-
import sys
import unittest
from poker import *
# グローバルに定義せずまとめる方法はある?
buta = [12, 13, 1, 2, 3]
four_cards = [13, 13, 13, 11, 13]
five_cards = [13, 13, 13, 13, 13]
fullhouse = [13, 13, 1, 1, 13]
normal_straight = [5, 3, 6, 2, 4]
one_pair = [13, 9, 13, 11, 12]
royal_straight = [1, 13, 12, 11, 10]
three_cards = [13, 13, 13, 11, 12]
two_pair = [13, 13, 10, 11, 10]
class TestCountCards(unittest.TestCase):
#テストメソッド実行毎に事前に実行する
def setUp(self):
print 'setUp'
#テストメソッド実行毎に事後に実行する
def tearDown(self):
print 'tearDown'
#ランダムなリストを出力する関数のテストコードはどう書けば良いのか?
def test_set_cards(self):
def check_cards(cards):
if 5 == len(cards):
for card in cards:
if card > 13:
return False
return True
self.assertEqual(True, check_cards(set_cards()))
self.assertEqual(True, check_cards(set_cards()))
self.assertEqual(True, check_cards(set_cards()))
self.assertEqual(True, check_cards(set_cards()))
self.assertEqual(True, check_cards(set_cards()))
self.assertEqual(True, check_cards(set_cards()))
self.assertEqual(True, check_cards(set_cards()))
self.assertEqual(True, check_cards(set_cards()))
self.assertEqual(True, check_cards(set_cards()))
def test_is_one_pair(self):
self.assertEqual(True, is_one_pair(one_pair))
self.assertEqual(False, is_one_pair(three_cards))
self.assertEqual(False, is_one_pair(four_cards))
self.assertEqual(False, is_one_pair(fullhouse))
self.assertEqual(False, is_one_pair(royal_straight))
self.assertEqual(False, is_one_pair(normal_straight))
self.assertEqual(False, is_one_pair(buta))
def test_is_two_pair(self):
self.assertEqual(False, is_two_pair(one_pair))
self.assertEqual(True, is_two_pair(two_pair))
self.assertEqual(False, is_two_pair(three_cards))
self.assertEqual(False, is_two_pair(four_cards))
self.assertEqual(False, is_two_pair(fullhouse))
self.assertEqual(False, is_two_pair(royal_straight))
self.assertEqual(False, is_two_pair(normal_straight))
self.assertEqual(False, is_two_pair(buta))
def test_is_three_cards(self):
self.assertEqual(False, is_three_cards(one_pair))
self.assertEqual(False, is_three_cards(two_pair))
self.assertEqual(True, is_three_cards(three_cards))
self.assertEqual(False, is_three_cards(four_cards))
self.assertEqual(False, is_three_cards(fullhouse))
self.assertEqual(False, is_three_cards(royal_straight))
self.assertEqual(False, is_three_cards(normal_straight))
self.assertEqual(False, is_three_cards(buta))
def test_is_four_cards(self):
self.assertEqual(False, is_four_cards(one_pair))
self.assertEqual(False, is_four_cards(two_pair))
self.assertEqual(False, is_four_cards(three_cards))
self.assertEqual(True, is_four_cards(four_cards))
self.assertEqual(False, is_four_cards(fullhouse))
self.assertEqual(False, is_four_cards(royal_straight))
self.assertEqual(False, is_four_cards(normal_straight))
self.assertEqual(False, is_four_cards(buta))
def test_is_five_cards(self):
self.assertEqual(False, is_four_cards(one_pair))
self.assertEqual(False, is_five_cards(two_pair))
self.assertEqual(False, is_five_cards(three_cards))
self.assertEqual(False, is_five_cards(four_cards))
self.assertEqual(True, is_five_cards(five_cards))
self.assertEqual(False, is_five_cards(fullhouse))
self.assertEqual(False, is_five_cards(royal_straight))
self.assertEqual(False, is_five_cards(normal_straight))
self.assertEqual(False, is_five_cards(buta))
def test_is_fullhouse(self):
self.assertEqual(False, is_fullhouse(one_pair))
self.assertEqual(False, is_fullhouse(two_pair))
self.assertEqual(False, is_fullhouse(three_cards))
self.assertEqual(False, is_fullhouse(four_cards))
self.assertEqual(True, is_fullhouse(fullhouse))
self.assertEqual(False, is_fullhouse(royal_straight))
self.assertEqual(False, is_fullhouse(normal_straight))
self.assertEqual(False, is_fullhouse(buta))
def test_is_normal_straight(self):
self.assertEqual(False, is_normal_straight(one_pair))
self.assertEqual(False, is_normal_straight(two_pair))
self.assertEqual(False, is_normal_straight(three_cards))
self.assertEqual(False, is_normal_straight(four_cards))
self.assertEqual(False, is_normal_straight(fullhouse))
self.assertEqual(False, is_normal_straight(royal_straight))
self.assertEqual(True, is_normal_straight(normal_straight))
self.assertEqual(False, is_normal_straight(buta))
def test_is_royal_straight(self):
self.assertEqual(False, is_royal_straight(one_pair))
self.assertEqual(False, is_royal_straight(two_pair))
self.assertEqual(False, is_royal_straight(three_cards))
self.assertEqual(False, is_royal_straight(four_cards))
self.assertEqual(False, is_royal_straight(fullhouse))
self.assertEqual(True, is_royal_straight(royal_straight))
self.assertEqual(False, is_royal_straight(normal_straight))
self.assertEqual(False, is_royal_straight(buta))
def test_grouping_cards(self):
def count_cards(card_dict):
num_of_cards = 0
for values in card_dict.values():
num_of_cards += values
return num_of_cards
val = grouping_cards(buta)
self.assertEqual(5, count_cards(val))
self.assertEqual(1, val[13])
def test_judge_hands(self):
self.assertRegexpMatches(judge_hands(buta), "buta")
self.assertRegexpMatches(judge_hands(four_cards), "four_cards")
self.assertRegexpMatches(judge_hands(five_cards), "five_cards")
self.assertRegexpMatches(judge_hands(fullhouse), "fullhouse")
self.assertRegexpMatches(judge_hands(normal_straight),
"normal_straight")
self.assertRegexpMatches(judge_hands(one_pair), "one_pair")
self.assertRegexpMatches(judge_hands(royal_straight), "royal_straight")
self.assertRegexpMatches(judge_hands(three_cards), "three_cards")
self.assertRegexpMatches(judge_hands(two_pair), "two_pair")
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment