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, timeout=6)
| 79 | |
| 80 | |
| 81 | def run_test(sample, test=None, debug=False, timeout=6): |
| 82 | """If test(generated_code) is not None it'll try to run the code. |
| 83 | |
| 84 | otherwise it'll just return an input and output pair. |
| 85 | """ |
| 86 | # Disable functionalities that can make destructive changes to the test. |
| 87 | reliability_guard() |
| 88 | |
| 89 | if debug: |
| 90 | print(f'start = {datetime.now().time()}') |
| 91 | |
| 92 | try: |
| 93 | in_outs = json.loads(sample['input_output']) |
| 94 | except ValueError: |
| 95 | in_outs = None |
| 96 | if in_outs: |
| 97 | if in_outs.get('fn_name') is None: |
| 98 | which_type = CODE_TYPE.standard_input # Standard input |
| 99 | method_name = None |
| 100 | else: |
| 101 | which_type = CODE_TYPE.call_based # Call-based |
| 102 | method_name = in_outs['fn_name'] |
| 103 | |
| 104 | if debug: |
| 105 | print(f'loaded input_output = {datetime.now().time()}') |
| 106 | |
| 107 | if test is None: |
| 108 | assert False, 'should not happen: test code is none' |
| 109 | return in_outs, {'error': 'no test code provided'} |
| 110 | elif test is not None: |
| 111 | results = [] |
| 112 | sol = 'from string import *\nfrom re import *\nfrom datetime import *\nfrom collections import *\nfrom heapq import *\nfrom bisect import *\nfrom copy import *\nfrom math import *\nfrom random import *\nfrom statistics import *\nfrom itertools import *\nfrom functools import *\nfrom operator import *\nfrom io import *\nfrom sys import *\nfrom json import *\nfrom builtins import *\nfrom typing import *\nimport string\nimport re\nimport datetime\nimport collections\nimport heapq\nimport bisect\nimport copy\nimport math\nimport random\nimport statistics\nimport itertools\nimport functools\nimport operator\nimport io\nimport sys\nimport json\nsys.setrecursionlimit(6*10**5)\n' # noqa: E501 |
| 113 | if debug: |
| 114 | print(f'loading test code = {datetime.now().time()}') |
| 115 | |
| 116 | if which_type == CODE_TYPE.call_based: |
| 117 | |
| 118 | sol += test |
| 119 | if debug: |
| 120 | print(f'sol = {sol}') |
| 121 | signal.alarm(timeout) |
| 122 | try: |
| 123 | tmp_sol = RuntimeModule.from_string('tmp_sol', '', sol) |
| 124 | if 'class Solution' not in test: |
| 125 | tmp = tmp_sol |
| 126 | else: |
| 127 | tmp = tmp_sol.Solution() |
| 128 | signal.alarm(0) |
| 129 | except Exception as e: |
| 130 | signal.alarm(0) |
| 131 | if debug: |
| 132 | print(f'type 0 compilation error = {e}') |
| 133 | results.append(-2) |
| 134 | return results, { |
| 135 | 'error': repr(e), |
| 136 | 'error_code': -1, |
| 137 | 'error_message': 'Compilation Error', |
| 138 | } |
no test coverage detected