summaryrefslogtreecommitdiffstats
path: root/Main/Python/grader.py
blob: 5802dbd793ba49291568ba0e2420ef5b754c194e (plain)
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
import io, sys, os, time
from tester import run


class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = io.StringIO()
        return self

    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio  # free up some memory
        sys.stdout = self._stdout


PATH = "2020/"
PROBLEM = "S3"

test_input_path = os.path.join(PATH, f"senior_data/{PROBLEM}/")

test_inputs = []
test_outputs = []
for root, dirs, files in os.walk(test_input_path):
    for i, file in enumerate(files):
        with open(os.path.join(root, file), "r") as f:
            if i % 2 == 0:
                test_inputs.append(f.read())
            else:
                test_outputs.append(f.read())

total = min(len(test_inputs), len(test_outputs))
correct = 0

counter = 1
for test_input, test_output in zip(test_inputs, test_outputs):
    with Capturing() as output:
        start = time.perf_counter()
        run(PATH, PROBLEM, test_input)
        end = time.perf_counter()

    if "\n".join(output) + "\n" == test_output:
        print(f"Test {counter} Passed. {round(end - start, 3)}s")
        correct += 1
    else:
        print(f"Test {counter} Failed. {round(end - start, 3)}s")
        print(f"{test_input = }")
        print(f"{test_output = }")
        print(f"{output = }")
        print()
    counter += 1

print("--------------------------------")
print(f"Tests passed: {correct}/{total}")