From 1c50bb6eb32831375919c7c90cef9d4279412155 Mon Sep 17 00:00:00 2001 From: Conrad Holt Date: Mon, 26 Feb 2024 20:30:13 -0600 Subject: [PATCH] Improve assert_samples_in test failure output === Before === > assert all_in, print_samples(samples) E AssertionError: None E assert False test_utils.py:37: AssertionError ------------------------------------------------- Captured stdout call ------------------------------------------------- Using seed 2353196025 [211106778865792] {211106778865792, 211106242027648, 70369290510464, 211106778898560} ['h1', 'f2', 'g2', 'e3', 'h3', 'f4', 'g6', 'h6']: 476 ['h1', 'f2', 'g2', 'h2', 'e3', 'h3', 'f4', 'g6', 'h6']: 17 ['h1', 'f2', 'g2', 'h2', 'e3', 'h3', 'g6', 'h6']: 5 ['h1', 'f2', 'g2', 'e3', 'h3', 'f4', 'g6']: 2 === After === > assert all_in, message("Unexpected bitboard in samples") E AssertionError: Unexpected bitboard in samples E assert False test_utils.py:47: AssertionError ------------------------------------------------- Captured stdout call ------------------------------------------------- Using seed 3713866156 Expected bitboards: [f2 g2 h2 g3 d6 f7 g7 h7 b8 d8 h8] Sample distribution: 493 [f2 g2 h2 g3 d6 f7 g7 h7 b8 d8 h8] 7 [f2 g2 h2 g3 d6 f7 g7 h7 b8 h8] --- unitary/quantum_chess/test_utils.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/unitary/quantum_chess/test_utils.py b/unitary/quantum_chess/test_utils.py index bbc187f3..39429f05 100644 --- a/unitary/quantum_chess/test_utils.py +++ b/unitary/quantum_chess/test_utils.py @@ -17,28 +17,38 @@ import unitary.quantum_chess.bit_utils as u -def print_samples(samples): - """For debugging only. Prints all the samples as lists of squares.""" +def bitboard_to_string(bitboard): + """Returns space-separated string of square names.""" + return "[" + " ".join(u.bitboard_to_squares(bitboard)) + "]" + + +def print_samples(samples, possibilities): + """For debugging only. Prints expected and actual lists of squares.""" + print('Expected bitboards:') + for bitboard in possibilities: + print(" " * 5, bitboard_to_string(bitboard)) + print('Sample distribution:') sample_dict = {} for sample in samples: if sample not in sample_dict: sample_dict[sample] = 0 sample_dict[sample] += 1 for key in sample_dict: - print(f"{u.bitboard_to_squares(key)}: {sample_dict[key]}") + print(f"{sample_dict[key]:5} {bitboard_to_string(key)}") def assert_samples_in(b, possibilities): + def message(message): + print_samples(samples, possibilities) + return message samples = b.sample(500) assert len(samples) == 500 all_in = all(sample in possibilities for sample in samples) - print(possibilities) - print(set(samples)) - assert all_in, print_samples(samples) + assert all_in, message("Unexpected bitboard in samples") # make sure each is represented at least once for p in possibilities: any_in = any(sample == p for sample in samples) - assert any_in, print_samples(samples) + assert any_in, message("Not all expected bitboards appeared") def assert_sample_distribution(b, probability_map, p_significant=1e-6):