use of org.evosuite.testcase.statements.ArrayStatement 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;
}
use of org.evosuite.testcase.statements.ArrayStatement in project evosuite by EvoSuite.
the class SameTraceObserver method visit.
/* (non-Javadoc)
* @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
*/
/**
* {@inheritDoc}
*/
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
// TODO: Only MethodStatement?
if (statement.isAssignmentStatement())
return;
if (statement instanceof PrimitiveStatement<?>)
return;
if (statement instanceof ArrayStatement)
return;
if (statement instanceof ConstructorStatement)
return;
try {
Object object = var.getObject(scope);
if (object == null)
return;
if (var.isPrimitive())
return;
if (var.isString() && Properties.INLINE)
// After inlining the value of assertions would be different
return;
SameTraceEntry entry = new SameTraceEntry(var);
for (VariableReference other : scope.getElements(var.getType())) {
if (other == var)
continue;
if (other.isPrimitive())
continue;
if (other.isWrapperType())
// Issues with inlining resulting in unstable assertions
continue;
Object otherObject = other.getObject(scope);
if (otherObject == null)
continue;
if (otherObject.getClass() != object.getClass())
continue;
try {
logger.debug("Comparison of {} with {}", var, other);
entry.addEntry(other, object == otherObject);
} catch (Throwable t) {
logger.debug("Exception during equals: " + t);
// ignore?
}
}
trace.addEntry(statement.getPosition(), var, entry);
} catch (CodeUnderTestException e) {
logger.debug("", e);
}
}
use of org.evosuite.testcase.statements.ArrayStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createArrayInitStmt.
@Override
public void createArrayInitStmt(final CaptureLog log, final int logRecNo) {
final int oid = log.objectIds.get(logRecNo);
final Object[] params = log.params.get(logRecNo);
final String arrTypeName = log.getTypeName(oid);
final Class<?> arrType = getClassForName(arrTypeName);
// --- create array instance creation e.g. int[] var = new int[10];
final ArrayReference arrRef;
// create array only once
if (this.oidToVarRefMap.containsKey(oid)) {
arrRef = (ArrayReference) this.oidToVarRefMap.get(oid);
} else {
arrRef = new ArrayReference(testCase, arrType);
final ArrayStatement arrStmt = new ArrayStatement(testCase, arrRef);
arrStmt.setSize(params.length);
testCase.addStatement(arrStmt);
this.oidToVarRefMap.put(oid, arrRef);
}
final Class<?> arrCompClass = arrType.getComponentType();
AssignmentStatement assignStmt;
ArrayIndex arrIndex;
VariableReference valueRef;
// is either an oid or null
Integer argOID;
for (int i = 0; i < params.length; i++) {
argOID = (Integer) params[i];
if (argOID == null) {
valueRef = testCase.addStatement(new NullStatement(testCase, arrCompClass));
} else {
valueRef = this.oidToVarRefMap.get(argOID);
if (valueRef == null) {
logger.info("ValueREF is NULL for " + argOID);
continue;
}
}
arrIndex = new ArrayIndex(testCase, arrRef, i);
assignStmt = new AssignmentStatement(testCase, arrIndex, valueRef);
testCase.addStatement(assignStmt);
logger.debug("Adding assignment (array): " + assignStmt.getCode());
}
}
Aggregations