Skip to content

Commit a043deb

Browse files
akxbdraco
andauthored
chore: add simple test for entropy (#184)
Co-authored-by: J. Nick Koston <nick@koston.org>
1 parent 08bd085 commit a043deb

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

tests/test_init.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ def test_ulid_to_bytes_or_none(impl):
106106
assert impl.ulid_to_bytes_or_none(None) is None
107107

108108

109+
def test_entropy(impl):
110+
"""Verify generated ULIDs have non-trivial entropy in the random part."""
111+
samples = [impl.ulid_at_time_bytes(1677627631.0)[6:] for _ in range(200)]
112+
assert len(set(samples)) == len(samples), (
113+
"Generated ULIDs should have unique entropy"
114+
)
115+
116+
for pos in range(10):
117+
distinct_bytes = {s[pos] for s in samples}
118+
assert len(distinct_bytes) > 1, f"Byte position {pos} has no variety"
119+
120+
for pos in range(20):
121+
byte_idx, shift = divmod(pos, 2)
122+
distinct_nibbles = {(s[byte_idx] >> (4 * shift)) & 0xF for s in samples}
123+
assert len(distinct_nibbles) > 1, f"Nibble position {pos} has no variety"
124+
125+
109126
def test_bytes_to_ulid_or_none(impl):
110127
"""Test bytes_to_ulid_or_none."""
111128
assert (

0 commit comments

Comments
 (0)