from src import calculate_fan, is_seven_pairs, is_cleared, is_big_pairs from src import Hand from src import MahjongTile # 测试用例 def test_basic_win(): """ 测试平胡(基本胡)计分 """ hand = Hand() # 模拟平胡手牌:四组顺子 + 一对将 hand.add_tile(MahjongTile("筒", 1)) hand.add_tile(MahjongTile("筒", 1)) # 将 hand.add_tile(MahjongTile("筒", 2)) hand.add_tile(MahjongTile("筒", 3)) # 顺子 hand.add_tile(MahjongTile("筒", 4)) hand.add_tile(MahjongTile("筒", 5)) hand.add_tile(MahjongTile("筒", 6)) # 顺子 hand.add_tile(MahjongTile("筒", 7)) hand.add_tile(MahjongTile("筒", 7)) hand.add_tile(MahjongTile("筒", 8)) hand.add_tile(MahjongTile("筒", 9)) # 顺子 hand.add_tile(MahjongTile("万", 1)) hand.add_tile(MahjongTile("万", 2)) hand.add_tile(MahjongTile("万", 3)) # 顺子 melds = [] conditions = {} fan = calculate_fan(hand.tiles, melds, is_self_draw=False, is_cleared=False, conditions=conditions) assert fan == 1, f"Expected 1 fan, got {fan}" def test_clear_win(): """ 测试清一色计分(不加自摸番) """ hand = [0] * 108 # 模拟清一色手牌 hand[0] = 2 hand[4] = 1 hand[5] = 1 hand[6] = 1 hand[10] = 1 hand[11] = 1 hand[12] = 1 hand[20] = 1 hand[21] = 1 hand[22] = 1 hand[23] = 1 melds = [] conditions = {"is_seven_pairs": False, "add_self_draw": False} fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=is_cleared(hand), conditions=conditions) assert fan == 3, f"Expected 3 fans (1 basic + 2 cleared), got {fan}" def test_pure_cleared(): """ 测试清对计分 """ hand = [2] * 6 + [0] * 102 # 手牌:清对 melds = [0, 1, 2] # 模拟碰 conditions = {"is_pure_cleared": True} fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=True, conditions=conditions) assert fan == 3, f"Expected 3 fans (pure cleared), got {fan}" def test_pure_cleared(): """ 测试清对计分 """ hand = [2] * 6 + [0] * 102 # 手牌:清对 melds = [0, 1, 2] # 模拟碰 conditions = {"is_pure_cleared": True} # 明确条件 fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=True, conditions=conditions) assert fan == 3, f"Expected 3 fans (pure cleared), got {fan}" def test_seven_pairs(): """ 测试七对计分 """ hand = [0] * 108 # 模拟七对手牌 hand[0] = 2 # 1条 hand[4] = 2 # 2条 hand[8] = 2 # 3条 hand[12] = 2 # 4条 hand[16] = 2 # 5条 hand[20] = 2 # 6条 hand[24] = 2 # 7条 melds = [] conditions = {"is_seven_pairs": is_seven_pairs(hand)} fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions) assert fan == 2, f"Expected 2 fans (7 pairs), got {fan}" def test_small_pairs(): """ 测试小七对计分 """ hand = [2] * 6 + [0] * 102 melds = [] conditions = {"is_small_pairs": True} fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions) assert fan == 2, f"Expected 2 fans (small pairs), got {fan}" def test_clear_seven_pairs(): """ 测试清七对计分 """ hand = [2] * 7 + [0] * 101 melds = [] conditions = {"is_clear_seven_pairs": True} fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=True, conditions=conditions) assert fan == 12, f"Expected 12 fans (clear seven pairs), got {fan}" def test_full_request(): """ 测试全求人计分 """ hand = [2] + [0] * 107 melds = [] conditions = {"is_full_request": True} fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions) assert fan == 6, f"Expected 6 fans (full request), got {fan}" def test_big_pairs(): """ 测试大对子计分 """ hand = [0] * 108 # 模拟大对子手牌 hand[0] = 3 # 1条 hand[4] = 3 # 2条 hand[8] = 3 # 3条 hand[12] = 2 # 将: 4条 melds = [] conditions = {"is_big_pairs": is_big_pairs(hand)} # Debug output print(f"Conditions: {conditions}") # 确保大对子检测正确 assert is_big_pairs(hand), "The hand is not identified as a big pairs hand." fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions) assert fan == 2, f"Expected 2 fans (big pairs), got {fan}" def test_gang_flower(): """ 测试杠上开花计分 """ hand = [0] * 108 # 模拟杠上开花手牌 hand[0] = 2 # 将: 两张1条 hand[3] = 1 # 2条 hand[4] = 1 # 3条 hand[5] = 1 # 4条 hand[10] = 1 # 5条 hand[11] = 1 # 6条 hand[12] = 1 # 7条 hand[20] = 1 # 8条 hand[21] = 1 # 9条 hand[22] = 1 # 1筒 melds = [] conditions = {"is_gang_flower": True} fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions) assert fan == 2, f"Expected 2 fans (1 basic + 1 gang flower), got {fan}" def test_rob_gang(): """ 测试抢杠胡计分 """ hand = [0] * 108 melds = [] conditions = {"is_rob_gang": True} fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions) assert fan == 1, f"Expected 1 fan (rob gang), got {fan}" def test_under_the_sea(): """ 测试海底捞月计分 """ hand = [0] * 108 melds = [] conditions = {"is_under_the_sea": True} fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions) assert fan == 1, f"Expected 1 fan (under the sea), got {fan}" def test_cannon(): """ 测试放炮计分 """ hand = [0] * 108 melds = [] conditions = {"is_cannon": True} fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions) assert fan == 1, f"Expected 1 fan (cannon), got {fan}" def test_tian_hu(): """ 测试天胡计分 """ hand = [0] * 108 melds = [] conditions = {"is_tian_hu": True} fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions) assert fan == 12, f"Expected 12 fans (tian hu), got {fan}" def test_di_hu(): """ 测试地胡计分 """ hand = [0] * 108 melds = [] conditions = {"is_di_hu": True} fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions) assert fan == 12, f"Expected 12 fans (di hu), got {fan}" def test_dragon_seven_pairs(): """ 测试龙七对计分 """ hand = [0] * 108 # 模拟龙七对手牌 hand[0] = 2 # 1条 hand[4] = 2 # 2条 hand[8] = 2 # 3条 hand[12] = 2 # 4条 hand[16] = 2 # 5条 hand[20] = 2 # 6条 hand[24] = 4 # 龙: 7条 melds = [] conditions = { "is_dragon_seven_pairs": True, "is_seven_pairs": True, } fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions) assert fan == 12, f"Expected 13 fans (1 self-draw + 12 dragon seven pairs), got {fan}" def test_self_draw(): """ 测试自摸计分 """ hand = [0] * 108 melds = [] conditions = {} fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions) assert fan == 1, f"Expected 1 fan (self-draw), got {fan}"