def run_case(self, testcase: DataDrivenTestCase) -> None: with use_custom_builtins( os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase): # Build the program. text = '\n'.join(testcase.input) options = Options() options.use_builtins_fixtures = True options.show_traceback = True options.strict_optional = True source = build.BuildSource('prog.py', 'prog', text) try: ctext = emitmodule.compile_module_to_c( sources=[source], module_name='prog', options=options, alt_lib_path=test_temp_dir) out = ctext.splitlines() except CompileError as e: out = e.messages # Verify output. assert_string_arrays_equal_wildcards( testcase.output, out, 'Invalid output ({}, line {})'.format(testcase.file, testcase.line))
def test_transform(testcase): """Perform a runtime checking transformation test case.""" expected = remove_comment_lines(testcase.output) func_names = get_func_names(expected) try: # Construct input as a single single. src = '\n'.join(testcase.input) # Parse and type check the input program. result = build.build(program_path='main', target=build.ICODE, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=test_temp_dir) a = [] for fn in func_names: a.append('def {}:'.format(fn)) try: funccode = result.icode[fn] except KeyError: raise RuntimeError('no icode for %s (%s)' % (fn, list(result.icode.keys()))) code = icode.render(funccode) a.extend(code) except CompileError as e: a = e.messages assert_string_arrays_equal_wildcards( expected, a, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))
def test_transform(testcase): """Perform a runtime checking transformation test case.""" expected = remove_comment_lines(testcase.output) func_names = get_func_names(expected) try: # Construct input as a single single. src = '\n'.join(testcase.input) # Parse and type check the input program. result = build.build(program_path='main', target=build.ICODE, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=test_temp_dir) a = [] for fn in func_names: a.append('def {}:'.format(fn)) try: funccode = result.icode[fn] except KeyError: raise RuntimeError('no icode for %s (%s)' % ( fn, list(result.icode.keys()))) code = icode.render(funccode) a.extend(code) except CompileError as e: a = e.messages assert_string_arrays_equal_wildcards( expected, a, 'Invalid source code output ({}, line {})'.format(testcase.file, testcase.line))
def test_cgen_compile(testcase): # Build the program. text = '\n'.join(testcase.input) try: build.build('_program.py', target=build.C, program_text=text, alt_lib_path='lib', flags=[build.COMPILE_ONLY, build.TEST_BUILTINS]) outfile = '_program.c' f = open(outfile) out = [s.rstrip('\n\r') for s in f.readlines()] f.close() os.remove(outfile) except errors.CompileError as e: out = e.messages # Verify output. assert_string_arrays_equal_wildcards(testcase.output, out, 'Invalid output ({}, line {})'.format( testcase.file, testcase.line))
def test_transform(testcase): """Perform a runtime checking transformation test case.""" expected = remove_comment_lines(testcase.output) try: # Construct input as a single single. src = '\n'.join(testcase.input) # Parse and type check the input program. Perform transform manually # so that we can skip some files. result = build.build(program_path='main', target=build.TRANSFORM, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=test_temp_dir) a = [] first = True # Transform each file separately. for fnam in sorted(result.files.keys()): f = result.files[fnam] # Skip the builtins module and files with '_skip.' in the path. if (not f.path.endswith('/builtins.py') and not f.path.endswith('/typing.py') and not f.path.endswith('/abc.py') and '_skip.' not in f.path): if not first: # Display path for files other than the first. a.append('{}:'.format(remove_prefix(f.path, test_temp_dir))) # Pretty print the transformed tree. v2 = PrettyPrintVisitor() f.accept(v2) s = v2.output() if s != '': a += s.split('\n') first = False except CompileError as e: a = e.messages assert_string_arrays_equal_wildcards( expected, a, 'Invalid source code output ({}, line {})'.format(testcase.file, testcase.line))
def test_transform(testcase): """Perform a runtime checking transformation test case.""" expected = remove_comment_lines(testcase.output) try: # Construct input as a single single. src = '\n'.join(testcase.input) # Parse and type check the input program. Perform transform manually # so that we can skip some files. result = build.build(program_path='main', target=build.TRANSFORM, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=test_temp_dir) a = [] first = True # Transform each file separately. for fnam in sorted(result.files.keys()): f = result.files[fnam] # Skip the builtins module and files with '_skip.' in the path. if (not f.path.endswith('/builtins.py') and not f.path.endswith('/typing.py') and not f.path.endswith('/abc.py') and '_skip.' not in f.path): if not first: # Display path for files other than the first. a.append('{}:'.format(remove_prefix(f.path, test_temp_dir))) # Pretty print the transformed tree. v2 = PrettyPrintVisitor() f.accept(v2) s = v2.output() if s != '': a += s.split('\n') first = False except CompileError as e: a = e.messages assert_string_arrays_equal_wildcards( expected, a, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))
def run_case(self, testcase: DataDrivenTestCase) -> None: """Perform a reference count opcode insertion transform test case.""" with use_custom_builtins( os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase): try: ir = build_ir_for_single_file(testcase.input) except CompileError as e: actual = e.messages else: assert len( ir ) == 1, "Only 1 function definition expected per test case" fn = ir[0] insert_ref_count_opcodes(fn) actual = format_func(fn) actual = actual[actual.index('L0:'):] expected_output = testcase.output assert_string_arrays_equal_wildcards( expected_output, actual, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))
def run_case(self, testcase: DataDrivenTestCase) -> None: with use_custom_builtins( os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase): text = '\n'.join(testcase.input) options = Options() options.use_builtins_fixtures = True options.show_traceback = True options.strict_optional = True source = build.BuildSource('native.py', 'native', text) try: ctext = emitmodule.compile_module_to_c( sources=[source], module_name='native', options=options, alt_lib_path=test_temp_dir) except CompileError as e: for line in e.messages: print(line) assert False, 'Compile error' cpath = os.path.join(test_temp_dir, 'native.c') with open(cpath, 'w') as f: f.write(ctext) try: native_lib_path = buildc.build_c_extension(cpath) except buildc.BuildError as err: heading('Generated C') with open(cpath) as f: print(f.read().rstrip()) heading('End C') heading('Build output') print(err.output.decode('utf8').rstrip('\n')) heading('End output') raise driver_path = os.path.join(test_temp_dir, 'driver.py') env = os.environ.copy() env['PYTHONPATH'] = os.path.dirname(native_lib_path) proc = subprocess.Popen(['python', driver_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) output, _ = proc.communicate() output = output.decode('utf8') outlines = output.splitlines() heading('Generated C') with open(cpath) as f: print(f.read().rstrip()) heading('End C') if proc.returncode != 0: print() print('*** Exit status: %d' % proc.returncode) # Verify output. assert_string_arrays_equal_wildcards( testcase.output, outlines, 'Invalid output ({}, line {})'.format(testcase.file, testcase.line)) assert proc.returncode == 0
def run_case(self, testcase: DataDrivenTestCase) -> None: """Perform a data-flow analysis test case.""" with use_custom_builtins( os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase): expected_output = testcase.output program_text = '\n'.join(testcase.input) options = Options() options.use_builtins_fixtures = True options.show_traceback = True source = build.BuildSource('main', '__main__', program_text) try: # Construct input as a single single. # Parse and type check the input program. result = build.build(sources=[source], options=options, alt_lib_path=test_temp_dir) except CompileError as e: actual = e.messages else: if result.errors: actual = result.errors else: module = genops.build_ir(result.files['__main__'], result.types) assert len( module.functions ) == 1, "Only 1 function definition expected per test case" fn = module.functions[0] actual = format_func(fn) actual = actual[actual.index('L0:'):] cfg = analysis.get_cfg(fn.blocks) args = set([Register(i) for i in range(len(fn.args))]) name = testcase.name if name.endswith('_MaybeDefined'): # Forward, maybe analysis_result = analysis.analyze_maybe_defined_regs( fn.blocks, cfg, args) elif name.endswith('_Liveness'): # Backward, maybe analysis_result = analysis.analyze_live_regs( fn.blocks, cfg) elif name.endswith('_MustDefined'): # Forward, must analysis_result = analysis.analyze_must_defined_regs( fn.blocks, cfg, args, num_regs=fn.env.num_regs()) elif name.endswith('_BorrowedArgument'): # Forward, must analysis_result = analysis.analyze_borrowed_arguments( fn.blocks, cfg, args) else: assert False, 'No recognized _AnalysisName suffix in test case' actual.append('') for key in sorted(analysis_result.before.keys()): pre = ', '.join(fn.env.names[reg] for reg in analysis_result.before[key]) post = ', '.join(fn.env.names[reg] for reg in analysis_result.after[key]) actual.append('%-8s %-23s %s' % (key, '{%s}' % pre, '{%s}' % post)) assert_string_arrays_equal_wildcards( expected_output, actual, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))