-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_correction.py
More file actions
211 lines (179 loc) · 7.93 KB
/
Copy pathtest_correction.py
File metadata and controls
211 lines (179 loc) · 7.93 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
"""
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)
if __name__ == "__main__":
pytest.main([__file__, "-v"])