+/EE379K

programming paradigms

git clone https://git.julia.blue/+/EE379K

Joseph RyanSet up gradle project for CFGffe584c

master
3.0 KiB101 linesraw
1// package gov.nasa.jpf;
2import gov.nasa.jpf.jvm.ClassFile;
3import gov.nasa.jpf.jvm.bytecode.GETFIELD;
4import gov.nasa.jpf.jvm.bytecode.IADD;
5import gov.nasa.jpf.jvm.bytecode.ISUB;
6import gov.nasa.jpf.report.Publisher;
7import gov.nasa.jpf.report.PublisherExtension;
8import gov.nasa.jpf.search.Search;
9import gov.nasa.jpf.search.SearchListener;
10import gov.nasa.jpf.vm.ChoiceGenerator;
11import gov.nasa.jpf.vm.ClassInfo;
12import gov.nasa.jpf.vm.ElementInfo;
13import gov.nasa.jpf.vm.Instruction;
14import gov.nasa.jpf.vm.MethodInfo;
15import gov.nasa.jpf.vm.ThreadInfo;
16import gov.nasa.jpf.vm.VM;
17import gov.nasa.jpf.vm.VMListener;
18import gov.nasa.jpf.ListenerAdapter;
19import java.io.*;
20import java.util.*;
21
22class LineInfo implements Comparable<LineInfo> {
23    int num;
24    String className;
25
26    public LineInfo(String className, int num) {
27        this.className = className;
28        this.num = num;
29    }
30
31    @Override
32    public int compareTo(LineInfo other) {
33        int temp = this.className.compareTo(other.className);
34        if (temp != 0) {
35            return temp;
36        }
37        if (this.num > other.num)
38            return 1;
39        if (this.num < other.num)
40            return -1;
41        else
42            return 0;
43    }
44
45    @Override
46    public String toString() {
47        return className + ":" + num;
48    }
49
50    @Override
51    public boolean equals(Object obj) {
52        LineInfo other = (LineInfo)obj;
53        return num == other.num && className.equals(other.className);
54    }
55
56    @Override
57    public int hashCode() {
58        return className.hashCode() + num;
59    }
60}
61
62public class CodeCov extends ListenerAdapter {
63    private Set<LineInfo> covered = new HashSet();
64    private String mainName;
65
66    @Override
67    public void methodEntered(VM vm, ThreadInfo currentThread,
68                              MethodInfo enteredMethod) {
69        if (enteredMethod.getName().equals("main")) {
70            mainName = enteredMethod.getFullName();
71        }
72    }
73
74    @Override
75    public void methodExited(VM vm, ThreadInfo currentThread,
76                             MethodInfo enteredMethod) {
77        if (enteredMethod.getFullName().equals(mainName)) {
78            List<LineInfo> lineList = new ArrayList(covered);
79            Collections.sort(lineList);
80            try {
81                FileWriter writer = new FileWriter("code.coverage");
82                for (LineInfo line : lineList) {
83                    writer.write(line.toString() + '\n');
84                    // System.out.println(line);
85                }
86                writer.close();
87            } catch (IOException ex) { System.out.println(ex.toString()); }
88        }
89    }
90
91    @Override
92    public void executeInstruction(VM vm, ThreadInfo currentThread,
93                                   Instruction ins) {
94        String line = ins.getFilePos();
95        if (line.contains(":")) {
96            LineInfo currentLine =
97                new LineInfo(line.split(":")[0], ins.getLineNumber());
98            covered.add(currentLine);
99        }
100    }
101}