Skip to content

Commit 4b94e84

Browse files
ashvardanianternaus
andcommitted
Improve: Stress-testing translations
Extends the testing suite with several fuzzy tests for binary string tranlations from Albumentations core image processing library. https://github.com/albumentations-team/albucore/blob/c0e924b9d2e74f787be413c33b05f42575cdc2c7/tests/test_lut.py#L25-L33 Co-authored-by: Vladimir Iglovikov <[email protected]>
1 parent c464b68 commit 4b94e84

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

scripts/test.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from random import choice, randint
22
from string import ascii_lowercase
3-
from typing import Optional
3+
from typing import Optional, Sequence, Dict
44
import tempfile
55
import os
66

@@ -718,6 +718,63 @@ def test_alignment_score_random(first_length: int, second_length: int):
718718
) == -baseline_edit_distance(a, b)
719719

720720

721+
def baseline_translate(body: str, lut: Sequence) -> str:
722+
return "".join([chr(lut[ord(c)]) for c in body])
723+
724+
725+
def translation_table_to_dict(lut: Sequence) -> Dict[str, str]:
726+
return {chr(i): chr(lut[i]) for i in range(256)}
727+
728+
729+
@pytest.mark.skipif(not numpy_available, reason="NumPy is not installed")
730+
@pytest.mark.parametrize("length", range(1, 300))
731+
def test_translations(length: int):
732+
733+
map_identity = np.arange(256, dtype=np.uint8)
734+
map_invert = np.arange(255, -1, -1, dtype=np.uint8)
735+
map_threshold = np.where(np.arange(256) > 127, 255, 0).astype(np.uint8)
736+
dict_identity = translation_table_to_dict(map_identity)
737+
dict_invert = translation_table_to_dict(map_invert)
738+
dict_threshold = translation_table_to_dict(map_threshold)
739+
view_identity = memoryview(map_identity)
740+
view_invert = memoryview(map_invert)
741+
view_threshold = memoryview(map_threshold)
742+
743+
body = get_random_string(length=length)
744+
body_bytes = body.encode("utf-8")
745+
746+
# Check mapping strings and byte-strings into new strings
747+
assert sz.translate(body, view_identity) == body
748+
assert sz.translate(body_bytes, view_identity) == body_bytes
749+
assert sz.translate(body_bytes, view_identity) == body_bytes.translate(
750+
view_identity
751+
)
752+
assert sz.translate(body_bytes, view_invert) == body_bytes.translate(view_invert)
753+
assert sz.translate(body_bytes, view_threshold) == body_bytes.translate(
754+
view_threshold
755+
)
756+
757+
# Check in-place translations
758+
body_after_identity = str(body)
759+
sz.translate(body, view_identity, inplace=True)
760+
assert body == body_after_identity.translate(dict_identity)
761+
body_after_invert = str(body)
762+
sz.translate(body, view_invert, inplace=True)
763+
assert body == body_after_invert.translate(dict_invert)
764+
body_after_threshold = str(body)
765+
sz.translate(body, view_threshold, inplace=True)
766+
assert body == body_after_threshold.translate(dict_threshold)
767+
768+
769+
@pytest.mark.repeat(3)
770+
@pytest.mark.parametrize("length", range(1, 300))
771+
@pytest.mark.skipif(not numpy_available, reason="NumPy is not installed")
772+
def test_translations_random(length: int):
773+
body = get_random_string(length=length)
774+
lut = np.random.randint(0, 256, size=256, dtype=np.uint8)
775+
assert sz.translate(body, memoryview(lut)) == baseline_translate(body, lut)
776+
777+
721778
@pytest.mark.parametrize("list_length", [10, 20, 30, 40, 50])
722779
@pytest.mark.parametrize("part_length", [5, 10])
723780
@pytest.mark.parametrize("variability", [2, 3])

0 commit comments

Comments
 (0)