import unittest
from dds3 import calc_dd_table, calc_all_tables_pbn
from test_utils import assert_raises
class TestCalcDDTable(unittest.TestCase):
def test_calc_dd_table_basic(self) -> None:
table_deal = {
"cards": [
[0x7FFC, 0, 0, 0], [0, 0x7FFC, 0, 0], [0, 0, 0x7FFC, 0], [0, 0, 0, 0x7FFC], ],
}
result = calc_dd_table(table_deal)
self.assertTrue("return_code" in result or "res_table" in result)
def test_calc_dd_table_result_structure(self) -> None:
table_deal = {
"cards": [
[0x7FFC, 0, 0, 0],
[0, 0x7FFC, 0, 0],
[0, 0, 0x7FFC, 0],
[0, 0, 0, 0x7FFC],
],
}
result = calc_dd_table(table_deal)
self.assertTrue(isinstance(result, dict))
def test_calc_dd_table_invalid_remain_cards_size(self) -> None:
table_deal = {
"cards": [[0, 0, 0]], }
assert_raises(ValueError, calc_dd_table, table_deal)
def test_calc_dd_table_remain_cards_all_zeros(self) -> None:
table_deal = {
"cards": [[0, 0, 0, 0] for _ in range(4)],
}
try:
result = calc_dd_table(table_deal)
self.assertTrue(isinstance(result, dict))
except RuntimeError:
pass
class TestCalcAllTablesPBN(unittest.TestCase):
def test_calc_all_tables_pbn_single_deal(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
result = calc_all_tables_pbn(deals)
self.assertTrue("no_of_boards" in result)
self.assertTrue("tables" in result)
self.assertTrue(isinstance(result["tables"], list))
def test_calc_all_tables_pbn_multiple_deals(self) -> None:
deals = [
"N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3",
"N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3",
]
result = calc_all_tables_pbn(deals)
self.assertTrue("no_of_boards" in result)
self.assertGreaterEqual(len(result["tables"]), 1)
def test_calc_all_tables_pbn_with_mode(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
result = calc_all_tables_pbn(deals, mode=0) self.assertTrue("tables" in result)
self.assertTrue("par_results" in result)
def test_calc_all_tables_pbn_default_mode_is_no_par(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
result = calc_all_tables_pbn(deals)
self.assertTrue("tables" in result)
def test_calc_all_tables_pbn_with_trump_filter(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
result = calc_all_tables_pbn(deals, trump_filter=[1, 1, 0, 0, 0])
self.assertTrue("no_of_boards" in result)
self.assertTrue("tables" in result)
def test_calc_all_tables_pbn_nt_row_and_filter_semantics(self) -> None:
deals = ["N:Q87.K932.QJT32.7 AKJ9632.J84.6.Q5 .AQT765.K87.J962 T54..A954.AKT843"]
all_strains = calc_all_tables_pbn(deals, trump_filter=[0, 0, 0, 0, 0])
all_rows = all_strains["tables"][0]["res_table"]
nt_only = calc_all_tables_pbn(deals, trump_filter=[1, 1, 1, 1, 0])
nt_only_rows = nt_only["tables"][0]["res_table"]
exclude_nt = calc_all_tables_pbn(deals, trump_filter=[0, 0, 0, 0, 1])
exclude_nt_rows = exclude_nt["tables"][0]["res_table"]
self.assertTrue(len(all_rows) == 5)
self.assertTrue(len(nt_only_rows) == 5)
self.assertTrue(len(exclude_nt_rows) == 5)
self.assertTrue(all(len(row) == 4 for row in all_rows))
self.assertTrue(all(len(row) == 4 for row in nt_only_rows))
self.assertTrue(all(len(row) == 4 for row in exclude_nt_rows))
self.assertTrue(nt_only_rows[4] == all_rows[4])
for strain in range(4):
self.assertTrue(nt_only_rows[strain] == [0, 0, 0, 0])
self.assertTrue(exclude_nt_rows[4] == [0, 0, 0, 0])
for strain in range(4):
self.assertTrue(exclude_nt_rows[strain] == all_rows[strain])
def test_calc_all_tables_pbn_default_trump_filter_all_zeros(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
result = calc_all_tables_pbn(deals) self.assertTrue("tables" in result)
def test_calc_all_tables_pbn_invalid_pbn(self) -> None:
deals = ["This is not a valid PBN"]
assert_raises((ValueError, RuntimeError), calc_all_tables_pbn, deals)
def test_calc_all_tables_pbn_empty_list(self) -> None:
deals = []
result = calc_all_tables_pbn(deals)
self.assertTrue("no_of_boards" in result)
self.assertTrue("tables" in result)
def test_calc_all_tables_pbn_invalid_trump_filter_size(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
assert_raises(ValueError, calc_all_tables_pbn, deals, trump_filter=[0, 0, 0])
def test_calc_all_tables_pbn_invalid_trump_filter_value(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
assert_raises(
ValueError,
calc_all_tables_pbn,
deals,
trump_filter=[0, 0, 2, 0, 0],
match="invalid value",
)
def test_calc_all_tables_pbn_result_structure(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
result = calc_all_tables_pbn(deals)
self.assertTrue(isinstance(result, dict))
self.assertTrue("no_of_boards" in result)
self.assertTrue("tables" in result)
self.assertTrue("par_results" in result)
self.assertTrue(isinstance(result["no_of_boards"], int))
self.assertTrue(isinstance(result["tables"], list))
self.assertTrue(isinstance(result["par_results"], list))
self.assertTrue(len(result["par_results"]) == 0)
result_with_par = calc_all_tables_pbn(deals, mode=0)
self.assertTrue(len(result_with_par["par_results"]) > 0)
class TestTableParity(unittest.TestCase):
def test_single_vs_batch_result_structure(self) -> None:
deals = ["N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"]
batch_result = calc_all_tables_pbn(deals)
self.assertTrue(len(batch_result["tables"]) >= 1)
single_table = batch_result["tables"][0]
self.assertTrue(isinstance(single_table, dict))
if __name__ == "__main__":
unittest.main()