|
1 | 1 | from random import choice, randint
|
2 | 2 | from string import ascii_lowercase
|
3 |
| -from typing import Optional |
| 3 | +from typing import Optional, Sequence, Dict |
4 | 4 | import tempfile
|
5 | 5 | import os
|
6 | 6 |
|
@@ -718,6 +718,63 @@ def test_alignment_score_random(first_length: int, second_length: int):
|
718 | 718 | ) == -baseline_edit_distance(a, b)
|
719 | 719 |
|
720 | 720 |
|
| 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 | + |
721 | 778 | @pytest.mark.parametrize("list_length", [10, 20, 30, 40, 50])
|
722 | 779 | @pytest.mark.parametrize("part_length", [5, 10])
|
723 | 780 | @pytest.mark.parametrize("variability", [2, 3])
|
|
0 commit comments