Search in sources :

Example 41 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class ReferenceLocalSearch method changeParameters.

/**
 * Switch parameter/callee variables with other available objects
 *
 * @param test
 * @param statement
 * @return
 */
private boolean changeParameters(TestChromosome test, int statement) {
    logger.debug("Changing parameters");
    Statement stmt = test.getTestCase().getStatement(statement);
    if (stmt instanceof MethodStatement) {
        return replaceMethodParameter(test, (MethodStatement) stmt);
    } else if (stmt instanceof ConstructorStatement) {
        return replaceConstructorParameter(test, (ConstructorStatement) stmt);
    } else if (stmt instanceof FieldStatement) {
        return replaceFieldSource(test, (FieldStatement) stmt);
    } else {
        return false;
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement)

Example 42 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class ReferenceLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    boolean hasImproved = false;
    int currentProbe = 0;
    backup(test);
    int oldLength = test.size();
    while (currentProbe < Properties.LOCAL_SEARCH_PROBES && !LocalSearchBudget.getInstance().isFinished()) {
        logger.info("Current probe on statement " + statement + ": " + currentProbe);
        List<Mutations> mutations = new ArrayList<Mutations>();
        mutations.add(Mutations.REPLACE);
        Statement st = test.getTestCase().getStatement(statement);
        if (!st.getReturnValue().isPrimitive() && !(st instanceof NullStatement)) {
            mutations.add(Mutations.CALL);
        }
        if (st.getNumParameters() > 0) {
            mutations.add(Mutations.PARAMETER);
        } else {
            mutations.remove(Mutations.PARAMETER);
        }
        int delta = 0;
        Mutations m = Randomness.choice(mutations);
        switch(m) {
            case REPLACE:
                replace(test, statement);
                if (test.size() > oldLength)
                    delta = test.size() - oldLength;
                break;
            case PARAMETER:
                changeParameters(test, statement);
                break;
            case CALL:
                addCall(test, statement);
                break;
        }
        if (test.isChanged()) {
            logger.info("Is changed");
            // logger.info("Test after mutation: " + test.getTestCase().toCode());
            if (objective.hasImproved(test)) {
                logger.info("Fitness has improved, keeping");
                currentProbe = 0;
                hasImproved = true;
                backup(test);
                statement += delta;
                positionDelta += delta;
                oldLength = test.size();
            } else {
                logger.info("Fitness has not improved, reverting");
                currentProbe++;
                restore(test);
            }
        } else {
            logger.info("Is not changed");
            currentProbe++;
        }
    }
    return hasImproved;
}
Also used : NullStatement(org.evosuite.testcase.statements.NullStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) ArrayList(java.util.ArrayList)

Example 43 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class AVMTestCaseLocalSearch method doSearch.

@Override
public boolean doSearch(TestChromosome individual, LocalSearchObjective<TestChromosome> objective) {
    logger.info("Test before local search: " + individual.getTestCase().toCode());
    boolean improved = false;
    // Only apply local search up to the point where an exception was thrown
    // TODO: Check whether this conflicts with test expansion
    int lastPosition = individual.size() - 1;
    if (individual.getLastExecutionResult() != null && !individual.isChanged()) {
        Integer lastPos = individual.getLastExecutionResult().getFirstPositionOfThrownException();
        if (lastPos != null)
            lastPosition = lastPos.intValue();
    }
    TestCase test = individual.getTestCase();
    for (int i = lastPosition; i >= 0; i--) {
        if (LocalSearchBudget.getInstance().isFinished())
            break;
        if (objective.isDone()) {
            break;
        }
        if (i >= individual.size()) {
            logger.warn("Test size decreased unexpectedly during local search, aborting local search");
            logger.warn(individual.getTestCase().toCode());
            break;
        }
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        final Statement statement = test.getStatement(i);
        if (!test.hasReferences(statement.getReturnValue()) && !statement.getReturnClass().equals(targetClass)) {
            logger.info("Return value of statement " + i + " is not referenced and not SUT, not doing local search");
            continue;
        }
        StatementLocalSearch search = StatementLocalSearch.getLocalSearchFor(statement);
        if (search != null) {
            logger.info("Applying local search of type " + search.getClass() + " to statement " + statement + " / " + individual.getTestCase().getStatement(i));
            if (search.doSearch(individual, i, (LocalSearchObjective<TestChromosome>) objective)) {
                improved = true;
            }
            // i = s.getPosition();
            logger.debug("Old position was: " + i + ", adjusting to: " + (i + search.getPositionDelta()));
            i += search.getPositionDelta();
            test = individual.getTestCase();
        } else {
            /*
				 * No statement local search has been produced for this
				 * statement. Skipping.
				 */
            continue;
        }
    }
    LocalSearchBudget.getInstance().countLocalSearchOnTest();
    // Return true iif search was successful
    return improved;
// TODO: Handle arrays in local search
// TODO: mutating an int might have an effect on array lengths
}
Also used : TestCase(org.evosuite.testcase.TestCase) Statement(org.evosuite.testcase.statements.Statement) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 44 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassList.

@Test
public void testGenericClassList() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithListTest.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithList.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertTrue(factory.hasCarvedTestCases());
    TestChromosome carved = factory.getChromosome();
    Assert.assertNotNull(carved);
    Assert.assertEquals("", 10, carved.test.size());
    for (int i = 0; i < carved.test.size(); i++) {
        Statement stmt = carved.test.getStatement(i);
        boolean valid = stmt.isValid();
        Assert.assertTrue("Invalid stmt at position " + i, valid);
    }
    String code = carved.toString();
    String setLong = "GenericObjectWrapperWithList<GenericObjectWrapperWithListTest.Foo>";
    Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Statement(org.evosuite.testcase.statements.Statement) Test(org.junit.Test)

Example 45 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassSet.

@Test
public void testGenericClassSet() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperSetTest.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapper.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertTrue(factory.hasCarvedTestCases());
    TestChromosome carved = factory.getChromosome();
    Assert.assertNotNull(carved);
    Assert.assertEquals("", 13, carved.test.size());
    for (int i = 0; i < carved.test.size(); i++) {
        Statement stmt = carved.test.getStatement(i);
        boolean valid = stmt.isValid();
        Assert.assertTrue("Invalid stmt at position " + i, valid);
    }
    String code = carved.toString();
    String setLong = "GenericObjectWrapper<HashSet<Long>>";
    Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
    code = carved.toString();
    setLong = "(Object)";
    Assert.assertFalse("generated code contains object cast " + setLong + "\n" + code, code.contains(setLong));
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Statement(org.evosuite.testcase.statements.Statement) Test(org.junit.Test)

Aggregations

Statement (org.evosuite.testcase.statements.Statement)48 MethodStatement (org.evosuite.testcase.statements.MethodStatement)25 VariableReference (org.evosuite.testcase.variable.VariableReference)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)16 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)11 TestCase (org.evosuite.testcase.TestCase)8 JUnitTestCarvedChromosomeFactory (org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 FieldStatement (org.evosuite.testcase.statements.FieldStatement)7 Method (java.lang.reflect.Method)6 TestChromosome (org.evosuite.testcase.TestChromosome)6 GenericMethod (org.evosuite.utils.generic.GenericMethod)6 FunctionalMockStatement (org.evosuite.testcase.statements.FunctionalMockStatement)5 NullStatement (org.evosuite.testcase.statements.NullStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)4 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)4 TestFactory (org.evosuite.testcase.TestFactory)3 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)3