-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
generate_test.py
executable file
·312 lines (260 loc) · 11.9 KB
/
generate_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python3
import unittest
import json
import colorlog
from logging import basicConfig, getLogger
from os import chdir, getenv
from subprocess import run, check_output
from shutil import copy
from pathlib import Path
from tempfile import TemporaryDirectory
from generate import Problem, param_to_str, casename
logger = getLogger(__name__)
@unittest.skipIf(getenv('ENABLE_GENERATE_TEST') is None, "generate test take long time")
class TestGenerateAll(unittest.TestCase):
def test_generate_all(self):
tomls = list(filter(lambda p: not p.match('test/**/info.toml'), Path('.').glob('**/info.toml')))
tomls = sorted(tomls, key=lambda x: x.parent.name)
cache_path = Path(getenv('VERSIONS_CACHE_PATH'))
versions = dict()
if cache_path.exists():
with open(cache_path, 'r') as cache_file:
versions = json.load(cache_file)
for toml in tomls:
problem = Problem(Path.cwd(), toml.parent)
name = problem.basedir.name
version = problem.problem_version()
with self.subTest(name=name):
if versions.get(name) == version:
logger.info('Skip generated problem: {}'.format(name))
else:
logger.info('Generate {}'.format(name))
problem.generate(mode=Problem.Mode.TEST)
problem.generate(mode=Problem.Mode.CLEAN)
versions[name] = version
with open(cache_path, 'w') as f:
json.dump(versions, f)
def create_test_dir(problem_name: str) -> TemporaryDirectory:
problem_dir = Path('test') / problem_name
files = check_output(
['git', 'ls-files', str(problem_dir)]).decode('utf-8').split()
new_dir = TemporaryDirectory()
for f in files:
path = Path(f).relative_to(Path('test'))
src = Path('test') / path # type: Path
trg = Path(new_dir.name) / path # type: Path
if not trg.parent.exists():
trg.parent.mkdir(parents=True)
copy(str(src), str(trg))
return new_dir
class TestSuccess(unittest.TestCase):
# select problem by problem id
def test_success_user(self):
with create_test_dir('simple_aplusb') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml')])
self.assertEqual(proc.returncode, 0)
def test_success_dev(self):
with create_test_dir('simple_aplusb') as test_dir:
proc = run(['./generate.py', str(Path(test_dir) /
'simple_aplusb/info.toml'), '--dev'])
self.assertEqual(proc.returncode, 0)
def test_success_test(self):
with create_test_dir('simple_aplusb') as test_dir:
proc = run(['./generate.py', str(Path(test_dir) /
'simple_aplusb/info.toml'), '--test'])
self.assertEqual(proc.returncode, 0)
class TestClean(unittest.TestCase):
# select problem by problem id
def test_clean(self):
with create_test_dir('simple_aplusb') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml')])
self.assertEqual(proc.returncode, 0)
self.assertTrue((Path(test_dir) / 'simple_aplusb' / 'in').exists())
proc = run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml'), '--clean'])
self.assertEqual(proc.returncode, 0)
self.assertFalse(
(Path(test_dir) / 'simple_aplusb' / 'in').exists())
# it is ok to run twice
proc = run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml'), '--clean'])
self.assertEqual(proc.returncode, 0)
# warn: --compile-checker is used in other project(e.g. kmyk/online-judge-verify-helper)
class TestCompileChecker(unittest.TestCase):
def test_compile_checker(self):
with create_test_dir('simple_aplusb') as test_dir:
checker = Path(test_dir) / 'simple_aplusb/checker'
self.assertFalse(checker.exists())
proc = run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml'), '--compile-checker'])
self.assertEqual(proc.returncode, 0)
self.assertTrue(checker.exists())
def test_compile_checker_nocopy(self):
checker = Path('test/simple_aplusb/checker')
if checker.exists():
checker.unlink()
proc = run(
['./generate.py', '-p', 'simple_aplusb', '--compile-checker'])
self.assertEqual(proc.returncode, 0)
self.assertTrue(checker.exists())
class TestVerify(unittest.TestCase):
def test_no_verify_user(self):
with create_test_dir('failed_verify') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'failed_verify/info.toml')])
self.assertEqual(proc.returncode, 0)
def test_no_verify_dev(self):
with create_test_dir('failed_verify') as test_dir:
proc = run(['./generate.py', str(Path(test_dir) /
'failed_verify/info.toml'), '--dev'])
self.assertNotEqual(proc.returncode, 0)
def test_no_verify_test(self):
with create_test_dir('failed_verify') as test_dir:
proc = run(['./generate.py', str(Path(test_dir) /
'failed_verify/info.toml'), '--test'])
self.assertNotEqual(proc.returncode, 0)
class TestNonExistProblem(unittest.TestCase):
def test_non_exist_problem(self):
proc = run(
['./generate.py', '-p', 'dummy_problem'])
self.assertNotEqual(proc.returncode, 0)
class TestUnusedGen(unittest.TestCase):
def test_unused_gen_user(self):
with create_test_dir('unused_gen') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'unused_gen/info.toml')])
self.assertNotEqual(proc.returncode, 0)
def test_unused_gen_dev(self):
with create_test_dir('unused_gen') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'unused_gen/info.toml'), '--dev'])
self.assertEqual(proc.returncode, 0)
def test_unused_gen_test(self):
with create_test_dir('unused_gen') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'unused_gen/info.toml'), '--test'])
self.assertNotEqual(proc.returncode, 0)
class TestNoTitle(unittest.TestCase):
def test_no_title_user(self):
with create_test_dir('no_title') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'no_title/info.toml')])
self.assertNotEqual(proc.returncode, 0)
def test_no_title_dev(self):
with create_test_dir('no_title') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'no_title/info.toml'), '--dev'])
self.assertEqual(proc.returncode, 0)
def test_no_title_test(self):
with create_test_dir('no_title') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'no_title/info.toml'), '--test'])
self.assertNotEqual(proc.returncode, 0)
class TestCallFromOutside(unittest.TestCase):
def test_call_from_outside(self):
cwd = Path.cwd()
try:
chdir('/')
proc = run(
[str(cwd / 'generate.py'), str(cwd / 'test/simple_aplusb/info.toml')])
self.assertEqual(proc.returncode, 0)
finally:
chdir(str(cwd))
class TestAllowRE(unittest.TestCase):
def test_allow_re(self):
with create_test_dir('allow_re') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'allow_re/info.toml'), '--test'])
self.assertEqual(proc.returncode, 0)
class TestAllowTLE(unittest.TestCase):
def test_allow_tle(self):
with create_test_dir('allow_tle') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'allow_tle/info.toml'), '--test'])
self.assertEqual(proc.returncode, 0)
class TestOtherCheckerPlace(unittest.TestCase):
def test_other_checker_place(self):
with create_test_dir('other_checker_place') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'other_checker_place/info.toml'), '--test'])
self.assertEqual(proc.returncode, 0)
class TestOtherVerifierPlace(unittest.TestCase):
def test_other_verifier_place(self):
with create_test_dir('other_verifier_place') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'other_verifier_place/info.toml'), '--test'])
self.assertEqual(proc.returncode, 0)
class TestCacheTest(unittest.TestCase):
def test_cache_user(self):
with create_test_dir('simple_aplusb') as test_dir:
in_path = Path(test_dir) / \
'simple_aplusb/in/random_00.in' # type: Path
self.assertFalse(in_path.exists())
run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml')])
self.assertTrue(in_path.exists())
time = in_path.stat().st_mtime_ns
run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml')])
self.assertTrue(in_path.exists())
self.assertEqual(time, in_path.stat().st_mtime_ns)
def test_cache_dev(self):
with create_test_dir('simple_aplusb') as test_dir:
in_path = Path(test_dir) / \
'simple_aplusb/in/random_00.in' # type: Path
self.assertFalse(in_path.exists())
run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml'), '--dev'])
self.assertTrue(in_path.exists())
time = in_path.stat().st_mtime_ns
run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml'), '--dev'])
self.assertTrue(in_path.exists())
self.assertNotEqual(time, in_path.stat().st_mtime_ns)
class TestListDependingFiles(unittest.TestCase):
def test_list_depending_files(self):
problem = Problem(Path.cwd(), Path('sample/aplusb'))
files = list(problem.list_depending_files()) # type: List[Path]
find_random = False
find_verifier = False
for f in files:
if f.resolve() == Path('common/random.h').resolve():
find_random = True
if f.resolve() == Path('sample/aplusb/verifier.cpp').resolve():
find_verifier = True
self.assertTrue(find_random)
self.assertTrue(find_verifier)
class TestCasename(unittest.TestCase):
# select problem by problem id
def test_casename(self):
self.assertEqual(casename('example', 0), "example_00")
self.assertEqual(casename('example', 1), "example_01")
self.assertEqual(casename('random', 10), "random_10")
class TestParam(unittest.TestCase):
# select problem by problem id
def test_convert_integer(self):
self.assertEqual(param_to_str('A', 100), "#define A (long long)100")
self.assertEqual(param_to_str('A', 1_000_000_007),
"#define A (long long)1000000007")
self.assertEqual(param_to_str('A', 998244353),
"#define A (long long)998244353")
if __name__ == "__main__":
handler = colorlog.StreamHandler()
formatter = colorlog.ColoredFormatter(
"%(log_color)s%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S",
log_colors={
'DEBUG': 'cyan',
'INFO': 'white',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
})
handler.setFormatter(formatter)
basicConfig(
level=getenv('LOG_LEVEL', 'DEBUG'),
handlers=[handler]
)
unittest.main()