Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi fix #23

Merged
merged 4 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/main/java/fr/inria/diversify/dspot/DSpot.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import fr.inria.diversify.util.FileUtils;
import fr.inria.diversify.util.InitUtils;
import fr.inria.diversify.util.PrintClassUtils;
import spoon.Launcher;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.*;
import java.util.stream.Collectors;

/**
* User: Simon
Expand All @@ -28,6 +28,11 @@ public class DSpot {

private List<Amplifier> amplifiers;
private int numberOfIterations;

public DiversityCompiler getCompiler() {
return compiler;
}

private DiversityCompiler compiler;
private InputProgram inputProgram;
private DiversifyClassLoader applicationClassLoader;
Expand Down Expand Up @@ -71,31 +76,29 @@ public DSpot(InputConfiguration configuration, int numberOfIterations, List<Ampl
}

public List<CtType> amplifiyAllTests() throws InterruptedException, IOException, ClassNotFoundException {
Launcher launcher = new Launcher();
launcher.addInputResource(this.inputProgram.getAbsoluteTestSourceCodeDir());
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
final List<CtType> amplifiedClassTest = new ArrayList<>();
launcher.getFactory().Class().getAll().forEach(classTest -> {
try {
amplifiedClassTest.add(amplifyTest(classTest.getQualifiedName()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
);
return amplifiedClassTest;
return inputProgram.getFactory().Class().getAll().stream()
.filter(ctClass ->
ctClass.getMethods().stream()
.filter(method ->
AmplificationChecker.isTest(method, inputProgram.getRelativeTestSourceCodeDir()))
.count() > 0)
.map(this::amplifyTest)
.collect(Collectors.toList());
}

public CtType amplifyTest(String fullName) throws InterruptedException, IOException, ClassNotFoundException {
return amplifyTest(inputProgram.getFactory().Type().get(fullName));
}

public CtType amplifyTest(CtType test) throws IOException, InterruptedException, ClassNotFoundException {
File logDir = new File(inputProgram.getProgramDir() + "/log");
Amplification testAmplification = new Amplification(inputProgram, compiler, applicationClassLoader, this.amplifiers, logDir);
List<CtMethod> ampTests = testAmplification.amplification(test, numberOfIterations);
return assertGenerator.generateAsserts(test, ampTests, AmplificationHelper.getAmpTestToParent());
public CtType amplifyTest(CtType test) {
try {
File logDir = new File(inputProgram.getProgramDir() + "/log");
Amplification testAmplification = new Amplification(inputProgram, compiler, applicationClassLoader, this.amplifiers, logDir);
List<CtMethod> ampTests = testAmplification.amplification(test, numberOfIterations);
return assertGenerator.generateAsserts(test, ampTests, AmplificationHelper.getAmpTestToParent());
} catch (IOException | InterruptedException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

public CtType amplifyTest(List<CtMethod> tests, CtType testClass) throws IOException, InterruptedException, ClassNotFoundException {
Expand Down
67 changes: 33 additions & 34 deletions src/main/java/fr/inria/diversify/dspot/TestSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class TestSelector {
protected List<Graph> graphCoverage;
protected int maxNumberOfTest;

public TestSelector(File logDir, int maxNumberOfTest) {
public TestSelector(File logDir, int maxNumberOfTest) {
this.logDir = logDir;
this.maxNumberOfTest = maxNumberOfTest;
}
Expand All @@ -44,22 +44,21 @@ protected void updateLogInfo() throws IOException {
logReader.addParser(graphReader);
logReader.addParser(coverageParser);
logReader.readLogs();
try {
if (branchCoverage == null) {
branchCoverage = coverageParser.getResult();
} else {
for (Coverage coverage : coverageParser.getResult()) {
Coverage previous = branchCoverage.stream()
.filter(ac -> ac.getName().equals(coverage.getName()))
.findFirst()
.orElse(null);
if (previous != null) {
branchCoverage.remove(previous);
}
branchCoverage.add(coverage);

if (branchCoverage == null) {
branchCoverage = coverageParser.getResult();
} else {
for (Coverage coverage : coverageParser.getResult()) {
Coverage previous = branchCoverage.stream()
.filter(ac -> ac.getName().equals(coverage.getName()))
.findFirst()
.orElse(null);
if (previous != null) {
branchCoverage.remove(previous);
}
branchCoverage.add(coverage);
}
} catch (Throwable e) {}
}

if (graphCoverage == null) {
graphCoverage = graphReader.getResult();
Expand All @@ -83,7 +82,7 @@ protected Collection<CtMethod> selectTestToAmp(Collection<CtMethod> oldTests, Co
Map<CtMethod, Set<String>> selectedTest = new HashMap<>();
for (CtMethod test : newTests) {
Set<String> tc = getTestCoverageFor(test);
if(!tc.isEmpty()) {
if (!tc.isEmpty()) {
Set<String> parentTc = getParentTestCoverageFor(test);
if (!parentTc.isEmpty()) {
selectedTest.put(test, new HashSet<>());
Expand All @@ -95,7 +94,7 @@ protected Collection<CtMethod> selectTestToAmp(Collection<CtMethod> oldTests, Co
}
}
Set<CtMethod> mths = new HashSet<>();
if(selectedTest.size() > maxNumberOfTest) {
if (selectedTest.size() > maxNumberOfTest) {
mths.addAll(reduceSelectedTest(selectedTest));
} else {
mths.addAll(selectedTest.keySet());
Expand All @@ -112,10 +111,10 @@ protected Collection<CtMethod> selectTestToAmp(Collection<CtMethod> oldTests, Co
}

Random r = new Random();
while(oldMths.size() > maxNumberOfTest) {
while (oldMths.size() > maxNumberOfTest) {
oldMths.remove(r.nextInt(oldMths.size()));
}
for(CtMethod oltMth : oldMths) {
for (CtMethod oltMth : oldMths) {
String testName = oltMth.getSimpleName();
testAges.put(testName, testAges.get(testName) - 1);
}
Expand All @@ -124,10 +123,10 @@ protected Collection<CtMethod> selectTestToAmp(Collection<CtMethod> oldTests, Co

protected Integer getAgesFor(CtMethod test) {
String testName = test.getSimpleName();
if(testName.contains("_cf")) {
if (testName.contains("_cf")) {
return 2;
}
if(!AmplificationHelper.getAmpTestToParent().containsKey(test)) {
if (!AmplificationHelper.getAmpTestToParent().containsKey(test)) {
return 3;
}
return 0;
Expand All @@ -138,10 +137,8 @@ public Collection<CtMethod> selectedAmplifiedTests(Collection<CtMethod> tests) {
for (CtMethod test : tests) {
Set<String> tc = getTestCoverageFor(test);
Set<String> parentTc = getParentTestCoverageFor(test);
if (!tc.isEmpty() && !parentTc.isEmpty()) {
if (!parentTc.containsAll(tc)) {
amplifiedTests.put(test, diff(tc, parentTc));
}
if (!tc.isEmpty() && !parentTc.containsAll(tc)) {
amplifiedTests.put(test, diff(tc, parentTc));
}
}
return reduceSelectedTest(amplifiedTests);
Expand All @@ -156,21 +153,23 @@ protected Collection<CtMethod> reduceSelectedTest(Map<CtMethod, Set<String>> sel
.collect(Collectors.toList());

List<CtMethod> methods = new ArrayList<>();
while(!sortedKey.isEmpty()) {
while (!sortedKey.isEmpty()) {
Set<String> key = new HashSet<>(sortedKey.remove(0));

if(map.containsKey(key)) {
if (map.containsKey(key)) {
methods.add(map.get(key).stream().findAny().get());

}
sortedKey = sortedKey.stream()
.map(k -> {k.removeAll(key); return k;})
.map(k -> {
k.removeAll(key);
return k;
})
.filter(k -> !k.isEmpty())
.sorted((l1, l2) -> Integer.compare(l2.size(), l1.size()))
.collect(Collectors.toList());

map.keySet().stream()
.forEach(set -> set.removeAll(key));
map.keySet().forEach(set -> set.removeAll(key));
}
return methods;
}
Expand All @@ -185,7 +184,7 @@ protected CtMethod getParent(CtMethod test) {

protected Set<String> getParentTestCoverageFor(CtMethod mth) {
CtMethod parent = getParent(mth);
if(parent != null) {
if (parent != null) {
String parentName = parent.getSimpleName();
if (parentName != null) {
return getCoverageFor(parentName);
Expand All @@ -211,8 +210,8 @@ protected Set<String> getCoverageFor(String mthName) {
}

protected void deleteLogFile() throws IOException {
for(File file : logDir.listFiles()) {
if(!file.getName().equals("info")) {
for (File file : logDir.listFiles()) {
if (!file.getName().equals("info")) {
FileUtils.forceDelete(file);
}
}
Expand All @@ -232,7 +231,7 @@ protected Set<String> diff(Set<String> set1, Set<String> set2) {
public Coverage getGlobalCoverage() {
Coverage coverage = new Coverage("global");

for(Coverage tc : branchCoverage) {
for (Coverage tc : branchCoverage) {
coverage.merge(tc);
}

Expand Down