use of org.evosuite.testcase.TestFactory 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.TestFactory in project evosuite by EvoSuite.
the class FixedLengthTestChromosomeFactory method getRandomTestCase.
/**
* Create a random individual
*
* @param size
*/
private TestCase getRandomTestCase(int size) {
TestCase test = new DefaultTestCase();
int num = 0;
TestFactory testFactory = TestFactory.getInstance();
// Then add random stuff
while (test.size() < size && num < Properties.MAX_ATTEMPTS) {
testFactory.insertRandomStatement(test, test.size() - 1);
num++;
}
return test;
}
use of org.evosuite.testcase.TestFactory 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;
}
use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.
the class RandomLengthTestFactory method getRandomTestCase.
/**
* Create a random individual
*
* @param size
*/
private TestCase getRandomTestCase(int size) {
boolean tracerEnabled = ExecutionTracer.isEnabled();
if (tracerEnabled)
ExecutionTracer.disable();
TestCase test = getNewTestCase();
int num = 0;
// Choose a random length in 0 - size
int length = Randomness.nextInt(size);
while (length == 0) length = Randomness.nextInt(size);
TestFactory testFactory = TestFactory.getInstance();
// Then add random stuff
while (test.size() < length && num < Properties.MAX_ATTEMPTS) {
testFactory.insertRandomStatement(test, test.size() - 1);
num++;
}
if (logger.isDebugEnabled())
logger.debug("Randomized test case:" + test.toCode());
if (tracerEnabled)
ExecutionTracer.enable();
return test;
}
use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.
the class LocalSearchArraySystemTest method getArrayTest.
private TestCase getArrayTest(int length) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
VariableReference arrayVar = test.addStatement(new ArrayStatement(test, int[].class, length));
for (int i = 0; i < length; i++) {
// Add value
VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, 0));
test.addStatement(new AssignmentStatement(test, new ArrayIndex(test, (ArrayReference) arrayVar, i), intVar));
}
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int[].class });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { arrayVar }));
test.addStatement(ms);
return test;
}
Aggregations