-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfault_loc.py
313 lines (286 loc) · 10.9 KB
/
fault_loc.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import sys
import os
import getopt
import numpy as np
techniques = ["dstar2", "dstar3", "jaccard", "ochiai", "tarantula", "zoltar"]
total_passed = 0 # total passed test cases
total_failed = 0 # total failed test cases
total = 0
passed_statements = [] # executions by passing test cases
failed_statements = [] # executions by failing test cases
verboseprint = None
def main(argv):
matrix = ''
spectra = ''
technique = ''
number = None
max_rank = None
dest_path = None
verbose = False
# Argument parsing
try:
opts, args = getopt.getopt(argv, "hm:s:t:vn:r:w:",["help", "matrix=", "spectra=", "technique=", "verbose"])
except getopt.GetoptError:
usage()
sys.exit()
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-m", "--matrix"):
matrix = arg
elif opt in ("-s", "--spectra"):
spectra = arg
elif opt in ("-t", "--technique"):
technique = arg
elif opt == "-n":
number = arg
elif opt == "-r":
max_rank = arg
elif opt == "-w":
dest_path = arg
elif opt in ("-v", "--verbose"):
verbose = True
# Verify input
global verboseprint
verboseprint = print if verbose else lambda *a, **k: None
verboseprint("[STATUS] Verifying your input ...")
verify_input(matrix, spectra, technique, number, max_rank, dest_path)
if number: number = int(number)
if max_rank: max_rank = int(max_rank)
verboseprint("[STATUS] Input verified.")
# Get matrix and spectra data
verboseprint("[STATUS] Loading matrix file ...")
verboseprint("[STATUS] Matrix file successfully loaded. Analyzing ...")
analyze_matrix(matrix)
verboseprint("[STATUS] Analyzing finished! Extracted test cases:")
verboseprint("[INFO] Total passed: {:d}".format(total_passed))
verboseprint("[INFO] Total failed: {:d}".format(total_failed))
verboseprint("[STATUS] Loading spectra file ...")
spectra_lines = load_spectra(spectra)
verboseprint("[STATUS] Spectra file successfully loaded.")
# Call design metric
verboseprint("[STATUS] Scoring test cases with technique " + technique)
scores = call_design_metric(technique)
verboseprint("[STATUS] Scores for test cases successfully generated!")
# Rank items
verboseprint("[STATUS] Ranking test cases ...")
ranks = rank(scores)
verboseprint("[STATUS] Test cases ranked!")
sorted_rank_indices = np.argsort(ranks)
verboseprint("[STATUS] Generating output ...")
for i, line in enumerate(spectra_lines):
spectra_lines[i] = line.split("#")[0] + "," + line.split("#")[1] + "," + str(ranks[i]) + ",{:.4f}".format(scores[i])
np_spectra_lines = np.array(spectra_lines)
sorted_lines = np_spectra_lines[sorted_rank_indices]
header = "File,Line,Rank,Suspiciousness"
output = sorted_lines
if number:
output = sorted_lines[:number]
if max_rank:
i, = np.where(np.sort(ranks) == max_rank)
if len(i) == 0:
output = sorted_lines
else:
output = sorted_lines[:(i[-1]+1)]
verboseprint("[STATUS] Output generated!")
if dest_path:
verboseprint("[STATUS] Writing output to file " + dest_path)
write_output(dest_path, header, output)
verboseprint("[STATUS] Output written!")
else:
verboseprint("[INFO] Printing results ...")
print(output)
verboseprint("[STATUS] Success! Exiting ...")
def analyze_matrix(matrix):
global passed_statements, failed_statements, total_failed, total_passed, scores, total
data = []
try:
with open(matrix, 'r') as csvfile:
for line in csvfile:
row = line.split()
if passed_statements == []:
passed_statements = np.zeros(len(row)-1)
failed_statements = np.zeros(len(row)-1)
if row[-1] == '+':
total_passed += 1
visited_statements = np.array(row[:-1]).astype(int)
passed_statements = np.add(passed_statements, visited_statements)
elif row[-1] == '-':
total_failed += 1
visited_statements = np.array(row[:-1]).astype(int)
failed_statements = np.add(failed_statements, visited_statements)
total = total_failed + total_passed
except Exception as ex:
verboseprint("[ERROR] Exception during matrix file parsing.")
print("Failed. Aborting ...")
sys.exit()
def verify_input(matrix, spectra, technique, number, max_rank, dest_path):
if(matrix == '' or spectra == '' or technique == ''):
usage()
sys.exit()
if not os.path.isfile(matrix):
verboseprint("[ERROR] Path of matrix is invalid.")
print("Failed. Aborting ...")
sys.exit()
if not os.path.isfile(spectra):
verboseprint("[ERROR] Path of spectra is invalid.")
print("Failed. Aborting ...")
sys.exit()
if not technique in techniques:
verboseprint("[ERROR] {:s} is not a valid technique. Possible arguments:".format(technique))
verboseprint(str(techniques))
print("Failed. Aborting ...")
sys.exit()
if number and max_rank:
verboseprint("[ERROR] Do not specify rank and number of outputs together.")
print("Failed. Aborting ...")
sys.exit()
if number:
try: number = int(number)
except ValueError:
verboseprint("[ERROR] Invalid number of test cases given.")
print("Failed. Aborting ...")
sys.exit()
if number <= 0:
verboseprint("[ERROR] Number of test cases has to be positive.")
print("Failed. Aborting ...")
sys.exit()
if max_rank:
try: max_rank = int(max_rank)
except ValueError:
verboseprint("[ERROR] Invalid rank given.")
print("Failed. Aborting ...")
sys.exit()
if max_rank <= 0:
verboseprint("[ERROR] Rank has to be positive.")
print("Failed. Aborting ...")
sys.exit()
if dest_path and not os.path.exists(dest_path):
try:
with open(dest_path, 'x') as tempfile:
pass
except OSError:
verboseprint("[ERROR] Destination path invalid.")
print("Failed. Aborting ...")
sys.exit()
if os.path.isdir(dest_path):
verboseprint("[ERROR] Destination is a directory.")
print("Failed. Aborting ...")
sys.exit()
def load_spectra(fname):
data = []
with open(fname, 'r') as text_file:
try:
for line in text_file.readlines():
data.append(line.replace('\n',''))
except Exception as e:
verboseprint("[ERROR] Exception during spectra parsing.")
print("Failed. Aborting ...")
sys.exit()
return data
def write_output(fname, header, output):
with open(fname, 'w+') as text_file:
try:
text_file.write(header + '\n')
for line in output:
text_file.write(line + '\n')
except Exception:
verboseprint("[ERROR] Exception during writing output to {:s}".format(fname))
print("Failed. Aborting ...")
sys.exit()
def call_design_metric(technique):
if technique == "dstar2":
scores = dstar2()
elif technique == "dstar3":
scores = dstar3()
elif technique == "jaccard":
scores = jaccard()
elif technique == "ochiai":
scores = ochiai()
elif technique == "tarantula":
scores = tarantula()
elif technique == "zoltar":
scores = zoltar()
else:
print("[ERROR] Technique not implemented yet.")
print("Failed. Aborting ...")
sys.exit()
return scores
def dstar2():
result = np.zeros(len(passed_statements))
for i, _ in enumerate(result):
exec_pass = passed_statements[i]
exec_fail = failed_statements[i]
noex_fail = total_failed - exec_fail
result[i] = exec_fail**2 / (noex_fail + exec_pass)
return result
def dstar3():
result = np.zeros(len(passed_statements))
for i, _ in enumerate(result):
exec_pass = passed_statements[i]
exec_fail = failed_statements[i]
noex_fail = total_failed - exec_fail
result[i] = exec_fail**3 / (noex_fail + exec_pass)
return result
def jaccard():
result = np.zeros(len(passed_statements))
for i, _ in enumerate(result):
exec_pass = passed_statements[i]
exec_fail = failed_statements[i]
result[i] = exec_fail / (total_failed + exec_pass)
return result
def ochiai():
result = np.zeros(len(passed_statements))
for i, _ in enumerate(result):
exec_pass = passed_statements[i]
exec_fail = failed_statements[i]
result[i] = exec_fail / np.sqrt(total_failed * (exec_fail + exec_pass))
return result
def tarantula():
result = np.zeros(len(passed_statements))
for i, _ in enumerate(result):
exec_pass = passed_statements[i]
exec_fail = failed_statements[i]
ratio_failing = (exec_fail/total_failed)
ratio_passing = (exec_pass/total_passed)
result[i] = ratio_failing / (ratio_failing + ratio_passing)
return result
def zoltar():
result = np.zeros(len(passed_statements))
for i, _ in enumerate(result):
exec_pass = passed_statements[i]
exec_fail = failed_statements[i]
noex_pass = total_passed - exec_pass
noex_fail = total_failed - exec_fail
result[i] = exec_fail / (total_failed + exec_pass + 1000 * (noex_fail * exec_pass / exec_fail))
return result
def usage():
print("\nPython command tool to evaluate Gzoltar outputs.\n")
print("fault_loc.py -m <matrix file> -s <spectra file> -t <technique>")
print("Techniques: " + ", ".join(x for x in techniques) + "\n")
print("Parameters:")
print("-m : specify matrix file (--matrix=)")
print("-s : specify spectra file (--spectra=)")
print("-t : specify technique for evaluation (--technique=)")
print("-w : specify output file")
print("-n : specify number of objects to output")
print("-r : specify number of ranks to output")
print("-v : verbose output (--verbose)")
print("-h : print this help")
def rank(v):
if v is None or len(v) == 0:
return []
desc_indices = np.flipud(np.argsort(v))
# Sort NaN values to the end
desc_indices = np.roll(desc_indices, -np.count_nonzero(np.isnan(v)))
result = np.empty(len(v),int)
result[desc_indices[0]] = 1
for i in range(1, len(result)):
if v[desc_indices[i]] == v[desc_indices[i-1]] or (np.isnan(v[desc_indices[i]]) and np.isnan(v[desc_indices[i-1]])):
result[desc_indices[i]] = result[desc_indices[i-1]]
else:
result[desc_indices[i]] = result[desc_indices[i-1]] + 1
return result
if __name__ == "__main__":
main(sys.argv[1:])