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;
}
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);
}
}
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();
}
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;
}
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);
}
Aggregations