-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_script_execution.py
More file actions
389 lines (297 loc) · 14.1 KB
/
Copy pathtest_script_execution.py
File metadata and controls
389 lines (297 loc) · 14.1 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python3
"""
Comprehensive script execution tests for Computational Physics 2016.
This module tests that each individual script can execute its main functionality
without crashing, with proper matplotlib backend handling for headless testing.
"""
import sys
import os
import subprocess
import tempfile
import unittest
from unittest.mock import patch, MagicMock
import importlib.util
from contextlib import contextmanager, redirect_stdout, redirect_stderr
from io import StringIO
# Set matplotlib backend before any imports
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
class TestIndividualScriptExecution(unittest.TestCase):
"""Test execution of individual computational physics scripts."""
def setUp(self):
"""Set up test environment."""
plt.ioff() # Turn off interactive mode
plt.close('all')
def tearDown(self):
"""Clean up after tests."""
plt.close('all')
@contextmanager
def mock_interactive_elements(self):
"""Context manager to mock interactive matplotlib elements."""
with patch('matplotlib.pyplot.show') as mock_show, \
patch('matplotlib.pyplot.pause') as mock_pause, \
patch.object(plt, 'show', mock_show), \
patch('builtins.input', return_value='n'): # Mock user input
yield mock_show, mock_pause
def load_script_module(self, script_name):
"""Load a script as a module safely."""
script_path = f"{script_name}.py"
if not os.path.exists(script_path):
self.skipTest(f"Script {script_path} not found")
spec = importlib.util.spec_from_file_location(script_name, script_path)
module = importlib.util.module_from_spec(spec)
# Capture stdout/stderr during module loading
stdout_capture = StringIO()
stderr_capture = StringIO()
try:
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
with self.mock_interactive_elements():
spec.loader.exec_module(module)
return module, stdout_capture.getvalue(), stderr_capture.getvalue()
except Exception as e:
self.fail(f"Failed to load {script_name}: {e}\nSTDOUT: {stdout_capture.getvalue()}\nSTDERR: {stderr_capture.getvalue()}")
def test_script_1_kicked_rotor(self):
"""Test script 1: Standard mapping (kicked rotor)."""
module, stdout, stderr = self.load_script_module("1_1_martin_roebke")
# Test that key classes exist
self.assertTrue(hasattr(module, 'StdAbb'))
self.assertTrue(hasattr(module, 'StdAbbPlot'))
# Test StdAbb initialization and basic computation
std_abb = module.StdAbb(K=2.0, max_iterationen=50)
t_arr, p_arr = std_abb.standard_abbildung(1.0, 0.5)
self.assertEqual(len(t_arr), 51) # max_iterationen + 1
self.assertEqual(len(p_arr), 51)
# Test StdAbbPlot creation (without showing)
with self.mock_interactive_elements():
plot = module.StdAbbPlot(std_abb)
self.assertIsNotNone(plot)
print("✓ Script 1 (kicked rotor) execution successful")
def test_script_2_numerical_derivatives(self):
"""Test script 2: Numerical differentiation."""
module, stdout, stderr = self.load_script_module("2_1_martin_roebke")
# Test function evaluations
x = 1.0
func_val = module.func(x, abl=0)
deriv_val = module.func(x, abl=1)
self.assertIsInstance(func_val, (int, float))
self.assertIsInstance(deriv_val, (int, float))
# Test numerical methods
import numpy as np
h_values = np.array([0.1, 0.01])
forward_diff = module.vorwaertsDiff(
lambda x: module.func(x, abl=0), x, h_values
)
self.assertEqual(len(forward_diff), 2)
print("✓ Script 2 (numerical derivatives) execution successful")
def test_script_3_execution(self):
"""Test script 3 basic execution."""
module, stdout, stderr = self.load_script_module("3_1_martin_roebke")
# Basic module loading test - script should load without errors
self.assertIsNotNone(module)
print("✓ Script 3 execution successful")
def test_script_4_execution(self):
"""Test script 4 basic execution."""
module, stdout, stderr = self.load_script_module("4_1_martin_roebke")
self.assertIsNotNone(module)
print("✓ Script 4 execution successful")
def test_script_5_quantum_potentials(self):
"""Test script 5: Quantum potentials."""
module, stdout, stderr = self.load_script_module("5_1_martin_roebke")
# Test potential functions
import sympy
x = sympy.Symbol('x')
if hasattr(module, 'doppelmulde'):
pot = module.doppelmulde()
self.assertIsInstance(pot, sympy.Basic)
if hasattr(module, 'parabel'):
pot = module.parabel()
self.assertIsInstance(pot, sympy.Basic)
print("✓ Script 5 (quantum potentials) execution successful")
def test_script_6_execution(self):
"""Test script 6 basic execution."""
module, stdout, stderr = self.load_script_module("6_1_martin_roebke")
self.assertIsNotNone(module)
print("✓ Script 6 execution successful")
def test_script_7_execution(self):
"""Test script 7 basic execution."""
module, stdout, stderr = self.load_script_module("7_1_martin_roebke")
self.assertIsNotNone(module)
print("✓ Script 7 execution successful")
def test_script_8_execution(self):
"""Test script 8 basic execution."""
module, stdout, stderr = self.load_script_module("8_1_martin_roebke")
self.assertIsNotNone(module)
print("✓ Script 8 execution successful")
def test_script_9_execution(self):
"""Test script 9 basic execution."""
module, stdout, stderr = self.load_script_module("9_1_martin_roebke")
self.assertIsNotNone(module)
print("✓ Script 9 execution successful")
def test_script_10_execution(self):
"""Test script 10 basic execution."""
module, stdout, stderr = self.load_script_module("10_1_martin_roebke")
self.assertIsNotNone(module)
print("✓ Script 10 execution successful")
def test_quantenmechanik_detailed(self):
"""Test quantenmechanik.py with detailed functionality."""
module, stdout, stderr = self.load_script_module("quantenmechanik")
# Test discretization
x, delta_x = module.diskretisierung(-5, 5, 100, retstep=True)
self.assertEqual(len(x), 100)
self.assertGreater(delta_x, 0)
# Test diagonalization with harmonic oscillator
import numpy as np
V = 0.5 * x**2
eigenvals, eigenfuncs = module.diagonalisierung(1.0, x, V)
self.assertEqual(len(eigenvals), 100)
self.assertEqual(eigenfuncs.shape, (100, 100))
# Check that eigenvalues are sorted
self.assertTrue(np.all(eigenvals[:-1] <= eigenvals[1:]))
print("✓ Quantenmechanik module detailed execution successful")
class TestScriptSafety(unittest.TestCase):
"""Test scripts for safety and proper resource handling."""
def setUp(self):
"""Set up test environment."""
matplotlib.use('Agg')
plt.ioff()
def tearDown(self):
"""Clean up after tests."""
plt.close('all')
def test_no_infinite_loops_or_blocking(self):
"""Test that scripts don't have infinite loops or blocking operations."""
# Test with timeout to ensure scripts don't hang
scripts = [f"{i}_1_martin_roebke" for i in range(1, 11)] + ["quantenmechanik"]
for script_name in scripts:
with self.subTest(script=script_name):
script_path = f"{script_name}.py"
if os.path.exists(script_path):
try:
# Use subprocess with timeout for safety
test_code = f'''
import sys
import importlib.util
import matplotlib
matplotlib.use('Agg')
# Mock show to prevent blocking
import matplotlib.pyplot as plt
plt.show = lambda: None
plt.pause = lambda x: None
# Import the module using importlib
spec = importlib.util.spec_from_file_location("{script_name}", "{script_path}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
print("Import successful")
'''
result = subprocess.run([
sys.executable, '-c', test_code
],
timeout=30, # 30 second timeout
capture_output=True,
text=True,
cwd=os.getcwd()
)
if result.returncode != 0 and "Import successful" not in result.stdout:
# Only fail if there's an actual error, not just warnings
if "Error" in result.stderr or "Exception" in result.stderr:
self.fail(f"Script {script_name} failed: {result.stderr}")
print(f"✓ {script_name} completed without hanging")
except subprocess.TimeoutExpired:
self.fail(f"Script {script_name} timed out (possible infinite loop)")
except Exception as e:
self.fail(f"Error testing {script_name}: {e}")
def test_memory_usage_reasonable(self):
"""Test that scripts don't use excessive memory."""
import psutil
import os
# Test a representative script
script_name = "1_1_martin_roebke"
process = psutil.Process(os.getpid())
initial_memory = process.memory_info().rss
# Import and use the script
spec = importlib.util.spec_from_file_location(script_name, f"{script_name}.py")
module = importlib.util.module_from_spec(spec)
with patch('matplotlib.pyplot.show'), patch('matplotlib.pyplot.pause'):
spec.loader.exec_module(module)
# Create some objects
std_abb = module.StdAbb(K=2.0, max_iterationen=1000)
std_abb.standard_abbildung(1.0, 0.5)
final_memory = process.memory_info().rss
memory_increase = final_memory - initial_memory
# Memory increase should be reasonable (less than 100MB for these scripts)
self.assertLess(memory_increase, 100 * 1024 * 1024,
f"Memory usage increased by {memory_increase / (1024*1024):.1f}MB")
print(f"✓ Memory usage test passed (increase: {memory_increase / (1024*1024):.1f}MB)")
class TestDependencyCompatibility(unittest.TestCase):
"""Test compatibility with specific dependency versions."""
def test_numpy_compatibility(self):
"""Test NumPy 2.2+ compatibility."""
import numpy as np
# Test new NumPy 2.x features and compatibility
arr = np.array([1, 2, 3, 4, 5])
# Test basic operations
self.assertTrue(np.all(arr > 0))
# Test that old functions still work
mean_val = np.mean(arr)
self.assertAlmostEqual(mean_val, 3.0)
# Test advanced operations
fft_result = np.fft.fft(arr)
self.assertEqual(len(fft_result), len(arr))
print("✓ NumPy 2.2+ compatibility verified")
def test_matplotlib_backend_handling(self):
"""Test matplotlib backend handling for different environments."""
import matplotlib.pyplot as plt
# Test that we can create figures without display
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 2])
# Test widgets can be created
from matplotlib.widgets import Slider, Button
ax_slider = plt.axes([0.1, 0.1, 0.8, 0.03])
slider = Slider(ax_slider, 'Test', 0, 10, valinit=5)
ax_button = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(ax_button, 'Test')
# Clean up
plt.close('all')
print("✓ Matplotlib backend handling verified")
def test_scipy_compatibility(self):
"""Test SciPy 1.15+ compatibility."""
from scipy import integrate, optimize
from scipy.linalg import eigh
import numpy as np
# Test integration
result, _ = integrate.quad(lambda x: x**2, 0, 1)
self.assertAlmostEqual(result, 1/3, places=5)
# Test optimization
result = optimize.minimize_scalar(lambda x: (x-2)**2)
self.assertTrue(result.success)
# Test linear algebra
A = np.array([[1, 2], [2, 1]])
eigenvals, _ = eigh(A)
self.assertEqual(len(eigenvals), 2)
print("✓ SciPy 1.15+ compatibility verified")
def run_execution_tests():
"""Run all script execution tests."""
print("=" * 60)
print("COMPUTATIONAL PHYSICS 2016 - SCRIPT EXECUTION TESTS")
print("=" * 60)
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# Add test classes
suite.addTests(loader.loadTestsFromTestCase(TestIndividualScriptExecution))
suite.addTests(loader.loadTestsFromTestCase(TestScriptSafety))
suite.addTests(loader.loadTestsFromTestCase(TestDependencyCompatibility))
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
print("=" * 60)
if result.wasSuccessful():
print("🎉 ALL SCRIPT EXECUTION TESTS PASSED!")
print("✅ All scripts execute correctly with updated dependencies")
else:
print("❌ Some execution tests failed!")
print(f"Failures: {len(result.failures)}")
print(f"Errors: {len(result.errors)}")
print("=" * 60)
return result.wasSuccessful()
if __name__ == '__main__':
success = run_execution_tests()
sys.exit(0 if success else 1)