Search in sources :

Example 1 with ArrayIndex

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);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 2 with ArrayIndex

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++);
    }
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) HashSet(java.util.HashSet)

Example 3 with ArrayIndex

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]"));
}
Also used : ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Test(org.junit.Test)

Example 4 with ArrayIndex

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);
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Type(java.lang.reflect.Type) Scope(org.evosuite.testcase.execution.Scope) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 5 with ArrayIndex

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"));
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) ObjectParameter(com.examples.with.different.packagename.ObjectParameter) Test(org.junit.Test)

Aggregations

ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)14 VariableReference (org.evosuite.testcase.variable.VariableReference)11 ArrayReference (org.evosuite.testcase.variable.ArrayReference)6 Test (org.junit.Test)5 FieldReference (org.evosuite.testcase.variable.FieldReference)4 GenericMethod (org.evosuite.utils.generic.GenericMethod)4 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)3 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)3 ArrayList (java.util.ArrayList)2 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)2 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)2 ObjectParameter (com.examples.with.different.packagename.ObjectParameter)1 TrivialInt (com.examples.with.different.packagename.TrivialInt)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 TestCase (org.evosuite.testcase.TestCase)1 TestFactory (org.evosuite.testcase.TestFactory)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 Scope (org.evosuite.testcase.execution.Scope)1