-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (48 loc) · 1.66 KB
/
main.py
File metadata and controls
60 lines (48 loc) · 1.66 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
import unittest
from pathlib import Path
import importlib
import time
import json
# TODO: move this to a function, make it csv?
answers = {}
with Path("answers.json").open() as f:
answers = json.load(f)
class AoCTests(unittest.TestCase):
pass
def generate_tests():
for year in range(15, 26):
year_mod = Path(f"_{year}")
if not year_mod.exists():
continue
for day in range(1, 26):
try:
mod = importlib.import_module(f"_{year}.d{day:02}")
except ModuleNotFoundError:
continue
for part in 'ab':
func_name = f"y{year}{day:02}{part}"
func = getattr(mod, func_name, None)
if func is None:
continue
test_name = f"test_{func_name}"
setattr(AoCTests, test_name, make_test(year, day, part, func))
def make_test(year, day, part, func):
def test(self):
path = Path("inputs") / str(year) / f"{day:02}.txt"
if not path.exists():
self.skipTest(f"Input file missing: {path}")
expected = answers.get(str(year), {}).get(f"{day:02}", {}).get(part)
if not expected:
self.skipTest("Answer is missing")
with path.open("r") as f:
start = time.perf_counter()
# TODO suppress any output to stdout from func?
result = func(f)
elapsed = time.perf_counter() - start
print(f"{result} ({elapsed:.6f} s)")
if expected is not None:
self.assertEqual(result, expected)
return test
generate_tests()
if __name__ == "__main__":
unittest.main(verbosity=2)