-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
373 lines (320 loc) · 11 KB
/
run.py
File metadata and controls
373 lines (320 loc) · 11 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
"""Test runner.
Parses .nbis test files with inline test annotations and executes them,
reporting pass/fail status and performance metrics.
"""
import argparse
import os
import re
import shutil
import sys
import time
from collections import defaultdict
from concurrent.futures import ProcessPoolExecutor, as_completed
from contextlib import redirect_stderr, redirect_stdout
from dataclasses import dataclass, field
from io import StringIO
from pathlib import Path
from typing import Optional
import rich.markup
import rich.padding
from rich.console import Console
from tqdm import tqdm
from numerobis.module import Module
from numerobis.utils import is_unix
from runtime.build_lib import build_lib
OUTPUT_DIR = Path("test-output")
os.makedirs(OUTPUT_DIR, exist_ok=True)
console = Console()
@dataclass
class TestResult:
file_name: str
file_path: str
line: int
throws: Optional[str]
thrown: Optional[str] = None
output: str = ""
times: dict[str, float] = field(default_factory=dict)
success: bool = False
def timeit(func):
t0 = time.perf_counter()
func()
return time.perf_counter() - t0
def safe_run(func):
code = 0
try:
code = func()
except SystemExit as e:
code = e.code
code = code if code else 0
if code != 0:
print(f"[E303]: {code}")
return code
def run_single_test(
file_path,
header_source,
test_source,
line,
throws,
cc,
linker,
print_code,
format_code,
use_cmake,
use_ccache,
run_exec=True,
):
output = StringIO()
times = {}
thrown_error = None
output_bin = str(OUTPUT_DIR / f"bin_{abs(hash((str(file_path), line)))}")
if not is_unix:
output_bin += ".exe"
try:
with redirect_stdout(output), redirect_stderr(output):
header_mod = Module(path=file_path, source=header_source)
header_mod.parse()
header_mod.typecheck()
mod = Module(path=file_path, source=test_source)
mod.namespaces.update(header_mod.namespaces)
mod.namespaces.imports.update(header_mod.namespaces.imports)
print(mod.meta.source.strip())
print()
times["Parsing"] = timeit(mod.parse)
times["Typechecking"] = timeit(mod.typecheck)
mod.header = mod.header.merge(header_mod.header)
mod.imports = header_mod.imports[:-1] + mod.imports
times["Compilation"] = timeit(mod.compile)
times["Linking"] = timeit(
lambda: mod.link(print_=print_code or format_code, format=format_code)
)
comp_success = []
times["C Compiler"] = timeit(
lambda: comp_success.append(
safe_run(
lambda: mod.cmake(
output_path=output_bin,
cc=cc,
linker=linker,
use_cmake=use_cmake,
use_ccache=use_ccache,
)
)
)
)
if run_exec and comp_success[0] == 0:
times["Execution"] = timeit(
lambda: safe_run(lambda: mod.run(path=output_bin))
)
except SystemExit:
pass
out_str = output.getvalue()
error_match = re.search(r"\[(E\d{3})\]", out_str)
thrown_error = error_match.group(1) if error_match else None
success = (throws == thrown_error) or (throws == "///")
return TestResult(
file_name=Path(file_path).name,
file_path=str(file_path),
line=line,
throws=throws,
thrown=thrown_error,
output=out_str,
times=times,
success=success,
)
def parse_args():
parser = argparse.ArgumentParser(description="Run Numerobis test suite")
output_group = parser.add_mutually_exclusive_group()
output_group.add_argument(
"-v", "--verbose", action="store_true", help="Show failed tests"
)
output_group.add_argument(
"-f", "--full", action="store_true", help="Show all tests"
)
parser.add_argument(
"-p", "--print", action="store_true", help="Print generated code"
)
parser.add_argument(
"-F", "--format", action="store_true", help="Print formatted code"
)
parser.add_argument("--cc", default="gcc", help="C compiler")
parser.add_argument("--linker", default=None, help="C linker")
parser.add_argument(
"-j", "--jobs", type=int, default=os.cpu_count() or 4, help="Parallel jobs"
)
parser.add_argument(
"--no-cmake",
dest="use_cmake",
action="store_false",
default=True,
help="Skip CMake and use direct GCC bindings (potentially unstable)",
)
parser.add_argument(
"--no-lib",
action="store_true",
help="Skip re-building the static runtime libraries",
)
parser.add_argument(
"--ccache",
dest="use_ccache",
action="store_true",
help="Use ccache to speed up recompilation.",
)
parser.add_argument("tests", nargs="*", help="Specific tests to run")
return parser.parse_args()
def main():
args = parse_args()
if not (args.verbose or args.full):
args.verbose = True
if not args.no_lib:
build_lib()
print()
tests_dir = Path("tests")
examples_dir = Path("examples")
files = sorted([f for f in os.listdir(tests_dir) if f.endswith(".nbis")])
example_files = sorted([f for f in os.listdir(examples_dir) if f.endswith(".nbis")])
if args.tests:
files = [f for f in files if f.removesuffix(".nbis") in args.tests]
example_files = example_files if "examples" in args.tests else []
console.print(
"[bold]Running tests[/bold] "
f"[dim]\\[mode={'full' if args.full else 'verbose' if args.verbose else 'quiet'}, "
f"output={'simple' if args.print else 'formatted' if args.format else 'none'}, "
f"compiler={args.cc}, "
f"linker={args.linker if args.linker else 'default'}, "
f"cmake={str(args.use_cmake).lower()}, "
f"ccache={str(args.use_ccache).lower()}][/dim]",
highlight=False,
)
if args.tests:
console.print(
f"[dim]Running {len(files)} selected test file(s)[/dim]", highlight=False
)
actual_time = time.time()
test_queue = []
# standard tests
for file in files:
path = tests_dir / file
lines = open(path, "r", encoding="utf-8").readlines()
header_src, chunk_src, curr_line, curr_throws, first = "", "", 1, None, True
for i, line in enumerate(lines):
if match := re.match(r"# ((---+)|(E\d{3})|(///+))", line.strip()):
if first:
header_src, first = chunk_src, False
else:
test_queue.append(
(path, header_src, chunk_src, curr_line, curr_throws, True)
)
curr_throws = (
match.group(1) if not match.group(1).startswith("-") else None
)
chunk_src, curr_line = "", i + 2
else:
chunk_src += line
test_queue.append((path, header_src, chunk_src, curr_line, curr_throws, True))
# example files
for file in example_files:
path = examples_dir / file
with open(path, "r", encoding="utf-8") as f:
test_queue.append((path, "", f.read(), 1, None, False))
results: list[TestResult] = []
fail_count = 0
with ProcessPoolExecutor(max_workers=args.jobs) as executor:
futures = [
executor.submit(
run_single_test,
p,
h,
c,
ll,
t,
args.cc,
args.linker,
args.print,
args.format,
args.use_cmake,
args.use_ccache,
run_exec,
)
for p, h, c, ll, t, run_exec in test_queue
]
with tqdm(total=len(futures), leave=False) as pbar:
for future in as_completed(futures):
res = future.result()
results.append(res)
if not res.success:
fail_count += 1
pbar.set_description(f"{res.file_name}")
pbar.set_postfix(errors=fail_count, line=res.line)
pbar.update(1)
results.sort(key=lambda x: (x.file_path, x.line))
cumulative = defaultdict(float)
passed = 0
for res in results:
if not res.success:
text = (
(f"expected [bold]{res.throws}[/bold]" if res.throws else "")
+ (", " if res.throws and res.thrown else "")
+ (f"raised [bold]{res.thrown}[/bold]" if res.thrown else "")
)
console.print(
f"[bold red][FAIL][/bold red] [red]{res.file_path}:{res.line}[/red]: {text}",
highlight=False,
)
if args.verbose or args.full:
console.print(
rich.padding.Padding(
f"[dim]{rich.markup.escape(res.output)}[/dim]", (0, 0, 0, 2)
),
highlight=False,
)
else:
passed += 1
if args.full:
console.print(
f"[bold green][PASS][/bold green] [green]{res.file_path}:{res.line}[/green]",
highlight=False,
)
console.print(
rich.padding.Padding(
f"[dim]{rich.markup.escape(res.output)}[/dim]", (0, 0, 0, 2)
),
highlight=False,
)
for k, v in res.times.items():
cumulative[k] += v
console.print("\n[bold][SUMMARY][/bold]")
t = len(results)
ratio_pct = passed / t if t > 0 else 0
color = (
"green"
if ratio_pct == 1 or t == 0
else "orange3"
if ratio_pct >= 0.6
else "red"
)
console.print(
f"[bold {color}]{passed}/{t}[/bold {color}] tests passed "
f"[dim]([bold {color}]{ratio_pct:.2%}[/bold {color}])[/dim]",
highlight=False,
)
total_time = sum(cumulative.values())
console.print(
f"[bold]Total time[/bold]: [bold cyan]{total_time:.3f}s[/bold cyan] "
f"[dim]([bold cyan]{total_time / t if t > 0 else 0:.4f}s[/bold cyan] average, "
f"[bold cyan]{time.time() - actual_time:.4f}s[/bold cyan] real)[/dim]",
highlight=False,
)
for key, value in cumulative.items():
console.print(
f" {key}: [bold cyan]{value:.3f}s[/bold cyan] "
f"[dim]([bold cyan]{value / t if t > 0 else 0:.4f}s[/bold cyan] average)[/dim]",
highlight=False,
)
shutil.rmtree(OUTPUT_DIR)
sys.exit(0 if passed == t else 1)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
console.print("[bold red]Interrupted by user[/bold red]")
sys.exit(1)