use of org.evosuite.testcase.statements.ArrayStatement in project evosuite by EvoSuite.
the class ArrayLocalSearch method doSearch.
/* (non-Javadoc)
* @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
*/
/**
* {@inheritDoc}
*/
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
boolean hasImproved = false;
ArrayStatement p = (ArrayStatement) test.getTestCase().getStatement(statement);
logger.debug("Doing array local search on statement " + statement + ": " + test.getTestCase().toCode());
int difference = stripAssignments(p, test, objective);
logger.debug("Adjusting position from " + statement + " to " + (statement - difference) + ": " + test.getTestCase().toCode());
positionDelta = difference;
statement = statement - difference;
p = (ArrayStatement) test.getTestCase().getStatement(statement);
hasImproved = searchLength(test, statement, objective);
TestCaseExpander expander = new TestCaseExpander();
int lengthWithoutAssignments = test.size();
p = (ArrayStatement) test.getTestCase().getStatement(statement);
expander.visitArrayStatement(test.getTestCase(), p);
int assignmentLength = test.size() - lengthWithoutAssignments;
for (int position = statement + 1; position < statement + assignmentLength; position++) {
logger.debug("Local search on statement " + position);
StatementLocalSearch search = StatementLocalSearch.getLocalSearchFor(test.getTestCase().getStatement(position));
if (search != null) {
if (search.doSearch(test, position, objective)) {
hasImproved = true;
}
}
}
logger.debug("Finished local search with result {}", p.getCode());
return hasImproved;
}
use of org.evosuite.testcase.statements.ArrayStatement 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.ArrayStatement in project evosuite by EvoSuite.
the class ArrayLocalSearch method searchLength.
private boolean searchLength(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
boolean hasImproved = false;
ArrayStatement p = (ArrayStatement) test.getTestCase().getStatement(statement);
logger.debug("Performing local search on array length, starting with length {}", p.size());
ExecutionResult oldResult = test.getLastExecutionResult();
oldLength = p.size();
boolean done = false;
while (!done) {
done = true;
// Try +1
p.setSize(oldLength + 1);
logger.debug("Trying increment of {}", p.getCode());
if (objective.hasImproved(test)) {
done = false;
hasImproved = true;
boolean improved = true;
while (improved) {
oldLength = p.size();
oldResult = test.getLastExecutionResult();
p.setSize(oldLength + 1);
logger.debug("Trying increment of {}", p.getCode());
improved = objective.hasImproved(test);
}
p.setSize(oldLength);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
} else {
if (oldLength > 0) {
// Restore original, try -1
p.setSize(oldLength);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
p.setSize(oldLength - 1);
} else {
p.setSize(Properties.MAX_ARRAY);
}
logger.debug("Trying decrement of {}", p.getCode());
if (objective.hasImproved(test)) {
done = false;
hasImproved = true;
boolean improved = true;
while (improved && p.size() > 0) {
oldLength = p.size();
oldResult = test.getLastExecutionResult();
p.setSize(oldLength - 1);
logger.debug("Trying decrement of {}", p.getCode());
improved = objective.hasImproved(test);
}
p.setSize(oldLength);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
} else {
p.setSize(oldLength);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
}
}
}
logger.debug("Finished local array length search with result {}", p.getCode());
return hasImproved;
}
use of org.evosuite.testcase.statements.ArrayStatement in project evosuite by EvoSuite.
the class TestCaseBuilder method appendArrayStmt.
public ArrayReference appendArrayStmt(Type type, int... length) {
ArrayStatement arrayStmt = new ArrayStatement(tc, type, length);
tc.addStatement(arrayStmt);
return (ArrayReference) arrayStmt.getReturnValue();
}
use of org.evosuite.testcase.statements.ArrayStatement in project evosuite by EvoSuite.
the class AbstractMOSA method removeUnusedVariables.
/**
* When a test case is changed via crossover and/or mutation, it can contains some
* primitive variables that are not used as input (or to store the output) of method calls.
* Thus, this method removes all these "trash" statements.
* @param chromosome
* @return true or false depending on whether "unused variables" are removed
*/
public boolean removeUnusedVariables(T chromosome) {
int sizeBefore = chromosome.size();
TestCase t = ((TestChromosome) chromosome).getTestCase();
List<Integer> to_delete = new ArrayList<Integer>(chromosome.size());
boolean has_deleted = false;
int num = 0;
for (Statement s : t) {
VariableReference var = s.getReturnValue();
boolean delete = false;
delete = delete || s instanceof PrimitiveStatement;
delete = delete || s instanceof ArrayStatement;
delete = delete || s instanceof StringPrimitiveStatement;
if (!t.hasReferences(var) && delete) {
to_delete.add(num);
has_deleted = true;
}
num++;
}
Collections.sort(to_delete, Collections.reverseOrder());
for (Integer position : to_delete) {
t.remove(position);
}
int sizeAfter = chromosome.size();
if (has_deleted)
logger.debug("Removed {} unused statements", (sizeBefore - sizeAfter));
return has_deleted;
}
Aggregations