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)
| 292 | |
| 293 | |
| 294 | def run_test(sample, test=None, debug=False): |
| 295 | """If test(generated_code) is not None it'll try to run the code. |
| 296 | |
| 297 | otherwise it'll just return an input and output pair. |
| 298 | """ |
| 299 | # Disable functionalities that can make destructive changes to the test. |
| 300 | reliability_guard() |
| 301 | |
| 302 | if debug: |
| 303 | print(f'start = {datetime.now().time()}') |
| 304 | |
| 305 | try: |
| 306 | in_outs = json.loads(sample['input_output']) |
| 307 | except ValueError: |
| 308 | in_outs = None |
| 309 | if in_outs: |
| 310 | if in_outs.get('fn_name') is None: |
| 311 | which_type = CODE_TYPE.standard_input # Standard input |
| 312 | method_name = None |
| 313 | else: |
| 314 | which_type = CODE_TYPE.call_based # Call-based |
| 315 | method_name = in_outs['fn_name'] |
| 316 | |
| 317 | if debug: |
| 318 | print(f'loaded input_output = {datetime.now().time()}') |
| 319 | |
| 320 | if test is None: |
| 321 | return in_outs |
| 322 | elif test is not None: |
| 323 | results = [] |
| 324 | 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 |
| 325 | if debug: |
| 326 | print(f'loading test code = {datetime.now().time()}') |
| 327 | |
| 328 | if which_type == CODE_TYPE.call_based: |
| 329 | sol += test |
| 330 | if debug: |
| 331 | print(f'sol = {sol}') |
| 332 | signal.alarm(timeout) |
| 333 | try: |
| 334 | tmp_sol = RuntimeModule.from_string('tmp_sol', '', sol) |
| 335 | if 'class Solution' not in test: |
| 336 | tmp = tmp_sol |
| 337 | else: |
| 338 | tmp = tmp_sol.Solution() |
| 339 | signal.alarm(0) |
| 340 | except Exception as e: |
| 341 | signal.alarm(0) |
| 342 | if debug: |
| 343 | print(f'type 0 compilation error = {e}') |
| 344 | results.append(-2) |
| 345 | return results |
| 346 | signal.alarm(0) |
| 347 | |
| 348 | elif which_type == CODE_TYPE.standard_input: |
| 349 | # sol |
| 350 | tmp_test = test.split('\n') |
| 351 |
no test coverage detected