Search in sources :

Example 6 with FieldReference

use of org.evosuite.testcase.variable.FieldReference in project evosuite by EvoSuite.

the class ClassReInitializer method getMoreClassesToReset.

/*
	 * TODO: I think this would be nicer if each type of statement registered
	 * the classes to reset as part of their execute() method
	 */
private static HashSet<String> getMoreClassesToReset(TestCase tc, ExecutionResult result) {
    HashSet<String> moreClassesForStaticReset = new HashSet<String>();
    for (int position = 0; position < result.getExecutedStatements(); position++) {
        Statement statement = tc.getStatement(position);
        // If we reset also after reads, get all fields
        if (Properties.RESET_STATIC_FIELD_GETS) {
            for (VariableReference var : statement.getVariableReferences()) {
                if (var.isFieldReference()) {
                    FieldReference fieldReference = (FieldReference) var;
                    moreClassesForStaticReset.add(fieldReference.getField().getOwnerClass().getClassName());
                }
            }
        }
        // Check for explicit assignments to static fields
        if (statement.isAssignmentStatement()) {
            if (statement.getReturnValue() instanceof FieldReference) {
                FieldReference fieldReference = (FieldReference) statement.getReturnValue();
                if (fieldReference.getField().isStatic()) {
                    moreClassesForStaticReset.add(fieldReference.getField().getOwnerClass().getClassName());
                }
            }
        } else if (statement instanceof FieldStatement) {
            // Check if we are invoking a non-pure method on a static field
            // variable
            FieldStatement fieldStatement = (FieldStatement) statement;
            if (fieldStatement.getField().isStatic()) {
                VariableReference fieldReference = fieldStatement.getReturnValue();
                if (Properties.RESET_STATIC_FIELD_GETS) {
                    moreClassesForStaticReset.add(fieldStatement.getField().getOwnerClass().getClassName());
                } else {
                    // Check if the field was passed to a non-pure method
                    for (int i = fieldStatement.getPosition() + 1; i < result.getExecutedStatements(); i++) {
                        Statement invokedStatement = tc.getStatement(i);
                        if (invokedStatement.references(fieldReference)) {
                            if (invokedStatement instanceof MethodStatement) {
                                if (fieldReference.equals(((MethodStatement) invokedStatement).getCallee())) {
                                    if (!CheapPurityAnalyzer.getInstance().isPure(((MethodStatement) invokedStatement).getMethod().getMethod())) {
                                        moreClassesForStaticReset.add(fieldStatement.getField().getOwnerClass().getClassName());
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if (statement instanceof PrivateFieldStatement) {
            PrivateFieldStatement fieldStatement = (PrivateFieldStatement) statement;
            if (fieldStatement.isStaticField()) {
                moreClassesForStaticReset.add(fieldStatement.getOwnerClassName());
            }
        }
    }
    return moreClassesForStaticReset;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) HashSet(java.util.HashSet)

Example 7 with FieldReference

use of org.evosuite.testcase.variable.FieldReference 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)

Example 8 with FieldReference

use of org.evosuite.testcase.variable.FieldReference in project evosuite by EvoSuite.

the class ArrayStatement method isValid.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.StatementInterface#isValid()
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean isValid() {
    int maxAssignment = 0;
    for (Statement statement : this.tc) {
        for (VariableReference var : statement.getVariableReferences()) {
            if (var.getAdditionalVariableReference() == this.retval) {
                VariableReference currentVar = var;
                while (currentVar instanceof FieldReference) {
                    currentVar = ((FieldReference) currentVar).getSource();
                }
                ArrayIndex index = (ArrayIndex) currentVar;
                maxAssignment = Math.max(maxAssignment, index.getArrayIndex());
            }
        }
    }
    if (maxAssignment > lengths[0]) {
        logger.warn("Max assignment = " + maxAssignment + ", length = " + lengths[0]);
        return false;
    }
    return super.isValid();
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 9 with FieldReference

use of org.evosuite.testcase.variable.FieldReference in project evosuite by EvoSuite.

the class ArrayStatement method mutate.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.AbstractStatement#mutate(org.evosuite.testcase.TestCase, org.evosuite.testcase.AbstractTestFactory)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean mutate(TestCase test, TestFactory factory) {
    int maxAssignment = 0;
    for (Statement statement : test) {
        for (VariableReference var : statement.getVariableReferences()) {
            if (var.getAdditionalVariableReference() == this.retval) {
                VariableReference currentVar = var;
                while (currentVar instanceof FieldReference) {
                    currentVar = ((FieldReference) currentVar).getSource();
                }
                if (!(currentVar instanceof ArrayIndex)) {
                    LoggingUtils.getEvoLogger().error("Found assignment to array without ArrayIndex:");
                    LoggingUtils.getEvoLogger().error(test.toCode());
                    LoggingUtils.getEvoLogger().error(statement.getPosition() + ", " + statement.getCode());
                }
                ArrayIndex index = (ArrayIndex) currentVar;
                maxAssignment = Math.max(maxAssignment, index.getArrayIndex());
            }
        }
    }
    int dim = 0;
    if (lengths.length > 1) {
        dim = Randomness.nextInt(lengths.length - 1);
    }
    int newLength = lengths[dim];
    while (newLength == lengths[dim]) {
        if (Randomness.nextDouble() <= Properties.RANDOM_PERTURBATION) {
            newLength = Randomness.nextInt(maxAssignment, Math.max(maxAssignment + 1, Properties.MAX_ARRAY)) + 1;
        } else {
            int max = Math.min(Math.abs(lengths[dim] - maxAssignment - 1), Properties.MAX_DELTA);
            if (max > 0)
                newLength = lengths[dim] + Randomness.nextInt(2 * max) - max;
            else
                newLength = lengths[dim] + Randomness.nextInt(Properties.MAX_DELTA);
        }
    }
    // TODO: Need to make sure this doesn't happen by construction
    if (newLength <= 0)
        newLength = 1;
    lengths[dim] = newLength;
    ((ArrayReference) retval).setLengths(lengths);
    return true;
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 10 with FieldReference

use of org.evosuite.testcase.variable.FieldReference in project evosuite by EvoSuite.

the class TestCaseBuilder method appendAssignment.

public void appendAssignment(VariableReference receiver, Field field, VariableReference value) {
    FieldReference fieldReference;
    if (receiver == null) {
        fieldReference = new FieldReference(tc, new GenericField(field, field.getDeclaringClass()));
    } else {
        fieldReference = new FieldReference(tc, new GenericField(field, receiver.getType()), receiver);
    }
    AssignmentStatement stmt = new AssignmentStatement(tc, fieldReference, value);
    tc.addStatement(stmt);
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Aggregations

FieldReference (org.evosuite.testcase.variable.FieldReference)12 VariableReference (org.evosuite.testcase.variable.VariableReference)11 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)7 GenericField (org.evosuite.utils.generic.GenericField)6 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)4 MethodStatement (org.evosuite.testcase.statements.MethodStatement)3 Test (org.junit.Test)3 DowncastExample (com.examples.with.different.packagename.test.DowncastExample)2 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)2 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 ClassWithField (com.examples.with.different.packagename.coverage.ClassWithField)1 AbstractSuperclass (com.examples.with.different.packagename.test.AbstractSuperclass)1 ConcreteSubclass (com.examples.with.different.packagename.test.ConcreteSubclass)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 EntityWithParametersStatement (org.evosuite.testcase.statements.EntityWithParametersStatement)1