Search in sources :

Example 1 with EntityWithParametersStatement

use of org.evosuite.testcase.statements.EntityWithParametersStatement 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 2 with EntityWithParametersStatement

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

Example 3 with EntityWithParametersStatement

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

the class InputObserver method afterStatement.

/* (non-Javadoc)
     * @see org.evosuite.testcase.ExecutionObserver#afterStatement(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
     */
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    if (statement instanceof EntityWithParametersStatement) {
        EntityWithParametersStatement parameterisedStatement = (EntityWithParametersStatement) statement;
        List<VariableReference> parRefs = parameterisedStatement.getParameterReferences();
        List<Object> argObjects = new ArrayList<>(parRefs.size());
        for (VariableReference parRef : parRefs) {
            Object parObject = null;
            try {
                if (parRef instanceof ArrayIndex || parRef instanceof FieldReference) {
                    parObject = parRef.getObject(scope);
                } else if (parRef instanceof ConstantValue) {
                    parObject = ((ConstantValue) parRef).getValue();
                } else {
                    parObject = parRef.getObject(scope);
                }
            } catch (CodeUnderTestException e) {
                e.printStackTrace();
            }
            argObjects.add(parObject);
        }
        assert parRefs.size() == argObjects.size();
        String className = parameterisedStatement.getDeclaringClassName();
        String methodDesc = parameterisedStatement.getDescriptor();
        String methodName = parameterisedStatement.getMethodName();
        inputCoverage.put(statement.getPosition(), InputCoverageGoal.createCoveredGoalsFromParameters(className, methodName, methodDesc, argObjects));
    // argumentsValues.put((EntityWithParametersStatement) statement, argObjects);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ConstantValue(org.evosuite.testcase.variable.ConstantValue) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Aggregations

EntityWithParametersStatement (org.evosuite.testcase.statements.EntityWithParametersStatement)3 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 Statement (org.evosuite.testcase.statements.Statement)2 ArrayList (java.util.ArrayList)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)1 ConstantValue (org.evosuite.testcase.variable.ConstantValue)1 FieldReference (org.evosuite.testcase.variable.FieldReference)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1