Skip to content

Commit

Permalink
Improve assert_samples_in test failure output
Browse files Browse the repository at this point in the history
=== 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]
  • Loading branch information
losos0 committed Feb 29, 2024
1 parent fd39956 commit 9f1386d
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions unitary/quantum_chess/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,39 @@
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):
Expand Down

0 comments on commit 9f1386d

Please sign in to comment.