use of org.evosuite.testcase.statements.NullStatement in project evosuite by EvoSuite.
the class ReferenceLocalSearch method replaceMethodParameter.
/**
* Go through parameters of method call and apply local search
*
* @param test
* @param statement
*/
private boolean replaceMethodParameter(TestChromosome test, MethodStatement statement) {
List<VariableReference> parameters = statement.getParameterReferences();
if (parameters.isEmpty())
return false;
int max = parameters.size();
if (!statement.isStatic()) {
max++;
}
int numParameter = Randomness.nextInt(max);
if (numParameter == parameters.size()) {
// replace callee
VariableReference callee = statement.getCallee();
List<VariableReference> objects = test.getTestCase().getObjects(callee.getType(), statement.getPosition());
objects.remove(callee);
if (objects.isEmpty())
return false;
VariableReference replacement = Randomness.choice(objects);
statement.setCallee(replacement);
test.setChanged(true);
} else {
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 ReferenceLocalSearch 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) {
boolean hasImproved = false;
int currentProbe = 0;
backup(test);
int oldLength = test.size();
while (currentProbe < Properties.LOCAL_SEARCH_PROBES && !LocalSearchBudget.getInstance().isFinished()) {
logger.info("Current probe on statement " + statement + ": " + currentProbe);
List<Mutations> mutations = new ArrayList<Mutations>();
mutations.add(Mutations.REPLACE);
Statement st = test.getTestCase().getStatement(statement);
if (!st.getReturnValue().isPrimitive() && !(st instanceof NullStatement)) {
mutations.add(Mutations.CALL);
}
if (st.getNumParameters() > 0) {
mutations.add(Mutations.PARAMETER);
} else {
mutations.remove(Mutations.PARAMETER);
}
int delta = 0;
Mutations m = Randomness.choice(mutations);
switch(m) {
case REPLACE:
replace(test, statement);
if (test.size() > oldLength)
delta = test.size() - oldLength;
break;
case PARAMETER:
changeParameters(test, statement);
break;
case CALL:
addCall(test, statement);
break;
}
if (test.isChanged()) {
logger.info("Is changed");
// logger.info("Test after mutation: " + test.getTestCase().toCode());
if (objective.hasImproved(test)) {
logger.info("Fitness has improved, keeping");
currentProbe = 0;
hasImproved = true;
backup(test);
statement += delta;
positionDelta += delta;
oldLength = test.size();
} else {
logger.info("Fitness has not improved, reverting");
currentProbe++;
restore(test);
}
} else {
logger.info("Is not changed");
currentProbe++;
}
}
return hasImproved;
}
Aggregations