{{ message }}
Repository navigation
Expand file tree
/
Copy pathtest_solve_all_boards.py
More file actions
220 lines (177 loc) · 10 KB
/
Copy pathtest_solve_all_boards.py
File metadata and controls
220 lines (177 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Tests for solve_all_boards_pbn and solve_all_boards_bin wrappers."""
import unittest
from dds3 import solve_all_boards_bin, solve_all_boards_pbn, solve_board, solve_board_pbn
from test_utils import assert_raises
# Reused across tests: a complete, valid 13-card deal in PBN notation.
_PBN = "N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3"
# Binary equivalent of the same deal for solve_all_boards_bin tests.
# remainCards[hand][suit] bitmasks for: N, E, S, W × ♠,♥,♦,♣
# Bit n is set for rank n (2→bit2=0x4, A→bit14=0x4000). Each suit sums to 0x7FFC.
_BINARY_DEAL = {
"trump": 4, # NT
"first": 0, # North
"remain_cards": [
[0x1840, 0x2064, 0x0920, 0x0700], # North ♠QJ6 ♥K652 ♦J85 ♣T98
[0x0188, 0x0A80, 0x44D0, 0x1010], # East ♠873 ♥J97 ♦AT764 ♣Q4
[0x2020, 0x0508, 0x3200, 0x40E4], # South ♠K5 ♥T83 ♦KQ9 ♣A7652
[0x4614, 0x5010, 0x000C, 0x2808], # West ♠AT942 ♥AQ4 ♦32 ♣KJ3
],
"current_trick_suit": (0, 0, 0),
"current_trick_rank": (0, 0, 0),
}
def _result_shape_ok(result: dict) -> bool:
"""Return True if a FutureTricks dict has the expected shape."""
return (
"nodes" in result
and "cards" in result
and "suit" in result
and "rank" in result
and "equals" in result
and "score" in result
and len(result["suit"]) == 13
)
class TestSolveAllBoardsPBN(unittest.TestCase):
"""Tests for solve_all_boards_pbn (PBN format batch input)."""
def test_single_board_returns_list_of_one(self) -> None:
boards = [{"remain_cards": _PBN, "trump": 4, "first": 0}]
results = solve_all_boards_pbn(boards)
self.assertIsInstance(results, list)
self.assertEqual(len(results), 1)
self.assertTrue(_result_shape_ok(results[0]))
def test_multiple_boards(self) -> None:
boards = [{"remain_cards": _PBN, "trump": 4, "first": 0}] * 3
results = solve_all_boards_pbn(boards)
self.assertEqual(len(results), 3)
for r in results:
self.assertTrue(_result_shape_ok(r))
def test_empty_list_returns_empty(self) -> None:
results = solve_all_boards_pbn([])
self.assertEqual(results, [])
def test_all_optional_keys_default(self) -> None:
"""Only remain_cards is required; all others have documented defaults."""
boards = [{"remain_cards": _PBN}]
results = solve_all_boards_pbn(boards)
self.assertEqual(len(results), 1)
self.assertTrue(_result_shape_ok(results[0]))
def test_result_matches_single_board_pbn(self) -> None:
"""Batch result for one board should agree with single-board result on card count."""
single = solve_board_pbn(_PBN, trump=4, first=0)
batch = solve_all_boards_pbn([{"remain_cards": _PBN, "trump": 4, "first": 0}])
# Only the first `cards` entries are meaningful; compare the count and top score.
self.assertEqual(single["cards"], batch[0]["cards"])
self.assertEqual(single["score"][0], batch[0]["score"][0])
def test_different_trump_suits_accepted(self) -> None:
for trump in range(5):
boards = [{"remain_cards": _PBN, "trump": trump, "first": 0}]
results = solve_all_boards_pbn(boards)
self.assertEqual(len(results), 1)
def test_different_first_seats_accepted(self) -> None:
for first in range(4):
boards = [{"remain_cards": _PBN, "trump": 4, "first": first}]
results = solve_all_boards_pbn(boards)
self.assertEqual(len(results), 1)
def test_invalid_trump_raises_value_error(self) -> None:
boards = [{"remain_cards": _PBN, "trump": 5}]
assert_raises(ValueError, solve_all_boards_pbn, boards, match="invalid value 5")
def test_invalid_first_raises_value_error(self) -> None:
boards = [{"remain_cards": _PBN, "first": 4}]
assert_raises(ValueError, solve_all_boards_pbn, boards, match="invalid value 4")
def test_invalid_trick_suit_raises_value_error(self) -> None:
boards = [{"remain_cards": _PBN, "current_trick_suit": (0, 0, 5)}]
assert_raises(ValueError, solve_all_boards_pbn, boards, match="invalid value 5")
def test_invalid_trick_rank_raises_value_error(self) -> None:
boards = [{"remain_cards": _PBN, "current_trick_rank": (2, 2, 15)}]
assert_raises(ValueError, solve_all_boards_pbn, boards, match="invalid value 15")
def test_too_many_boards_raises_value_error(self) -> None:
boards = [{"remain_cards": _PBN}] * 201
assert_raises(ValueError, solve_all_boards_pbn, boards, match="exceeds maximum")
def test_invalid_pbn_string_raises_error(self) -> None:
boards = [{"remain_cards": "not a pbn"}]
assert_raises((ValueError, RuntimeError), solve_all_boards_pbn, boards)
def test_per_board_target_and_solutions(self) -> None:
boards = [{"remain_cards": _PBN, "trump": 4, "first": 0, "target": 9, "solutions": 1, "mode": 0}]
results = solve_all_boards_pbn(boards)
self.assertTrue(_result_shape_ok(results[0]))
def test_max_threads_accepted_and_matches_default(self) -> None:
"""Passing an explicit max_threads must be accepted and give the same results as the default."""
boards = [{"remain_cards": _PBN, "trump": 4, "first": 0}] * 3
default = solve_all_boards_pbn(boards)
for max_threads in (1, 2):
capped = solve_all_boards_pbn(boards, max_threads=max_threads)
self.assertEqual(len(capped), len(default))
for capped_r, default_r in zip(capped, default):
# Only the first `cards` entries of a result are meaningful.
cards = capped_r["cards"]
self.assertEqual(cards, default_r["cards"])
for key in ("suit", "rank", "equals", "score"):
self.assertEqual(capped_r[key][:cards], default_r[key][:cards])
class TestSolveAllBoardsBin(unittest.TestCase):
"""Tests for solve_all_boards_bin (binary format batch input)."""
def test_single_board_returns_list_of_one(self) -> None:
results = solve_all_boards_bin([_BINARY_DEAL])
self.assertIsInstance(results, list)
self.assertEqual(len(results), 1)
self.assertTrue(_result_shape_ok(results[0]))
def test_multiple_boards(self) -> None:
results = solve_all_boards_bin([_BINARY_DEAL] * 3)
self.assertEqual(len(results), 3)
for r in results:
self.assertTrue(_result_shape_ok(r))
def test_empty_list_returns_empty(self) -> None:
results = solve_all_boards_bin([])
self.assertEqual(results, [])
def test_result_matches_single_board_bin(self) -> None:
"""Batch result for one board should agree with single-board result on card count."""
single = solve_board(_BINARY_DEAL)
batch = solve_all_boards_bin([_BINARY_DEAL])
self.assertEqual(single["cards"], batch[0]["cards"])
self.assertEqual(single["score"][0], batch[0]["score"][0])
def test_invalid_trump_raises_value_error(self) -> None:
bad = {**_BINARY_DEAL, "trump": 5}
assert_raises(ValueError, solve_all_boards_bin, [bad], match="invalid value 5")
def test_invalid_first_raises_value_error(self) -> None:
bad = {**_BINARY_DEAL, "first": 4}
assert_raises(ValueError, solve_all_boards_bin, [bad], match="invalid value 4")
def test_invalid_trick_suit_raises_value_error(self) -> None:
bad = {**_BINARY_DEAL, "current_trick_suit": (0, 0, 5)}
assert_raises(ValueError, solve_all_boards_bin, [bad], match="invalid value 5")
def test_invalid_trick_rank_raises_value_error(self) -> None:
bad = {**_BINARY_DEAL, "current_trick_rank": (2, 2, 15)}
assert_raises(ValueError, solve_all_boards_bin, [bad], match="invalid value 15")
def test_too_many_boards_raises_value_error(self) -> None:
assert_raises(ValueError, solve_all_boards_bin, [_BINARY_DEAL] * 201, match="exceeds maximum")
def test_invalid_remain_cards_size_raises_value_error(self) -> None:
bad = {**_BINARY_DEAL, "remain_cards": [[0, 0, 0, 0]] * 3} # 3 hands instead of 4
assert_raises(ValueError, solve_all_boards_bin, [bad])
def test_per_board_target_and_solutions(self) -> None:
board = {**_BINARY_DEAL, "target": 9, "solutions": 1, "mode": 0}
results = solve_all_boards_bin([board])
self.assertTrue(_result_shape_ok(results[0]))
def test_different_trump_suits_accepted(self) -> None:
for trump in range(5):
board = {**_BINARY_DEAL, "trump": trump}
results = solve_all_boards_bin([board])
self.assertEqual(len(results), 1)
def test_max_threads_accepted_and_matches_default(self) -> None:
"""Passing an explicit max_threads must be accepted and give the same results as the default."""
boards = [_BINARY_DEAL] * 3
default = solve_all_boards_bin(boards)
for max_threads in (1, 2):
capped = solve_all_boards_bin(boards, max_threads=max_threads)
self.assertEqual(len(capped), len(default))
for capped_r, default_r in zip(capped, default):
# Only the first `cards` entries of a result are meaningful.
cards = capped_r["cards"]
self.assertEqual(cards, default_r["cards"])
for key in ("suit", "rank", "equals", "score"):
self.assertEqual(capped_r[key][:cards], default_r[key][:cards])
class TestSolveAllBoardsParity(unittest.TestCase):
"""Cross-function consistency between PBN and binary batch variants."""
def test_pbn_and_bin_agree_on_cards(self) -> None:
"""solve_all_boards_pbn and solve_all_boards_bin should agree on trick count and top score."""
pbn_results = solve_all_boards_pbn([{"remain_cards": _PBN, "trump": 4, "first": 0}])
bin_results = solve_all_boards_bin([_BINARY_DEAL])
self.assertEqual(pbn_results[0]["cards"], bin_results[0]["cards"])
self.assertEqual(pbn_results[0]["score"][0], bin_results[0]["score"][0])
if __name__ == "__main__":
unittest.main()
You can’t perform that action at this time.
