use of org.evosuite.testcase.variable.ArrayIndex 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.variable.ArrayIndex in project evosuite by EvoSuite.
the class TestCaseBuilder method appendAssignment.
/**
* var := array[index]
*
* @param var
* @param array
* @param index
*/
public void appendAssignment(VariableReference var, ArrayReference array, int index) {
ArrayIndex arrayIndex = new ArrayIndex(tc, array, index);
AssignmentStatement stmt = new AssignmentStatement(tc, var, arrayIndex);
tc.addStatement(stmt);
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testWrapperCastInArray.
@Test
public void testWrapperCastInArray() {
// Short[] shortArray0 = new Short[5];
// Integer[] integerArray0 = new Integer[9];
// integerArray0[0] = (Integer) shortArray0[3];
TestCase tc = new DefaultTestCase();
ArrayStatement shortArrayStatement = new ArrayStatement(tc, Short[].class, 5);
tc.addStatement(shortArrayStatement);
ArrayStatement intArrayStatement = new ArrayStatement(tc, Integer[].class, 9);
tc.addStatement(intArrayStatement);
ArrayIndex intIndex = new ArrayIndex(tc, intArrayStatement.getArrayReference(), 0);
ArrayIndex shortIndex = new ArrayIndex(tc, shortArrayStatement.getArrayReference(), 3);
AssignmentStatement assignmentStatement = new AssignmentStatement(tc, intIndex, shortIndex);
tc.addStatement(assignmentStatement);
String code = tc.toCode();
System.out.println(tc);
assertFalse(code.contains("integerArray0[0] = (Integer) shortArray0[3]"));
}
use of org.evosuite.testcase.variable.ArrayIndex 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