use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method getArguments.
private ArrayList<VariableReference> getArguments(final Object[] methodArgs, final Class<?>[] methodParamTypeClasses) throws IllegalArgumentException {
ArrayList<VariableReference> args = new ArrayList<VariableReference>();
// is either an oid or null
Integer argOID;
for (int i = 0; i < methodArgs.length; i++) {
argOID = (Integer) methodArgs[i];
if (argOID == null) {
args.add(testCase.addStatement(new NullStatement(testCase, methodParamTypeClasses[i])));
} else {
VariableReference ref = this.oidToVarRefMap.get(argOID);
if (ref == null) {
throw new RuntimeException("VariableReference is null for argOID " + argOID + "; have oids: " + this.oidToVarRefMap.keySet());
} else {
args.add(ref);
}
}
}
return args;
}
use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createFieldWriteAccessStmt.
@Override
public void createFieldWriteAccessStmt(CaptureLog log, int logRecNo) {
// assumption: all necessary statements are created and there is one variable for each referenced object
final Object[] methodArgs = log.params.get(logRecNo);
final int oid = log.objectIds.get(logRecNo);
final int captureId = log.captureIds.get(logRecNo);
final String fieldName = log.getNameOfAccessedFields(captureId);
final String typeName = log.getTypeName(oid);
try {
final Class<?> type = getClassForName(typeName);
final String fieldDesc = log.descList.get(logRecNo);
final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type), this.oidToVarRefMap.get(oid));
final AssignmentStatement assignment;
final Integer arg = (Integer) methodArgs[0];
if (arg == null) {
final NullStatement nullStmt = new NullStatement(testCase, fieldType);
final VariableReference nullReference = testCase.addStatement(nullStmt);
assignment = new AssignmentStatement(testCase, targetFieldRef, nullReference);
} else {
assignment = new AssignmentStatement(testCase, targetFieldRef, this.oidToVarRefMap.get(arg));
}
final VariableReference varRef = testCase.addStatement(assignment);
logger.debug("Adding assignment statement: " + assignment.getCode());
if (arg != null) {
this.oidToVarRefMap.put(arg, varRef);
}
} catch (final Exception e) {
CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating field write access stmt. Log: %s", logRecNo, log);
}
}
use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createMapInitStmt.
@Override
public void createMapInitStmt(final CaptureLog log, final int logRecNo) {
try {
final int oid = log.objectIds.get(logRecNo);
final Object[] params = log.params.get(logRecNo);
String collTypeName = log.getTypeName(oid);
Class<?> collType = getClassForName(collTypeName);
// -- determine if an alternative collection must be used for code generation
final boolean isPublic = java.lang.reflect.Modifier.isPublic(collType.getModifiers());
if (!isPublic || !hasDefaultConstructor(collType)) {
collType = HashMap.class;
}
// -- create code for instantiating collection
final List<VariableReference> noParams = Collections.emptyList();
final ConstructorStatement constrStmt = new ConstructorStatement(testCase, new GenericConstructor(collType.getConstructor(new Class<?>[0]), collType), noParams);
final VariableReference collRef = testCase.addStatement(constrStmt);
this.oidToVarRefMap.put(oid, collRef);
// --- fill collection
MethodStatement methodStmt;
// is either an oid or null
Integer argOID;
ArrayList<VariableReference> paramList = new ArrayList<VariableReference>();
for (int i = 0; i < params.length; i++) {
argOID = (Integer) params[i];
if (argOID == null) {
paramList.add(testCase.addStatement(new NullStatement(testCase, Object.class)));
} else {
paramList.add(this.oidToVarRefMap.get(argOID));
}
if (i % 2 == 1) {
final Method method = collType.getMethod("put", Object.class, Object.class);
replaceNullWithNullReferences(paramList, Object.class, Object.class);
methodStmt = new MethodStatement(testCase, new GenericMethod(method, collType), collRef, paramList);
testCase.addStatement(methodStmt);
paramList = new ArrayList<VariableReference>(2);
}
}
} catch (final Exception e) {
CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating map init stmt", logRecNo);
}
}
use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class ArrayLocalSearch method stripAssignments.
private int stripAssignments(ArrayStatement statement, TestChromosome test, LocalSearchObjective<TestChromosome> objective) {
int difference = 0;
ArrayReference arrRef = (ArrayReference) statement.getReturnValue();
TestFactory factory = TestFactory.getInstance();
for (int position = test.size() - 1; position > statement.getPosition(); position--) {
logger.debug("Current delete position: " + position);
if (test.getTestCase().getStatement(position) instanceof AssignmentStatement) {
logger.debug("Is assignment statement");
AssignmentStatement assignment = (AssignmentStatement) test.getTestCase().getStatement(position);
Statement valueStatement = test.getTestCase().getStatement(assignment.getValue().getStPosition());
if (assignment.getReturnValue().getAdditionalVariableReference() == arrRef) {
int currentDelta = 0;
int differenceDelta = 0;
logger.debug("Assigns to target array. Checking if we can remove it without worsening fitness");
backup(test);
try {
factory.deleteStatement(test.getTestCase(), position);
if (valueStatement instanceof PrimitiveStatement || valueStatement instanceof NullStatement) {
if (!test.getTestCase().hasReferences(valueStatement.getReturnValue())) {
if (valueStatement.getPosition() < statement.getPosition())
differenceDelta = 1;
currentDelta = 1;
logger.debug("Deleting primitive statement assigned to this array at " + valueStatement.getPosition());
factory.deleteStatement(test.getTestCase(), valueStatement.getPosition());
}
}
if (!objective.hasNotWorsened(test)) {
logger.debug("Fitness has decreased, so restoring test");
restore(test);
currentDelta = 0;
differenceDelta = 0;
}
} catch (ConstructionFailedException e) {
currentDelta = 0;
differenceDelta = 0;
restore(test);
}
position -= currentDelta;
difference += differenceDelta;
}
}
}
return difference;
}
use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class ReferenceLocalSearch method replace.
/**
* Replace the call with a completely different call
*
* @param test
* @param statement
* @return
*/
private boolean replace(TestChromosome test, int statement) {
logger.debug("Replacing call");
TestFactory factory = TestFactory.getInstance();
Statement theStatement = test.getTestCase().getStatement(statement);
VariableReference var = theStatement.getReturnValue();
int oldLength = test.size();
try {
VariableReference replacement = null;
if (Randomness.nextDouble() < Properties.NULL_PROBABILITY) {
NullStatement nullStatement = new NullStatement(test.getTestCase(), var.getType());
replacement = test.getTestCase().addStatement(nullStatement, statement);
} else if (!var.isPrimitive()) {
// Test cluster does not keep track of generators for primitives
replacement = factory.createObject(test.getTestCase(), var.getType(), statement, 0, null);
}
if (replacement != null) {
int oldStatement = statement + (test.size() - oldLength);
for (int i = oldStatement + 1; i < test.size(); i++) {
test.getTestCase().getStatement(i).replace(var, replacement);
}
factory.deleteStatement(test.getTestCase(), oldStatement);
test.setChanged(true);
}
} catch (ConstructionFailedException e) {
if (test.size() < oldLength) {
restore(test);
}
test.setChanged(test.size() != oldLength);
}
return false;
}
Aggregations