Repository navigation
Expand file tree
/
Copy pathruntests.py
More file actions
84 lines (68 loc) · 1.91 KB
/
Copy pathruntests.py
File metadata and controls
84 lines (68 loc) · 1.91 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
import array
import gc
import importlib
import sys
def run_tests(testmod, verbose=True):
for name in dir(testmod):
if not name.startswith('test'):
continue
func = getattr(testmod, name)
print(f"{name}()")
func()
print("add()")
if testmod.add(11, 23) != 34:
raise AssertionError("add() failed badly")
print(flush=True)
def run_refleak_tests(testmod):
# Run tests and check for reference leaks
warmups = 3
runs = 3
repcount = warmups + runs
refcount = array.array('q', [0] * repcount)
rep_range = tuple(range(repcount))
for i in rep_range:
print(f"Run #{i + 1}", flush=True)
run_tests(testmod)
gc.collect()
refcount[i] = sys.gettotalrefcount()
# Compute differences
previous = None
for i in rep_range:
value = refcount[i]
if previous is not None:
refcount[i] = value - previous
previous = value
deltas = list(refcount[warmups:])
if all(delta >= 1 for delta in deltas):
print(f'ERROR: Tests leaked {deltas} references')
sys.exit(1)
elif any(deltas):
print(f'WARNING: Tests leaked {deltas} references (this is fine)')
def main():
if len(sys.argv) < 2:
print("usage: python runtests.py TEST_MODULE_NAME")
sys.exit(1)
module_name = sys.argv[1]
testmod = importlib.import_module(module_name)
newline = False
for name in (
'__STDC_VERSION__',
'__STRICT_ANSI__',
'__cplusplus',
'_MSVC_LANG',
):
try:
value = getattr(testmod, name)
except AttributeError:
pass
else:
print(f'{name}: {value}')
newline = True
if newline:
print()
if hasattr(sys, 'gettotalrefcount'):
run_refleak_tests(testmod)
else:
run_tests(testmod)
if __name__ == "__main__":
main()