def setup(): program = Program('./resource/Triangle_bug', GranularityLevel.LINE) assert len(program.target_files) == 1 assert program.target_files[0] == 'Triangle.java' patch = Patch(program) return patch, program
def test_create(self): program = Program('./resource/Triangle_bug', GranularityLevel.LINE) random_line_insertion = LineInsertion.create( program, line_file='Triangle.java', ingr_file='Triangle.java') assert isinstance(random_line_insertion, LineInsertion)
def test_create(self): program = Program('./resource/Triangle_bug', GranularityLevel.LINE) random_line_deletion_0 = LineReplacement.create( program, line_file='Triangle.java', ingr_file='Triangle.java', del_rate=0) assert isinstance(random_line_deletion_0, LineReplacement) assert random_line_deletion_0.ingredient is not None random_line_deletion_1 = LineReplacement.create( program, line_file='Triangle.java', ingr_file='Triangle.java', del_rate=1) assert isinstance(random_line_deletion_1, LineReplacement) assert random_line_deletion_1.ingredient is None
if __name__ == "__main__": parser = argparse.ArgumentParser(description='PYGGI Improvment Example') parser.add_argument('project_path', type=str, default='../sample/Triangle_fast') parser.add_argument('--epoch', type=int, default=30, help='total epoch(default: 30)') parser.add_argument('--iter', type=int, default=100, help='total iterations per epoch(default: 100)') args = parser.parse_args() program = Program(args.project_path, GranularityLevel.LINE) class MyLocalSearch(LocalSearch): def get_neighbour(self, patch): if len(patch) > 0 and random.random() < 0.5: patch.remove(random.randrange(0, len(patch))) else: edit_operator = random.choice( [LineDeletion, LineInsertion, LineReplacement]) patch.add(edit_operator.create(program)) return patch def get_fitness(self, patch): return float(patch.test_result.custom['runtime']) def is_valid_patch(self, patch):
def test_create(self): program = Program('./resource/Triangle_bug', GranularityLevel.LINE) random_line_moving = LineMoving.create(program) assert random_line_moving.x is not None assert random_line_moving.y is not None
def test_create(self): program = Program('./resource/Triangle_bug', GranularityLevel.LINE) random_line_deletion = LineDeletion.create(program) assert random_line_deletion.x is not None
def test_parse(self, setup): program = setup contents = Program.parse(program.granularity_level, program.path, program.target_files) assert 'Triangle.java' in contents assert len(contents['Triangle.java']) > 0
def test_clean_tmp_dir(self, setup): program = setup os.mkdir(os.path.join(program.tmp_path, 'test_dir')) Program.clean_tmp_dir(program.tmp_path) assert not os.listdir(program.tmp_path)
def test_eq(self, setup): patch, program = setup program2 = Program('./resource/Triangle_bug', GranularityLevel.LINE) patch2 = Patch(program2) assert patch == patch2
""" Improving non-functional properties :: python improve_python.py ../sample/Triangle_fast_python """ import sys import random import argparse from pyggi import Program, Patch, GranularityLevel, TestResult from pyggi.algorithms import LocalSearch from pyggi.atomic_operator import LineReplacement, LineInsertion from pyggi.custom_operator import LineDeletion if __name__ == "__main__": parser = argparse.ArgumentParser(description='PYGGI Improvment Example') parser.add_argument('project_path', type=str, default='../sample/Triangle_fast_python') parser.add_argument('--epoch', type=int, default=30, help='total epoch(default: 30)') parser.add_argument('--iter', type=int, default=100, help='total iterations per epoch(default: 100)') args = parser.parse_args() program = Program(args.project_path, GranularityLevel.AST) program.print_modification_points('triangle.py')
def result_parser(stdout, stderr): import re m = re.findall("runtime: ([0-9.]+)", stdout) if len(m) > 0: runtime = m[0] failed = re.findall("([0-9]+) failed", stdout) pass_all = len(failed) == 0 print(runtime) print(failed) print(pass_all) exit() return TestResult(True, {'runtime': runtime, 'pass_all': pass_all}) else: return TestResult(False, None) triangle = Program( "sample/nonogram", granularity_level=GranularityLevel.LINE) # triangle.print_modification_points('triangle.py') # See sample/Triangle_bug_python/get_spectrum.py weights = [0] * len(triangle.modification_points['nonogram.py']) #weights[14] = 1 #weights[15] = 1 #weights[16] = 1 triangle.set_modification_weights('nonogram.py', weights) valid_edit_operators = [LineDeletion, LineMoving, LineReplacement] tabu = [] patch = Patch(triangle) for i in range(1000):
# Custom parser for the results of pytest def result_parser(stdout, stderr): import re m = re.findall("runtime: ([0-9.]+)", stdout) if len(m) > 0: runtime = m[0] failed = re.findall("([0-9]+) failed", stdout) pass_all = len(failed) == 0 return TestResult(True, {'runtime': runtime, 'pass_all': pass_all}) else: return TestResult(False, None) # Create new Program instance for 'sample/Triangle_fast_python' triangle = Program("sample/Triangle_fast_python", granularity_level=GranularityLevel.AST) triangle.print_modification_points('triangle.py') # Set modification weights weights = [0.01] * len(triangle.modification_points['triangle.py']) weights[7] = 1.0 # delay() triangle.set_modification_weights('triangle.py', weights) # Create new Patch patch = Patch(triangle) valid_edit_operators = [StmtDeletion, StmtMoving, StmtReplacement] edit_operator = random.choice(valid_edit_operators) patch.add(edit_operator.create(triangle, method='weighted')) # Print the patch's info, test results, and line differences made by the patch print(patch)
if __name__ == "__main__": parser = argparse.ArgumentParser(description='PYGGI Bug Repair Example') parser.add_argument('project_path', type=str, default='../sample/Triangle_bug_python') parser.add_argument('--epoch', type=int, default=30, help='total epoch(default: 30)') parser.add_argument('--iter', type=int, default=10000, help='total iterations per epoch(default: 10000)') args = parser.parse_args() program = Program(args.project_path, GranularityLevel.LINE) program.set_modification_weights('triangle.py', [1] * 35) class MyTabuSearch(LocalSearch): def get_neighbour(self, patch): while True: temp_patch = patch.clone() if len(temp_patch) > 0 and random.random() < 0.5: temp_patch.remove(random.randrange(0, len(temp_patch))) else: edit_operator = random.choice( [LineDeletion, LineInsertion, LineReplacement]) temp_patch.add( edit_operator.create(program, method="weighted")) if not any(item == temp_patch for item in self.tabu): self.tabu.append(temp_patch)