forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_jit_string.py
319 lines (279 loc) · 12.5 KB
/
test_jit_string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from test_jit import JitTestCase
from torch.testing._internal.common_utils import run_tests
from typing import List, Tuple
class TestScript(JitTestCase):
def test_str_ops(self):
def test_str_is(s: str) -> Tuple[bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool]:
return s.isupper(), s.islower(), s.isdigit(), s.isspace(), \
s.isalnum(), s.isalpha(), s.isdecimal(), s.isnumeric(), \
s.isidentifier(), s.istitle(), s.isprintable()
def test_str_to(s: str) -> Tuple[str, str, str, str, str]:
return s.upper(), s.lower(), s.capitalize(), s.title(), s.swapcase()
def test_str_strip(s: str) -> Tuple[str, str, str]:
return (
s.lstrip(),
s.rstrip(),
s.strip(),
)
def test_str_strip_char_set(s: str, char_set: str) -> Tuple[str, str, str]:
return (
s.lstrip(char_set),
s.rstrip(char_set),
s.strip(char_set),
)
inputs = ["", "12a", "!B", "12", "a", "B", "aB", "$12", "B12", "AB ",
" \t", " \n", "\na", "abc", "123.3", "s a", "b12a ",
"more strings with spaces", "Titular Strings", "\x0acan'tprintthis",
"spaces at the end ", " begin"]
def test_str_center(i: int, s: str) -> str:
return s.center(i)
def test_str_center_fc(i: int, s: str) -> str:
return s.center(i, '*')
def test_str_center_error(s: str) -> str:
return s.center(10, '**')
def test_ljust(s: str, i: int) -> str:
return s.ljust(i)
def test_ljust_fc(s: str, i: int, fc: str) -> str:
return s.ljust(i, fc)
def test_ljust_fc_err(s: str) -> str:
return s.ljust(10, '**')
def test_rjust(s: str, i: int) -> str:
return s.rjust(i)
def test_rjust_fc(s: str, i: int, fc: str) -> str:
return s.rjust(i, fc)
def test_rjust_fc_err(s: str) -> str:
return s.rjust(10, '**')
def test_zfill(s: str, i: int) -> str:
return s.zfill(i)
for input in inputs:
self.checkScript(test_str_is, (input,))
self.checkScript(test_str_to, (input,))
self.checkScript(test_str_strip, (input,))
for char_set in ["abc", "123", " ", "\t"]:
self.checkScript(test_str_strip_char_set, (input, char_set))
for i in range(7):
self.checkScript(test_str_center, (i, input,))
self.checkScript(test_str_center_fc, (i, input,))
self.checkScript(test_ljust, (input, i))
self.checkScript(test_ljust_fc, (input, i, '*'))
self.checkScript(test_rjust, (input, i))
self.checkScript(test_rjust_fc, (input, i, '*'))
self.checkScript(test_zfill, (input, i))
with self.assertRaises(Exception):
test_str_center_error("error")
test_ljust("error")
def test_count() -> Tuple[int, int, int, int, int, int, int, int, int, int, int, int]:
return (
"hello".count("h"),
"hello".count("h", 0, 1),
"hello".count("h", -3),
"hello".count("h", -10, 1),
"hello".count("h", 0, -10),
"hello".count("h", 0, 10),
"hello".count("ell"),
"hello".count("ell", 0, 1),
"hello".count("ell", -3),
"hello".count("ell", -10, 1),
"hello".count("ell", 0, -10),
"hello".count("ell", 0, 10)
)
self.checkScript(test_count, ())
def test_endswith() -> Tuple[bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool]:
return (
"hello".endswith("lo"),
"hello".endswith("lo", 0),
"hello".endswith("lo", -2),
"hello".endswith("lo", -8),
"hello".endswith("lo", 0, -5),
"hello".endswith("lo", -2, 3),
"hello".endswith("lo", -8, 4),
"hello".endswith("l"),
"hello".endswith("l", 0),
"hello".endswith("l", -2),
"hello".endswith("l", -8),
"hello".endswith("l", 0, -5),
"hello".endswith("l", -2, 3),
"hello".endswith("l", -8, 4)
)
self.checkScript(test_endswith, ())
def test_startswith() -> Tuple[bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool]:
return (
"hello".startswith("lo"),
"hello".startswith("lo", 0),
"hello".startswith("lo", -2),
"hello".startswith("lo", -8),
"hello".startswith("lo", 0, -5),
"hello".startswith("lo", -2, 3),
"hello".startswith("lo", -8, 4),
"hello".startswith("l"),
"hello".startswith("l", 0),
"hello".startswith("l", -2),
"hello".startswith("l", -8),
"hello".startswith("l", 0, -5),
"hello".startswith("l", -2, 3),
"hello".startswith("l", -8, 4)
)
self.checkScript(test_startswith, ())
def test_expandtabs() -> Tuple[str, str, str, str, str, str]:
return (
'xyz\t82345\tabc'.expandtabs(),
'xyz\t32345\tabc'.expandtabs(3),
'xyz\t52345\tabc'.expandtabs(5),
'xyz\t62345\tabc'.expandtabs(6),
'xyz\t72345\tabc'.expandtabs(7),
'xyz\t62345\tabc'.expandtabs(-5),
)
self.checkScript(test_expandtabs, ())
def test_rfind() -> Tuple[int, int, int, int, int, int, int, int, int]:
return (
"hello123abc".rfind("llo"),
"hello123abc".rfind("12"),
"hello123abc".rfind("ab"),
"hello123abc".rfind("ll", -1),
"hello123abc".rfind("12", 4),
"hello123abc".rfind("ab", -7),
"hello123abc".rfind("ll", -1, 8),
"hello123abc".rfind("12", 4, -4),
"hello123abc".rfind("ab", -7, -20),
)
self.checkScript(test_rfind, ())
def test_find() -> Tuple[int, int, int, int, int, int, int, int, int]:
return (
"hello123abc".find("llo"),
"hello123abc".find("12"),
"hello123abc".find("ab"),
"hello123abc".find("ll", -1),
"hello123abc".find("12", 4),
"hello123abc".find("ab", -7),
"hello123abc".find("ll", -1, 8),
"hello123abc".find("12", 4, -4),
"hello123abc".find("ab", -7, -20),
)
self.checkScript(test_find, ())
def test_index() -> Tuple[int, int, int, int, int, int]:
return (
"hello123abc".index("llo"),
"hello123abc".index("12"),
"hello123abc".index("ab"),
"hello123abc".index("12", 4),
"hello123abc".index("ab", -7),
"hello123abc".index("12", 4, -4),
)
self.checkScript(test_index, ())
def test_rindex() -> Tuple[int, int, int, int, int, int]:
return (
"hello123abc".rindex("llo"),
"hello123abc".rindex("12"),
"hello123abc".rindex("ab"),
"hello123abc".rindex("12", 4),
"hello123abc".rindex("ab", -7),
"hello123abc".rindex("12", 4, -4),
)
self.checkScript(test_rindex, ())
def test_replace() -> Tuple[str, str, str, str, str, str, str]:
return (
"hello123abc".replace("llo", "sdf"),
"ff".replace("f", "ff"),
"abc123".replace("a", "testing"),
"aaaaaa".replace("a", "testing", 3),
"bbb".replace("a", "testing", 3),
"ccc".replace("c", "ccc", 3),
"cc".replace("c", "ccc", -3),
)
self.checkScript(test_replace, ())
def test_partition() -> Tuple[Tuple[str, str, str], Tuple[str, str, str], Tuple[str, str, str],
Tuple[str, str, str], Tuple[str, str, str], Tuple[str, str, str],
Tuple[str, str, str]]:
return (
"hello123abc".partition("llo"),
"ff".partition("f"),
"abc123".partition("a"),
"aaaaaa".partition("testing"),
"bbb".partition("a"),
"ccc".partition("ccc"),
"cc".partition("ccc"),
)
self.checkScript(test_partition, ())
def test_rpartition() -> Tuple[Tuple[str, str, str], Tuple[str, str, str], Tuple[str, str, str],
Tuple[str, str, str], Tuple[str, str, str], Tuple[str, str, str],
Tuple[str, str, str]]:
return (
"hello123abc".rpartition("llo"),
"ff".rpartition("f"),
"abc123".rpartition("a"),
"aaaaaa".rpartition("testing"),
"bbb".rpartition("a"),
"ccc".rpartition("ccc"),
"cc".rpartition("ccc"),
)
self.checkScript(test_rpartition, ())
def test_split() -> Tuple[List[str], List[str], List[str], List[str], List[str],
List[str], List[str], List[str], List[str], List[str], List[str]]:
return (
"a a a a a".split(),
"a a a a a".split(),
" a a\ta \v a \v\f\n a \t ".split(),
" a a a a a ".split(" "),
"a a a a a ".split(" ", 10),
"a a a a a ".split(" ", -1),
"a a a a a ".split(" ", 3),
" a a a a a ".split("*"),
" a*a a*a a".split("*"),
" a*a a*a a ".split("*", -1),
" a*a a*a a ".split("a*", 10),
)
self.checkScript(test_split, ())
# test raising error for empty separator
def test_split_empty_separator():
s = "test"
return s.split("")
self.checkScriptRaisesRegex(test_split_empty_separator, (), Exception,
"empty separator")
def test_rsplit() -> Tuple[List[str], List[str], List[str], List[str], List[str],
List[str], List[str], List[str], List[str]]:
return (
"a a a a a".rsplit(),
" a a a a a ".rsplit(" "),
"a a a a a ".rsplit(" ", 10),
"a a a a a ".rsplit(" ", -1),
"a a a a a ".rsplit(" ", 3),
" a a a a a ".rsplit("*"),
" a*a a*a a ".rsplit("*"),
" a*a a*a a ".rsplit("*", -1),
" a*a a*a a".rsplit("a*", 10),
)
self.checkScript(test_rsplit, ())
def test_splitlines() -> Tuple[List[str], List[str], List[str], List[str],
List[str], List[str]]:
return (
"hello\ntest".splitlines(),
"hello\n\ntest\n".splitlines(),
"hello\ntest\n\n".splitlines(),
"hello\vtest".splitlines(),
"hello\v\f\ntest".splitlines(),
"hello\ftest".splitlines(),
)
self.checkScript(test_splitlines, ())
def test_str_cmp(a: str, b: str) -> Tuple[bool, bool, bool, bool, bool, bool]:
return a != b, a == b, a < b, a > b, a <= b, a >= b
for i in range(len(inputs) - 1):
self.checkScript(test_str_cmp, (inputs[i], inputs[i + 1]))
def test_str_join():
return (
",".join(["a"]),
",".join(["a", "b", "c"]),
",".join(["aa", "bb", "cc"]),
",".join(["a,a", "bb", "c,c"]),
"**a**".join(["b", "c", "d", "e"]),
"".join(["a", "b", "c"]),
)
self.checkScript(test_str_join, ())
def test_bool_conversion(a: str):
if a:
return a
else:
return "default"
self.checkScript(test_bool_conversion, ("nonempty",))
self.checkScript(test_bool_conversion, ("",))
if __name__ == '__main__':
run_tests()