Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_numpy_random_state():
if np is None:
pytest.skip('requires numpy')
data = np.random.RandomState(0)
# assert ub.hash_data(data).startswith('ujsidscotcycsqwnkxgbsxkcedplzvytmfmr')
assert ub.hash_data(data, hasher='sha512', types=True, base='abc').startswith('snkngbxghabesvowzalqtvdvjtvslmxve')
# _hashable_sequence(data)
def check_hash(want, input_):
count = counter[0] = counter[0] + 1
got = ub.hash_data(input_, hasher='sha512', base='abc', types=True)
got = got[0:32]
# assert got.startswith(want), 'want={}, got={}'.format(want, got)
print('check_hash({!r}, {!r})'.format(got, input_))
if want is not None and not got.startswith(want):
item = (got, input_, count, want)
failed.append(item)
def test_numpy_object_array():
"""
_HASHABLE_EXTENSIONS = ub.util_hash._HASHABLE_EXTENSIONS
"""
if np is None:
pytest.skip('requires numpy')
# An object array should have the same repr as a list of a tuple of data
data = np.array([1, 2, 3], dtype=object)
objhash = ub.hash_data(data)
assert ub.hash_data([1, 2, 3]) == objhash
assert ub.hash_data((1, 2, 3)) == objhash
# Ensure this works when the object array is nested
data = [np.array([1, 2, 3], dtype=object)]
objhash = ub.hash_data(data)
assert ub.hash_data([[1, 2, 3]]) == objhash
assert ub.hash_data([(1, 2, 3)]) == objhash
assert ub.hash_data(([1, 2, 3],)) == objhash
def check_hash(want, input_):
count = counter[0] = counter[0] + 1
got = ub.hash_data(input_, hasher='sha1', base='hex', types=False)
# assert got.startswith(want), 'want={}, got={}'.format(want, got)
print('check_hash({!r}, {!r})'.format(got, input_))
if want is not None and not got.startswith(want):
item = (got, input_, count, want)
failed.append(item)
def _test_xxhash():
try:
import xxhash # NOQA
except ImportError:
pass
else:
assert ub.hash_data('foo', hasher='xxh64') == '33bf00a859c4ba3f'
def test_hash_data_custom_base():
data = 1
# A larger base means the string can be shorter
hashid_26 = ub.hash_data(data, base='abc', hasher='sha512', types=True)
assert len(hashid_26) == 109
# assert hashid_26.startswith('lejivmqndqzp')
assert hashid_26.startswith('rfsmlqsjsuzllgp')
hashid_16 = ub.hash_data(data, base='hex', hasher='sha512', types=True)
# assert hashid_16.startswith('8bf2a1f4dbea6e59e5c2ec4077498c44')
assert hashid_16.startswith('d7c9cea9373eb7ba20444ec65e0186b')
assert len(hashid_16) == 128
# Binary should have len 512 because the default hasher is sha512
hashid_2 = ub.hash_data(data, base=['0', '1'], hasher='sha512', types=True)
assert len(hashid_2) == 512
assert hashid_2.startswith('110101111100100111001110101010010')
def test_ndarray_zeros():
if np is None:
pytest.skip('requires numpy')
data = np.zeros((3, 3), dtype=np.int64)
hashid = ub.hash_data(data)
assert hashid != ub.hash_data(data.ravel()), (
'shape should influence data')
assert hashid != ub.hash_data(data.astype(np.float32))
assert hashid != ub.hash_data(data.astype(np.int32))
assert hashid != ub.hash_data(data.astype(np.int8))
def test_uuid():
data = uuid.UUID('12345678-1234-1234-1234-123456789abc')
sequence = b''.join(hash_sequence(data))
assert sequence == b'UUID\x124Vx\x124\x124\x124\x124Vx\x9a\xbc'
assert ub.hash_data(data, types=True, base='abc', hasher='sha512').startswith('nkklelnjzqbi')
assert ub.hash_data(data.bytes, types=True) != ub.hash_data(data, types=True), (
'the fact that it is a UUID should reflect in the hash')
assert ub.hash_data(data.bytes, types=False) == ub.hash_data(data, types=False), (
'the hash should be equal when ignoring types')