-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kernel_definition.py
More file actions
209 lines (162 loc) · 7.05 KB
/
test_kernel_definition.py
File metadata and controls
209 lines (162 loc) · 7.05 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
#!/usr/bin/env python3
"""
Test KernelDefinitionProofStep to ensure it moves elements from fx=0 to Ker f correctly.
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from PyQt6.QtWidgets import QApplication
from widget.diagram_scene import DiagramScene
from widget.object_node import Object
from core.proof_step import KernelDefinitionProofStep
def test_kernel_definition_pattern_recognition():
"""Test that the proof step recognizes fx=0 patterns correctly."""
test_cases = [
("fα=0:A", True, "Simple function application fα=0"),
("gβ=0,other:B", True, "Function application with other elements"),
("hxy=0:C", True, "Multi-character element"),
("f=0:D", False, "No element (just function)"),
("α=0:E", False, "No function (just element)"),
("fa=b:F", False, "No zero equality"),
("simple:G", False, "No pattern at all"),
]
print("Testing kernel definition pattern recognition:")
all_passed = True
for text, expected, description in test_cases:
result = KernelDefinitionProofStep._contains_kernel_definition_pattern(text)
status = "✅" if result == expected else "❌"
print(f" {status} {description}: '{text}' → {result} (expected {expected})")
if result != expected:
all_passed = False
return all_passed
def test_kernel_definition_info_extraction():
"""Test extraction of function and element from fx=0 patterns."""
test_cases = [
("fα=0:A", "f", "α"),
("gβγ=0:B", "g", "βγ"),
("hxyz=0:C", "h", "xyz"),
("simple:D", None, None),
]
print("\nTesting kernel definition info extraction:")
all_passed = True
for text, expected_func, expected_elem in test_cases:
func, elem = KernelDefinitionProofStep._extract_kernel_definition_info(text)
func_ok = func == expected_func
elem_ok = elem == expected_elem
status = "✅" if func_ok and elem_ok else "❌"
print(f" {status} '{text}' → func: '{func}' (exp: '{expected_func}'), elem: '{elem}' (exp: '{expected_elem}')")
if not (func_ok and elem_ok):
all_passed = False
return all_passed
def test_kernel_definition_application_new_kernel():
"""Test applying kernel definition when kernel node doesn't exist."""
app = QApplication(sys.argv)
# Create scene and enable abelian category
scene = DiagramScene()
scene._is_abelian_category = True
# Create object with fx=0
obj = Object("A")
obj.setPos(100, 100)
obj.set_text("fα=0:A")
scene.addItem(obj)
print("\nTest 1: Creating new kernel node")
print(f"Before: {obj.get_display_text()}")
print(f"Objects in scene: {len([item for item in scene.items() if hasattr(item, 'get_text') and not hasattr(item, 'get_source')])}")
# Check if proof step is applicable
if not KernelDefinitionProofStep.is_applicable([obj], []):
print("❌ FAILURE: Proof step not applicable")
return False
# Get button text
button_text = KernelDefinitionProofStep.button_text([obj], [])
print(f"Button text: {button_text}")
# Apply proof step
proof_step = KernelDefinitionProofStep(scene, [obj], [])
proof_step.apply()
# Check results
print(f"After - Original node: {obj.get_display_text()}")
# Find all objects in scene
objects = [item for item in scene.items() if hasattr(item, 'get_text') and not hasattr(item, 'get_source')]
print(f"Objects in scene after: {len(objects)}")
# Look for kernel node
kernel_node = None
for item in objects:
if "Ker f" in item.get_display_text():
kernel_node = item
break
if kernel_node:
print(f"Kernel node created: {kernel_node.get_display_text()}")
# Check if original node no longer has fα=0
if "fα=0" not in obj.get_display_text():
print("✅ SUCCESS: Element removed from original node")
else:
print("❌ FAILURE: Element not removed from original node")
return False
# Check if kernel node has α
if "α:" in kernel_node.get_display_text():
print("✅ SUCCESS: Element added to kernel node")
return True
else:
print("❌ FAILURE: Element not found in kernel node")
return False
else:
print("❌ FAILURE: Kernel node not created")
return False
def test_kernel_definition_application_existing_kernel():
"""Test applying kernel definition when kernel node already exists."""
app = QApplication(sys.argv)
# Create scene and enable abelian category
scene = DiagramScene()
scene._is_abelian_category = True
# Create object with fx=0
obj = Object("A")
obj.setPos(100, 100)
obj.set_text("fβ=0:A")
scene.addItem(obj)
# Create existing kernel node
kernel_obj = Object("Ker f")
kernel_obj.setPos(200, 100)
kernel_obj.set_text("γ:Ker f") # Already has element γ
scene.addItem(kernel_obj)
print("\nTest 2: Using existing kernel node")
print(f"Before - Original: {obj.get_display_text()}")
print(f"Before - Kernel: {kernel_obj.get_display_text()}")
# Apply proof step
proof_step = KernelDefinitionProofStep(scene, [obj], [])
proof_step.apply()
print(f"After - Original: {obj.get_display_text()}")
print(f"After - Kernel: {kernel_obj.get_display_text()}")
# Check results
if "fβ=0" not in obj.get_display_text() and "γ,β:Ker f" in kernel_obj.get_display_text():
print("✅ SUCCESS: Element moved to existing kernel node")
return True
else:
print("❌ FAILURE: Element not properly moved to existing kernel node")
return False
def test_button_text():
"""Test that the button text correctly identifies the kernel definition."""
app = QApplication(sys.argv)
obj = Object("Test")
obj.set_text("fα=0:A")
button_text = KernelDefinitionProofStep.button_text([obj], [])
expected = "Move α to Ker f"
print(f"\nButton text test:")
print(f"Input: fα=0:A")
print(f"Button text: '{button_text}'")
print(f"Expected: '{expected}'")
if button_text == expected:
print("✅ SUCCESS: Button text correct")
return True
else:
print("❌ FAILURE: Button text incorrect")
return False
if __name__ == "__main__":
success1 = test_kernel_definition_pattern_recognition()
success2 = test_kernel_definition_info_extraction()
success3 = test_kernel_definition_application_new_kernel()
success4 = test_kernel_definition_application_existing_kernel()
success5 = test_button_text()
if success1 and success2 and success3 and success4 and success5:
print("\n🎉 All kernel definition tests passed!")
else:
print("\n💥 Some kernel definition tests failed!")
sys.exit(0 if all([success1, success2, success3, success4, success5]) else 1)