Search in sources :

Example 61 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class StringLiteralAmplifier method amplify.

@Override
protected Set<CtLiteral<String>> amplify(CtLiteral<String> existingLiteral) {
    final Factory factory = existingLiteral.getFactory();
    Set<CtLiteral<String>> values = new HashSet<>();
    String value = (String) existingLiteral.getValue();
    // TODO idk if we should add all values around
    values.addAll(this.testClassToBeAmplified.getElements(new TypeFilter<CtLiteral<String>>(CtLiteral.class)).stream().filter(element -> element.getValue() instanceof String && element.getValue() != null && !element.equals(existingLiteral)).map(CtLiteral::clone).collect(Collectors.toList()));
    if (value != null) {
        if (value.length() > 2) {
            int length = value.length();
            // add one random char
            int index = AmplificationHelper.getRandom().nextInt(length - 2) + 1;
            values.add(factory.createLiteral(value.substring(0, index - 1) + AmplificationHelper.getRandomChar() + value.substring(index, length)));
            // replace one random char
            index = AmplificationHelper.getRandom().nextInt(length - 2) + 1;
            values.add(factory.createLiteral(value.substring(0, index) + AmplificationHelper.getRandomChar() + value.substring(index, length)));
            // remove one random char
            index = AmplificationHelper.getRandom().nextInt(length - 2) + 1;
            values.add(factory.createLiteral(value.substring(0, index) + value.substring(index + 1, length)));
        } else {
            values.add(factory.createLiteral("" + AmplificationHelper.getRandomChar()));
        }
    }
    // add one random string
    values.add(factory.createLiteral(AmplificationHelper.getRandomString(10)));
    // add empty string
    values.add(factory.createLiteral(""));
    return values;
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) HashSet(java.util.HashSet) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Set(java.util.Set) Factory(spoon.reflect.factory.Factory) CtLiteral(spoon.reflect.code.CtLiteral) Collectors(java.util.stream.Collectors) CtLiteral(spoon.reflect.code.CtLiteral) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) HashSet(java.util.HashSet)

Example 62 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class NumberLiteralAmplifierTest method testShortMutation.

@Test
public void testShortMutation() throws Exception {
    final String nameMethod = "methodShort";
    final short originalValue = 23;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Short> expectedValues = Arrays.asList((short) 22, (short) 24, Short.MIN_VALUE, Short.MAX_VALUE, (short) 0);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(5, mutantMethods.size());
    for (int i = 0; i < mutantMethods.size(); i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
    }
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 63 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class NumberLiteralAmplifierTest method testIntMutation.

@Test
public void testIntMutation() throws Exception {
    final String nameMethod = "methodInteger";
    final int originalValue = 23;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Integer> expectedValues = Arrays.asList(22, 24, 2147483647, -2147483648, 0);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(5, mutantMethods.size());
    for (int i = 0; i < mutantMethods.size(); i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
    }
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 64 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class NumberLiteralAmplifierTest method testLongMutation.

@Test
public void testLongMutation() throws Exception {
    final String nameMethod = "methodLong";
    final long originalValue = 23L;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Long> expectedValues = Arrays.asList(22L, 24L, Long.MIN_VALUE, Long.MAX_VALUE, 0L);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(5, mutantMethods.size());
    for (int i = 0; i < mutantMethods.size(); i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
    }
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 65 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class NumberLiteralAmplifierTest method testDoubleMutation.

@Test
public void testDoubleMutation() throws Exception {
    final String nameMethod = "methodDouble";
    final double originalValue = 23.0D;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Double> expectedValues = Arrays.asList(22.0D, 24.0D, Double.MAX_VALUE, Double.MIN_VALUE, Double.MIN_NORMAL, Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0D);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(9, mutantMethods.size());
    for (int i = 0; i < mutantMethods.size(); i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
    }
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Aggregations

TypeFilter (spoon.reflect.visitor.filter.TypeFilter)132 Test (org.junit.Test)119 Launcher (spoon.Launcher)54 Factory (spoon.reflect.factory.Factory)52 CtMethod (spoon.reflect.declaration.CtMethod)43 CtInvocation (spoon.reflect.code.CtInvocation)27 CtClass (spoon.reflect.declaration.CtClass)24 CtLiteral (spoon.reflect.code.CtLiteral)18 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)18 AbstractTest (fr.inria.AbstractTest)17 List (java.util.List)17 CtTypeReference (spoon.reflect.reference.CtTypeReference)17 ArrayList (java.util.ArrayList)14 CtBlock (spoon.reflect.code.CtBlock)10 CtConstructorCall (spoon.reflect.code.CtConstructorCall)10 CtIf (spoon.reflect.code.CtIf)10 CtStatement (spoon.reflect.code.CtStatement)10 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)10 File (java.io.File)9 CtElement (spoon.reflect.declaration.CtElement)8