-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_correction.py
More file actions
339 lines (299 loc) · 12.8 KB
/
test_correction.py
File metadata and controls
339 lines (299 loc) · 12.8 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
"""
Tests for FSA Correction Module.
Tests the correction module that returns Result with FSAFeedback.
"""
import pytest
from evaluation_function.schemas import ValidationError, ErrorCode
from evaluation_function.schemas.utils import make_fsa
from evaluation_function.schemas.result import Result, FSAFeedback
from evaluation_function.schemas.params import Params
from evaluation_function.correction import analyze_fsa_correction
# =============================================================================
# Fixtures - DFAs
# =============================================================================
@pytest.fixture
def dfa_accepts_a():
"""DFA that accepts exactly 'a'."""
return make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "q2", "symbol": "b"},
{"from_state": "q1", "to_state": "q2", "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"]
)
@pytest.fixture
def dfa_accepts_a_or_b():
"""DFA that accepts 'a' or 'b'."""
return make_fsa(
states=["q0", "q1", "q2"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "q1", "symbol": "b"},
{"from_state": "q1", "to_state": "q2", "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"]
)
@pytest.fixture
def equivalent_dfa():
"""DFA equivalent to dfa_accepts_a with different state names."""
return make_fsa(
states=["s0", "s1", "s2"],
alphabet=["a", "b"],
transitions=[
{"from_state": "s0", "to_state": "s1", "symbol": "a"},
{"from_state": "s0", "to_state": "s2", "symbol": "b"},
{"from_state": "s1", "to_state": "s2", "symbol": "a"},
{"from_state": "s1", "to_state": "s2", "symbol": "b"},
{"from_state": "s2", "to_state": "s2", "symbol": "a"},
{"from_state": "s2", "to_state": "s2", "symbol": "b"},
],
initial="s0",
accept=["s1"]
)
# =============================================================================
# Helper: Default Params
# =============================================================================
@pytest.fixture
def default_params():
"""Default Params object for analyze_fsa_correction."""
return Params(
expected_type="DFA",
check_completeness=True,
check_minimality=True,
evaluation_mode="strict",
highlight_errors=True,
feedback_verbosity="detailed"
)
# =============================================================================
# Test Main Pipeline - Returns Result
# =============================================================================
class TestAnalyzeFsaCorrection:
"""Test the main analysis pipeline returns Result."""
def test_equivalent_fsas_correct(self, dfa_accepts_a, equivalent_dfa, default_params):
result = analyze_fsa_correction(dfa_accepts_a, equivalent_dfa, default_params)
print(result)
assert isinstance(result, Result)
assert result.is_correct is True
assert "Correct" in result.feedback
def test_different_fsas_incorrect(self, dfa_accepts_a, dfa_accepts_a_or_b, default_params):
result = analyze_fsa_correction(dfa_accepts_a, dfa_accepts_a_or_b, default_params)
assert isinstance(result, Result)
assert result.is_correct is False
def test_result_has_fsa_feedback(self, dfa_accepts_a, equivalent_dfa, default_params):
result = analyze_fsa_correction(dfa_accepts_a, equivalent_dfa, default_params)
assert result.fsa_feedback is not None
assert isinstance(result.fsa_feedback, FSAFeedback)
def test_fsa_feedback_has_structural_info(self, dfa_accepts_a, equivalent_dfa, default_params):
result = analyze_fsa_correction(dfa_accepts_a, equivalent_dfa, default_params)
assert result.fsa_feedback.structural is not None
assert result.fsa_feedback.structural.num_states == 3
def test_different_fsas_have_errors(self, dfa_accepts_a, dfa_accepts_a_or_b, default_params):
result = analyze_fsa_correction(dfa_accepts_a, dfa_accepts_a_or_b, default_params)
assert result.fsa_feedback is not None
assert len(result.fsa_feedback.errors) > 0
# =============================================================================
# Test Invalid FSAs
# =============================================================================
class TestInvalidFsas:
"""Test handling of invalid FSAs."""
def test_invalid_initial_state(self, default_params):
invalid = make_fsa(
states=["q0"],
alphabet=["a"],
transitions=[],
initial="invalid",
accept=[]
)
result = analyze_fsa_correction(invalid, invalid, default_params)
assert result.is_correct is False
assert result.fsa_feedback is not None
assert len(result.fsa_feedback.errors) > 0
def test_invalid_accept_state(self, default_params):
invalid = make_fsa(
states=["q0"],
alphabet=["a"],
transitions=[],
initial="q0",
accept=["invalid"]
)
result = analyze_fsa_correction(invalid, invalid, default_params)
assert result.is_correct is False
# =============================================================================
# Test Minimality
# =============================================================================
class TestAnalyzeFsaCorrectionMinimality:
"""Test analyze_fsa_correction with minimality checking."""
def test_minimal_fsa_passes(self, dfa_accepts_a, equivalent_dfa):
params = Params(
expected_type="DFA",
check_completeness=True,
check_minimality=True,
evaluation_mode="strict",
highlight_errors=True,
feedback_verbosity="detailed"
)
result = analyze_fsa_correction(dfa_accepts_a, equivalent_dfa, params)
assert result.is_correct is True
def test_non_minimal_fsa_fails_when_required(self, equivalent_dfa):
non_minimal = make_fsa(
states=["q0", "q1", "q2", "unreachable"],
alphabet=["a", "b"],
transitions=[
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "q2", "symbol": "b"},
{"from_state": "q1", "to_state": "q2", "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"},
{"from_state": "unreachable", "to_state": "unreachable", "symbol": "a"},
{"from_state": "unreachable", "to_state": "unreachable", "symbol": "b"},
],
initial="q0",
accept=["q1"]
)
params = Params(
expected_type="DFA",
check_completeness=True,
check_minimality=True,
evaluation_mode="strict",
highlight_errors=True,
feedback_verbosity="detailed"
)
result = analyze_fsa_correction(non_minimal, equivalent_dfa, params)
# Should have minimality error
assert result.fsa_feedback is not None
assert any(e.code == ErrorCode.NOT_MINIMAL for e in result.fsa_feedback.errors)
# =============================================================================
# Test Epsilon Transitions (End-to-End)
# =============================================================================
class TestEpsilonTransitionCorrection:
"""Test the full correction pipeline with ε-NFA inputs."""
@pytest.fixture
def nfa_params(self):
"""Params that allow NFA/ε-NFA student submissions."""
return Params(
expected_type="any",
check_completeness=False,
check_minimality=False,
evaluation_mode="lenient",
highlight_errors=True,
feedback_verbosity="detailed",
)
def test_epsilon_nfa_vs_equivalent_dfa_correct(self, nfa_params):
"""ε-NFA student answer equivalent to DFA expected should be correct."""
# ε-NFA accepts exactly "a": q0 --ε--> q1 --a--> q2
student_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 accepts exactly "a": s0 --a--> s1
expected_dfa = make_fsa(
states=["s0", "s1"],
alphabet=["a"],
transitions=[
{"from_state": "s0", "to_state": "s1", "symbol": "a"},
],
initial="s0",
accept=["s1"],
)
result = analyze_fsa_correction(student_enfa, expected_dfa, nfa_params)
assert isinstance(result, Result)
assert result.is_correct is True
def test_epsilon_nfa_vs_different_dfa_incorrect(self, nfa_params):
"""ε-NFA accepting 'a' vs DFA accepting 'b' should be incorrect."""
student_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"],
)
expected_dfa = make_fsa(
states=["s0", "s1"],
alphabet=["a", "b"],
transitions=[
{"from_state": "s0", "to_state": "s1", "symbol": "b"},
],
initial="s0",
accept=["s1"],
)
result = analyze_fsa_correction(student_enfa, expected_dfa, nfa_params)
assert isinstance(result, Result)
assert result.is_correct is False
assert result.fsa_feedback is not None
assert len(result.fsa_feedback.errors) > 0
def test_multi_epsilon_nfa_vs_dfa_correct(self, nfa_params):
"""ε-NFA for (a|b) with branching epsilons should match equivalent DFA."""
student_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"],
)
expected_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"],
)
result = analyze_fsa_correction(student_enfa, expected_dfa, nfa_params)
assert isinstance(result, Result)
assert result.is_correct is True
def test_epsilon_nfa_structural_info_reports_nondeterministic(self, nfa_params):
"""ε-NFA should have structural info reporting non-deterministic."""
student_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"],
)
expected_dfa = make_fsa(
states=["s0", "s1"],
alphabet=["a"],
transitions=[
{"from_state": "s0", "to_state": "s1", "symbol": "a"},
],
initial="s0",
accept=["s1"],
)
result = analyze_fsa_correction(student_enfa, expected_dfa, nfa_params)
assert result.fsa_feedback is not None
assert result.fsa_feedback.structural is not None
assert result.fsa_feedback.structural.is_deterministic is False
if __name__ == "__main__":
pytest.main([__file__, "-v"])