use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class SymbolicObserver method after.
private void after(AssignmentStatement s, Scope scope) {
VariableReference lhs = s.getReturnValue();
VariableReference rhs = s.getValue();
ReferenceExpressionPair readResult = read(rhs, scope);
if (lhs instanceof FieldReference) {
writeField((FieldReference) lhs, readResult, scope);
} else if (lhs instanceof ArrayIndex) {
writeArray((ArrayIndex) lhs, readResult, scope);
} else {
writeVariable(lhs, readResult);
}
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class TestCaseExpander method visitArrayStatement.
public void visitArrayStatement(TestCase test, ArrayStatement statement) {
ArrayReference arrRef = (ArrayReference) statement.getReturnValue();
Set<Integer> assignments = new HashSet<Integer>();
int position = statement.getPosition() + 1;
while (position < test.size()) {
Statement st = test.getStatement(position);
if (st instanceof AssignmentStatement) {
if (st.getReturnValue() instanceof ArrayIndex) {
ArrayIndex arrayIndex = (ArrayIndex) st.getReturnValue();
if (arrayIndex.getArray().equals(arrRef)) {
assignments.add(arrayIndex.getArrayIndex());
}
}
} else if (st instanceof PrimitiveStatement) {
// OK, ignore
} else {
break;
}
position++;
}
position = statement.getPosition() + 1;
for (int i = 0; i < statement.size(); i++) {
if (assignments.contains(i))
continue;
ArrayIndex index = new ArrayIndex(test, arrRef, i);
VariableReference retVal = null;
if (index.isPrimitive()) {
PrimitiveStatement<?> primitive = PrimitiveStatement.getPrimitiveStatement(test, index.getGenericClass());
retVal = test.addStatement(primitive, position++);
} else {
NullStatement nullStatement = new NullStatement(test, index.getType());
retVal = test.addStatement(nullStatement, position++);
}
AssignmentStatement assignment = new AssignmentStatement(test, index, retVal);
test.addStatement(assignment, position++);
}
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testCastAndBoxingInArray.
@Test
public void testCastAndBoxingInArray() {
// short[] shortArray0 = new short[5];
// Long[] longArray0 = new Long[5];
// longArray0[0] = (Long) shortArray0[1]; <-- this gives a compile error
TestCase tc = new DefaultTestCase();
ArrayStatement shortArrayStatement = new ArrayStatement(tc, short[].class, 5);
tc.addStatement(shortArrayStatement);
ArrayStatement longArrayStatement = new ArrayStatement(tc, Long[].class, 5);
tc.addStatement(longArrayStatement);
ArrayIndex longIndex = new ArrayIndex(tc, longArrayStatement.getArrayReference(), 0);
ArrayIndex shortIndex = new ArrayIndex(tc, shortArrayStatement.getArrayReference(), 1);
AssignmentStatement assignmentStatement = new AssignmentStatement(tc, longIndex, shortIndex);
tc.addStatement(assignmentStatement);
String code = tc.toCode();
System.out.println(tc);
assertFalse(code.contains("longArray0[0] = (Long) shortArray0[1]"));
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testArray.
@Test
public void testArray() throws Exception {
TestCase tc = new DefaultTestCase();
/*
String s = "...";
String[] array = new String[1];
array[0] = s;
Foo foo = mock(Foo.class);
getFirstInArray(foo);
*/
final String MOCKED_VALUE = "Hello 42!!!";
VariableReference aString = tc.addStatement(new StringPrimitiveStatement(tc, MOCKED_VALUE));
ArrayReference mockedArray = (ArrayReference) tc.addStatement(new ArrayStatement(tc, String[].class, 1));
ArrayIndex arrayIndex = new ArrayIndex(tc, mockedArray, 0);
AssignmentStatement stmt = new AssignmentStatement(tc, arrayIndex, aString);
tc.addStatement(stmt);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, Foo.class, Foo.class);
VariableReference mock = tc.addStatement(mockStmt);
VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("getFirstInArray", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
// if not executed, should be no way to tell if needs new inputs
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
Assert.assertEquals(0, mockStmt.getNumParameters());
// execute first time with default mock
Scope scope = execute(tc);
Object obj = scope.getObject(result);
// default mock value should be null for objects/arrays
Assert.assertNull(obj);
// after execution, there should be one variable to provide
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(1, types.size());
Assert.assertEquals(String[].class, types.get(0));
// add int variable to list of mock expected returns
mockStmt.addMissingInputs(Arrays.asList(mockedArray));
Assert.assertEquals(1, mockStmt.getNumParameters());
Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedArray));
// re-execute with initialized mock
scope = execute(tc);
String val = (String) scope.getObject(result);
Assert.assertEquals(MOCKED_VALUE, val);
}
use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.
the class TestConstantInliner method testArrayIndexInlining.
@Test
public void testArrayIndexInlining() throws NoSuchMethodException, SecurityException {
DefaultTestCase test = new DefaultTestCase();
ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(Object.class.getConstructor(), Object.class), new ArrayList<VariableReference>());
VariableReference objectVar = test.addStatement(cs);
ArrayStatement as = new ArrayStatement(test, Object[].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, objectVar));
test.addStatement(new AssignmentStatement(test, ai1, objectVar));
test.addStatement(new AssignmentStatement(test, ai2, objectVar));
ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(ObjectParameter.class.getConstructor(), ObjectParameter.class), new ArrayList<VariableReference>());
VariableReference sut = test.addStatement(sutCS);
List<VariableReference> parameters = new ArrayList<VariableReference>();
parameters.add(ai0);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
parameters = new ArrayList<VariableReference>();
parameters.add(ai1);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
parameters = new ArrayList<VariableReference>();
parameters.add(ai2);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
System.out.println(test.toCode());
ConstantInliner inliner = new ConstantInliner();
inliner.inline(test);
String code = test.toCode();
assertFalse(code.contains("objectParameter0.testMe(objectArray0"));
}
Aggregations