use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.
the class ContractViolation method same.
/**
* Determine if we have already seen an instance of this violation
*
* @param other
* a {@link org.evosuite.contracts.ContractViolation} object.
* @return a boolean.
*/
public boolean same(ContractViolation other) {
// Same contract?
if (!contract.getClass().equals(other.contract.getClass()))
return false;
// Same type of statement?
if (!statement.getClass().equals(other.statement.getClass()))
return false;
// Same exception type?
if (exception != null && other.exception != null) {
if (!exception.getClass().equals(other.exception.getClass()))
return false;
}
// Same method call / constructor?
if (statement instanceof MethodStatement) {
MethodStatement ms1 = (MethodStatement) statement;
MethodStatement ms2 = (MethodStatement) other.statement;
if (ms1.getMethod().getMethod().equals(ms2.getMethod().getMethod())) {
return true;
}
} else if (statement instanceof ConstructorStatement) {
ConstructorStatement ms1 = (ConstructorStatement) statement;
ConstructorStatement ms2 = (ConstructorStatement) other.statement;
if (ms1.getConstructor().getConstructor().equals(ms2.getConstructor().getConstructor())) {
return true;
}
} else if (statement instanceof AssignmentStatement) {
VariableReference var1 = statement.getReturnValue();
VariableReference var2 = other.statement.getReturnValue();
if (var1 instanceof FieldReference && var2 instanceof FieldReference) {
if (((FieldReference) var1).getField().getField().equals(((FieldReference) var2).getField().getField()))
return true;
}
}
return false;
}
use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.
the class DowncastTest method testFieldReferenceDoesNotNeedDowncast.
@Test
public void testFieldReferenceDoesNotNeedDowncast() throws NoSuchMethodException, NoSuchFieldException {
TestCaseBuilder builder = new TestCaseBuilder();
VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor());
VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getAbstractFoo"));
// This would be set during execution
num0.setType(ConcreteSubclass.class);
VariableReference bool0 = builder.appendBooleanPrimitive(true);
DefaultTestCase test = builder.getDefaultTestCase();
FieldReference fr = new FieldReference(test, new GenericField(AbstractSuperclass.class.getField("fieldInAbstractClass"), AbstractSuperclass.class), num0);
AssignmentStatement statement = new AssignmentStatement(test, fr, bool0);
test.addStatement(statement);
test.removeDownCasts();
System.out.println(test);
FieldReference fr2 = (FieldReference) test.getStatement(3).getReturnValue();
assertEquals(AbstractSuperclass.class, fr2.getSource().getVariableClass());
}
use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createArrayInitStmt.
@Override
public void createArrayInitStmt(final CaptureLog log, final int logRecNo) {
final int oid = log.objectIds.get(logRecNo);
final Object[] params = log.params.get(logRecNo);
final String arrTypeName = log.getTypeName(oid);
final Class<?> arrType = getClassForName(arrTypeName);
// --- create array instance creation e.g. int[] var = new int[10];
final ArrayReference arrRef;
// create array only once
if (this.oidToVarRefMap.containsKey(oid)) {
arrRef = (ArrayReference) this.oidToVarRefMap.get(oid);
} else {
arrRef = new ArrayReference(testCase, arrType);
final ArrayStatement arrStmt = new ArrayStatement(testCase, arrRef);
arrStmt.setSize(params.length);
testCase.addStatement(arrStmt);
this.oidToVarRefMap.put(oid, arrRef);
}
final Class<?> arrCompClass = arrType.getComponentType();
AssignmentStatement assignStmt;
ArrayIndex arrIndex;
VariableReference valueRef;
// is either an oid or null
Integer argOID;
for (int i = 0; i < params.length; i++) {
argOID = (Integer) params[i];
if (argOID == null) {
valueRef = testCase.addStatement(new NullStatement(testCase, arrCompClass));
} else {
valueRef = this.oidToVarRefMap.get(argOID);
if (valueRef == null) {
logger.info("ValueREF is NULL for " + argOID);
continue;
}
}
arrIndex = new ArrayIndex(testCase, arrRef, i);
assignStmt = new AssignmentStatement(testCase, arrIndex, valueRef);
testCase.addStatement(assignStmt);
logger.debug("Adding assignment (array): " + assignStmt.getCode());
}
}
Aggregations