use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class ReferenceLocalSearch method changeParameters.
/**
* Switch parameter/callee variables with other available objects
*
* @param test
* @param statement
* @return
*/
private boolean changeParameters(TestChromosome test, int statement) {
logger.debug("Changing parameters");
Statement stmt = test.getTestCase().getStatement(statement);
if (stmt instanceof MethodStatement) {
return replaceMethodParameter(test, (MethodStatement) stmt);
} else if (stmt instanceof ConstructorStatement) {
return replaceConstructorParameter(test, (ConstructorStatement) stmt);
} else if (stmt instanceof FieldStatement) {
return replaceFieldSource(test, (FieldStatement) stmt);
} else {
return false;
}
}
use of org.evosuite.testcase.statements.Statement 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;
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class AVMTestCaseLocalSearch method doSearch.
@Override
public boolean doSearch(TestChromosome individual, LocalSearchObjective<TestChromosome> objective) {
logger.info("Test before local search: " + individual.getTestCase().toCode());
boolean improved = false;
// Only apply local search up to the point where an exception was thrown
// TODO: Check whether this conflicts with test expansion
int lastPosition = individual.size() - 1;
if (individual.getLastExecutionResult() != null && !individual.isChanged()) {
Integer lastPos = individual.getLastExecutionResult().getFirstPositionOfThrownException();
if (lastPos != null)
lastPosition = lastPos.intValue();
}
TestCase test = individual.getTestCase();
for (int i = lastPosition; i >= 0; i--) {
if (LocalSearchBudget.getInstance().isFinished())
break;
if (objective.isDone()) {
break;
}
if (i >= individual.size()) {
logger.warn("Test size decreased unexpectedly during local search, aborting local search");
logger.warn(individual.getTestCase().toCode());
break;
}
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
final Statement statement = test.getStatement(i);
if (!test.hasReferences(statement.getReturnValue()) && !statement.getReturnClass().equals(targetClass)) {
logger.info("Return value of statement " + i + " is not referenced and not SUT, not doing local search");
continue;
}
StatementLocalSearch search = StatementLocalSearch.getLocalSearchFor(statement);
if (search != null) {
logger.info("Applying local search of type " + search.getClass() + " to statement " + statement + " / " + individual.getTestCase().getStatement(i));
if (search.doSearch(individual, i, (LocalSearchObjective<TestChromosome>) objective)) {
improved = true;
}
// i = s.getPosition();
logger.debug("Old position was: " + i + ", adjusting to: " + (i + search.getPositionDelta()));
i += search.getPositionDelta();
test = individual.getTestCase();
} else {
/*
* No statement local search has been produced for this
* statement. Skipping.
*/
continue;
}
}
LocalSearchBudget.getInstance().countLocalSearchOnTest();
// Return true iif search was successful
return improved;
// TODO: Handle arrays in local search
// TODO: mutating an int might have an effect on array lengths
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassList.
@Test
public void testGenericClassList() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithListTest.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithList.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertTrue(factory.hasCarvedTestCases());
TestChromosome carved = factory.getChromosome();
Assert.assertNotNull(carved);
Assert.assertEquals("", 10, carved.test.size());
for (int i = 0; i < carved.test.size(); i++) {
Statement stmt = carved.test.getStatement(i);
boolean valid = stmt.isValid();
Assert.assertTrue("Invalid stmt at position " + i, valid);
}
String code = carved.toString();
String setLong = "GenericObjectWrapperWithList<GenericObjectWrapperWithListTest.Foo>";
Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassSet.
@Test
public void testGenericClassSet() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperSetTest.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapper.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertTrue(factory.hasCarvedTestCases());
TestChromosome carved = factory.getChromosome();
Assert.assertNotNull(carved);
Assert.assertEquals("", 13, carved.test.size());
for (int i = 0; i < carved.test.size(); i++) {
Statement stmt = carved.test.getStatement(i);
boolean valid = stmt.isValid();
Assert.assertTrue("Invalid stmt at position " + i, valid);
}
String code = carved.toString();
String setLong = "GenericObjectWrapper<HashSet<Long>>";
Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
code = carved.toString();
setLong = "(Object)";
Assert.assertFalse("generated code contains object cast " + setLong + "\n" + code, code.contains(setLong));
}
Aggregations