forked from yosupo06/library-checker-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_test.py
executable file
·289 lines (236 loc) · 11.7 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
#!/usr/bin/env python3
import unittest
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
from typing import List
logger = getLogger(__name__)
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)
# 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 TestUnusedExample(unittest.TestCase):
def test_unused_example_user(self):
with create_test_dir('unused_example') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'unused_example/info.toml')])
self.assertEqual(proc.returncode, 0)
def test_unused_example_dev(self):
with create_test_dir('unused_example') as test_dir:
proc = run(['./generate.py', str(Path(test_dir) / 'unused_example/info.toml'), '--dev'])
self.assertEqual(proc.returncode, 0)
def test_unused_example_test(self):
with create_test_dir('unused_example') as test_dir:
proc = run(['./generate.py', str(Path(test_dir) / 'unused_example/info.toml'), '--test'])
self.assertNotEqual(proc.returncode, 0)
class TestNonExistExample(unittest.TestCase):
def test_no_html(self):
with create_test_dir('nonexist_example') as test_dir:
proc = run(
['./generate.py', str(Path(test_dir) / 'nonexist_example/info.toml')])
self.assertEqual(proc.returncode, 0)
def test_non_exist_dev(self):
with create_test_dir('nonexist_example') as test_dir:
proc = run(['./generate.py', str(Path(test_dir) / 'nonexist_example/info.toml'), '--dev'])
self.assertNotEqual(proc.returncode, 0)
def test_non_exist_test(self):
with create_test_dir('nonexist_example') as test_dir:
proc = run(['./generate.py', str(Path(test_dir) / 'nonexist_example/info.toml'), '--test'])
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 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())
proc = run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml')])
self.assertTrue(in_path.exists())
time = in_path.stat().st_mtime_ns
proc = 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())
proc = run(
['./generate.py', str(Path(test_dir) / 'simple_aplusb/info.toml'), '--dev'])
self.assertTrue(in_path.exists())
time = in_path.stat().st_mtime_ns
proc = 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 TestGenerateHtml(unittest.TestCase):
def test_generate_html_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)
self.assertTrue((Path(test_dir) / 'simple_aplusb' / 'task.html').exists())
def test_generate_html_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)
self.assertTrue((Path(test_dir) / 'simple_aplusb' / 'task.html').exists())
class TestHtmlDir(unittest.TestCase):
def test_generate_html_dev(self):
with create_test_dir('simple_aplusb') as test_dir:
html_dir = TemporaryDirectory()
proc = run(['./generate.py', str(Path(test_dir) /
'simple_aplusb/info.toml'), '--dev', '--htmldir', html_dir.name])
self.assertEqual(proc.returncode, 0)
self.assertTrue((Path(html_dir.name) / 'simple_aplusb.html').exists())
def test_generate_html_test(self):
with create_test_dir('simple_aplusb') as test_dir:
html_dir = TemporaryDirectory()
proc = run(['./generate.py', str(Path(test_dir) /
'simple_aplusb/info.toml'), '--test', '--htmldir', html_dir.name])
self.assertEqual(proc.returncode, 0)
self.assertTrue((Path(html_dir.name) / 'simple_aplusb.html').exists())
if __name__ == "__main__":
basicConfig(
level=getenv('LOG_LEVEL', 'DEBUG'),
format="%(asctime)s %(levelname)s %(name)s : %(message)s"
)
unittest.main()