test_hashtable.py 1011 B

123456789101112131415161718192021222324252627282930
  1. import pytest
  2. import random
  3. from numpy.core._multiarray_tests import identityhash_tester
  4. @pytest.mark.parametrize("key_length", [1, 3, 6])
  5. @pytest.mark.parametrize("length", [1, 16, 2000])
  6. def test_identity_hashtable(key_length, length):
  7. # use a 30 object pool for everything (duplicates will happen)
  8. pool = [object() for i in range(20)]
  9. keys_vals = []
  10. for i in range(length):
  11. keys = tuple(random.choices(pool, k=key_length))
  12. keys_vals.append((keys, random.choice(pool)))
  13. dictionary = dict(keys_vals)
  14. # add a random item at the end:
  15. keys_vals.append(random.choice(keys_vals))
  16. # the expected one could be different with duplicates:
  17. expected = dictionary[keys_vals[-1][0]]
  18. res = identityhash_tester(key_length, keys_vals, replace=True)
  19. assert res is expected
  20. # check that ensuring one duplicate definitely raises:
  21. keys_vals.insert(0, keys_vals[-2])
  22. with pytest.raises(RuntimeError):
  23. identityhash_tester(key_length, keys_vals)