Search in sources :

Example 1 with ArrayStatement

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

the class ArrayLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    boolean hasImproved = false;
    ArrayStatement p = (ArrayStatement) test.getTestCase().getStatement(statement);
    logger.debug("Doing array local search on statement " + statement + ": " + test.getTestCase().toCode());
    int difference = stripAssignments(p, test, objective);
    logger.debug("Adjusting position from " + statement + " to " + (statement - difference) + ": " + test.getTestCase().toCode());
    positionDelta = difference;
    statement = statement - difference;
    p = (ArrayStatement) test.getTestCase().getStatement(statement);
    hasImproved = searchLength(test, statement, objective);
    TestCaseExpander expander = new TestCaseExpander();
    int lengthWithoutAssignments = test.size();
    p = (ArrayStatement) test.getTestCase().getStatement(statement);
    expander.visitArrayStatement(test.getTestCase(), p);
    int assignmentLength = test.size() - lengthWithoutAssignments;
    for (int position = statement + 1; position < statement + assignmentLength; position++) {
        logger.debug("Local search on statement " + position);
        StatementLocalSearch search = StatementLocalSearch.getLocalSearchFor(test.getTestCase().getStatement(position));
        if (search != null) {
            if (search.doSearch(test, position, objective)) {
                hasImproved = true;
            }
        }
    }
    logger.debug("Finished local search with result {}", p.getCode());
    return hasImproved;
}
Also used : ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) TestCaseExpander(org.evosuite.testcase.TestCaseExpander)

Example 2 with ArrayStatement

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

the class ArrayLocalSearch method stripAssignments.

private int stripAssignments(ArrayStatement statement, TestChromosome test, LocalSearchObjective<TestChromosome> objective) {
    int difference = 0;
    ArrayReference arrRef = (ArrayReference) statement.getReturnValue();
    TestFactory factory = TestFactory.getInstance();
    for (int position = test.size() - 1; position > statement.getPosition(); position--) {
        logger.debug("Current delete position: " + position);
        if (test.getTestCase().getStatement(position) instanceof AssignmentStatement) {
            logger.debug("Is assignment statement");
            AssignmentStatement assignment = (AssignmentStatement) test.getTestCase().getStatement(position);
            Statement valueStatement = test.getTestCase().getStatement(assignment.getValue().getStPosition());
            if (assignment.getReturnValue().getAdditionalVariableReference() == arrRef) {
                int currentDelta = 0;
                int differenceDelta = 0;
                logger.debug("Assigns to target array. Checking if we can remove it without worsening fitness");
                backup(test);
                try {
                    factory.deleteStatement(test.getTestCase(), position);
                    if (valueStatement instanceof PrimitiveStatement || valueStatement instanceof NullStatement) {
                        if (!test.getTestCase().hasReferences(valueStatement.getReturnValue())) {
                            if (valueStatement.getPosition() < statement.getPosition())
                                differenceDelta = 1;
                            currentDelta = 1;
                            logger.debug("Deleting primitive statement assigned to this array at " + valueStatement.getPosition());
                            factory.deleteStatement(test.getTestCase(), valueStatement.getPosition());
                        }
                    }
                    if (!objective.hasNotWorsened(test)) {
                        logger.debug("Fitness has decreased, so restoring test");
                        restore(test);
                        currentDelta = 0;
                        differenceDelta = 0;
                    }
                } catch (ConstructionFailedException e) {
                    currentDelta = 0;
                    differenceDelta = 0;
                    restore(test);
                }
                position -= currentDelta;
                difference += differenceDelta;
            }
        }
    }
    return difference;
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestFactory(org.evosuite.testcase.TestFactory) Statement(org.evosuite.testcase.statements.Statement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 3 with ArrayStatement

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

the class ArrayLocalSearch method searchLength.

private boolean searchLength(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    boolean hasImproved = false;
    ArrayStatement p = (ArrayStatement) test.getTestCase().getStatement(statement);
    logger.debug("Performing local search on array length, starting with length {}", p.size());
    ExecutionResult oldResult = test.getLastExecutionResult();
    oldLength = p.size();
    boolean done = false;
    while (!done) {
        done = true;
        // Try +1
        p.setSize(oldLength + 1);
        logger.debug("Trying increment of {}", p.getCode());
        if (objective.hasImproved(test)) {
            done = false;
            hasImproved = true;
            boolean improved = true;
            while (improved) {
                oldLength = p.size();
                oldResult = test.getLastExecutionResult();
                p.setSize(oldLength + 1);
                logger.debug("Trying increment of {}", p.getCode());
                improved = objective.hasImproved(test);
            }
            p.setSize(oldLength);
            test.setLastExecutionResult(oldResult);
            test.setChanged(false);
        } else {
            if (oldLength > 0) {
                // Restore original, try -1
                p.setSize(oldLength);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
                p.setSize(oldLength - 1);
            } else {
                p.setSize(Properties.MAX_ARRAY);
            }
            logger.debug("Trying decrement of {}", p.getCode());
            if (objective.hasImproved(test)) {
                done = false;
                hasImproved = true;
                boolean improved = true;
                while (improved && p.size() > 0) {
                    oldLength = p.size();
                    oldResult = test.getLastExecutionResult();
                    p.setSize(oldLength - 1);
                    logger.debug("Trying decrement of {}", p.getCode());
                    improved = objective.hasImproved(test);
                }
                p.setSize(oldLength);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
            } else {
                p.setSize(oldLength);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
            }
        }
    }
    logger.debug("Finished local array length search with result {}", p.getCode());
    return hasImproved;
}
Also used : ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 4 with ArrayStatement

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

the class TestCaseBuilder method appendArrayStmt.

public ArrayReference appendArrayStmt(Type type, int... length) {
    ArrayStatement arrayStmt = new ArrayStatement(tc, type, length);
    tc.addStatement(arrayStmt);
    return (ArrayReference) arrayStmt.getReturnValue();
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement)

Example 5 with ArrayStatement

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

the class AbstractMOSA method removeUnusedVariables.

/**
 * When a test case is changed via crossover and/or mutation, it can contains some
 * primitive variables that are not used as input (or to store the output) of method calls.
 * Thus, this method removes all these "trash" statements.
 * @param chromosome
 * @return true or false depending on whether "unused variables" are removed
 */
public boolean removeUnusedVariables(T chromosome) {
    int sizeBefore = chromosome.size();
    TestCase t = ((TestChromosome) chromosome).getTestCase();
    List<Integer> to_delete = new ArrayList<Integer>(chromosome.size());
    boolean has_deleted = false;
    int num = 0;
    for (Statement s : t) {
        VariableReference var = s.getReturnValue();
        boolean delete = false;
        delete = delete || s instanceof PrimitiveStatement;
        delete = delete || s instanceof ArrayStatement;
        delete = delete || s instanceof StringPrimitiveStatement;
        if (!t.hasReferences(var) && delete) {
            to_delete.add(num);
            has_deleted = true;
        }
        num++;
    }
    Collections.sort(to_delete, Collections.reverseOrder());
    for (Integer position : to_delete) {
        t.remove(position);
    }
    int sizeAfter = chromosome.size();
    if (has_deleted)
        logger.debug("Removed {} unused statements", (sizeBefore - sizeAfter));
    return has_deleted;
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) TestCase(org.evosuite.testcase.TestCase) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) ArrayList(java.util.ArrayList) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)8 VariableReference (org.evosuite.testcase.variable.VariableReference)4 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)3 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)3 ArrayReference (org.evosuite.testcase.variable.ArrayReference)3 TestFactory (org.evosuite.testcase.TestFactory)2 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 NullStatement (org.evosuite.testcase.statements.NullStatement)2 Statement (org.evosuite.testcase.statements.Statement)2 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestCase (org.evosuite.testcase.TestCase)1 TestCaseExpander (org.evosuite.testcase.TestCaseExpander)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1