Search in sources :

Example 11 with NullStatement

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

the class ReferenceLocalSearch method replaceMethodParameter.

/**
 * Go through parameters of method call and apply local search
 *
 * @param test
 * @param statement
 */
private boolean replaceMethodParameter(TestChromosome test, MethodStatement statement) {
    List<VariableReference> parameters = statement.getParameterReferences();
    if (parameters.isEmpty())
        return false;
    int max = parameters.size();
    if (!statement.isStatic()) {
        max++;
    }
    int numParameter = Randomness.nextInt(max);
    if (numParameter == parameters.size()) {
        // replace callee
        VariableReference callee = statement.getCallee();
        List<VariableReference> objects = test.getTestCase().getObjects(callee.getType(), statement.getPosition());
        objects.remove(callee);
        if (objects.isEmpty())
            return false;
        VariableReference replacement = Randomness.choice(objects);
        statement.setCallee(replacement);
        test.setChanged(true);
    } else {
        VariableReference parameter = parameters.get(numParameter);
        List<VariableReference> objects = test.getTestCase().getObjects(parameter.getType(), statement.getPosition());
        objects.remove(parameter);
        objects.remove(statement.getReturnValue());
        NullStatement nullStatement = new NullStatement(test.getTestCase(), parameter.getType());
        if (!parameter.isPrimitive())
            objects.add(nullStatement.getReturnValue());
        if (objects.isEmpty())
            return false;
        VariableReference replacement = Randomness.choice(objects);
        if (replacement == nullStatement.getReturnValue()) {
            test.getTestCase().addStatement(nullStatement, statement.getPosition());
        }
        statement.replaceParameterReference(replacement, numParameter);
        test.setChanged(true);
    }
    return false;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) NullStatement(org.evosuite.testcase.statements.NullStatement)

Example 12 with NullStatement

use of org.evosuite.testcase.statements.NullStatement 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)

Aggregations

NullStatement (org.evosuite.testcase.statements.NullStatement)12 VariableReference (org.evosuite.testcase.variable.VariableReference)8 ArrayList (java.util.ArrayList)4 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)4 MethodStatement (org.evosuite.testcase.statements.MethodStatement)4 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)3 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)3 Statement (org.evosuite.testcase.statements.Statement)3 Method (java.lang.reflect.Method)2 TestFactory (org.evosuite.testcase.TestFactory)2 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)2 FieldStatement (org.evosuite.testcase.statements.FieldStatement)2 ArrayReference (org.evosuite.testcase.variable.ArrayReference)2 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)2 GenericMethod (org.evosuite.utils.generic.GenericMethod)2 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ResourceList (org.evosuite.classpath.ResourceList)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1