MCPcopy Index your code
hub / github.com/python/cpython / test_uuid6_uniqueness

Method test_uuid6_uniqueness

Lib/test/test_uuid.py:767–795  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

765 equal(u.fields[5], fake_node_value)
766
767 def test_uuid6_uniqueness(self):
768 # Test that UUIDv6-generated values are unique.
769
770 # Unlike UUIDv8, only 62 bits can be randomized for UUIDv6.
771 # In practice, however, it remains unlikely to generate two
772 # identical UUIDs for the same 60-bit timestamp if neither
773 # the node ID nor the clock sequence is specified.
774 uuids = {self.uuid.uuid6() for _ in range(1000)}
775 self.assertEqual(len(uuids), 1000)
776 versions = {u.version for u in uuids}
777 self.assertSetEqual(versions, {6})
778
779 timestamp = 0x1ec9414c_232a_b00
780 fake_nanoseconds = (timestamp - 0x1b21dd21_3814_000) * 100
781
782 with mock.patch('time.time_ns', return_value=fake_nanoseconds):
783 def gen():
784 with mock.patch.object(self.uuid, '_last_timestamp_v6', None):
785 return self.uuid.uuid6(node=0, clock_seq=None)
786
787 # By the birthday paradox, sampling N = 1024 UUIDs with identical
788 # node IDs and timestamps results in duplicates with probability
789 # close to 1 (not having a duplicate happens with probability of
790 # order 1E-15) since only the 14-bit clock sequence is randomized.
791 N = 1024
792 uuids = {gen() for _ in range(N)}
793 self.assertSetEqual({u.node for u in uuids}, {0})
794 self.assertSetEqual({u.time for u in uuids}, {timestamp})
795 self.assertLess(len(uuids), N, 'collision property does not hold')
796
797 def test_uuid6_node(self):
798 # Make sure the given node ID appears in the UUID.

Callers

nothing calls this directly

Calls 4

assertSetEqualMethod · 0.80
genFunction · 0.70
assertEqualMethod · 0.45
assertLessMethod · 0.45

Tested by

no test coverage detected