-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_simulation.py
More file actions
98 lines (81 loc) · 3.2 KB
/
test_simulation.py
File metadata and controls
98 lines (81 loc) · 3.2 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
#!/usr/bin/env python3
"""
Test script for the traffic simulation.
This script runs a quick test to verify all components work correctly.
"""
import pygame
import sys
import time
from main import TrafficSimulation
def test_simulation():
"""Test the traffic simulation components."""
print("Testing Traffic Simulation Components...")
try:
# Initialize pygame
pygame.init()
# Create simulation instance
print("✓ Creating simulation instance...")
simulation = TrafficSimulation()
# Test component initialization
print("✓ Testing component initialization...")
assert simulation.scheduler is not None
assert simulation.traffic_light is not None
assert simulation.sync_manager is not None
assert simulation.dashboard is not None
assert len(simulation.lanes) == 4
# Test vehicle spawning
print("✓ Testing vehicle spawning...")
initial_vehicle_count = len(simulation.vehicles)
simulation.spawn_vehicle()
assert len(simulation.vehicles) > initial_vehicle_count
# Test scheduling algorithms
print("✓ Testing scheduling algorithms...")
algorithms = ["FCFS", "Round Robin", "Priority", "Dynamic"]
for algo in algorithms:
simulation.scheduler.set_algorithm(algo)
assert simulation.scheduler.get_current_algorithm() == algo
# Test traffic light states
print("✓ Testing traffic light states...")
for direction in simulation.lanes.keys():
state = simulation.traffic_light.get_light_state(direction)
assert state in ['red', 'yellow', 'green']
# Test synchronization
print("✓ Testing synchronization...")
stats = simulation.sync_manager.get_statistics()
assert 'lock_contention_count' in stats
assert 'deadlock_resolution_count' in stats
# Test dashboard update
print("✓ Testing dashboard update...")
simulation.dashboard.update(
"FCFS",
{'north': 2, 'south': 1, 'east': 3, 'west': 0},
10,
"Medium"
)
# Test simulation update (one frame)
print("✓ Testing simulation update...")
simulation.update(16) # ~60 FPS
# Test event handling
print("✓ Testing event handling...")
# Simulate key press events
test_events = [
pygame.event.Event(pygame.KEYDOWN, key=pygame.K_1),
pygame.event.Event(pygame.KEYDOWN, key=pygame.K_SPACE),
]
pygame.event.post(test_events[0])
pygame.event.post(test_events[1])
simulation.handle_events()
print("✓ All tests passed!")
print("\n🎉 Traffic Simulation is ready to run!")
print("Run 'python main.py' to start the simulation.")
return True
except Exception as e:
print(f"❌ Test failed: {e}")
import traceback
traceback.print_exc()
return False
finally:
pygame.quit()
if __name__ == "__main__":
success = test_simulation()
sys.exit(0 if success else 1)