|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from numpy.testing import assert_array_equal |
| 4 | + |
| 5 | +from stringdtype import StringDType |
| 6 | + |
| 7 | +TEST_DATA = ["hello", "Ae¢☃€ 😊", "entry\nwith\nnewlines", "entry\twith\ttabs"] |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def string_array(): |
| 12 | + return np.array(TEST_DATA, dtype=StringDType()) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def unicode_array(): |
| 17 | + return np.array(TEST_DATA, dtype=np.unicode_) |
| 18 | + |
| 19 | + |
| 20 | +UNARY_FUNCTIONS = [ |
| 21 | + "str_len", |
| 22 | + "capitalize", |
| 23 | + "expandtabs", |
| 24 | + "isalnum", |
| 25 | + "isalpha", |
| 26 | + "isdigit", |
| 27 | + "islower", |
| 28 | + "isspace", |
| 29 | + "istitle", |
| 30 | + "isupper", |
| 31 | + "lower", |
| 32 | + "splitlines", |
| 33 | + "swapcase", |
| 34 | + "title", |
| 35 | + "upper", |
| 36 | + "isnumeric", |
| 37 | + "isdecimal", |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("function_name", UNARY_FUNCTIONS) |
| 42 | +def test_unary(string_array, unicode_array, function_name): |
| 43 | + func = getattr(np.char, function_name) |
| 44 | + sres = func(string_array) |
| 45 | + ures = func(unicode_array) |
| 46 | + if sres.dtype == StringDType(): |
| 47 | + ures = ures.astype(StringDType()) |
| 48 | + assert_array_equal(sres, ures) |
| 49 | + |
| 50 | + |
| 51 | +# None means that the argument is a string array |
| 52 | +BINARY_FUNCTIONS = [ |
| 53 | + ("add", (None, None)), |
| 54 | + ("multiply", (None, 2)), |
| 55 | + ("mod", ("format: %s", None)), |
| 56 | + ("center", (None, 25)), |
| 57 | + ("count", (None, "A")), |
| 58 | + ("encode", (None, "UTF-8")), |
| 59 | + ("endswith", (None, "lo")), |
| 60 | + ("find", (None, "A")), |
| 61 | + ("index", (None, "e")), |
| 62 | + ("join", ("-", None)), |
| 63 | + ("ljust", (None, 12)), |
| 64 | + ("partition", (None, "A")), |
| 65 | + ("replace", (None, "A", "B")), |
| 66 | + ("rfind", (None, "A")), |
| 67 | + ("rindex", (None, "e")), |
| 68 | + ("rjust", (None, 12)), |
| 69 | + ("rpartition", (None, "A")), |
| 70 | + ("split", (None, "A")), |
| 71 | + ("startswith", (None, "A")), |
| 72 | + ("zfill", (None, 12)), |
| 73 | +] |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize("function_name, args", BINARY_FUNCTIONS) |
| 77 | +def test_binary(string_array, unicode_array, function_name, args): |
| 78 | + func = getattr(np.char, function_name) |
| 79 | + if args == (None, None): |
| 80 | + sres = func(string_array, string_array) |
| 81 | + ures = func(unicode_array, unicode_array) |
| 82 | + elif args[0] is None: |
| 83 | + sres = func(string_array, *args[1:]) |
| 84 | + ures = func(string_array, *args[1:]) |
| 85 | + elif args[1] is None: |
| 86 | + sres = func(args[0], string_array) |
| 87 | + ures = func(args[0], string_array) |
| 88 | + else: |
| 89 | + # shouldn't ever happen |
| 90 | + raise RuntimeError |
| 91 | + if sres.dtype == StringDType(): |
| 92 | + ures = ures.astype(StringDType()) |
| 93 | + assert_array_equal(sres, ures) |
| 94 | + |
| 95 | + |
| 96 | +def test_strip(string_array, unicode_array): |
| 97 | + rjs = np.char.rjust(string_array, 25) |
| 98 | + rju = np.char.rjust(unicode_array, 25) |
| 99 | + |
| 100 | + ljs = np.char.ljust(string_array, 25) |
| 101 | + lju = np.char.ljust(unicode_array, 25) |
| 102 | + |
| 103 | + assert_array_equal( |
| 104 | + np.char.lstrip(rjs), |
| 105 | + np.char.lstrip(rju).astype(StringDType()), |
| 106 | + ) |
| 107 | + |
| 108 | + assert_array_equal( |
| 109 | + np.char.rstrip(ljs), |
| 110 | + np.char.rstrip(lju).astype(StringDType()), |
| 111 | + ) |
| 112 | + |
| 113 | + assert_array_equal( |
| 114 | + np.char.strip(ljs), |
| 115 | + np.char.strip(lju).astype(StringDType()), |
| 116 | + ) |
| 117 | + |
| 118 | + assert_array_equal( |
| 119 | + np.char.strip(rjs), |
| 120 | + np.char.strip(rju).astype(StringDType()), |
| 121 | + ) |
0 commit comments