-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_num.py
249 lines (229 loc) · 9.41 KB
/
count_num.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import subprocess
import random
from tqdm import tqdm
import numpy as np
import math
import json
import os
import glob
import re
from nltk.translate.bleu_score import corpus_bleu
def remove_double_slash_comments(text):
return re.sub(r'//.*', '', text)
def get_mutant_all_info(project,num,way):
com_n = 0
bleu_n = 0
ast_n = 0
for i in range(num):
mutant_list = []
mutant_path = '%s/mutant/%s/%s-%s.json' % (way,project,project,i+1)
print("****************************",mutant_path)
with open(mutant_path, 'r') as f1:
mutants = json.load(f1)
com_path = '%s/mutant_tested/%s/%s_%s_test.json' % (way,project,project,i+1)
with open(com_path, 'r') as f:
com_mutants = json.load(f)
for mutant in mutants:
Is_compilable = False
dict={}
dict["id"]=mutant['id']
dict["line"]=mutant["line"]
dict["filepath"]=mutant["filepath"]
dict["precode"]=mutant["precode"]
dict["aftercode"]=mutant["aftercode"]
for com in com_mutants:
if mutant['id'] == com['mutant_id:']:
try:
com_n+=1
dict['compilable'] = True
dict['fail_test_number:'] = com['fail_test_number:']
dict['fail_test:'] = com['fail_test:']
dict['bleu'] = 0.5
dict['ast distance'] = 0.5
Is_compilable = True
except Exception as e:
continue
if Is_compilable == False:
dict['compilable'] = False
mutant_list.append(dict)
print(len(mutant_list),com_n,bleu_n,ast_n)
savepath='%s/mutant_all_info/%s/%s_%s_test.json' % (way,project,project,i+1)
with open(savepath, 'w', encoding='utf-8') as file1:
json.dump(mutant_list, file1, ensure_ascii=False,indent=4)
def sample(sum,sample_num,filepath):
sample_list = []
folder_path = filepath
mutant_files = glob.glob(os.path.join(folder_path, '**/*.json'), recursive=True)
for mutant_file in mutant_files:
with open(mutant_file, "r", encoding="utf-8") as f:
mutants = json.load(f)
m = round(len(mutants) * (sample_num / sum))
m_list = random.sample(mutants,m)
sample_list.extend(m_list)
return(sample_list)
def findFault(filter_list, d4j_path):
kill_tests = []
bleu_list = []
ast_distance = []
for mutant in filter_list:
try:
if mutant['fail_test_number:']>0:
test=mutant['fail_test:'].split('\n')[1:]
for t in test:
if len(t)>0:
if t.split(' ')[-1] not in kill_tests:
kill_tests.append(t.split(' ')[-1])
except Exception as e:
continue
find = 0
pros = ['Chart','Lang','Time','Mockito','Math','Closure','Cli','Codec','Csv','Gson','JacksonCore','Jsoup']
pro_nums = [26,65,27,38,106,133,40,18,16,18,26,93]
for pro,nums in zip(pros, pro_nums):
for i in range(nums):
folder_path = '%s/framework/projects/%s/trigger_tests/%s' % (d4j_path,pro,i+1)
with open(folder_path, "r", encoding="utf-8") as f:
data = f.readlines()
trigger_tests=[]
for j in data:
if "---" in j:
trigger_tests.append(j.replace('---','').strip())
for te in trigger_tests:
if te in kill_tests:
find+=1
break
return find,np.mean(bleu_list),np.mean(ast_distance)
def filter(sample_list):
com = 0
filter_list = []
good=0
notk=0
for mutant in sample_list:
try:
if mutant['compilable']:
com += 1
file_path=mutant['filepath']
with open(file_path, "r", encoding="utf-8") as f:
mutantfile = f.readlines()
if mutant['precode'] in mutantfile[mutant['line']-1] and mutant['precode'] != mutant['aftercode']and mutant['precode'] != "\n":
if len(mutant['precode'].strip()) > 0:
if mutant['precode'].strip()[0].isalpha():
filter_list.append(mutant)
good += 1
if mutant['fail_test_number:'] == 0:
notk += 1
else:
if len(mutant['aftercode'].strip()) > 0:
if mutant['aftercode'].strip()[0] != '/':
filter_list.append(mutant)
good+=1
if mutant['fail_test_number:'] == 0:
notk += 1
else:
filter_list.append(mutant)
good += 1
if mutant['fail_test_number:'] == 0:
notk += 1
except Exception as e:
continue
score = (good - notk) / good
return filter_list,com,score
def get_coupling_rate(filter_list,d4j_path):
trigger_tests = []
coupling_mutant = 0
pros = ['Chart','Lang','Time','Mockito','Math','Closure','Cli','Codec','Csv','Gson','JacksonCore','Jsoup']
pro_nums = [26,65,27,38,106,133,40,18,16,18,26,93]
for pro,nums in zip(pros,pro_nums):
for i in range(nums):
folder_path = '%s/framework/projects/%s/trigger_tests/%s' % (d4j_path, pro, i+1)
with open(folder_path, "r", encoding="utf-8") as f:
data = f.readlines()
for j in data:
if "---" in j:
trigger_tests.append(j.replace('---','').strip())
for mutant in filter_list:
for tri_test in trigger_tests:
if tri_test in mutant["fail_test:"]:
coupling_mutant += 1
break
coupling_rate = coupling_mutant / len(filter_list)
return coupling_rate
def get_ochiai(filter_list, d4j_path):
coupling_mutant = 0
pros = ['Chart','Lang','Time','Mockito','Math','Closure','Cli','Codec','Csv','Gson','JacksonCore','Jsoup']
pro_nums = [26,65,27,38,106,133,40,18,16,18,26,93]
fault_similarity_num = 0
for pro,nums in zip(pros,pro_nums):
for i in range(nums):
is_fault_similarity = False
folder_path = '%s/framework/projects/%s/trigger_tests/%s' % (d4j_path, pro, i+1)
with open(folder_path, "r", encoding="utf-8") as f:
data = f.readlines()
trigger_tests = []
for j in data:
if "---" in j:
trigger_tests.append(j.replace('---','').strip())
for mutant in filter_list:
fenzi = 0
for tri_test in trigger_tests:
if tri_test in mutant['fail_test:']:
fenzi += 1
mutant_fail_number = int(mutant['fail_test_number:'])
if mutant_fail_number > 0:
Ochiai = fenzi/math.sqrt(len(trigger_tests)*mutant_fail_number)
else:
Ochiai = 0
if Ochiai > 0.8:
is_fault_similarity = True
if is_fault_similarity:
fault_similarity_num += 1
return fault_similarity_num
def get_result(way, all_mutant, sample_num, d4j_path):
get_mutant_all_info('Chart',26,way)
get_mutant_all_info('Lang',65,way)
get_mutant_all_info('Time',27,way)
get_mutant_all_info('Mockito',38,way)
get_mutant_all_info('Math',106,way)
get_mutant_all_info('Closure',133,way)
get_mutant_all_info('Cli',40,way)
get_mutant_all_info('Codec',18,way)
get_mutant_all_info('Csv',16,way)
get_mutant_all_info('Gson',18,way)
get_mutant_all_info('JacksonCore',26,way)
get_mutant_all_info('Jsoup',93,way)
filepath = f'{way}/mutant_all_info'
sum_list=[]
com_list=[]
score_list=[]
fault_list=[]
ast_list=[]
coupling_list=[]
ochiai_list=[]
filter_num=[]
for i in tqdm(range(10)):
sample_list = sample(all_mutant, sample_num, filepath)
filter_list, com, score = filter(sample_list)
find_fault = findFault(filter_list, d4j_path)
coupling_rate = get_coupling_rate(filter_list, d4j_path)
ochiai = get_ochiai(filter_list, d4j_path)
sum_list.append(len(sample_list))
com_list.append(com)
filter_num.append(len(filter_list))
score_list.append(score)
fault_list.append(find_fault)
coupling_list.append(coupling_rate)
ochiai_list.append(ochiai)
####################################
print('com:',np.mean(com_list)/np.mean(sum_list))
useless = (np.mean(com_list) - np.mean(filter_num)) / np.mean(sum_list)
print('useless:',useless)
print('bug det:',np.mean(fault_list) / 605)
print('coup:',np.mean(coupling_list))
print('ochiai:',np.mean(ochiai_list) / 605)
if __name__ == '__main__':
way = 'Deepseek'
d4j_path = 'your own path'
get_result(way, 33511, 11397, d4j_path)
# ways = ['Deepseek', 'Codellama', 'Gpt/gpt3.5', 'Gpt/gpt4o', 'Gpt/gpt4omini', 'Leam', 'Major', 'Starchat']
# numbers = [33511, 26053, 30427, 30739, 33197, 34540, 19841, 26852]
# for way, number in zip(ways, numbers):
# get_result(way, number, 11397, d4j_path)