Remove edges in constructor
This commit is contained in:
88
Project-02-03-04/test_cfg_refactoring.py
Normal file
88
Project-02-03-04/test_cfg_refactoring.py
Normal file
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Add the current directory to Python path so we can import our modules
|
||||
sys.path.insert(0, '/home/janniclas/Projekte/Construction-of-Compilers/Project-02-03-04')
|
||||
|
||||
import triplayacc as yacc
|
||||
import cfg_build
|
||||
from cfg.CFG import CFG
|
||||
from cfg.CFG_Node import (CFG_START, CFG_END)
|
||||
|
||||
def test_cfg_refactoring():
|
||||
"""Test that the CFG refactoring works correctly"""
|
||||
|
||||
test_files = [
|
||||
'example.tripla',
|
||||
'simpleSequence.tripla',
|
||||
'condition.tripla',
|
||||
'factorial.tripla'
|
||||
]
|
||||
|
||||
print("Testing CFG refactoring...")
|
||||
print("=" * 50)
|
||||
|
||||
for filename in test_files:
|
||||
print(f"\nTesting {filename}...")
|
||||
|
||||
try:
|
||||
# Reset the global FUNCTIONS registry
|
||||
cfg_build.FUNCTIONS.clear()
|
||||
|
||||
source = Path(f'triplaprograms/{filename}').read_text()
|
||||
ast = yacc.parser.parse(source)
|
||||
|
||||
# Create CFG the old way (without AST parameter)
|
||||
start1 = CFG_START()
|
||||
end1 = CFG_END()
|
||||
last1 = ast.cfa(start1, end1)
|
||||
if last1 is not None:
|
||||
last1.add_child(end1)
|
||||
cfg_old = CFG(start1, end1) # Old constructor
|
||||
dot_old = cfg_old.to_dot()
|
||||
|
||||
# Create CFG the new way (with AST parameter)
|
||||
cfg_build.FUNCTIONS.clear()
|
||||
start2 = CFG_START()
|
||||
end2 = CFG_END()
|
||||
last2 = ast.cfa(start2, end2)
|
||||
if last2 is not None:
|
||||
last2.add_child(end2)
|
||||
cfg_new = CFG(start2, end2, ast) # New constructor
|
||||
dot_new = cfg_new.to_dot()
|
||||
|
||||
# Compare the outputs
|
||||
if dot_old == dot_new:
|
||||
print(f" ✓ PASS: DOT outputs are identical")
|
||||
else:
|
||||
print(f" ✗ FAIL: DOT outputs differ")
|
||||
print(f" Old length: {len(dot_old)}")
|
||||
print(f" New length: {len(dot_new)}")
|
||||
return False
|
||||
|
||||
# Verify basic structure
|
||||
if 'digraph CFG' not in dot_new:
|
||||
print(f" ✗ FAIL: Missing graph declaration")
|
||||
return False
|
||||
|
||||
if '->' not in dot_new:
|
||||
print(f" ✗ FAIL: Missing edges")
|
||||
return False
|
||||
|
||||
print(f" ✓ PASS: Basic structure verified")
|
||||
print(f" ✓ Generated DOT with {len(dot_new)} characters")
|
||||
|
||||
except Exception as e:
|
||||
print(f" ✗ FAIL: Exception occurred: {e}")
|
||||
return False
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("✓ All tests passed! CFG refactoring is working correctly.")
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_cfg_refactoring()
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user