Files
poker_task3/tests/test_task3.py
2025-09-20 00:09:22 +08:00

100 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""测试任务3 - 使用poker_task1进行扑克牌EMD距离计算"""
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
# 从poker_task3.task3模块导入函数
from poker_task3.task3 import parseTurnInput, calHandEquityHist, calPokerEmdTurn
from poker_task3.card import Card
def test_parse_turn_input():
"""测试TURN阶段输入解析"""
input_text = """
As Ks
7s 6s
8h 9d 9c Qh
"""
hand1, hand2, board = parseTurnInput(input_text)
assert len(hand1) == 2
assert len(hand2) == 2
assert len(board) == 4
assert str(hand1[0]) == "As"
assert str(hand1[1]) == "Ks"
assert str(hand2[0]) == "7s"
assert str(hand2[1]) == "6s"
print("✓ TURN输入解析测试通过")
def test_hand_equity_Hist():
"""测试手牌胜率分布计算"""
hole_cards = Card.parse_cards("As Ks")
board_cards = Card.parse_cards("8h 9d 9c Qh")
Hist = calHandEquityHist(hole_cards, board_cards)
assert len(Hist) == 30 # 胜率分布的区间数量改进为30个区间
assert all(isinstance(x, (int, float)) for x in Hist)
assert all(x >= 0 for x in Hist) # 所有值都应该是非负的
assert abs(sum(Hist) - 1.0) < 1e-10 # 分布总和应该为1标准化后
print("✓ 手牌胜率分布测试通过")
def test_poker_emd_calculation():
"""测试主要的EMD计算函数"""
input_text = """As Ks
7s 6s
8h 9d 9c Qh"""
distance = calPokerEmdTurn(input_text)
assert isinstance(distance, (int, float))
assert distance >= 0 # EMD总是非负的
print(f"✓ 扑克牌EMD计算测试通过 (距离: {distance:.3f})")
def test_different_hand_strengths():
"""测试不同强度手牌的EMD"""
# 强牌 vs 弱牌
input_text = """As Ks
2c 3d
8h 9d 9c Qh"""
distance = calPokerEmdTurn(input_text)
assert isinstance(distance, (int, float))
assert distance >= 0 # 应该有非负距离
print(f"✓ 不同手牌强度测试通过 (距离: {distance:.3f})")
def test_error_handling():
"""测试无效输入的错误处理"""
# 错误的行数
result = parseTurnInput("As Ks\n7s 6s")
assert result == (None, None, None), "应该返回(None, None, None)"
# 手牌中牌数错误
result = parseTurnInput("As Ks Qs\n7s 6s\n8h 9d 9c Qh")
assert result == (None, None, None), "应该返回(None, None, None)"
# 公共牌数错误
result = parseTurnInput("As Ks\n7s 6s\n8h 9d 9c")
assert result == (None, None, None), "应该返回(None, None, None)"
print("✓ 错误处理测试通过")
if __name__ == "__main__":
test_parse_turn_input()
test_hand_equity_Hist()
test_poker_emd_calculation()
test_different_hand_strengths()
test_error_handling()
print("所有任务3测试通过! ✓")