MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / test_translations

Function test_translations

scripts/test_stringzilla.py:917–949  ·  view source on GitHub ↗
(length: int, seed_value: int)

Source from the content-addressed store, hash-verified

915@pytest.mark.parametrize("length", range(1, 300))
916@pytest.mark.parametrize("seed_value", SEED_VALUES)
917def test_translations(length: int, seed_value: int):
918 seed_random_generators(seed_value)
919
920 map_identity = np.arange(256, dtype=np.uint8)
921 map_invert = np.arange(255, -1, -1, dtype=np.uint8)
922 map_threshold = np.where(np.arange(256) > 127, 255, 0).astype(np.uint8)
923 dict_identity = translation_table_to_dict(map_identity)
924 dict_invert = translation_table_to_dict(map_invert)
925 dict_threshold = translation_table_to_dict(map_threshold)
926 view_identity = memoryview(map_identity)
927 view_invert = memoryview(map_invert)
928 view_threshold = memoryview(map_threshold)
929
930 body = get_random_string(length=length)
931 body_bytes = body.encode("utf-8")
932
933 # Check mapping strings and byte-strings into new strings
934 assert sz.translate(body, view_identity) == body
935 assert sz.translate(body_bytes, view_identity) == body_bytes
936 assert sz.translate(body_bytes, view_identity) == body_bytes.translate(view_identity)
937 assert sz.translate(body_bytes, view_invert) == body_bytes.translate(view_invert)
938 assert sz.translate(body_bytes, view_threshold) == body_bytes.translate(view_threshold)
939
940 # Check in-place translations on mutable byte-arrays - all of them return nothing
941 after_identity = bytearray(body_bytes)
942 assert sz.translate(after_identity, view_identity, inplace=True) is None
943 assert sz.equal(after_identity, body.translate(dict_identity))
944 after_invert = bytearray(body_bytes)
945 assert sz.translate(after_invert, view_invert, inplace=True) is None
946 assert sz.equal(after_invert, body.translate(dict_invert))
947 after_threshold = bytearray(body_bytes)
948 assert sz.translate(after_threshold, view_threshold, inplace=True) is None
949 assert sz.equal(after_threshold, body.translate(dict_threshold))
950
951
952@pytest.mark.parametrize("length", list(range(0, 300)) + [1024, 4096, 100000])

Callers

nothing calls this directly

Calls 3

seed_random_generatorsFunction · 0.70
get_random_stringFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…