Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/main.cpython-312.pyc
Binary file not shown.
Binary file added agents/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added agents/__pycache__/world_builder.cpython-312.pyc
Binary file not shown.
Empty file added tests/__init__.py
Empty file.
Binary file added tests/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys
from unittest.mock import MagicMock

# Mock numpy
class MockNumPy:
bool_ = bool
float64 = float
int64 = int
def __getattr__(self, name):
return MagicMock()

sys.modules["numpy"] = MockNumPy()

# Mock pygame
mock_pygame = MagicMock()
sys.modules["pygame"] = mock_pygame
sys.modules["pygame.Surface"] = MagicMock()
sys.modules["pygame.Color"] = MagicMock()
61 changes: 61 additions & 0 deletions tests/test_world_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
import math
from agents.world_builder import Lane, Vector2D

def test_lane_distance_to_point_on_line():
"""Test point exactly on the lane center line."""
lane = Lane(id=1, road_id=1, index=0, width=3.5, direction=Vector2D(1, 0),
start_pos=Vector2D(0, 0), end_pos=Vector2D(10, 0))
point = Vector2D(5, 0)
assert lane.distance_to_point(point) == 0.0

def test_lane_distance_to_point_perpendicular():
"""Test point perpendicular to the lane center line."""
lane = Lane(id=1, road_id=1, index=0, width=3.5, direction=Vector2D(1, 0),
start_pos=Vector2D(0, 0), end_pos=Vector2D(10, 0))
point = Vector2D(5, 5)
assert lane.distance_to_point(point) == 5.0

def test_lane_distance_to_point_beyond_start():
"""Test point beyond the start of the lane."""
lane = Lane(id=1, road_id=1, index=0, width=3.5, direction=Vector2D(1, 0),
start_pos=Vector2D(0, 0), end_pos=Vector2D(10, 0))
point = Vector2D(-5, 0)
# The projection should be clamped to start_pos
assert lane.distance_to_point(point) == 5.0

def test_lane_distance_to_point_beyond_end():
"""Test point beyond the end of the lane."""
lane = Lane(id=1, road_id=1, index=0, width=3.5, direction=Vector2D(1, 0),
start_pos=Vector2D(0, 0), end_pos=Vector2D(10, 0))
point = Vector2D(15, 0)
# The projection should be clamped to end_pos
assert lane.distance_to_point(point) == 5.0

def test_lane_distance_to_point_zero_length():
"""Test lane with zero length (start_pos == end_pos)."""
lane = Lane(id=1, road_id=1, index=0, width=3.5, direction=Vector2D(1, 0),
start_pos=Vector2D(5, 5), end_pos=Vector2D(5, 5))
point = Vector2D(5, 10)
assert lane.distance_to_point(point) == 5.0

def test_lane_distance_to_point_diagonal():
"""Test diagonal lane distance calculation."""
# Lane from (0,0) to (10,10)
lane = Lane(id=1, road_id=1, index=0, width=3.5, direction=Vector2D(1, 1).normalized(),
start_pos=Vector2D(0, 0), end_pos=Vector2D(10, 10))

# Point at (0, 10)
# Projection should be (5, 5)
# Distance from (0, 10) to (5, 5) is sqrt(5^2 + (-5)^2) = sqrt(50)
point = Vector2D(0, 10)
assert lane.distance_to_point(point) == pytest.approx(math.sqrt(50))

def test_lane_distance_to_point_negative_coords():
"""Test distance calculation with negative coordinates."""
lane = Lane(id=1, road_id=1, index=0, width=3.5, direction=Vector2D(0, 1),
start_pos=Vector2D(-10, -10), end_pos=Vector2D(-10, 0))
point = Vector2D(-15, -5)
# Projection should be (-10, -5)
# Distance from (-15, -5) to (-10, -5) is 5.0
assert lane.distance_to_point(point) == 5.0