Search in sources :

Example 21 with Statement

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

Example 22 with Statement

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

the class AbstractMOSA method mutate.

/**
 * Method used to mutate an offspring
 */
private void mutate(T offspring, T parent) {
    offspring.mutate();
    TestChromosome tch = (TestChromosome) offspring;
    if (!offspring.isChanged()) {
        // if offspring is not changed, we try
        // to mutate it once again
        offspring.mutate();
    }
    if (!hasMethodCall(offspring)) {
        tch.setTestCase(((TestChromosome) parent).getTestCase().clone());
        boolean changed = tch.mutationInsert();
        if (changed) {
            for (Statement s : tch.getTestCase()) s.isValid();
        }
        offspring.setChanged(changed);
    }
    notifyMutation(offspring);
}
Also used : 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) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 23 with Statement

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

the class AbstractMOSA method hasMethodCall.

/**
 * This method checks whether the test has only primitive type statements. Indeed,
 * crossover and mutation can lead to tests with no method calls (methods or constructors call),
 * thus, when executed they will never cover something in the class under test.
 *
 * @param test to check
 * @return true if the test has at least one method or constructor call (i.e., the test may
 * cover something when executed; false otherwise
 */
private boolean hasMethodCall(T test) {
    boolean flag = false;
    TestCase tc = ((TestChromosome) test).getTestCase();
    for (Statement s : tc) {
        if (s instanceof MethodStatement) {
            MethodStatement ms = (MethodStatement) s;
            boolean isTargetMethod = ms.getDeclaringClassName().equals(Properties.TARGET_CLASS);
            if (isTargetMethod)
                return true;
        }
        if (s instanceof ConstructorStatement) {
            ConstructorStatement ms = (ConstructorStatement) s;
            boolean isTargetMethod = ms.getDeclaringClassName().equals(Properties.TARGET_CLASS);
            if (isTargetMethod)
                return true;
        }
    }
    return flag;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) 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) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 24 with Statement

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

the class MethodCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate fitness
 *
 * @param individual
 *            a {@link org.evosuite.testcase.ExecutableChromosome} object.
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    double fitness = 1.0;
    List<Integer> exceptionPositions = asSortedList(result.getPositionsWhereExceptionsWereThrown());
    for (Statement stmt : result.test) {
        if (!isValidPosition(exceptionPositions, stmt.getPosition())) {
            break;
        }
        if ((stmt instanceof MethodStatement || stmt instanceof ConstructorStatement)) {
            EntityWithParametersStatement ps = (EntityWithParametersStatement) stmt;
            String className = ps.getDeclaringClassName();
            String methodDesc = ps.getDescriptor();
            String methodName = ps.getMethodName() + methodDesc;
            if (this.className.equals(className) && this.methodName.equals(methodName)) {
                fitness = 0.0;
                break;
            }
        }
    }
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement)

Example 25 with Statement

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

the class MethodNoExceptionCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate fitness
 *
 * @param individual
 *            a {@link org.evosuite.testcase.ExecutableChromosome} object.
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    double fitness = 1.0;
    List<Integer> exceptionPositions = new ArrayList<Integer>();
    if (Properties.BREAK_ON_EXCEPTION) {
        // we consider only the first thrown exception
        if (!result.getPositionsWhereExceptionsWereThrown().isEmpty()) {
            int firstPosition = Collections.min(result.getPositionsWhereExceptionsWereThrown());
            exceptionPositions.add(firstPosition);
        }
    } else {
        // we consider all thrown exceptions (if any)
        exceptionPositions.addAll(result.getPositionsWhereExceptionsWereThrown());
    }
    for (Statement stmt : result.test) {
        if (exceptionPositions.contains(stmt.getPosition())) {
            if (Properties.BREAK_ON_EXCEPTION)
                // if we look at the first exception, then no need to iterate over the remaining statements
                break;
            else
                // otherwise we simple skip statements throwing an exception
                continue;
        }
        if ((stmt instanceof MethodStatement || stmt instanceof ConstructorStatement)) {
            EntityWithParametersStatement ps = (EntityWithParametersStatement) stmt;
            String className = ps.getDeclaringClassName();
            String methodDesc = ps.getDescriptor();
            String methodName = ps.getMethodName() + methodDesc;
            if (this.className.equals(className) && this.methodName.equals(methodName)) {
                fitness = 0.0;
                break;
            }
        }
    }
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ArrayList(java.util.ArrayList)

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