Skip to content

Commit

Permalink
Refactor all lit ampl (#353)
Browse files Browse the repository at this point in the history
* feat: refactor the way that All Literal Amplifiers are done, it is now a specific Amplifier, rather than a list

* fix: remove type cast when removing assertions

* test: add a test of amplification on one class two method cf #352

* test: fix oracle in test One Class Two Methods
  • Loading branch information
danglotb authored Mar 7, 2018
1 parent 0f91661 commit ece2fe8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fr.inria.diversify.dspot.amplifier;

import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by Benjamin DANGLOT
* benjamin.danglot@inria.fr
* on 07/03/18
*/
public class AllLiteralAmplifiers implements Amplifier {

private List<Amplifier> literalAmplifiers;

public AllLiteralAmplifiers() {
this.literalAmplifiers = Arrays.asList(
new StringLiteralAmplifier(),
new NumberLiteralAmplifier(),
new BooleanLiteralAmplifier(),
new CharLiteralAmplifier()
);
}

@Override
public List<CtMethod> apply(CtMethod testMethod) {
return this.literalAmplifiers.stream()
.flatMap(amplifier -> amplifier.apply(testMethod).stream())
.collect(Collectors.toList());
}

@Override
public void reset(CtType testClass) {
this.literalAmplifiers.forEach(amplifier -> amplifier.reset(testClass));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void removeAssertion(CtInvocation<?> invocation) {
clone = ((CtUnaryOperator) clone).getOperand();
}
if (clone instanceof CtStatement) {
clone.getTypeCasts().clear();
invocation.insertBefore((CtStatement) clone);
} else if (! (clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
CtTypeReference<?> typeOfParameter = clone.getType();
Expand Down
59 changes: 28 additions & 31 deletions dspot/src/main/java/fr/inria/stamp/JSAPOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Created by Benjamin DANGLOT
Expand Down Expand Up @@ -72,24 +72,20 @@ public TestSelector buildSelector() {
}

public enum AmplifierEnum {
MethodAdd(Collections.singletonList(new TestMethodCallAdder())),
MethodRemove(Collections.singletonList(new TestMethodCallRemover())),
TestDataMutator(Collections.singletonList(new TestDataMutator())),
StatementAdd(Collections.singletonList(new StatementAdd())),
StringLiteralAmplifier(Collections.singletonList(new StringLiteralAmplifier())),
NumberLiteralAmplifier(Collections.singletonList(new NumberLiteralAmplifier())),
BooleanLiteralAmplifier(Collections.singletonList(new BooleanLiteralAmplifier())),
CharLiteralAmplifier(Collections.singletonList(new CharLiteralAmplifier())),
AllLiteralAmplifiers(Arrays.asList(new StringLiteralAmplifier(),
new NumberLiteralAmplifier(),
new BooleanLiteralAmplifier(),
new CharLiteralAmplifier())
),
MethodAdd(new TestMethodCallAdder()),
MethodRemove(new TestMethodCallRemover()),
TestDataMutator(new TestDataMutator()),
StatementAdd(new StatementAdd()),
StringLiteralAmplifier(new StringLiteralAmplifier()),
NumberLiteralAmplifier(new NumberLiteralAmplifier()),
BooleanLiteralAmplifier(new BooleanLiteralAmplifier()),
CharLiteralAmplifier(new CharLiteralAmplifier()),
AllLiteralAmplifiers(new AllLiteralAmplifiers()),
None(null);
public final List<Amplifier> amplifiers;
public final Amplifier amplifier;

private AmplifierEnum(List<Amplifier> amplifiers) {
this.amplifiers = amplifiers;
private AmplifierEnum(Amplifier amplifier) {
this.amplifier = amplifier;
}
}

Expand Down Expand Up @@ -141,14 +137,14 @@ public static Configuration parse(String[] args) {
);
}

public static Stream<Amplifier> stringToAmplifier(String amplifier) {
public static Amplifier stringToAmplifier(String amplifier) {
try {
return AmplifierEnum.valueOf(amplifier).amplifiers.stream();
return AmplifierEnum.valueOf(amplifier).amplifier;
} catch (IllegalArgumentException e) {
LOGGER.warn("Wrong values for amplifiers: {} is not recognized", amplifier);
LOGGER.warn("Possible values are: StringLiteralAmplifier | NumberLiteralAmplifier | CharLiteralAmplifier | BooleanLiteralAmplifier | AllLiteralAmplifiers | MethodAdd | MethodRemove | TestDataMutator | StatementAdd | None");
LOGGER.warn("No amplifier has been added for {}", amplifier);
return Stream.of();
return null;
}
}

Expand All @@ -157,7 +153,8 @@ public static List<Amplifier> buildAmplifiersFromString(String[] amplifiersAsStr
return Collections.emptyList();
} else {
return Arrays.stream(amplifiersAsString)
.flatMap(JSAPOptions::stringToAmplifier)
.map(JSAPOptions::stringToAmplifier)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}
Expand Down Expand Up @@ -218,15 +215,15 @@ private static JSAP initJSAP() {
selector.setHelp("[optional] specify the test adequacy criterion to be maximized with amplification");
selector.setDefault("PitMutantScoreSelector");

FlaggedOption specificTestCase = new FlaggedOption("test");
specificTestCase.setStringParser(JSAP.STRING_PARSER);
specificTestCase.setShortFlag('t');
specificTestCase.setList(true);
specificTestCase.setAllowMultipleDeclarations(false);
specificTestCase.setLongFlag("test");
specificTestCase.setDefault("all");
specificTestCase.setUsageName("my.package.MyClassTest");
specificTestCase.setHelp("[optional] fully qualified names of test classes to be amplified. If the value is all, DSpot will amplify the whole test suite. You can also use regex to describe a set of test classes.");
FlaggedOption specificTestClass = new FlaggedOption("test");
specificTestClass.setStringParser(JSAP.STRING_PARSER);
specificTestClass.setShortFlag('t');
specificTestClass.setList(true);
specificTestClass.setAllowMultipleDeclarations(false);
specificTestClass.setLongFlag("test");
specificTestClass.setDefault("all");
specificTestClass.setUsageName("my.package.MyClassTest");
specificTestClass.setHelp("[optional] fully qualified names of test classes to be amplified. If the value is all, DSpot will amplify the whole test suite. You can also use regex to describe a set of test classes.");

FlaggedOption output = new FlaggedOption("output");
output.setStringParser(JSAP.STRING_PARSER);
Expand Down Expand Up @@ -311,7 +308,7 @@ private static JSAP initJSAP() {
jsap.registerParameter(iteration);
jsap.registerParameter(selector);
jsap.registerParameter(maxTestAmplified);
jsap.registerParameter(specificTestCase);
jsap.registerParameter(specificTestClass);
jsap.registerParameter(testCases);
jsap.registerParameter(output);
jsap.registerParameter(cleanOutput);
Expand Down
34 changes: 34 additions & 0 deletions dspot/src/test/java/fr/inria/stamp/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,32 @@ public void testOneClassOneMethod() throws Throwable {
}
}

@Test
public void testOneClassTwoMethods() throws Throwable {
Main.main(new String[]{
"--path-to-properties", "src/test/resources/test-projects/test-projects.properties",
"--test-criterion", "JacocoCoverageSelector",
"--amplifiers", "MethodAdd" + PATH_SEPARATOR + "TestDataMutator" + PATH_SEPARATOR + "StatementAdd",
"--iteration", "1",
"--randomSeed", "72",
"--maven-home", DSpotUtils.buildMavenHome(new InputConfiguration("src/test/resources/test-projects/test-projects.properties")),
"--test", "example.TestSuiteExample",
"--cases", "test2" + PATH_SEPARATOR + "test3",
"--output-path", "target/trash",
"--max-test-amplified", "200"
});
final File reportFile = new File("target/trash/example.TestSuiteExample_jacoco_instr_coverage_report.txt");
assertTrue(reportFile.exists());
assertTrue(new File("target/trash/example.TestSuiteExample_jacoco_instr_coverage.json").exists());
assertTrue(new File("target/trash/example/TestSuiteExampleAmpl.java").exists());
try (BufferedReader reader = new BufferedReader(new FileReader(reportFile))) {
String content = reader.lines().reduce("", (acc, line) -> acc + line + nl);
assertEquals(expectedReportOneClassTwoMethods, content);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static final String expectedReportOneClassOneMethod = nl +
"======= REPORT =======" + nl +
"Initial instruction coverage: 33 / 37" + nl +
Expand All @@ -218,6 +244,14 @@ public void testOneClassOneMethod() throws Throwable {
"Amplified instruction coverage: 37 / 37" + nl +
"100" + DECIMAL_SEPARATOR + "00%" + nl;

private static final String expectedReportOneClassTwoMethods = nl +
"======= REPORT =======" + nl +
"Initial instruction coverage: 33 / 37" + nl +
"89" + DECIMAL_SEPARATOR + "19%" + nl +
"Amplification results with 9 amplified tests." + nl +
"Amplified instruction coverage: 37 / 37" + nl +
"100" + DECIMAL_SEPARATOR + "00%" + nl;

@Test
public void testRegexOnWholePackage() throws Throwable {
Main.main(new String[]{
Expand Down

0 comments on commit ece2fe8

Please sign in to comment.