use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class TestConstantInliner method testNumericArrayIndexInlining.
@Test
public void testNumericArrayIndexInlining() throws NoSuchMethodException, SecurityException {
DefaultTestCase test = new DefaultTestCase();
PrimitiveStatement<?> primitiveStatement = PrimitiveStatement.getPrimitiveStatement(test, int.class);
VariableReference intVar = test.addStatement(primitiveStatement);
ArrayStatement as = new ArrayStatement(test, int[].class, 3);
test.addStatement(as);
ArrayReference arrayVar = as.getArrayReference();
ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
test.addStatement(new AssignmentStatement(test, ai0, intVar));
test.addStatement(new AssignmentStatement(test, ai1, intVar));
test.addStatement(new AssignmentStatement(test, ai2, intVar));
ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(TrivialInt.class.getConstructor(), TrivialInt.class), new ArrayList<>());
VariableReference sut = test.addStatement(sutCS);
List<VariableReference> parameters = new ArrayList<VariableReference>();
parameters.add(ai0);
test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
parameters = new ArrayList<>();
parameters.add(ai1);
test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
parameters = new ArrayList<>();
parameters.add(ai2);
test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
System.out.println(test.toCode());
ConstantInliner inliner = new ConstantInliner();
inliner.inline(test);
String code = test.toCode();
System.out.println(test.toCode());
assertFalse(code.contains("trivialInt0.testMe(int"));
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class InputObserver method afterStatement.
/* (non-Javadoc)
* @see org.evosuite.testcase.ExecutionObserver#afterStatement(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
*/
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
if (statement instanceof EntityWithParametersStatement) {
EntityWithParametersStatement parameterisedStatement = (EntityWithParametersStatement) statement;
List<VariableReference> parRefs = parameterisedStatement.getParameterReferences();
List<Object> argObjects = new ArrayList<>(parRefs.size());
for (VariableReference parRef : parRefs) {
Object parObject = null;
try {
if (parRef instanceof ArrayIndex || parRef instanceof FieldReference) {
parObject = parRef.getObject(scope);
} else if (parRef instanceof ConstantValue) {
parObject = ((ConstantValue) parRef).getValue();
} else {
parObject = parRef.getObject(scope);
}
} catch (CodeUnderTestException e) {
e.printStackTrace();
}
argObjects.add(parObject);
}
assert parRefs.size() == argObjects.size();
String className = parameterisedStatement.getDeclaringClassName();
String methodDesc = parameterisedStatement.getDescriptor();
String methodName = parameterisedStatement.getMethodName();
inputCoverage.put(statement.getPosition(), InputCoverageGoal.createCoveredGoalsFromParameters(className, methodName, methodDesc, argObjects));
// argumentsValues.put((EntityWithParametersStatement) statement, argObjects);
}
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class ArrayStatement method isValid.
/* (non-Javadoc)
* @see org.evosuite.testcase.StatementInterface#isValid()
*/
/**
* {@inheritDoc}
*/
@Override
public boolean isValid() {
int maxAssignment = 0;
for (Statement statement : this.tc) {
for (VariableReference var : statement.getVariableReferences()) {
if (var.getAdditionalVariableReference() == this.retval) {
VariableReference currentVar = var;
while (currentVar instanceof FieldReference) {
currentVar = ((FieldReference) currentVar).getSource();
}
ArrayIndex index = (ArrayIndex) currentVar;
maxAssignment = Math.max(maxAssignment, index.getArrayIndex());
}
}
}
if (maxAssignment > lengths[0]) {
logger.warn("Max assignment = " + maxAssignment + ", length = " + lengths[0]);
return false;
}
return super.isValid();
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class ArrayStatement method mutate.
/* (non-Javadoc)
* @see org.evosuite.testcase.AbstractStatement#mutate(org.evosuite.testcase.TestCase, org.evosuite.testcase.AbstractTestFactory)
*/
/**
* {@inheritDoc}
*/
@Override
public boolean mutate(TestCase test, TestFactory factory) {
int maxAssignment = 0;
for (Statement statement : test) {
for (VariableReference var : statement.getVariableReferences()) {
if (var.getAdditionalVariableReference() == this.retval) {
VariableReference currentVar = var;
while (currentVar instanceof FieldReference) {
currentVar = ((FieldReference) currentVar).getSource();
}
if (!(currentVar instanceof ArrayIndex)) {
LoggingUtils.getEvoLogger().error("Found assignment to array without ArrayIndex:");
LoggingUtils.getEvoLogger().error(test.toCode());
LoggingUtils.getEvoLogger().error(statement.getPosition() + ", " + statement.getCode());
}
ArrayIndex index = (ArrayIndex) currentVar;
maxAssignment = Math.max(maxAssignment, index.getArrayIndex());
}
}
}
int dim = 0;
if (lengths.length > 1) {
dim = Randomness.nextInt(lengths.length - 1);
}
int newLength = lengths[dim];
while (newLength == lengths[dim]) {
if (Randomness.nextDouble() <= Properties.RANDOM_PERTURBATION) {
newLength = Randomness.nextInt(maxAssignment, Math.max(maxAssignment + 1, Properties.MAX_ARRAY)) + 1;
} else {
int max = Math.min(Math.abs(lengths[dim] - maxAssignment - 1), Properties.MAX_DELTA);
if (max > 0)
newLength = lengths[dim] + Randomness.nextInt(2 * max) - max;
else
newLength = lengths[dim] + Randomness.nextInt(Properties.MAX_DELTA);
}
}
// TODO: Need to make sure this doesn't happen by construction
if (newLength <= 0)
newLength = 1;
lengths[dim] = newLength;
((ArrayReference) retval).setLengths(lengths);
return true;
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class EntityWithParametersStatement method getUniqueVariableReferences.
@Override
public List<VariableReference> getUniqueVariableReferences() {
List<VariableReference> references = new ArrayList<>();
references.add(retval);
references.addAll(parameters);
for (VariableReference param : parameters) {
if (param instanceof ArrayIndex)
references.add(((ArrayIndex) param).getArray());
}
return references;
}
Aggregations