-
Notifications
You must be signed in to change notification settings - Fork 0
/
mips_test.py
97 lines (80 loc) · 3.36 KB
/
mips_test.py
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
import operator
import os
import sys
from colorama import init, Fore
from utils import get_mips_output, remove_space
import concurrent.futures as confu
import multiprocessing.pool as mpp
init(autoreset=True)
def get_test_names(test_set):
return os.listdir(test_set)
score_sum = 0
cycles_sum = 0
def run_unit(dir_name, test, test_set, option=None):
try:
testfile_name = os.path.join(test_set, test)
output_name = os.path.join(test_set, test, "output.txt")
# lhs_out = get_exe_output(dir_name, testfile_name, "error.txt")
lhs_out, cycles = get_mips_output(dir_name, testfile_name)
ref_cycles_path = os.path.join(test_set, test, "ref_cycles")
lhs_out = remove_space(lhs_out)
if not os.path.exists(output_name):
with open(output_name, "w") as f:
for line in lhs_out:
f.write(line)
f.write("\n")
rhs_out = remove_space(open(output_name).readlines())
if operator.eq(lhs_out, rhs_out):
if not os.path.exists(ref_cycles_path):
os.system("echo {} > {}".format(cycles, ref_cycles_path))
ref_cycles = int(open(ref_cycles_path).readline())
score = ref_cycles / cycles
global cycles_sum, score_sum
cycles_sum += cycles
score_sum += score * cycles
print(Fore.GREEN + "passed: {}. Cycles: {}, Score: {:.2f}".format(test, cycles, score))
return True
else:
print(Fore.LIGHTRED_EX + "failed: {}".format(test))
length = min(len(lhs_out), len(rhs_out))
for i in range(length):
if lhs_out[i] != rhs_out[i]:
print(Fore.LIGHTRED_EX + "found {} while expected '{}' at line {}"
.format("'{}'".format(lhs_out[i]), "'{}'".format(rhs_out[i]), i + 1))
return False
if len(lhs_out) > len(rhs_out):
print(Fore.LIGHTRED_EX + "found {} while expected {} at line {}"
.format("'{}'".format(lhs_out[length]), "NOTHING", length + 1))
elif len(lhs_out) < len(rhs_out):
print(Fore.LIGHTRED_EX + "found {} while expected {} at line {}"
.format("NOTHING", "'{}'".format(rhs_out[length]), length + 1))
return False
# exit(-1)
except:
print(Fore.LIGHTRED_EX + "failed: {}. unable to compile and run".format(test))
return False
def unit(dir_name, test_set, option=None):
ncases = 0
npassed = 0
for test in get_test_names(test_set):
ncases += 1
if run_unit(dir_name, test, test_set, option):
npassed += 1
if ncases == npassed:
global cycles_sum, score_sum
weighted_score = score_sum / cycles_sum
print(Fore.GREEN + "Passed {}/{}, Weighted Score: {:.2f}".format(npassed, ncases, weighted_score))
else:
print(Fore.LIGHTRED_EX + "Passed {}/{}".format(npassed, ncases))
if __name__ == "__main__":
# unit("zyc", "test_set_mips")
if not os.path.exists("outputfiles"):
os.mkdir("outputfiles")
if len(sys.argv) <= 3:
unit(sys.argv[1], sys.argv[2])
else:
if sys.argv[3][0] == '-':
unit(sys.argv[1], sys.argv[2], sys.argv[3][1:])
else:
run_unit(sys.argv[1], sys.argv[3], sys.argv[2])
# pass