python: Add bhash tests

This commit is contained in:
2021-09-28 09:57:33 +02:00
parent 3852c4efc9
commit 52935bc765
2 changed files with 27 additions and 0 deletions

View File

@ -9,4 +9,8 @@ test('test_phogen_map',
'PHOGEN_MAP_PY=@0@'.format(_phogen_map_py), 'PHOGEN_MAP_PY=@0@'.format(_phogen_map_py),
'PHOGEN_WORD_LIST=@0@'.format(_word_list)]) 'PHOGEN_WORD_LIST=@0@'.format(_word_list)])
test('test_bhash',
find_program('test_bhash.py'),
env: ['SOURCE_ROOT=' + meson.project_source_root(),
'PYTHONPATH=' + PYTHONPATH])

23
tests/test_bhash.py Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
import passgeny.bhash
import sys
TEST_STRING = b'hello world'
bh = passgeny.bhash.Bhash()
bh.from_bytes(TEST_STRING)
# Scan the string in reverse order (::-1) as bhash should use big endian arithmetics
for c in TEST_STRING[::-1]:
hc = bh.modulo(256)
if hc != c:
raise Exception("Returned value is {}, expected {}".format(chr(hc), c))
# This should throw an exception as we consumned all the bits in bhash
try:
bh.modulo(2)
except passgeny.bhash.BhashException as e:
print("Properly received exception: {}".format(e))
else:
raise Exception("Did not receive BhashException.")