-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
executable file
·174 lines (134 loc) · 4.28 KB
/
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
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
169
170
171
172
173
174
#!/usr/bin/env python
"""
IFJ 15 Test Suite
Author: Pavol Plaskon <[email protected]>
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
"""
import os
import random
import subprocess
import sys
dash_line = "-------------------------------------"
def print_head(text):
print(dash_line)
print(dash_line)
print("\n{}\n".format(text))
print(dash_line)
def print_test_head(n, source):
global to_print
to_print += dash_line + "\n"
to_print += "TEST {}: {}\n".format(n, source)
def check_ret_code(returned, expected):
global to_print
if returned == int(expected):
to_print += "Return value: OK\n"
return True
else:
to_print += "Expected return code: {}\nProgram returned: {}\n".format(expected, returned)
return False
def check_stdout(out, expected):
global to_print
if out != expected:
to_print += "UNexpected output: {}\n".format(out)
to_print += "Expected: {}\n".format(expected)
return False
return True
def check_stderr(err):
"""Expect 'IFJ15' in error output."""
global to_print
if 'IFJ15' not in err:
to_print += "UNexpected error output: {}\n".format(err)
return False
return True
def print_statistics(n, n_ok, n_fail, fails):
print(dash_line)
print("Number of tests: {}\nSucceed tests: {}\nFAILED: {}\n"
.format(n, n_ok, n_fail))
if n_fail != 0:
print("Failed tests:")
for k, v in sorted(fails.items()):
print("TEST {}: {}".format(k,v))
if not(os.path.isfile('ifj') and os.access('ifj', os.X_OK)):
print("No executable file named 'ifj' found, try 'make'.")
sys.exit()
#
# OK
#
tests_dir = os.path.join(os.getcwd(), 'tests/tests_ok')
x = 0
x_ok = 0
x_fail = 0
fails = {}
to_print = ''
print_head("IFJ15: Tests without errors.")
for source in [f for f in os.listdir(tests_dir)
if not f.endswith('.out') and f.startswith('test')]:
to_print = ''
x = x + 1
source_path = os.path.join(tests_dir, source)
proc = subprocess.Popen(
["./ifj", source_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
with open(source_path + '.out', 'r') as f_out:
expected_out = f_out.read()
out, err = proc.communicate()
ret_code = proc.returncode
print_test_head(x, source)
c_ret = check_ret_code(ret_code, 0)
c_out = check_stdout(out, expected_out)
c_err = not err
if c_ret and c_out and c_err:
x_ok = x_ok + 1
else:
print(to_print)
x_fail = x_fail + 1
fails[x] = source
#
# Error tests
#
tests_dir = os.path.join(os.getcwd(), 'tests')
fx = 0
fx_ok = 0
fx_fail = 0
ffails = {}
print_head("IFJ15: Tests containing some errors.")
for source in [f for f in os.listdir(tests_dir)
if os.path.isfile(os.path.join(tests_dir, f)) and
f.startswith('test')]:
to_print = ''
fx = fx + 1
source_path = os.path.join(tests_dir, source)
proc = subprocess.Popen(['./ifj', source_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
out, err = proc.communicate()
ret_code = proc.returncode
print_test_head(fx, source)
exp_ret = source[-2:] if source[-2].isdigit() else source[-1]
c_ret = check_ret_code(ret_code, exp_ret)
c_out = check_stdout(out, '')
# c_err = check_stderr(err.decode('utf-8'))
if c_ret and c_out: # and c_err: dont't care about stderr
fx_ok = fx_ok + 1
else:
print(to_print)
fx_fail = fx_fail + 1
ffails[fx] = source
print_head('RESULTS')
print("Tests for correct source files:")
print_statistics(x, x_ok, x_fail, fails)
print(dash_line)
print("Tests for incorrect source files:")
print_statistics(fx, fx_ok, fx_fail, ffails)