From 52935bc76525b1ba069fa1c90b844bea705b181b Mon Sep 17 00:00:00 2001 From: Mitja HORVAT Date: Tue, 28 Sep 2021 09:57:33 +0200 Subject: [PATCH] python: Add bhash tests --- tests/meson.build | 4 ++++ tests/test_bhash.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100755 tests/test_bhash.py diff --git a/tests/meson.build b/tests/meson.build index bda9fd7..499db8c 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -9,4 +9,8 @@ test('test_phogen_map', 'PHOGEN_MAP_PY=@0@'.format(_phogen_map_py), 'PHOGEN_WORD_LIST=@0@'.format(_word_list)]) +test('test_bhash', + find_program('test_bhash.py'), + env: ['SOURCE_ROOT=' + meson.project_source_root(), + 'PYTHONPATH=' + PYTHONPATH]) diff --git a/tests/test_bhash.py b/tests/test_bhash.py new file mode 100755 index 0000000..d1e3cd7 --- /dev/null +++ b/tests/test_bhash.py @@ -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.")