File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
109126def test_bytes_to_ulid_or_none (impl ):
110127 """Test bytes_to_ulid_or_none."""
111128 assert (
You can’t perform that action at this time.
0 commit comments