-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathtest_str_attributes.py
executable file
·524 lines (454 loc) · 14.7 KB
/
test_str_attributes.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
def capitalize():
s: str
s = "tom and jerry"
print(s.capitalize())
assert s.capitalize() == "Tom and jerry"
s = "12wddd"
print(s)
assert s.capitalize() == s
s = " tom and jerry"
print(s.capitalize())
assert s.capitalize() == s
assert "empty string" .capitalize() == "Empty string"
assert "".capitalize() == ""
assert "lPyThOn".capitalize() == "Lpython"
x: str
x = "lPyThOn"
assert x.capitalize() == "Lpython"
def lower():
s: str
s = "AaaaAABBbbbbBB!@12223BN"
assert s.lower() == "aaaaaabbbbbbbb!@12223bn"
assert "DDd12Vv" .lower() == "ddd12vv"
assert "".lower() == ""
def upper():
s: str
s = "AaaaAABBbbbbBB!@12223BN"
assert s.upper() == "AAAAAABBBBBBBB!@12223BN"
assert "DDd12Vv".upper() == "DDD12VV"
assert "".upper() == ""
def strip():
s: str
s = " AASAsaSas "
assert s.rstrip() == " AASAsaSas"
assert s.lstrip() == "AASAsaSas "
assert s.strip() == "AASAsaSas"
assert " AASAsaSas " .rstrip() == " AASAsaSas"
assert " AASAsaSas " .lstrip() == "AASAsaSas "
assert " AASAsaSas " .strip() == "AASAsaSas"
assert "".strip() == ""
def swapcase():
s: str
s = "aaaaaabbbbbbbb!@12223bn"
assert s.swapcase() == "AAAAAABBBBBBBB!@12223BN"
assert "AASAsaSas" .swapcase() == "aasaSAsAS"
assert "".swapcase() == ""
def find():
s: str
sub: str
s = "AaaaAABBbbbbBB!@12223BN"
sub = "@"
assert s.find(sub) == 15
assert s.find('B') == 6
assert "empty strings" .find("string") == 6
s2: str
s2 = "Well copying a string from a website makes us prone to copyright claims. Can you just write something of your own? Like just take this review comment and put it as a string?"
assert s2.find("of") == 102
assert s2.find("own") == 110
assert s2.find("this") == 130
assert s2.find("") == 0
assert "".find("dd") == -1
assert "".find("") == 0
s2 = ""
assert s2.find("") == 0
assert s2.find("we") == -1
assert "".find("") == 0
def count():
s: str
sub: str
s = "ABC ABCDAB ABCDABCDABDE"
sub = "ABC"
print(s.count(sub), s.count("ABC"))
assert s.count(sub) == 4
assert s.count("ABC") == 4
sub = "AB"
print(s.count(sub), s.count("AB"))
assert s.count(sub) == 6
assert s.count("AB") == 6
sub = "ABC"
print("ABC ABCDAB ABCDABCDABDE".count(sub), "ABC ABCDAB ABCDABCDABDE".count("ABC"))
assert "ABC ABCDAB ABCDABCDABDE".count(sub) == 4
assert "ABC ABCDAB ABCDABCDABDE".count("ABC") == 4
sub = "AB"
assert "ABC ABCDAB ABCDABCDABDE".count(sub) == 6
assert "ABC ABCDAB ABCDABCDABDE".count("AB") == 6
def startswith():
s: str
s = " empty"
assert s.startswith(" ") == True
assert " @" .startswith(" ") == True
assert " emptyAaaaAABBbbbbBB" .startswith(s) == True
assert " emptyAaaaAABBbbbbBB" .startswith("AABB") == False
assert " emptyAaaaAABBbbbbBB" .startswith("emptyAaxX") == False
assert "". startswith("sd") == False
assert "empty" .startswith("") == True
assert "".startswith("") == True
assert s.startswith("") == True
s = ""
assert s.startswith("") == True
assert s.startswith("sdd") == False
assert "".startswith("ok") == False
def endswith():
# The following test suite fulfils the control flow graph coverage
# in terms of Statement Coverage and Branch Coverage associated with endwith() functionality.
# Case 1: When string is constant and suffix is also constant
assert "".endswith("") == True
assert "".endswith(" ") == False
assert "".endswith("%") == False
assert "".endswith("a1234PT#$") == False
assert "".endswith("blah blah") == False
assert " rendezvous 5:30 ".endswith("") == True
assert " rendezvous 5:30 ".endswith(" ") == True
assert " rendezvous 5:30 ".endswith(" 5:30 ") == True
assert " rendezvous 5:30 ".endswith("apple") == False
assert "two plus".endswith("longer than string") == False
# Case 2: When string is constant and suffix is variable
suffix: str
suffix = ""
assert "".endswith(suffix) == True
suffix = " "
assert "".endswith(suffix) == False
suffix = "5:30 "
assert " rendezvous 5:30 ".endswith(suffix) == True
suffix = ""
assert " rendezvous 5:30 ".endswith(suffix) == True
suffix = "apple"
assert " rendezvous 5:30 ".endswith(suffix) == False
suffix = "longer than string"
assert "two plus".endswith(suffix) == False
# Case 3: When string is variable and suffix is either constant or variable
s: str
s = ""
assert s.endswith("") == True
assert s.endswith("apple") == False
assert s.endswith(" ") == False
assert s.endswith(suffix) == False
s = " rendezvous 5 "
assert s.endswith(" $3324") == False
assert s.endswith("5 ") == True
assert s.endswith(s) == True
suffix = "vous 5 "
assert s.endswith(suffix) == True
suffix = "apple"
assert s.endswith(suffix) == False
def partition():
# Note: Both string or seperator cannot be empty
# Case 1: When string is constant and seperator is also constant
assert " ".partition(" ") == ("", " ", " ")
assert "apple mango".partition(" ") == ("apple", " ", "mango")
assert "applemango".partition("afdnjkfsn") == ("applemango", "", "")
assert "applemango".partition("an") == ("applem", "an", "go")
assert "applemango".partition("mango") == ("apple", "mango", "")
assert "applemango".partition("applemango") == ("", "applemango", "")
assert "applemango".partition("ppleman") == ("a", "ppleman", "go")
assert "applemango".partition("pplt") == ("applemango", "", "")
# Case 2: When string is constant and seperator is variable
seperator: str
seperator = " "
assert " ".partition(seperator) == ("", " ", " ")
seperator = " "
assert "apple mango".partition(seperator) == ("apple", " ", "mango")
seperator = "5:30 "
assert " rendezvous 5:30 ".partition(
seperator) == (" rendezvous ", "5:30 ", "")
seperator = "^&"
assert "@#$%^&*()#!".partition(seperator) == ("@#$%", "^&", "*()#!")
seperator = "daddada "
assert " rendezvous 5:30 ".partition(
seperator) == (" rendezvous 5:30 ", "", "")
seperator = "longer than string"
assert "two plus".partition(seperator) == ("two plus", "", "")
# Case 3: When string is variable and seperator is either constant or variable
s: str
s = "tomorrow"
assert s.partition("apple") == ("tomorrow", "", "")
assert s.partition("rr") == ("tomo", "rr", "ow")
assert s.partition(seperator) == ("tomorrow", "", "")
s = "rendezvous 5"
assert s.partition(" ") == ("rendezvous", " ", "5")
assert s.partition("5") == ("rendezvous ", "5", "")
assert s.partition(s) == ("", "rendezvous 5", "")
seperator = "vous "
assert s.partition(seperator) == ("rendez", "vous ", "5")
seperator = "apple"
assert s.partition(seperator) == ("rendezvous 5", "", "")
def is_lower():
# Case 1: When constant string is present
assert "".islower() == False
assert "APPLE".islower() == False
assert "4432632479".islower() == False
assert "%#$#$#32a".islower() == True
assert "apple".islower() == True
assert "apple is a fruit".islower() == True
# Case 2: When variable string is present
s: str
s = "APPLE"
assert s.islower() == False
s = "238734587"
assert s.islower() == False
s = "%#$#$#32a"
assert s.islower() == True
s = "apple"
assert s.islower() == True
s = "apple is a fruit"
assert s.islower() == True
def is_upper():
# Case 1: When constant string is present
assert "".isupper() == False
assert "apple".isupper() == False
assert "4432632479".isupper() == False
assert "%#$#$#32A".isupper() == True
assert "APPLE".isupper() == True
assert "APPLE IS A FRUIT".isupper() == True
# Case 2: When variable string is present
s: str
s = "apple"
assert s.isupper() == False
s = "238734587"
assert s.isupper() == False
s = "%#$#$#32A"
assert s.isupper() == True
s = "APPLE"
assert s.isupper() == True
s = "APPLE IS A FRUIT"
assert s.isupper() == True
def is_decimal():
# Case 1: When constant string is present
assert "".isdecimal() == False
assert "apple".isdecimal() == False
assert "4432632479".isdecimal() == True
assert "%#$#$#32A".isdecimal() == False
assert "1.25".isdecimal() == False
assert "-325".isdecimal() == False
assert "12 35".isdecimal() == False
# Case 2: When variable string is present
s: str
s = "apple"
assert s.isdecimal() == False
s = "238734587"
assert s.isdecimal() == True
s = "%#$#$#32A"
assert s.isdecimal() == False
s = "1.35"
assert s.isdecimal() == False
s = "-42556"
assert s.isdecimal() == False
s = "12 34"
assert s.isdecimal() == False
def is_ascii():
# Case 1: When constant string is present
assert "".isascii() == True
assert " ".isascii() == True
assert "Hello, World123!".isascii() == True
assert "Hëllö, Wörld!".isascii() == False
assert "This is a test string with some non-ASCII characters: 🚀".isascii() == False
assert "\t\n\r".isascii() == True
assert "12 35".isascii() == True
# # Case 2: When variable string is present
s: str
s = " "
assert s.isascii() == True
s = "Hello, World!"
assert s.isascii() == True
s = "Hëllö, Wörld!"
assert s.isascii() == False
s = "This is a test string with some non-ASCII characters: 🚀"
assert s.isascii() == False
s = "\t\n\r"
assert s.isascii() == True
s = "123 45 6"
assert s.isascii() == True
def is_alpha():
a: str = "helloworld"
b: str = "hj kl"
c: str = "a12(){}A"
d: str = " "
e: str = ""
res: bool = a.isalpha()
res2: bool = b.isalpha()
res3: bool = c.isalpha()
res4: bool = d.isalpha()
res5: bool = e.isalpha()
assert res == True
assert res2 == False
assert res3 == False
assert res4 == False
assert res5 == False
assert "helloworld".isalpha() == True
assert "hj kl".isalpha() == False
assert "a12(){}A".isalpha() == False
assert " ".isalpha() == False
assert "".isalpha() == False
def is_title():
a: str = "Hello World"
b: str = "Hj'kl"
c: str = "hELlo wOrlD"
d: str = " Hello"
e: str = " "
res: bool = a.istitle()
res2: bool = b.istitle()
res3: bool = c.istitle()
res4: bool = d.istitle()
res5: bool = e.istitle()
assert res == True
assert res2 == False
assert res3 == False
assert res4 == True
assert res5 == False
assert "Hello World".istitle() == True
assert "Hj'kl".istitle() == False
assert "hELlo wOrlD".istitle() == False
assert " Hello".istitle() == True
assert " ".istitle() == False
def is_space():
s0: str = ""
assert s0.isspace() == False
assert "".isspace() == False
s1: str = " \t\n\v\f\r"
assert s1.isspace() == True
assert " \t\n\v\f\r".isspace() == True
s2: str = " \t\n\v\f\rabcd"
assert s2.isspace() == False
assert " \t\n\v\f\rabcd".isspace() == False
s3: str = "abcd \t\n\v\f\ref"
assert s3.isspace() == False
assert "abcd \t\n\v\f\ref".isspace() == False
s4: str = " \\t\n\v\f\r"
assert s4.isspace() == False
assert " \\t\n\v\f\r".isspace() == False
s5: str = " \\t\\n\\v\\f\\r"
assert s5.isspace() == False
assert " \\t\\n\\v\\f\\r".isspace() == False
s6: str = "Hello, LPython!\n"
assert s6.isspace() == False
assert "Hello, LPython!\n".isspace() == False
s7: str = "\t\tHello! \n"
assert s7.isspace() == False
assert "\t\tHello! \n".isspace() == False
s8: str = " \t \n \v \f \r "
assert s8.isspace() == True
assert " \t \n \v \f \r ".isspace() == True
assert "\n".isspace() == True
assert " ".isspace() == True
assert "\r".isspace() == True
assert "".isspace() == False
s: str = " "
assert s.isspace() == True
s = "a"
assert s.isspace() == False
s = ""
assert s.isspace() == False
def is_alnum():
a: str = "helloworld"
b: str = "hj kl"
c: str = "a12(){}A"
d: str = " "
e: str = ""
f: str = "ab23"
g: str = "ab2%3"
res: bool = a.isalnum()
res2: bool = b.isalnum()
res3: bool = c.isalnum()
res4: bool = d.isalnum()
res5: bool = e.isalnum()
res6: bool = f.isalnum()
res7: bool = g.isalnum()
assert res == True
assert res2 == False
assert res3 == False
assert res4 == False
assert res5 == False
assert res6 == True
assert res7 == False
assert "helloworld".isalnum() == True
assert "hj kl".isalnum() == False
assert "a12(){}A".isalnum() == False
assert " ".isalnum() == False
assert "".isalnum() == False
assert "ab23".isalnum() == True
assert "ab2%3".isalnum() == False
def is_numeric():
a: str = "123"
b: str = "12 34"
c: str = "-123"
d: str = "12.3"
e: str = " "
f: str = ""
g: str = "ab2%3"
res: bool = a.isnumeric()
res2: bool = b.isnumeric()
res3: bool = c.isnumeric()
res4: bool = d.isnumeric()
res5: bool = e.isnumeric()
res6: bool = f.isnumeric()
res7: bool = g.isnumeric()
assert res == True
assert res2 == False
assert res3 == False
assert res4 == False
assert res5 == False
assert res6 == False
assert res7 == False
assert "123".isnumeric() == True
assert "12 34".isnumeric() == False
assert "-123".isnumeric() == False
assert "12.3".isnumeric() == False
assert " ".isnumeric() == False
assert "".isnumeric() == False
assert "ab2%3".isnumeric() == False
def center():
s: str = "test"
assert s.center(8,'*') == "**test**"
assert s.center(11) == " test "
assert s.center(2) == "test"
assert s.center(4) == "test"
assert s.center(9,'/') == "///test//"
def expandtabs():
s: str = '01\t012\t0123\t01234'
assert s.expandtabs() == "01 012 0123 01234"
assert s.expandtabs(4) == "01 012 0123 01234"
assert s.expandtabs(-1) == "01012012301234"
s = '\t'
assert s.expandtabs() == " "
s = ''
assert s.expandtabs() == ""
s = '\tThis\ris\na\ttest'
assert s.expandtabs(4) == " This\ris\na test"
s = '\t\t\t'
assert s.expandtabs(2) == " "
s = 'test\ttest'
assert s.expandtabs(0) == "testtest"
assert s.expandtabs(-5) == "testtest"
def check():
capitalize()
lower()
upper()
strip()
swapcase()
find()
count()
startswith()
endswith()
partition()
is_lower()
is_upper()
is_decimal()
is_ascii()
is_alpha()
is_title()
is_space()
is_alnum()
is_numeric()
center()
expandtabs()
check()