Skip to content

Commit

Permalink
fix: quote and dquote are discarded for the random char
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb committed Dec 20, 2016
1 parent d8b6504 commit 94a5ae8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
17 changes: 7 additions & 10 deletions src/main/java/fr/inria/diversify/dspot/amp/TestDataMutator.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,11 @@ protected List<CtMethod> createAllStringMutant(CtMethod method, CtLiteral litera
}


protected CtMethod createStringMutant(CtMethod method, int original_lit_index, String newValue) {
private CtMethod createStringMutant(CtMethod method, int original_lit_index, String newValue) {
dataCount++;
//clone the method
CtMethod cloned_method = AmplificationHelper.cloneMethod(method, "_literalMutation");
//get the lit_indexth literal of the cloned method
CtLiteral newLiteral = Query.getElements(cloned_method, new TypeFilter<CtLiteral>(CtLiteral.class))
.get(original_lit_index);

newLiteral.setValue(newValue);

Query.getElements(cloned_method, new TypeFilter<>(CtLiteral.class))
.get(original_lit_index).replace(cloned_method.getFactory().Code().createLiteral(newValue));
return cloned_method;
}

Expand Down Expand Up @@ -170,8 +165,10 @@ protected Set<String> stringMutated(CtLiteral literal) {
return values;
}

protected char getRandomChar() {
return (char) (AmplificationHelper.getRandom().nextInt(94) + 32);
private char getRandomChar() {
int value = AmplificationHelper.getRandom().nextInt(94) + 32;
char c = (char) ((value == 34 || value == 39) ? value + (AmplificationHelper.getRandom().nextBoolean() ? 1 : -1) : value);
return c;//excluding " and '
}

protected Set<? extends Number> numberMutated(CtLiteral literal) {
Expand Down
33 changes: 19 additions & 14 deletions src/test/java/fr/inria/diversify/dspot/DSpotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public void test() throws Exception, InvalidSdkException {
DSpot dspot = new DSpot(configuration);

CtType amplifiedTest = dspot.amplifyTest("example.TestSuiteExample");
assertEquals(19, amplifiedTest.getMethods().size());
assertEquals(18, amplifiedTest.getMethods().size());
assertEquals(originalTestBody, amplifiedTest.getMethod("test1").getBody().toString());
assertEquals(expectedAmplifiedBody, amplifiedTest.getMethod("test1_cf5_cf236").getBody().toString());

amplifiedTest.getMethods();

assertEquals(expectedAmplifiedBody, amplifiedTest.getMethod("test1_cf6_cf34_cf167").getBody().toString());
}

private final String pathToPropertiesFile = "src/test/resources/test-projects/test-projects.properties";
Expand All @@ -53,18 +56,20 @@ public void test() throws Exception, InvalidSdkException {

private final String expectedAmplifiedBody = "{" + nl +
" example.Example ex = new example.Example();" + nl +
" int vc_1 = 0;" + nl +
" junit.framework.Assert.assertEquals(vc_1, 0);" + nl +
" java.lang.String s = \"abcd\";" + nl +
" junit.framework.Assert.assertEquals(s, \"abcd\");" + nl +
" char o_test1_cf5_cf236__5 = ex.charAt(s, vc_1);" + nl +
" junit.framework.Assert.assertEquals(o_test1_cf5_cf236__5, 'a');" + nl +
" int vc_35 = 715956334;" + nl +
" junit.framework.Assert.assertEquals(vc_35, 715956334);" + nl +
" java.lang.String vc_10 = \"abcd\";" + nl +
" junit.framework.Assert.assertEquals(vc_10, \"abcd\");" + nl +
" char o_test1_cf5_cf236__8 = ex.charAt(vc_10, vc_35);" + nl +
" junit.framework.Assert.assertEquals(o_test1_cf5_cf236__8, 'd');" + nl +
" int vc_5 = -619987209;" + nl +
" junit.framework.Assert.assertEquals(vc_5, -619987209);" + nl +
" java.lang.String vc_0 = \"abcd\";" + nl +
" junit.framework.Assert.assertEquals(vc_0, \"abcd\");" + nl +
" char o_test1_cf6_cf34_cf167__5 = ex.charAt(vc_0, vc_5);" + nl +
" junit.framework.Assert.assertEquals(o_test1_cf6_cf34_cf167__5, 'a');" + nl +
" char o_test1_cf6_cf34_cf167__6 = ex.charAt(vc_0, vc_5);" + nl +
" junit.framework.Assert.assertEquals(o_test1_cf6_cf34_cf167__6, 'a');" + nl +
" int vc_29 = 995075168;" + nl +
" junit.framework.Assert.assertEquals(vc_29, 995075168);" + nl +
" java.lang.String vc_8 = \"abcd\";" + nl +
" junit.framework.Assert.assertEquals(vc_8, \"abcd\");" + nl +
" char o_test1_cf6_cf34_cf167__9 = ex.charAt(vc_8, vc_29);" + nl +
" junit.framework.Assert.assertEquals(o_test1_cf6_cf34_cf167__9, 'd');" + nl +
" org.junit.Assert.assertEquals('a', ex.charAt(\"abcd\", 0));" + nl +
"}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import org.junit.Test;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.visitor.filter.TypeFilter;

import java.security.cert.CollectionCertStoreParameters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
Expand Down Expand Up @@ -168,9 +172,9 @@ public void testStringMutation() throws Exception, InvalidSdkException {
final String originalValue = "MyStringLiteral";
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
TestDataMutator amplifcator = getTestDataMutator(literalMutationClass);
TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<CtMethod> mutantMethods = amplifcator.apply(method);
List<CtMethod> mutantMethods = amplificator.apply(method);

assertEquals(4, mutantMethods.size());
for (int i = 0; i < mutantMethods.size(); i++) {
Expand Down Expand Up @@ -221,7 +225,8 @@ public void testStringMutationRandom() throws Exception, InvalidSdkException {
* this distance is equals to 1, since we do not stack mutation.
* 3 cases: one char less, one char more and one (and only one) different char.
*/
private void assertDistanceBetweenOriginalAndMuted(String original, String mutant) {
private void assertDistanceBetweenOriginalAndMuted(String original, String mutant) throws InvalidSdkException, Exception {

byte[] originalBytes = original.getBytes();
byte[] mutantBytes = mutant.getBytes();

Expand All @@ -243,7 +248,22 @@ private void assertDistanceBetweenOriginalAndMuted(String original, String mutan
}
}

assertTrue(addCharAssertion || removeCharAssertion || (diffFound && replaceCharAssertion) || "MySecondStringLiteral".equals(mutant));
List<String> existingStringLiterals = Utils.getFactory().Class().getAll()
.stream()
.flatMap(ctType ->
ctType.getElements(new TypeFilter<CtLiteral>(CtLiteral.class) {
@Override
public boolean matches(CtLiteral literal) {
try {
return Utils.getFactory().Type().STRING.equals(literal.getType()) && super.matches(literal);
} catch (InvalidSdkException | Exception ignored) {
}
return false;
}
}).stream().map(ctLiteral -> (String) ctLiteral.getValue())
).collect(Collectors.toList());

assertTrue(addCharAssertion || removeCharAssertion || (diffFound && replaceCharAssertion) || existingStringLiterals.contains(mutant));
}

@Test
Expand Down

0 comments on commit 94a5ae8

Please sign in to comment.