vimalk78's picture
Add complete Python backend with AI-powered crossword generation
38c016b
raw
history blame
2.91 kB
#!/usr/bin/env python3
"""
Quick test to verify the bounds checking fix.
"""
import sys
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent # Go up from test-integration to backend-py
sys.path.insert(0, str(project_root))
from src.services.crossword_generator_fixed import CrosswordGeneratorFixed
def test_bounds_checking():
"""Test that placement score calculation doesn't crash with out-of-bounds access."""
print("πŸ§ͺ Testing bounds checking fix...")
generator = CrosswordGeneratorFixed()
# Create a small grid
grid = [["." for _ in range(5)] for _ in range(5)]
# Test placement that would go out of bounds
placement = {
"row": 3, # Starting at row 3
"col": 2, # Starting at col 2
"direction": "vertical"
}
# Word that would extend beyond grid (3+8=11 > 5)
word = "ELEPHANT" # 8 letters, would go from row 3 to row 10 (out of bounds)
try:
# This should NOT crash with bounds checking
score = generator._calculate_placement_score(grid, word, placement, [])
print(f"βœ… Success! Placement score calculated: {score}")
print("βœ… Bounds checking is working correctly")
return True
except IndexError as e:
print(f"❌ IndexError still occurs: {e}")
return False
except Exception as e:
print(f"❌ Other error: {e}")
return False
def test_valid_placement():
"""Test that valid placements still work correctly."""
print("\nπŸ§ͺ Testing valid placement scoring...")
generator = CrosswordGeneratorFixed()
# Create a grid with some letters
grid = [["." for _ in range(8)] for _ in range(8)]
grid[2][2] = "A" # Place an 'A' at position (2,2)
# Test placement that intersects properly
placement = {
"row": 2,
"col": 1,
"direction": "horizontal"
}
word = "CAT" # Should intersect at the 'A'
try:
score = generator._calculate_placement_score(grid, word, placement, [])
print(f"βœ… Valid placement score: {score}")
# Should have intersection bonus (score > 100)
if score > 300: # Base 100 + intersection 200
print("βœ… Intersection detection working")
else:
print(f"⚠️ Expected intersection bonus, got score {score}")
return True
except Exception as e:
print(f"❌ Error with valid placement: {e}")
return False
if __name__ == "__main__":
print("πŸ”§ Testing crossword generator bounds fix\n")
test1_pass = test_bounds_checking()
test2_pass = test_valid_placement()
if test1_pass and test2_pass:
print("\nβœ… All tests passed! The bounds checking fix is working.")
else:
print("\n❌ Some tests failed. More work needed.")