Search in sources :

Example 31 with CtLiteral

use of spoon.reflect.code.CtLiteral in project dspot by STAMP-project.

the class TestDataMutatorTest method testDoubleMutation.

@Test
public void testDoubleMutation() throws Exception {
    /*
            Test the amplification on numbers (double) literal
                4 operations: i+1, i−1, i×2, i÷2
                and 1 literals present that is different that the muted
         */
    final String nameMethod = "methodDouble";
    final double originalValue = 23.0D;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Double> expectedValues = Arrays.asList(22.0D, 24.0D, 46.0D, (23.0D / 2.0D), 32.0D, 0.0D);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(6, mutantMethods.size());
    for (int i = 0; i < mutantMethods.size(); i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + SUFFIX_MUTATION + "Number" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertTrue(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 32 with CtLiteral

use of spoon.reflect.code.CtLiteral in project dspot by STAMP-project.

the class TestDataMutatorTest method testIntMutation.

@Test
public void testIntMutation() throws Exception {
    /*
            Test the amplification on numbers (integer) literal
                4 operations: i+1, i−1, i×2, i÷2.
                and 1 literals present that is different that the muted
                and 0
         */
    final String nameMethod = "methodInteger";
    final int originalValue = 23;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Integer> expectedValues = Arrays.asList(22, 24, 46, (23 / 2), 32, 0);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(6, mutantMethods.size());
    for (int i = 0; i < mutantMethods.size(); i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + SUFFIX_MUTATION + "Number" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertTrue(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 33 with CtLiteral

use of spoon.reflect.code.CtLiteral in project dspot by STAMP-project.

the class TestDataMutatorTest method testCharacterMutation.

@Test
public void testCharacterMutation() throws Exception {
    /*
            Test the amplification on character literal
         */
    final String nameMethod = "methodCharacter";
    final char originalValue = 'z';
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    TestDataMutator mutator = getTestDataMutator(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<CtMethod> mutantMethods = mutator.apply(method);
    List<Character> expectedValues = Arrays.asList('\0', ' ', 'h', '{', 'y', System.getProperty("line.separator").charAt(0));
    assertEquals(6, mutantMethods.size());
    for (int i = 0; i < mutantMethods.size(); i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + SUFFIX_MUTATION + "Char" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertTrue(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 34 with CtLiteral

use of spoon.reflect.code.CtLiteral in project dspot by STAMP-project.

the class TestDataMutatorTest method testBooleanMutation.

@Test
public void testBooleanMutation() throws Exception {
    /*
            Test the amplification on boolean literal
         */
    final String nameMethod = "methodBoolean";
    final boolean originalValue = true;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    TestDataMutator mutator = getTestDataMutator(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<CtMethod> mutantMethods = mutator.apply(method);
    CtMethod mutantMethod = mutantMethods.get(0);
    assertEquals(1, mutantMethods.size());
    assertEquals(nameMethod + SUFFIX_MUTATION + "Boolean" + "1", mutantMethod.getSimpleName());
    CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
    assertEquals(!(originalValue), 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 35 with CtLiteral

use of spoon.reflect.code.CtLiteral in project dspot by STAMP-project.

the class ChangeMinimizerTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() throws Exception {
    /*
            ChangeMinimizer keeps only the assertions that trigger the failure on the second version
         */
    final CtClass testClass = Utils.findClass("example.TestSuiteExample");
    final String configurationPath = Utils.getInputConfiguration().getProperty("configPath");
    final String pathToFolder = Utils.getInputConfiguration().getProperty("folderPath");
    InputConfiguration inputConfiguration = new InputConfiguration(configurationPath);
    InputProgram inputProgram = InputConfiguration.initInputProgram(inputConfiguration);
    inputConfiguration.setInputProgram(inputProgram);
    String pathToChangedVersionOfProgram = pathToFolder + DSpotUtils.shouldAddSeparator.apply(pathToFolder) + (inputConfiguration.getProperty("targetModule") != null ? inputConfiguration.getProperty("targetModule") + DSpotUtils.shouldAddSeparator.apply(pathToFolder) : "");
    inputProgram.setProgramDir(pathToChangedVersionOfProgram);
    Initializer.initialize(Utils.getInputConfiguration(), inputProgram);
    final HashMap<CtMethod<?>, Failure> failurePerAmplifiedTest = new HashMap<>();
    final CtMethod<?> test2 = Utils.findMethod(testClass, "test2");
    failurePerAmplifiedTest.put(test2, new Failure(Description.EMPTY, new StringIndexOutOfBoundsException(-1)));
    final ChangeMinimizer changeMinimizer = new ChangeMinimizer(testClass, inputConfiguration, inputProgram, pathToChangedVersionOfProgram, failurePerAmplifiedTest);
    final CtInvocation assertion = test2.getElements(new TypeFilter<CtInvocation>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation element) {
            return AmplificationChecker.isAssert(element);
        }
    }).get(0);
    final CtInvocation clone = assertion.clone();
    clone.getElements(new TypeFilter<CtLiteral>(CtLiteral.class) {

        @Override
        public boolean matches(CtLiteral element) {
            return element.getValue() instanceof Integer && super.matches(element);
        }
    }).get(0).setValue(-1);
    test2.getBody().insertEnd(clone);
    assertEquals(3, test2.getBody().getStatements().size());
    final CtMethod<?> minimize = changeMinimizer.minimize(test2);
    assertEquals(2, minimize.getBody().getStatements().size());
}
Also used : HashMap(java.util.HashMap) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtMethod(spoon.reflect.declaration.CtMethod) Failure(org.junit.runner.notification.Failure) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Aggregations

CtLiteral (spoon.reflect.code.CtLiteral)38 Test (org.junit.Test)28 CtMethod (spoon.reflect.declaration.CtMethod)19 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)19 AbstractTest (fr.inria.AbstractTest)13 Factory (spoon.reflect.factory.Factory)10 Launcher (spoon.Launcher)8 CtInvocation (spoon.reflect.code.CtInvocation)6 CtStatement (spoon.reflect.code.CtStatement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 CtExpression (spoon.reflect.code.CtExpression)5 List (java.util.List)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 CtIf (spoon.reflect.code.CtIf)3 CtClass (spoon.reflect.declaration.CtClass)3 CtElement (spoon.reflect.declaration.CtElement)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 AssertionRemover (fr.inria.diversify.dspot.assertGenerator.AssertionRemover)2