forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_str_01.py
More file actions
131 lines (117 loc) · 2.67 KB
/
test_str_01.py
File metadata and controls
131 lines (117 loc) · 2.67 KB
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
def f():
x: str
x = "ok"
assert x == "ok"
x = "abcdefghijkl"
assert x == "abcdefghijkl"
x = x + "123"
assert x == "abcdefghijkl123"
def test_str_concat():
a: str
a = "abc"
b: str
b = "def"
c: str
c = a + b
assert c == "abcdef"
a = ""
b = "z"
c = a + b
assert c == "z"
def test_str_index():
a: str
a = "012345"
assert a[2] == "2"
assert a[-1] == "5"
assert a[-6] == "0"
def test_str_slice():
a: str
a = "012345"
assert a[2:4] == "23"
assert a[2:3] == a[2]
assert a[:4] == "0123"
assert a[3:] == "345"
# TODO:
# assert a[0:5:-1] == ""
def test_str_title():
a: str = "hello world"
b: str = "hj'kl"
c: str = "hELlo wOrlD"
d: str = "{Hel1o}world"
res: str = a.title()
res2: str = b.title()
res3: str = c.title()
res4: str = d.title()
assert res == "Hello World"
assert res2 == "Hj'Kl"
assert res3 == "Hello World"
assert res4 == "{Hel1O}World"
def test_str_repeat():
a: str
a = "Xyz"
assert a*3 == "XyzXyzXyz"
assert a*2*3 == "XyzXyzXyzXyzXyzXyz"
assert 3*a*3 == "XyzXyzXyzXyzXyzXyzXyzXyzXyz"
assert a*-1 == ""
def test_str_join():
a: str
a = ","
p:list[str] = ["a","b"]
res:str = a.join(p)
assert res == "a,b"
def test_str_join2():
a: str
a = "**"
p:list[str] = ["a","b"]
res:str = a.join(p)
assert res == "a**b"
def test_str_join_empty_str():
a: str
a = ""
p:list[str] = ["a","b"]
res:str = a.join(p)
assert res == "ab"
def test_str_join_empty_list():
a: str
a = "ab"
p:list[str] = []
res:str = a.join(p)
assert res == ""
def test_constant_str_subscript():
assert "abc"[2] == "c"
assert "abc"[:2] == "ab"
def test_str_split():
a: str = "1,2,3"
b: str = "1,2,,3,"
c: str = "1and2and3"
d: str = "1 2 3"
e: str = " 1 2 3 "
f: str = "123"
res: list[str] = a.split(",")
res1: list[str] = b.split(",")
res2: list[str] = c.split("and")
res3: list[str] = d.split()
res4: list[str] = e.split()
res5: list[str] = f.split(" ")
# res6: list[str] = "".split(" ")
assert res == ["1", "2", "3"]
assert res1 == ["1", "2", "", "3", ""]
assert res2 == ["1", "2", "3"]
assert res3 == ["1", "2", "3"]
assert res4 == ["1", "2", "3"]
assert res5 == ["123"]
# assert res6 == [""]
def check():
f()
test_str_concat()
test_str_index()
test_str_slice()
test_str_repeat()
test_str_join()
test_str_join2()
test_str_join_empty_str()
test_str_join_empty_list()
test_constant_str_subscript()
test_str_title()
test_str_split()
check()