If test(generated_code) is not None it'll try to run the code. otherwise it'll just return an input and output pair.
(sample, test=None, debug=False)
| 342 | |
| 343 | |
| 344 | def run_test(sample, test=None, debug=False): |
| 345 | """If test(generated_code) is not None it'll try to run the code. |
| 346 | |
| 347 | otherwise it'll just return an input and output pair. |
| 348 | """ |
| 349 | # Disable functionalities that can make destructive changes to the test. |
| 350 | reliability_guard() |
| 351 | |
| 352 | if debug: |
| 353 | print(f'start = {datetime.now().time()}') |
| 354 | |
| 355 | try: |
| 356 | in_outs = json.loads(sample['input_output']) |
| 357 | except ValueError: |
| 358 | in_outs = None |
| 359 | if in_outs: |
| 360 | if in_outs.get('fn_name') is None: |
| 361 | which_type = CODE_TYPE.standard_input # Standard input |
| 362 | method_name = None |
| 363 | else: |
| 364 | which_type = CODE_TYPE.call_based # Call-based |
| 365 | method_name = in_outs['fn_name'] |
| 366 | |
| 367 | if debug: |
| 368 | print(f'loaded input_output = {datetime.now().time()}') |
| 369 | |
| 370 | if test is None: |
| 371 | return in_outs |
| 372 | elif test is not None: |
| 373 | results = [] |
| 374 | sol = 'import sys\nimport time\nimport itertools\nfrom itertools import accumulate, product, permutations, combinations\nimport collections\nfrom collections import Counter, OrderedDict, deque, defaultdict, ChainMap\nfrom functools import lru_cache\nimport math\nfrom math import sqrt, sin, cos, tan, ceil, fabs, floor, gcd, exp, log, log2\nimport fractions\nfrom typing import List, Tuple\nimport numpy as np\nimport random\nimport heapq\nfrom heapq import *\n' # noqa: E501 |
| 375 | if debug: |
| 376 | print(f'loading test code = {datetime.now().time()}') |
| 377 | |
| 378 | if which_type == CODE_TYPE.call_based: |
| 379 | sol += test |
| 380 | if debug: |
| 381 | print(f'sol = {sol}') |
| 382 | signal.alarm(timeout) |
| 383 | try: |
| 384 | tmp_sol = RuntimeModule.from_string('tmp_sol', '', sol) |
| 385 | if 'class Solution' not in test: |
| 386 | tmp = tmp_sol |
| 387 | else: |
| 388 | tmp = tmp_sol.Solution() |
| 389 | signal.alarm(0) |
| 390 | except Exception as e: |
| 391 | signal.alarm(0) |
| 392 | if debug: |
| 393 | print(f'type 0 compilation error = {e}') |
| 394 | results.append(-2) |
| 395 | return results |
| 396 | signal.alarm(0) |
| 397 | |
| 398 | elif which_type == CODE_TYPE.standard_input: |
| 399 | # sol |
| 400 | tmp_test = test.split('\n') |
| 401 |
no test coverage detected