-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.py
435 lines (421 loc) · 16.8 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import unittest
from typing import Dict, List, Optional, Union
import lchelper.codegen
from lchelper.common import (
Example,
FunctionSignature,
Interaction,
InteractiveProblemSignature,
Problem,
ProblemSignature,
)
class EndToEndTest(unittest.TestCase):
def _test_problem_set(
self,
url: str,
site: str = "leetcode",
ignore_problems: Optional[List[int]] = None,
):
available_users = [user for user in lchelper.get_users() if user.site == site]
assert (
len(available_users) > 0
), f'User cookie from site "{site}" required for end-to-end tests'
user = available_users[0]
problems = lchelper.get_problems(
url, site, lchelper.get_cookie_path(user.username, user.site)
)
codegen: Dict[str, lchelper.codegen.CodeGen] = {
lang: codegen_klass() for lang, codegen_klass in lchelper.LANGUAGES.items()
}
ignore_problems = ignore_problems or []
for idx, problem in enumerate(problems):
if idx in ignore_problems:
continue
problem_signature = lchelper.parse_problem(problem, site)
for lang, gen in codegen.items():
_, _ = gen.generate_code(problem, problem_signature)
def test_contests(self):
contests = [
("weekly-contest-183", []),
("weekly-contest-182", []),
("weekly-contest-181", []),
("weekly-contest-180", []),
("weekly-contest-163", []),
("biweekly-contest-14", []),
]
for contest, ignore_problems in contests:
url = f"https://leetcode.com/contest/{contest}"
self._test_problem_set(url, ignore_problems=ignore_problems)
class ParseTest(unittest.TestCase):
def _function_equal(
self, parsed_function: FunctionSignature, function: FunctionSignature
):
assert parsed_function.return_type == function.return_type
assert parsed_function.name == function.name
assert parsed_function.arguments == function.arguments
def _test_parse_problem(
self,
problem: Problem,
signature: Union[ProblemSignature, InteractiveProblemSignature],
):
parsed_signature = lchelper.parse_problem(problem)
assert type(parsed_signature) is type(signature)
if isinstance(signature, InteractiveProblemSignature):
assert parsed_signature.class_name == signature.class_name
for parsed_function, function in zip(
parsed_signature.functions, signature.functions
):
self._function_equal(parsed_function, function)
else:
self._function_equal(parsed_signature.function, signature.function)
assert len(parsed_signature.examples) == len(signature.examples)
for idx in range(len(parsed_signature.examples)):
if isinstance(signature, InteractiveProblemSignature):
for parsed_example, example in zip(
parsed_signature.examples[idx], signature.examples[idx]
):
assert parsed_example.function == example.function
assert parsed_example.input == example.input
assert parsed_example.output == example.output
else:
assert (
parsed_signature.examples[idx].input
== signature.examples[idx].input
)
assert (
parsed_signature.examples[idx].output
== signature.examples[idx].output
)
# from lchelper.codegen.cpp import generate_code
# solution_code, test_code = generate_code(problem, signature)
# print(solution_code)
# print(test_code)
def test_parse_problem_1(self):
problem = Problem(
url="",
name="Shift 2D Grid",
statement="",
examples=[
(
"Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1\n"
"Output: [[9,1,2],[3,4,5],[6,7,8]]"
),
(
"Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4\n"
"Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]"
),
(
"Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9\n"
"Output: [[1,2,3],[4,5,6],[7,8,9]]"
),
],
code=[
"class Solution {",
"public:",
" vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {",
" ",
" }",
"};",
],
)
signature = ProblemSignature(
function=FunctionSignature(
return_type="vector<vector<int>>",
name="shiftGrid",
arguments=[("vector<vector<int>>&", "grid"), ("int", "k")],
),
examples=[
Example(
{"grid": [[1, 2, 3], [4, 5, 6], [7, 8, 9]], "k": 1},
[[9, 1, 2], [3, 4, 5], [6, 7, 8]],
),
Example(
{
"grid": [
[3, 8, 1, 9],
[19, 7, 2, 5],
[4, 6, 11, 10],
[12, 0, 21, 13],
],
"k": 4,
},
[[12, 0, 21, 13], [3, 8, 1, 9], [19, 7, 2, 5], [4, 6, 11, 10]],
),
Example(
{"grid": [[1, 2, 3], [4, 5, 6], [7, 8, 9]], "k": 9},
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
),
],
)
self._test_parse_problem(problem, signature)
def test_parse_problem_2(self):
problem = Problem(
url="",
name="Find Elements in a Contaminated Binary Tree",
statement="",
examples=[
(
"Input\n"
'["FindElements","find","find"]\n'
"[[[-1,null,-1]],[1],[2]]\n"
"Output\n"
"[null,false,true]\n"
"Explanation\n"
"FindElements findElements = new FindElements([-1,null,-1]); \n"
"findElements.find(1); // return False \n"
"findElements.find(2); // return True "
),
(
"Input\n"
'["FindElements","find","find","find"]\n'
"[[[-1,-1,-1,-1,-1]],[1],[3],[5]]\n"
"Output\n"
"[null,true,true,false]\n"
"Explanation\n"
"FindElements findElements = new FindElements([-1,-1,-1,-1,-1]);\n"
"findElements.find(1); // return True\n"
"findElements.find(3); // return True\n"
"findElements.find(5); // return False"
),
(
"Input\n"
'["FindElements","find","find","find","find"]\n'
"[[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]]\n"
"Output\n"
"[null,true,false,false,true]\n"
"Explanation\n"
"FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]);\n"
"findElements.find(2); // return True\n"
"findElements.find(3); // return False\n"
"findElements.find(4); // return False\n"
"findElements.find(5); // return True"
),
],
code=[
"/**",
" * Definition for a binary tree node.",
" * struct TreeNode {",
" * int val;",
" * TreeNode *left;",
" * TreeNode *right;",
" * TreeNode(int x) : val(x), left(NULL), right(NULL) {}",
" * };",
" */",
"class FindElements {",
"public:",
" FindElements(TreeNode* root) {",
" ",
" }",
" ",
" bool find(int target) {",
" ",
" }",
"};",
"",
"/**",
" * Your FindElements object will be instantiated and called as such:",
" * FindElements* obj = new FindElements(root);",
" * bool param_1 = obj->find(target);",
" */",
],
)
signature = InteractiveProblemSignature(
class_name="FindElements",
functions=[
FunctionSignature(
return_type="FindElements",
name="FindElements",
arguments=[("TreeNode*", "root")],
),
FunctionSignature(
return_type="bool", name="find", arguments=[("int", "target")]
),
],
examples=[
[
Interaction(
function="FindElements",
input={"root": [-1, None, -1]},
output=None,
),
Interaction(function="find", input={"target": 1}, output=False),
Interaction(function="find", input={"target": 2}, output=True),
],
[
Interaction(
function="FindElements",
input={"root": [-1, -1, -1, -1, -1]},
output=None,
),
Interaction(function="find", input={"target": 1}, output=True),
Interaction(function="find", input={"target": 3}, output=True),
Interaction(function="find", input={"target": 5}, output=False),
],
[
Interaction(
function="FindElements",
input={"root": [-1, None, -1, -1, None, -1]},
output=None,
),
Interaction(function="find", input={"target": 2}, output=True),
Interaction(function="find", input={"target": 3}, output=False),
Interaction(function="find", input={"target": 4}, output=False),
Interaction(function="find", input={"target": 5}, output=True),
],
],
)
self._test_parse_problem(problem, signature)
def test_parse_problem_3(self):
problem = Problem(
url="",
name="Greatest Sum Divisible by Three",
statement="",
examples=[
(
"Input: nums = [3,6,5,1,8]\n"
"Output: 18\n"
"Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3)."
),
(
"Input: nums = [4]\n"
"Output: 0\n"
"Explanation: Since 4 is not divisible by 3, do not pick any number."
),
(
"Input: nums = [1,2,3,4,4]\n"
"Output: 12\n"
"Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3)."
),
],
code=[
"class Solution {",
"public:",
" int maxSumDivThree(vector<int>& nums) {",
" ",
" }",
"};",
],
)
signature = ProblemSignature(
function=FunctionSignature(
return_type="int",
name="maxSumDivThree",
arguments=[("vector<int>&", "nums")],
),
examples=[
Example({"nums": [3, 6, 5, 1, 8]}, 18),
Example({"nums": [4]}, 0),
Example({"nums": [1, 2, 3, 4, 4]}, 12),
],
)
self._test_parse_problem(problem, signature)
def test_parse_problem_4(self):
problem = Problem(
url="",
name="Minimum Moves to Move a Box to Their Target Location",
statement="",
examples=[
(
'Input: grid = [["#","#","#","#","#","#"],\n'
' ["#","T","#","#","#","#"],\n'
' ["#",".",".","B",".","#"],\n'
' ["#",".","#","#",".","#"],\n'
' ["#",".",".",".","S","#"],\n'
' ["#","#","#","#","#","#"]]\n'
"Output: 3\n"
"Explanation: We return only the number of times the box is pushed."
),
(
'Input: grid = [["#","#","#","#","#","#"],\n'
' ["#","T","#","#","#","#"],\n'
' ["#",".",".","B",".","#"],\n'
' ["#","#","#","#",".","#"],\n'
' ["#",".",".",".","S","#"],\n'
' ["#","#","#","#","#","#"]]\n'
"Output: -1"
),
(
'Input: grid = [["#","#","#","#","#","#"],\n'
' ["#","T",".",".","#","#"],\n'
' ["#",".","#","B",".","#"],\n'
' ["#",".",".",".",".","#"],\n'
' ["#",".",".",".","S","#"],\n'
' ["#","#","#","#","#","#"]]\n'
"Output: 5\n"
"Explanation: push the box down, left, left, up and up."
),
(
'Input: grid = [["#","#","#","#","#","#","#"],\n'
' ["#","S","#",".","B","T","#"],\n'
' ["#","#","#","#","#","#","#"]]\n'
"Output: -1"
),
],
code=[
"class Solution {",
"public:",
" int minPushBox(vector<vector<char>>& grid) {",
" ",
" }",
"};",
],
)
signature = ProblemSignature(
function=FunctionSignature(
return_type="int",
name="minPushBox",
arguments=[("vector<vector<char>>&", "grid")],
),
examples=[
Example(
{
"grid": [
["#", "#", "#", "#", "#", "#"],
["#", "T", "#", "#", "#", "#"],
["#", ".", ".", "B", ".", "#"],
["#", ".", "#", "#", ".", "#"],
["#", ".", ".", ".", "S", "#"],
["#", "#", "#", "#", "#", "#"],
]
},
3,
),
Example(
{
"grid": [
["#", "#", "#", "#", "#", "#"],
["#", "T", "#", "#", "#", "#"],
["#", ".", ".", "B", ".", "#"],
["#", "#", "#", "#", ".", "#"],
["#", ".", ".", ".", "S", "#"],
["#", "#", "#", "#", "#", "#"],
]
},
-1,
),
Example(
{
"grid": [
["#", "#", "#", "#", "#", "#"],
["#", "T", ".", ".", "#", "#"],
["#", ".", "#", "B", ".", "#"],
["#", ".", ".", ".", ".", "#"],
["#", ".", ".", ".", "S", "#"],
["#", "#", "#", "#", "#", "#"],
]
},
5,
),
Example(
{
"grid": [
["#", "#", "#", "#", "#", "#", "#"],
["#", "S", "#", ".", "B", "T", "#"],
["#", "#", "#", "#", "#", "#", "#"],
]
},
-1,
),
],
)
self._test_parse_problem(problem, signature)