use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class ReferenceLocalSearch method replaceConstructorParameter.
/**
* Go through parameters of constructor call and apply local search
*
* @param test
* @param statement
*/
private boolean replaceConstructorParameter(TestChromosome test, ConstructorStatement statement) {
List<VariableReference> parameters = statement.getParameterReferences();
if (parameters.isEmpty())
return false;
int numParameter = Randomness.nextInt(parameters.size());
VariableReference parameter = parameters.get(numParameter);
List<VariableReference> objects = test.getTestCase().getObjects(parameter.getType(), statement.getPosition());
objects.remove(parameter);
objects.remove(statement.getReturnValue());
NullStatement nullStatement = new NullStatement(test.getTestCase(), parameter.getType());
if (!parameter.isPrimitive())
objects.add(nullStatement.getReturnValue());
if (objects.isEmpty())
return false;
VariableReference replacement = Randomness.choice(objects);
if (replacement == nullStatement.getReturnValue()) {
test.getTestCase().addStatement(nullStatement, statement.getPosition());
}
statement.replaceParameterReference(replacement, numParameter);
test.setChanged(true);
return false;
}
use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class TestCaseBuilder method appendNull.
public VariableReference appendNull(Type type) {
NullStatement stmt = new NullStatement(tc, type);
tc.addStatement(stmt);
return stmt.getReturnValue();
}
use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class NullReferenceSearch method doSearch.
/* (non-Javadoc)
* @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
*/
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
NullStatement nullStatement = (NullStatement) test.getTestCase().getStatement(statement);
TestCase newTest = test.getTestCase();
TestCase oldTest = newTest.clone();
ExecutionResult oldResult = test.getLastExecutionResult();
// double oldFitness = test.getFitness();
Map<FitnessFunction<?>, Double> oldFitnesses = test.getFitnessValues();
Map<FitnessFunction<?>, Double> oldLastFitnesses = test.getPreviousFitnessValues();
try {
TestFactory.getInstance().attemptGeneration(newTest, nullStatement.getReturnType(), statement);
if (!objective.hasImproved(test)) {
test.setTestCase(oldTest);
test.setLastExecutionResult(oldResult);
// test.setFitness(oldFitness);
test.setFitnessValues(oldFitnesses);
test.setPreviousFitnessValues(oldLastFitnesses);
} else {
return true;
}
} catch (ConstructionFailedException e) {
// If we can't construct it, then ignore
}
return false;
}
use of org.evosuite.testcase.statements.NullStatement 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());
}
}
use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createCollectionInitStmt.
@Override
public void createCollectionInitStmt(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)) {
if (Set.class.isAssignableFrom(collType)) {
collTypeName = HashSet.class.getName();
collType = HashSet.class;
} else if (List.class.isAssignableFrom(collType)) {
collTypeName = ArrayList.class.getName();
collType = ArrayList.class;
} else if (Queue.class.isAssignableFrom(collType)) {
collTypeName = ArrayDeque.class.getName();
collType = ArrayDeque.class;
} else {
CodeGeneratorException.propagateError("[logRecNo = %s] - collection %s is not supported", logRecNo, collType);
}
}
// -- 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;
Method method;
for (int i = 0; i < params.length; i++) {
paramList = new ArrayList<VariableReference>(1);
argOID = (Integer) params[i];
if (argOID == null || !this.oidToVarRefMap.containsKey(argOID)) {
VariableReference var = testCase.addStatement(new NullStatement(testCase, Object.class));
paramList.add(var);
} else {
VariableReference var = this.oidToVarRefMap.get(argOID);
paramList.add(var);
}
method = collType.getMethod("add", Object.class);
methodStmt = new MethodStatement(testCase, new GenericMethod(method, collType), collRef, paramList);
testCase.addStatement(methodStmt);
}
} catch (final Exception e) {
CodeGeneratorException.propagateError("[logRecNo = %s] - an unexpected error occurred while creating collection init stmt", logRecNo, e);
}
}
Aggregations