-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·391 lines (302 loc) · 11.7 KB
/
tests.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
import unittest
import nfa_utils
class TestNFA(unittest.TestCase):
def test_single_symbol_nfa(self):
print("Testing single symbol regex NFA")
nfa = nfa_utils.get_single_symbol_regex("x")
print(nfa)
# test NFA state
self.assertEqual(nfa.alphabet,{'x'})
self.assertEqual(nfa.states, {0, 1})
self.assertEqual(nfa.transition_function, {(0, 'x'): {1}})
self.assertEqual(nfa.accept_states, {1})
self.assertEqual(nfa.in_states, {0})
# test NFA behaviour
self.assertFalse(nfa.is_accepting())
nfa.feed_symbol("x")
self.assertEqual(nfa.in_states, {1})
self.assertTrue(nfa.is_accepting())
nfa.feed_symbol("x")
self.assertFalse(nfa.is_accepting())
self.assertEqual(nfa.in_states, set())
self.assertTrue(nfa.is_dead())
def test_nfa_concat(self):
print("Testing concatenated NFA a.b")
# recognizes "a.b"
nfa = nfa_utils.get_concat(nfa_utils.get_single_symbol_regex("a"),
nfa_utils.get_single_symbol_regex("b"))
print(nfa)
self.assertFalse(nfa.is_accepting())
nfa.feed_symbol("a")
self.assertFalse(nfa.is_accepting())
nfa.feed_symbol("b")
self.assertTrue(nfa.is_accepting())
nfa.feed_symbol("a")
self.assertFalse(nfa.is_accepting())
def test_big_nfa_concat(self):
print("Testing concatenated NFA a.b.c.c.b.a.G.G.G")
concat_strings = ["abc", "cba", "GGG"]
# construct a large NFA by creating 3 sub NFAs, and
# concatenating them together
nfa = None
for str in concat_strings:
# construct sub nfa
sub_nfa = None
for c in str:
if sub_nfa is None:
sub_nfa = nfa_utils.get_single_symbol_regex(c)
else:
sub_nfa = nfa_utils.get_concat(sub_nfa, nfa_utils.get_single_symbol_regex(c))
# combine this sub NFA with the overall NFA
if nfa is None:
nfa = sub_nfa
else:
nfa = nfa_utils.get_concat(nfa, sub_nfa)
print(nfa)
# ensure the NFA does not accept until the entire input string has been fed in
input_str = "".join(concat_strings)
for symbol in input_str:
self.assertFalse(nfa.is_accepting())
nfa.feed_symbol(symbol)
self.assertTrue(nfa.is_accepting())
def test_empty_string(self):
print("Testing empty string transitions")
# construct an NFA equivalent to a|<empty string>
nfa = nfa_utils.get_single_symbol_regex("a")
nfa.add_state(2, True)
nfa.add_transition(0, "", {2})
nfa.reset()
print(nfa)
# NFA should accept straight away due to the empty string transition
# from the initial state to an accept state
self.assertTrue(nfa.is_accepting())
# after feeding an 'a', the NFA should accept; state 2 is dead
# but a transition from state 0 to 1 (accepts) should still work
nfa.feed_symbol("a")
self.assertTrue(nfa.is_accepting())
# after feeding a final a, the NFA should be dead (no transitions available
# for the current "active" states)
nfa.feed_symbol("a")
self.assertFalse(nfa.is_accepting())
self.assertTrue(nfa.is_dead())
def test_nfa_union(self):
print("Testing NFA union a.b|c.d")
# build NFA
nfa = nfa_utils.get_union(
nfa_utils.get_regex_nfa("a.b"),
nfa_utils.get_regex_nfa("c.d")
)
nfa.reset()
print(nfa)
# test accepts a.b
self.assertFalse(nfa.is_accepting())
nfa.feed_symbol("a")
self.assertFalse(nfa.is_accepting())
nfa.feed_symbol("b")
self.assertTrue(nfa.is_accepting())
nfa.feed_symbol("c")
self.assertFalse(nfa.is_accepting())
# test accepts c.d
nfa.reset()
self.assertFalse(nfa.is_accepting())
nfa.feed_symbol("c")
self.assertFalse(nfa.is_accepting())
nfa.feed_symbol("d")
self.assertTrue(nfa.is_accepting())
nfa.feed_symbol("e")
self.assertFalse(nfa.is_accepting())
def test_kleene_star(self):
print("Testing kleene star NFA a*")
# build NFA
nfa = nfa_utils.get_regex_nfa("a*")
nfa.reset()
print(nfa)
# test if accepts empty string
self.assertTrue(nfa.is_accepting())
# test if accepts 1 or many a's
for i in range(20):
nfa.feed_symbol("a")
self.assertTrue(nfa.is_accepting())
# test if rejects other symbols
nfa.feed_symbol("b")
self.assertFalse(nfa.is_accepting())
self.assertTrue(nfa.is_dead())
def test_big_kleene_star(self):
print("Testing big kleene star NFA a*b*c*")
# build NFA
nfa = nfa_utils.get_regex_nfa("a*b*c*")
nfa.reset()
print(nfa)
# test if accepts empty string (none of a, b, or c)
self.assertTrue(nfa.is_accepting())
# test if rejects other symbol
nfa.feed_symbol("d")
self.assertFalse(nfa.is_accepting())
# test if accepts many a's, b's, and c's in order
symbols = ["a", "b", "c"]
# test different numbers of a's, b's, and c's
for i in range(10):
nfa.reset()
# feed each symbol a number of times
for symbol in symbols:
for j in range(i + 1):
nfa.feed_symbol(symbol)
self.assertTrue(nfa.is_accepting())
# test if rejects other symbol
nfa.feed_symbol("d")
self.assertFalse(nfa.is_accepting())
def test_one_or_more_of(self):
print("Testing \"one or more of\" operator a+")
# build NFA
nfa = nfa_utils.get_regex_nfa("a+")
nfa.reset()
print(nfa)
# test if rejects empty string
self.assertFalse(nfa.is_accepting())
# test if accepts 1 or many a's
for i in range(20):
nfa.feed_symbol("a")
self.assertTrue(nfa.is_accepting())
# test if rejects other symbols
nfa.feed_symbol("b")
self.assertFalse(nfa.is_accepting())
self.assertTrue(nfa.is_dead())
def zero_or_one_of(self):
print("Testing \"zero or one of\" operator a?bcd")
# build NFA
nfa = nfa_utils.get_regex_nfa("a?bcd")
nfa.reset()
print(nfa)
# test rejects empty string
self.assertFalse(nfa.is_accepting())
# test accepts "bcd"
nfa.feed_symbols("bcd")
self.assertTrue(nfa.is_accepting())
nfa.reset()
# test also accepts "abcd"
nfa.feed_symbol("abcd")
self.assertTrue(nfa.is_accepting())
nfa.reset()
# test rejects "aabcd"
nfa.feed_symbols("aabcd")
self.assertFalse(nfa.is_accepting())
nfa.reset()
# test rejects "aaaaaaaaabcd"
nfa.feed_symbols("aaaaaaaaabcd")
self.assertFalse(nfa.is_accepting())
nfa.reset()
def test_nfa_equals(self):
print("Testing NFA equals function")
# test equal
nfa_a = nfa_utils.get_regex_nfa("a.b|c")
nfa_b = nfa_utils.get_regex_nfa("a.b|c")
self.assertEqual(nfa_a, nfa_b)
# test NOT equal
nfa_a = nfa_utils.get_regex_nfa("a.b|c")
nfa_b = nfa_utils.get_regex_nfa("a.b.c.d")
self.assertNotEqual(nfa_a, nfa_b)
def test_implicit_concatenation(self):
print("Testing implicit NFA concatenation")
nfa_a = nfa_utils.get_regex_nfa("a.b.c.d|c")
nfa_b = nfa_utils.get_regex_nfa("abcd|c")
self.assertEqual(nfa_a, nfa_b)
def test_readme_example_1(self):
print("Testing README example \"p.y.t.h.o.n or python\"")
# test with and without implicit concatenation
variations = ["python", "p.y.t.h.o.n"]
for variation in variations:
# build NFA
nfa = nfa_utils.get_regex_nfa(variation)
nfa.reset()
print(nfa)
# test rejects empty string
self.assertFalse(nfa.is_accepting())
# test accepts "python"
nfa.feed_symbols("python")
self.assertTrue(nfa.is_accepting())
nfa.reset()
# test rejects "java"
nfa.feed_symbol("java")
self.assertFalse(nfa.is_accepting())
nfa.reset()
def test_readme_example_2(self):
print("Testing README example \"python|java|C#\"")
# build NFA
nfa = nfa_utils.get_regex_nfa("python|java|C#")
nfa.reset()
print(nfa)
# test rejects empty string
self.assertFalse(nfa.is_accepting())
accept_list = ["python", "java", "C#"]
reject_list = ["perl", "C++", "Go"]
# test accepts all in accept list
for symbol_input in accept_list:
nfa.feed_symbols(symbol_input)
self.assertTrue(nfa.is_accepting())
nfa.reset()
# test reject all in reject list
for symbol_input in reject_list:
nfa.feed_symbols(symbol_input)
self.assertFalse(nfa.is_accepting())
nfa.reset()
def test_readme_example_3(self):
print("Testing README example \"o+k then or o*ok then\"")
# test both + symbol and * symbol variations
variations = ["o+k then", "o*ok then"]
for variation in variations:
# build NFA
nfa = nfa_utils.get_regex_nfa(variation)
nfa.reset()
print(nfa)
# test rejects empty string
self.assertFalse(nfa.is_accepting())
accept_list = ["ok then", "ooook then", "ooooooook then"]
reject_list = ["k then", "okay", "oki-doki"]
# test accepts all in accept list
for symbol_input in accept_list:
nfa.feed_symbols(symbol_input)
self.assertTrue(nfa.is_accepting())
nfa.reset()
# test reject all in reject list
for symbol_input in reject_list:
nfa.feed_symbols(symbol_input)
self.assertFalse(nfa.is_accepting())
nfa.reset()
def test_readme_example_4(self):
print("Testing README example \"c?loud\"")
# build NFA
nfa = nfa_utils.get_regex_nfa("c?loud")
nfa.reset()
print(nfa)
# test rejects empty string
self.assertFalse(nfa.is_accepting())
accept_list = ["cloud", "loud"]
reject_list = ["oud", "ccloud"]
# test accepts all in accept list
for symbol_input in accept_list:
nfa.feed_symbols(symbol_input)
self.assertTrue(nfa.is_accepting())
nfa.reset()
# test reject all in reject list
for symbol_input in reject_list:
nfa.feed_symbols(symbol_input)
self.assertFalse(nfa.is_accepting())
nfa.reset()
def test_readme_example_5(self):
print("Testing README example \"H?A?h?a?*!*|H?E?h?e?*!*\"")
# build NFA
nfa = nfa_utils.get_regex_nfa("H?A?h?a?*!*|H?E?h?e?*!*")
nfa.reset()
print(nfa)
accept_list = ["Hah", "heh", "Haha", "AAAAAAAAAAHAHAHAHAHA!!", "eeeehehehehehe", "hhhaaaaaaaaaaaa", "HEHEEE!"]
reject_list = ["Heaha", "Haha!h!", "!haha", "I don't get it"]
# test accepts all in accept list
for symbol_input in accept_list:
nfa.feed_symbols(symbol_input)
self.assertTrue(nfa.is_accepting())
nfa.reset()
# test reject all in reject list
for symbol_input in reject_list:
nfa.feed_symbols(symbol_input)
self.assertFalse(nfa.is_accepting())
nfa.reset()