-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest
executable file
·168 lines (152 loc) · 5.63 KB
/
test
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
#!/usr/bin/env python
"""
Run all programs of form: src/<lang>/<executable>
For some chosen programs, language independent input / output
data is supplied and verified checked.
For the others, the program is simply run and stdout is checked.
"""
import os
import subprocess
import sys
# Parameters
base_dir = os.path.abspath(os.getcwd())
data_base_dir = os.path.join(base_dir, 'data')
src_dir = os.path.join(base_dir, 'src')
in_ext = '.in'
out_ext = '.out'
sep = '-' * 75
class TestDescription:
def __init__(self, data):
self.data = data
known_basenames_test = {
'HeapSort.java': TestDescription('sort'),
'QuickSort.java': TestDescription('sort'),
'QuickSortTail.java': TestDescription('sort'),
'Tac.java': TestDescription('tac'),
'merge_sort.cpp': TestDescription('sort'),
'quick_sort.cpp': TestDescription('sort'),
'sum_array.cpp': TestDescription('sum_array'),
'sum_array_parallel.cpp': TestDescription('sum_array'),
'tac.c': TestDescription('tac'),
}
known_basenames = known_basenames_test.keys()
# Assert that the stdout is the same as the expected one, and that the exit_status is 0.
# If not, print diagnostic error message and exit 1.
def assert_output(exit_status, stdout, expected_stdout, stderr, test_id = None, input_path = None):
fail = False
if stderr:
print
print sep
print 'stderr\n'
print stderr
if exit_status != 0:
print
print 'Exit status != 0: {}'.format(exit_status)
if stdout:
print 'stdout\n'
print stdout
fail = True
elif stdout != expected_stdout:
print
print 'stdout'
print
print stdout
print sep
print 'Expected stdout'
print
print expected_stdout
fail = True
if fail:
if input_path:
print
print sep
print 'Input'
print
with open(input_path, 'r') as input_file:
print input_file.read()
if test_id:
msg = 'Failed test id: ' + test_id
else:
msg = 'A test failed.'
print msg
sys.exit(1);
def run(cmd):
process = subprocess.Popen(
cmd,
shell = False,
stdin = None,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
universal_newlines = True
)
stdout, stderr = process.communicate()
exit_status = process.wait()
return [exit_status, stdout, stderr]
if __name__ == '__main__':
if len(sys.argv) > 1:
paths = sys.argv[1:]
else:
paths = []
for lang in os.listdir(src_dir):
if os.path.isdir(os.path.join(src_dir, lang)):
paths.append(lang)
basenames = []
for path in paths:
new_path = os.path.join(src_dir, path)
if os.path.isdir(new_path):
path = new_path
if os.path.isdir(path):
basenames.extend(os.listdir(path))
else:
basenames.append(os.path.split(path)[1])
basenames.sort()
built_langs = set()
for basename in basenames:
basename_noext, lang = os.path.splitext(basename)
lang = lang[1:]
if lang:
lang_dir = os.path.join(src_dir, lang)
if (os.path.exists(lang_dir)):
print basename
if lang == 'c' or lang == 'cpp':
run_cmd = [os.path.join(lang_dir, basename_noext + '.out')]
elif lang == 'java':
run_cmd = ['java', '-cp', lang_dir, '-ea', basename_noext]
# Build.
if lang not in built_langs:
built_langs.add(lang)
os.chdir(lang_dir)
# Maybe add an option later to also clean everything.
#if subprocess.call(['make', 'clean'], stdout=open(os.devnull, 'wb')) != 0:
#print 'error: make clean.'
#sys.exit(1);
if subprocess.call(['make'], stdout=open(os.devnull, 'wb')) != 0:
print 'error: make failed.'
sys.exit(1);
os.chdir(base_dir)
if basename in known_basenames:
# Run executable by passing the data input file to them.
# Get input data.
data_dir = os.path.join(data_base_dir, known_basenames_test[basename].data)
data_basenames = [os.path.splitext(p)[0]
for p
in os.listdir(data_dir)
if os.path.splitext(p)[1] == '.in'
and os.path.isfile(os.path.join(data_dir, p))]
# Run tests.
for data_basename in data_basenames:
input_path = os.path.join(data_dir, data_basename) + in_ext
output_path = os.path.join(data_dir, data_basename) + out_ext
exit_status, stdout, stderr = run(run_cmd + [input_path])
with open(output_path, 'r') as output_file:
expected_stdout = output_file.read()
assert_output(exit_status, stdout, expected_stdout, stderr, data_basename, input_path)
else:
# Run executable without any command line arguments,
# and check its exit status.
# Ignore stdout. Print stderr.
exit_status, stdout, stderr = run(run_cmd)
assert_output(exit_status, stdout, stdout, stderr, None)
print
print 'All tests passed.'
sys.exit(0);