-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinMin.js
152 lines (135 loc) · 5.47 KB
/
linMin.js
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
const chalk = require('chalk')
const fs = require('fs')
var start = new Date().getTime();
let args = process.argv.slice(2);
let pathToFile = args[0]
let alpha = parseFloat(args[1])
let outputName = pathToFile.split("/");
outputName = outputName[outputName.length-1]
outputName = outputName.replace(".csv","");
var CSVProcessor = (function(path){
let lines = null;
let header = null;
let totalmutants = null;
let data = fs.readFileSync(path,'utf8');
// Get an array of comma separated lines`
let templines = data.split('\n')
header = templines.slice(0,1)[0].split('|').splice(2);
let linesExceptFirst = templines.slice(1,templines.length-1); //uptil the last item(exclusive) since it is empty string
let linesArr = linesExceptFirst.map(line=>line.split('|').splice(2));
lines = linesArr.filter(line=>line.indexOf('-1') === -1 && line.indexOf('-2') === -1)
for(var i = 0; i < lines.length;i++){
var last = lines[i][lines[i].length - 1]
last = last.split('\r')[0]
lines[i][lines[i].length-1] = last
}
totalmutants = lines.length;
// console.log(`${chalk.bgGreen("totalmutants = "+totalmutants)}`);
// console.log(`${chalk.bgGreen("Header = "+header)}`);
// console.log(lines);
return{
getMutationScore : function(testCaseIndices){
let killedMutants = 0;
for (let line of lines){
for (let idx of testCaseIndices){
if (line[idx] === '0'){
killedMutants += 1
break;
}
}
}
return parseFloat(((killedMutants/totalmutants)*100).toFixed(2));
},
getTotalMutants : function(){
return totalmutants;
},
getHeader : function(){
return header;
},
outputCSVHeader: function(){
if (!fs.existsSync("linMinOutputs.csv")){
var header = "DateTime,Name,MaxRR,tolerance,TotalMutants,ExecutionTime(ms),ExecutionTime,MutationScoreOriginal,MutationScoreReduced,ReducedSet,OriginalSetSize,ReducedSetSize"
fs.appendFileSync("linMinOutputs.csv",header+"\n",'utf8')
}
},
outputToCSV: function(str){
fs.appendFileSync("linMinOutputs.csv",str,'utf8')
}
};
})(pathToFile);
var linMin = (function(mutationScorer,tolerance){
var getRandomUnmarkedTestCase = function(arr,markers){
var newArr = getAllUnmarkedTestCases(arr,markers);
return newArr[Math.floor(Math.random() * newArr.length)]
}
var getAllUnmarkedTestCases = function(arr,markers){
var newArr = []
for (var i = 0 ; i < arr.length ; i++){
if (markers[i] === 0){
newArr.push(arr[i])
}
}
return newArr;
}
var linearSearch = function(list_,markers,tolerance,maxRR){
let ts = list_;
var iter = 1;
while (markers.includes(0)){
// console.log(`${chalk.bgMagenta("Iteration no. " + iter)}`)
// console.log(`${chalk.red("TS = "+ ts)} ${chalk.green("Makrers = " + markers)}`);
let testCase = getRandomUnmarkedTestCase(list_,markers);
// console.log(`${chalk.magenta("Test Case picked = "+ testCase)}`)
markers[testCase] = 1;
ts = ts.filter(e => e != testCase);
// console.log(`${chalk.magenta("Score of testset " + ts + " = " + mutationScorer.getMutationScore(ts))}`)
if (mutationScorer.getMutationScore(ts) + tolerance < maxRR){
ts.push(testCase);
// console.log("Adding it back");
}
else{
// console.log("Not adding back");
}
iter++;
}
return ts;
}
return {
start: function(){
CSVProcessor.outputCSVHeader();
let arr = []
for (let i = 0; i < CSVProcessor.getHeader().length; i++){
arr.push(i)
}
var markers = new Array(arr.length).fill(0); //test cases number and their index in arrays are same
let maxMutationScore = parseFloat(CSVProcessor.getMutationScore(arr));
console.log(`${chalk.bgMagenta("MaxRR = "+ maxMutationScore)}`)
console.log(`${chalk.bgMagenta("tolerance = "+ tolerance)}`)
console.log(`${chalk.bgMagenta("Total mutants = "+ CSVProcessor.getTotalMutants())}`)
console.log(`${chalk.bgMagenta("Test cases = " )} ${chalk.yellow("[ "+arr+" ]")}\n`)
var datetime = ""
var d = new Date();
var months = ["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"]
datetime = d.getDate()+" "+months[d.getMonth()]+";"+d.getHours()+"h:"+d.getMinutes()+"min:"+d.getSeconds()+"sec";
var str = datetime+","+outputName+","+maxMutationScore+","+tolerance+","+CSVProcessor.getTotalMutants()+",";
CSVProcessor.outputToCSV(str)
return linearSearch(arr,markers,tolerance,maxMutationScore);
}
};
})(CSVProcessor,alpha);
let reducedSet = linMin.start();
var end = new Date().getTime();
var time = end - start;
var seconds = Math.floor(time/1000);
var minutes = Math.floor(seconds/60);
let arr = [];
for (let i = 0; i < CSVProcessor.getHeader().length; i++){
arr.push(i)
}
var str = time+"ms"+","+minutes+"m "+ (seconds-minutes*60)+"s " + (time - seconds*1000)+ "ms"+","+CSVProcessor.getMutationScore(arr)+","+CSVProcessor.getMutationScore(reducedSet)+","+reducedSet.join(" ")+","+CSVProcessor.getHeader().length+","+reducedSet.length+"\n";
CSVProcessor.outputToCSV(str)
console.log(`${chalk.bgMagenta("Execution Time = "+minutes+"m "+ (seconds-minutes*60)+"s " + (time - seconds*1000)+ "ms")}`)
console.log(`${chalk.bgMagenta("Mutation Score for originalSet = " + CSVProcessor.getMutationScore(arr) + " %")}`)
console.log(`${chalk.bgMagenta("Mutation Score for reducedSet = " + CSVProcessor.getMutationScore(reducedSet) + " %")}`)
console.log(`${chalk.bgMagenta("Reduced set = ")} ${reducedSet}`)
console.log(`${chalk.bgMagenta("Reduced set Size :")} ${reducedSet.length}`)
console.log(`${chalk.bgMagenta("Original set Size :")} ${CSVProcessor.getHeader().length}`)