-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.java
More file actions
176 lines (141 loc) · 6.59 KB
/
Counter.java
File metadata and controls
176 lines (141 loc) · 6.59 KB
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
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidParameterException;
import java.util.*;
import java.util.stream.Collectors;
/**
* A Counter maps mutants that make a larger set of tests fail (dominator mutants) to the set
* of mutants that fail a subset of the same tests (subsumed mutants)
* and exports the outcome into a CSV file
*
* @author <ardier@cs.washington.edu>Ardi Madadi</ardier@cs.washington.edu>
* @version 0.16
* @since 0.01
*/
public class Counter {
//Let's just count some mutants
public static void main(String[] args) throws IOException {
if (args.length != 4) {
throw new InvalidParameterException("Usage: javac Counter killMapPath project version triggerSettings");
}
//adding the mutant records from kill map
var killMapPath = args[0];
//Generating the output path based on Project Name/Version/Trigger Setting/
var dominatorFile = Paths.get("graphs", args[1] + "-" + args[2] + "-" + args[3] + ".csv");
Map<Integer, Set<Integer>> mutantToTest = readKillMap(killMapPath);
//sort mutants alphabetically
List<Integer> sortedMutantList = sortMutants(mutantToTest);
//map dominator mutants to subsumed mutants
Map<Integer, Set<Integer>> domToSub = dominatorToSubsumed(mutantToTest);
//printing the output
export(sortedMutantList, domToSub, dominatorFile);
}
/**
* Reads a `killMap.csv`-formatted file as a map from tests to mutants killed.
*
* @param path The filesystem path to the file to read.
* @return A mapping from test IDs (as integers) to the set of mutants kill (as integers).
*/
private static HashMap<Integer, Set<Integer>> readKillMap(String path) throws IOException {
HashMap<Integer, Set<Integer>> mutantToTest = new HashMap<>();
var scanner = new BufferedReader(new FileReader(path));
//parsing a CSV file into Scanner class constructor
//var sc = new Scanner((scanner));
//sc.useDelimiter(";"); //sets the delimiter pattern
var lineTracker = scanner.readLine();
while ((lineTracker = scanner.readLine()) != null) //returns a boolean value
{
String[] lineKeeper = lineTracker.split(",");
int test = Integer.parseInt(lineKeeper[0]);
int mutant = Integer.parseInt(lineKeeper[1]);
if (!mutantToTest.containsKey(mutant)) {
Set<Integer> setter = new HashSet<>();
mutantToTest.put(Integer.parseInt(lineKeeper[1]), setter);
}
mutantToTest.get(mutant).add(test);
}
scanner.close();
return mutantToTest;
}
/**
* Reads a mapping of mutants to tests failed for those mutants
*
* @param mutantToTestMethod A map from mutants to tests failed for those mutants
* @return a mapping from mutant IDs (as integers) for mutants
* that make a larger set of tests fail (dominator mutants) to the set
* of mutants (as integers) that fail a subset of the same tests (subsumed mutants)
*/
private static HashMap<Integer, Set<Integer>> dominatorToSubsumed(Map<Integer, Set<Integer>> mutantToTestMethod) {
HashMap<Integer, Set<Integer>> domToSub = new HashMap<>();
for (Integer m : mutantToTestMethod.keySet()) {
for (Integer n : mutantToTestMethod.keySet()) {
if (mutantToTestMethod.get(m).containsAll(mutantToTestMethod.get(n))) {
if (!domToSub.containsKey(m)) {
domToSub.put(m, new HashSet<>());
}
domToSub.get(m).add(n);
}
}
}
return domToSub;
}
/**
* Reads a mapping of mutants to tests failed
*
* @param mutantToTest A map from mutants to the tests failed for those mutants
* @return a list of all mutant IDs (as integers) sorted in numeric order
*/
private static List<Integer> sortMutants(Map<Integer, Set<Integer>> mutantToTest) {
//List<String> sortedMutantList = new ArrayList<>(mutantToTest.keySet());
//sortedMutantList.sort(Comparator.naturalOrder());
List<Integer> sortedMutantList = mutantToTest.keySet().stream()
.sorted()
.distinct()
.collect(Collectors.toList());
return sortedMutantList;
}
/**
* Reads a list of all mutant IDs (as integers) sorted in order
* and a mapping of all dominator mutants (as integers)
* to their subsumed mutants (as strings).
* Writes csv file
*
* @param sortedMutantListFinal a list of all mutant IDs (as integers) sorted in order
* @param dominatorToSubsumedMethod a mapping from mutant IDs (as integers) for mutants
* that make a larger set of tests fail (dominator mutants) to the set
* of mutants (as integers) that fail a subset of the same
* tests (subsumed mutants)
* @param dominatorFile a path for the generated csv file based on project name,
* version, and whether the test is triggering or non-triggering
*/
private static void export(List<Integer> sortedMutantListFinal,
Map<Integer, Set<Integer>> dominatorToSubsumedMethod,
Path dominatorFile) throws IOException {
ArrayList<String> dominatorGraph = new ArrayList<>();
var firstRow = sortedMutantListFinal.stream()
.map(i -> i.toString())
.collect(Collectors.joining(","));
dominatorGraph.add("," + firstRow + ",");
//going through all mutants
for (var counter : sortedMutantListFinal) {
var rowBuilder = new StringBuilder(counter + ",");
for (var innerCounter : sortedMutantListFinal) {
if (dominatorToSubsumedMethod.containsKey(counter)) {
if (dominatorToSubsumedMethod.get(counter).contains(innerCounter)) {
rowBuilder.append(innerCounter + ",");
} else {
rowBuilder.append(",");
}
} else {
rowBuilder.append(",");
}
}
dominatorGraph.add(rowBuilder.toString());
}
//Saving the final graph
Files.write(dominatorFile, dominatorGraph, StandardCharsets.UTF_8);
}
}