-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_validation.py
More file actions
498 lines (447 loc) · 16.5 KB
/
test_validation.py
File metadata and controls
498 lines (447 loc) · 16.5 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
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
"""
Comprehensive tests for FSA validation, analysis, and comparison.
Covers validation rules, determinism, completeness, reachability,
dead states, string acceptance, language equivalence, and DFA isomorphism.
"""
import pytest
from evaluation_function.validation.validation import *
from evaluation_function.schemas.utils import make_fsa
from evaluation_function.schemas import ErrorCode
class TestFSAValidation:
"""Tests for basic FSA validation rules."""
def test_valid_fsa_basic(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "a"}],
initial="q0",
accept=["q1"],
)
assert is_valid_fsa(fsa).ok
def test_invalid_initial_state(self):
fsa = make_fsa(
states=["q1"],
alphabet=["a"],
transitions=[],
initial="q0",
accept=[],
)
result = is_valid_fsa(fsa)
assert not result.ok
assert ErrorCode.INVALID_INITIAL in [e.code for e in result.errors]
def test_invalid_accept_state(self):
fsa = make_fsa(
states=["q0"],
alphabet=["a"],
transitions=[],
initial="q0",
accept=["q1"],
)
result = is_valid_fsa(fsa)
assert not result.ok
assert ErrorCode.INVALID_ACCEPT in [e.code for e in result.errors]
def test_invalid_transition_source(self):
fsa = make_fsa(
states=["q1"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "a"}],
initial="q1",
accept=[],
)
result = is_valid_fsa(fsa)
assert ErrorCode.INVALID_TRANSITION_SOURCE in [e.code for e in result.errors]
def test_invalid_transition_destination(self):
fsa = make_fsa(
states=["q0"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "a"}],
initial="q0",
accept=[],
)
result = is_valid_fsa(fsa)
assert ErrorCode.INVALID_TRANSITION_DEST in [e.code for e in result.errors]
def test_invalid_transition_symbol(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "b"}],
initial="q0",
accept=["q1"],
)
result = is_valid_fsa(fsa)
assert ErrorCode.INVALID_SYMBOL in [e.code for e in result.errors]
class TestDeterminism:
"""Tests for determinism checking."""
def test_deterministic_fsa(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "q0", "symbol": "b"},
{"from_state": "q1", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q0", "symbol": "b"},
],
initial="q0",
accept=["q1"],
)
assert is_deterministic(fsa).ok
def test_nondeterministic_fsa(self):
fsa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "q2", "symbol": "a"},
],
initial="q0",
accept=["q2"],
)
result = is_deterministic(fsa)
assert not result.ok
assert ErrorCode.DUPLICATE_TRANSITION in [e.code for e in result.errors]
class TestCompleteness:
"""Tests for DFA completeness."""
def test_complete_dfa(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "q0", "symbol": "b"},
{"from_state": "q1", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q0", "symbol": "b"},
],
initial="q0",
accept=["q1"],
)
assert is_complete(fsa).ok
def test_incomplete_dfa(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a", "b"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "a"}],
initial="q0",
accept=["q1"],
)
result = is_complete(fsa)
assert not result.ok
assert ErrorCode.MISSING_TRANSITION in [e.code for e in result.errors]
def test_complete_requires_deterministic(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q0", "symbol": "a"},
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
],
initial="q0",
accept=["q1"],
)
result = is_complete(fsa)
codes = [e.code for e in result.errors]
assert ErrorCode.NOT_DETERMINISTIC in codes
assert ErrorCode.DUPLICATE_TRANSITION in codes
class TestReachabilityAndDeadStates:
"""Tests for unreachable and dead states."""
def test_find_unreachable_states(self):
fsa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "a"}],
initial="q0",
accept=["q1"],
)
result = find_unreachable_states(fsa)
assert not result.ok
assert ErrorCode.UNREACHABLE_STATE in [e.code for e in result.errors]
def test_find_dead_states(self):
fsa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q2", "symbol": "b"},
{"from_state": "q2", "to_state": "q2", "symbol": "a"},
{"from_state": "q2", "to_state": "q2", "symbol": "b"},
],
initial="q0",
accept=["q1"],
)
result = find_dead_states(fsa)
assert not result.ok
assert ErrorCode.DEAD_STATE in [e.code for e in result.errors]
def test_dead_states_no_accept_states(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q0", "symbol": "a"},
],
initial="q0",
accept=[],
)
result = find_dead_states(fsa)
assert len(result.errors) == 2
class TestStringAcceptance:
"""Tests for string acceptance."""
def test_accepts_string(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "a"}],
initial="q0",
accept=["q1"],
)
assert accepts_string(fsa, "a").ok
def test_rejected_no_transition(self):
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "a"}],
initial="q0",
accept=["q1"],
)
result = accepts_string(fsa, "aa")
assert ErrorCode.TEST_CASE_FAILED in [e.code for e in result.errors]
def test_empty_string(self):
fsa = make_fsa(
states=["q0"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q0", "symbol": "a"}],
initial="q0",
accept=["q0"],
)
assert accepts_string(fsa, "").ok
class TestLanguageEquivalence:
"""Tests for language equivalence."""
def test_accept_same_string(self):
fsa1 = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q1", "symbol": "a"}],
initial="q0",
accept=["q1"],
)
fsa2 = make_fsa(
states=["s0", "s1"],
alphabet=["a"],
transitions=[{"from_state": "s0", "to_state": "s1", "symbol": "a"}],
initial="s0",
accept=["s1"],
)
assert fsas_accept_same_string(fsa1, fsa2, "a").ok
def test_language_mismatch(self):
fsa1 = make_fsa(
states=["q0"],
alphabet=["a"],
transitions=[{"from_state": "q0", "to_state": "q0", "symbol": "a"}],
initial="q0",
accept=["q0"],
)
fsa2 = make_fsa(
states=["s0"],
alphabet=["b"],
transitions=[{"from_state": "s0", "to_state": "s0", "symbol": "b"}],
initial="s0",
accept=["s0"],
)
result = fsas_accept_same_language(fsa1, fsa2)
assert not result.ok
assert ErrorCode.LANGUAGE_MISMATCH in [e.code for e in result.errors]
class TestIsomorphism:
"""Tests for DFA isomorphism checking."""
def test_isomorphic_dfas(self):
fsa_user = make_fsa(
states=["q0", "q1"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "q0", "symbol": "b"},
{"from_state": "q1", "to_state": "q0", "symbol": "a"},
{"from_state": "q1", "to_state": "q1", "symbol": "b"},
],
initial="q0",
accept=["q1"],
)
fsa_sol = make_fsa(
states=["s0", "s1"],
alphabet=["a", "b"],
transitions=[
{"from_state": "s0", "to_state": "s1", "symbol": "a"},
{"from_state": "s0", "to_state": "s0", "symbol": "b"},
{"from_state": "s1", "to_state": "s0", "symbol": "a"},
{"from_state": "s1", "to_state": "s1", "symbol": "b"},
],
initial="s0",
accept=["s1"],
)
assert are_isomorphic(fsa_user, fsa_sol).ok
class TestEpsilonTransitions:
"""Tests for epsilon transition handling across the validation pipeline."""
def test_valid_fsa_with_epsilon_unicode(self):
"""ε-NFA with Unicode ε should pass structural validation."""
fsa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "ε"},
{"from_state": "q1", "to_state": "q2", "symbol": "a"},
],
initial="q0",
accept=["q2"],
)
assert is_valid_fsa(fsa).ok
def test_valid_fsa_with_epsilon_string(self):
"""ε-NFA with 'epsilon' string should pass structural validation."""
fsa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "epsilon"},
{"from_state": "q1", "to_state": "q2", "symbol": "a"},
],
initial="q0",
accept=["q2"],
)
assert is_valid_fsa(fsa).ok
def test_valid_fsa_with_empty_string_epsilon(self):
"""ε-NFA with empty string epsilon should pass structural validation."""
fsa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": ""},
{"from_state": "q1", "to_state": "q2", "symbol": "a"},
],
initial="q0",
accept=["q2"],
)
assert is_valid_fsa(fsa).ok
def test_epsilon_nfa_is_not_deterministic(self):
"""ε-NFA should be flagged as non-deterministic."""
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "ε"},
],
initial="q0",
accept=["q1"],
)
result = is_deterministic(fsa)
assert not result.ok
assert ErrorCode.NOT_DETERMINISTIC in [e.code for e in result.errors]
def test_accepts_string_via_epsilon_closure(self):
"""ε-NFA should accept 'a' by following q0 --ε--> q1 --a--> q2."""
fsa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "ε"},
{"from_state": "q1", "to_state": "q2", "symbol": "a"},
],
initial="q0",
accept=["q2"],
)
assert accepts_string(fsa, "a").ok
def test_rejects_string_with_epsilon_nfa(self):
"""ε-NFA that accepts 'a' should reject empty string."""
fsa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "ε"},
{"from_state": "q1", "to_state": "q2", "symbol": "a"},
],
initial="q0",
accept=["q2"],
)
result = accepts_string(fsa, "")
assert not result.ok
def test_accepts_empty_string_via_epsilon(self):
"""ε-NFA should accept empty string when initial reaches accept via ε."""
fsa = make_fsa(
states=["q0", "q1"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "ε"},
],
initial="q0",
accept=["q1"],
)
assert accepts_string(fsa, "").ok
def test_epsilon_nfa_equivalent_to_dfa(self):
"""ε-NFA and DFA accepting the same language should be equivalent."""
enfa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "ε"},
{"from_state": "q1", "to_state": "q2", "symbol": "a"},
],
initial="q0",
accept=["q2"],
)
dfa = make_fsa(
states=["s0", "s1"],
alphabet=["a"],
transitions=[
{"from_state": "s0", "to_state": "s1", "symbol": "a"},
],
initial="s0",
accept=["s1"],
)
assert fsas_accept_same_language(enfa, dfa).ok
def test_epsilon_nfa_not_equivalent_to_different_dfa(self):
"""ε-NFA and DFA accepting different languages should not be equivalent."""
enfa = make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "ε"},
{"from_state": "q1", "to_state": "q2", "symbol": "a"},
],
initial="q0",
accept=["q2"],
)
dfa = make_fsa(
states=["s0", "s1"],
alphabet=["a", "b"],
transitions=[
{"from_state": "s0", "to_state": "s1", "symbol": "b"},
],
initial="s0",
accept=["s1"],
)
result = fsas_accept_same_language(enfa, dfa)
assert not result.ok
def test_multi_epsilon_nfa_equivalent_to_dfa(self):
"""ε-NFA for (a|b) with branching epsilons should match equivalent DFA."""
# q0 --ε--> q1, q0 --ε--> q2, q1 --a--> q3, q2 --b--> q3
enfa = make_fsa(
states=["q0", "q1", "q2", "q3"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "ε"},
{"from_state": "q0", "to_state": "q2", "symbol": "ε"},
{"from_state": "q1", "to_state": "q3", "symbol": "a"},
{"from_state": "q2", "to_state": "q3", "symbol": "b"},
],
initial="q0",
accept=["q3"],
)
dfa = make_fsa(
states=["s0", "s1"],
alphabet=["a", "b"],
transitions=[
{"from_state": "s0", "to_state": "s1", "symbol": "a"},
{"from_state": "s0", "to_state": "s1", "symbol": "b"},
],
initial="s0",
accept=["s1"],
)
assert fsas_accept_same_language(enfa, dfa).ok
if __name__ == "__main__":
pytest.main([__file__, "-v"])