-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiaolong-surely-TestXML.py
235 lines (204 loc) · 8.49 KB
/
xiaolong-surely-TestXML.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
#!/usr/bin/env python
# ---------------------------
# XML.py
# Copyright (C) 2013
# Xiaolong Li & Shirley Li
# -------------------------------
"""
To test the program:
% python TestXML.py >& TestXML.py.out
% chmod ugo+x TestXML.py
% TestXML.py >& TestXML.py.out
"""
# -------
# imports
# -------
import StringIO
import unittest
from XML import *
# -----------
# TestXML
# -----------
class TestXML (unittest.TestCase) :
# ----------
# xml_read
# ----------
def test_xml_read_1 (self) :
reader = StringIO.StringIO("<html>\n\t"
"<head> </head>\n\t<body></body>\n</html>")
token_list = xml_read(reader)
self.assert_(token_list == ["html", "head", "/head",
"body", "/body", "/html"])
def test_xml_read_2 (self) :
reader = StringIO.StringIO("<Team><Cooly></Cooly></Team>")
token_list = xml_read(reader)
self.assert_(token_list == ["Team", "Cooly", "/Cooly", "/Team"])
def test_xml_read_3 (self) :
reader = StringIO.StringIO("<silly> \t\n </silly>\t\n")
token_list = xml_read(reader)
self.assert_(token_list == ["silly", "/silly"])
# ----------
# xml_create_dict
# ----------
def test_xml_create_dict_1 (self) :
token_list = ["Team", "Cooly", "/Cooly", "/Team"]
dict_list = xml_create_dict(token_list)
self.assert_(len(dict_list) == 1)
self.assert_(dict_list[0] ==
{"Team": [1, {"Cooly": [2, {}]}]})
def test_xml_create_dict_2 (self) :
token_list = ["THU", "Team", "ACRush", "/ACRush", "Jelly",
"/Jelly", "Cooly", "/Cooly", "/Team", "JiaJia", "Team",
"Ahyangyi", "/Ahyangyi", "Dragon", "/Dragon", "Cooly",
"Amber", "/Amber", "/Cooly", "/Team", "/JiaJia", "/THU",
"Team", "Cooly", "/Cooly", "/Team"]
dict_list = xml_create_dict(token_list)
self.assert_(len(dict_list) == 2)
self.assert_(dict_list[0] ==
{"THU":[1, {"Team":[2, {"ACRush":[3, {}], "Jelly":[4, {}],
"Cooly": [5, {}]}], "JiaJia":[6, {"Team":[7, {"Ahyangyi":
[8, {}], "Dragon": [9, {}], "Cooly":[10, {"Amber": [11, {}
]}]}]}]}]})
self.assert_(dict_list[1] ==
{"Team": [1, {"Cooly": [2, {}]}]})
def test_xml_create_dict_3 (self) :
token_list = ["html", "head", "/head", "body", "/body", "/html",
"body", "/body"]
dict_list = xml_create_dict(token_list)
self.assert_(len(dict_list) == 2)
self.assert_(dict_list[0] ==
{"html":[1, {"head":[2, {}], "body":[3, {}]}]})
self.assert_(dict_list[1] ==
{"body":[1, {}]})
# ----------
# xml_match
# ----------
def test_xml_match_1 (self) :
# false case
tree = {"Team":[1, {"Hotty":[2, {"Cooly":[3, {}]}]}]}
pattern = {"Team": [1, {"Cooly": [2, {}]}]}
self.assert_(xml_match(tree, pattern) == False)
def test_xml_match_2 (self) :
# false case
tree = {"html":[1, {"head":[2, {}], "foot":[3, {}]}]}
pattern = {"html":[1, {"head":[2, {}], "body":[3, {}]}]}
self.assert_(xml_match(tree, pattern) == False)
def test_xml_match_3 (self) :
# true case
tree = {"Team":[2, {"ACRush":[3, {}], "Jelly":[4, {}],
"Cooly": [5, {}]}]}
pattern = {"Team": [1, {"Cooly": [2, {}]}]}
self.assert_(xml_match(tree, pattern) == True)
def test_xml_match_4 (self) :
# true case
tree = {"Team":[7, {"Ahyangyi":[8, {}], "Dragon": [9, {}],
"Cooly":[10, {"Amber": [11, {}]}]}]}
pattern = {"Team": [1, {"Cooly": [2, {}]}]}
self.assert_(xml_match(tree, pattern) == True)
# ----------
# xml_match
# ----------
def test_xml_match_all_1 (self) :
# no match case
tree = {"THU":[1, {"Team":[2, {"ACRush":[3, {}], "Jelly":[4, {}],
"Cooly": [5, {}]}], "JiaJia":[6, {"Team":[7, {"Ahyangyi":
[8, {}], "Dragon": [9, {}], "Cooly":[10, {"Amber": [11, {}
]}]}]}]}]}
pattern = {"Hi":[1, {}]}
self.assert_(xml_linked2list(xml_match_all(tree, pattern)[0]) == [])
def test_xml_match_all_2 (self) :
# no match case
tree = {"THU":[1, {"Team":[2, {"ACRush":[3, {}], "Jelly":[4, {}],
"Cooly": [5, {}]}], "JiaJia":[6, {"Team":[7, {"Ahyangyi":
[8, {}], "Dragon": [9, {}], "Cooly":[10, {"Amber": [11, {}
]}]}]}]}]}
pattern = {"ACRush":[1, {}]}
self.assert_(xml_linked2list(xml_match_all(tree, pattern)[0]) == [3])
def test_xml_match_all_3 (self) :
# no match case
tree = {"THU":[1, {"Team":[2, {"ACRush":[3, {}], "Jelly":[4, {}],
"Cooly": [5, {}]}], "JiaJia":[6, {"Team":[7, {"Ahyangyi":
[8, {}], "Dragon": [9, {}], "Cooly":[10, {"Amber": [11, {}
]}]}]}]}]}
pattern = {"Team": [1, {"Cooly": [2, {}]}]}
l = xml_linked2list(xml_match_all(tree, pattern)[0])
l.sort()
self.assert_(l == [2, 7])
# ----------
# xml_solve
# ----------
def test_xml_solve_1 (self) :
reader = StringIO.StringIO("<html>\n\t"
"<head> </head>\n\t<body></body>\n</html><body></body>")
writer = StringIO.StringIO()
xml_solve(reader, writer)
self.assert_(writer.getvalue() == "1\n3\n")
def test_xml_solve_2 (self) :
reader = StringIO.StringIO("""<THU>
<Team>
<ACRush></ACRush>
<Jelly></Jelly>
<Cooly></Cooly>
</Team>
<JiaJia>
<Team>
<Ahyangyi></Ahyangyi>
<Dragon></Dragon>
<Cooly><Amber></Amber></Cooly>
</Team>
</JiaJia>
</THU>
<Team><Cooly></Cooly></Team>""")
writer = StringIO.StringIO()
xml_solve(reader, writer)
self.assert_(writer.getvalue() == "2\n2\n7\n")
def test_xml_solve_3 (self) :
reader = StringIO.StringIO("""<THU>
<Team>
<ACRush></ACRush>
<Jelly></Jelly>
<Cooly></Cooly>
</Team>
<JiaJia>
<Team>
<Ahyangyi></Ahyangyi>
<Dragon></Dragon>
<Amber><Cooly></Cooly></Amber>
</Team>
</JiaJia>
</THU>
<Team><Cooly></Cooly></Team>""")
writer = StringIO.StringIO()
xml_solve(reader, writer)
self.assert_(writer.getvalue() == "1\n2\n")
def test_xml_solve_4 (self) :
reader = StringIO.StringIO("""<Team><Cooly></Cooly></Team>
<THU>
<Team>
<ACRush></ACRush>
<Jelly></Jelly>
<Cooly></Cooly>
</Team>
<JiaJia>
<Team>
<Ahyangyi></Ahyangyi>
<Dragon></Dragon>
<Amber><Cooly></Cooly></Amber>
</Team>
</JiaJia>
</THU>""")
writer = StringIO.StringIO()
xml_solve(reader, writer)
self.assert_(writer.getvalue() == "0\n")
def test_xml_solve_5 (self) :
reader = StringIO.StringIO("""<Team><Cooly></Cooly></Team>
<Team><Cooly></Cooly></Team>""")
writer = StringIO.StringIO()
xml_solve(reader, writer)
self.assert_(writer.getvalue() == "1\n1\n")
# ----
# main
# ----
print ("TestXML.py")
unittest.main()
print ("Done.")