-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
76 lines (59 loc) · 2.11 KB
/
Copy pathtest.py
File metadata and controls
76 lines (59 loc) · 2.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
import os
from db import *
def run_test(test_input, test_output):
test_input = test_input.strip()
commands = test_input.split("\n")
expected_results = test_output.strip().split("\n")
actual_results = list()
db = MemDB()
for cmd in commands:
if "UNSET" in cmd:
_, k = cmd.split(" ")
db.remove(k)
elif "SET" in cmd:
_, k, v = cmd.split(" ")
db.set(k, v)
elif "GET" in cmd:
_, k = cmd.split(" ")
r = db.get(k)
if r:
actual_results.append(r)
else:
actual_results.append("NULL")
elif "BEGIN" in cmd:
db.begin()
elif "ROLLBACK" in cmd:
r = db.rollback()
if r is not None:
actual_results.append(r)
elif "COMMIT" in cmd:
r = db.commit()
if r is not None:
actual_results.append(r)
else:
raise ValueError("Error: unrecognized command {}".format(cmd))
actual_results = [str(x) for x in actual_results]
if expected_results == actual_results:
print("Test passed")
return True
else:
print("Test failed")
print("Expected: {}".format(expected_results))
print("Actual: {}".format(actual_results))
return False
if __name__ == "__main__":
TEST_DIR = "tests/"
test_inputs = sorted([x for x in os.listdir(TEST_DIR) if "input" in x])
total_test_cnt = len(test_inputs)
failed_test_cnt = 0
print("------------")
for i, t in enumerate(test_inputs):
print("Running test {}: {}".format(i+1, t))
input_file = open(TEST_DIR + t, "r").read()
output_correct = t.replace("input", "output")
output_file = open(TEST_DIR + output_correct, "r").read()
if not run_test(input_file, output_file):
failed_test_cnt += 1
print("------------")
success_test_cnt = total_test_cnt - failed_test_cnt
print("Ran {} Tests: {} passed; {} failed".format(total_test_cnt, success_test_cnt, failed_test_cnt))