Search in sources :

Example 1 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.

the class SymbolicObserver method readArray.

private ReferenceExpressionPair readArray(ArrayIndex rhs, Scope scope) {
    ArrayReference arrayReference = rhs.getArray();
    ReferenceConstant symb_array = (ReferenceConstant) symb_references.get(arrayReference.getName());
    int conc_index = rhs.getArrayIndex();
    Class<?> componentClass = arrayReference.getComponentClass();
    try {
        Object conc_array = arrayReference.getObject(scope);
        if (componentClass.equals(int.class)) {
            int conc_value = Array.getInt(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newIntegerRef = newIntegerReference(conc_value, expr);
            return new ReferenceExpressionPair(newIntegerRef, expr);
        } else if (componentClass.equals(char.class)) {
            char conc_value = Array.getChar(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newCharacterRef = newCharacterReference(conc_value, expr);
            return new ReferenceExpressionPair(newCharacterRef, expr);
        } else if (componentClass.equals(boolean.class)) {
            boolean conc_value = Array.getBoolean(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value ? 1 : 0);
            ReferenceConstant newBooleanRef = newBooleanReference(conc_value, expr);
            return new ReferenceExpressionPair(newBooleanRef, expr);
        } else if (componentClass.equals(byte.class)) {
            byte conc_value = Array.getByte(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newByteRef = newByteReference(conc_value, expr);
            return new ReferenceExpressionPair(newByteRef, expr);
        } else if (componentClass.equals(short.class)) {
            short conc_value = Array.getShort(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newShortRef = newShortReference(conc_value, expr);
            return new ReferenceExpressionPair(newShortRef, expr);
        } else if (componentClass.equals(long.class)) {
            long conc_value = Array.getLong(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newLongRef = newLongReference(conc_value, expr);
            return new ReferenceExpressionPair(newLongRef, expr);
        } else if (componentClass.equals(float.class)) {
            float conc_value = Array.getFloat(conc_array, conc_index);
            RealValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newFloatRef = newFloatReference(conc_value, expr);
            return new ReferenceExpressionPair(newFloatRef, expr);
        } else if (componentClass.equals(double.class)) {
            double conc_value = Array.getDouble(conc_array, conc_index);
            RealValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newDoubleRef = newDoubleReference(conc_value, expr);
            return new ReferenceExpressionPair(newDoubleRef, expr);
        } else {
            Object conc_value = Array.get(conc_array, conc_index);
            if (conc_value instanceof String) {
                StringValue expr = env.heap.array_load(symb_array, conc_index, (String) conc_value);
                ReferenceConstant newStringRef = newStringReference((String) conc_value, expr);
                return new ReferenceExpressionPair(newStringRef, expr);
            } else {
                ReferenceExpression ref = env.heap.getReference(conc_value);
                if (conc_value != null && isWrapper(conc_value)) {
                    ReferenceConstant nonNullRef = (ReferenceConstant) ref;
                    Expression<?> expr = findOrCreate(conc_value, nonNullRef);
                    return new ReferenceExpressionPair(ref, expr);
                } else {
                    return new ReferenceExpressionPair(ref, null);
                }
            }
        }
    } catch (CodeUnderTestException e) {
        throw new RuntimeException(e);
    }
}
Also used : RealValue(org.evosuite.symbolic.expr.fp.RealValue) ArrayReference(org.evosuite.testcase.variable.ArrayReference) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 2 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.

the class SymbolicObserver method writeArray.

private void writeArray(ArrayIndex lhs, ReferenceExpressionPair readResult, Scope scope) {
    ArrayReference arrayReference = lhs.getArray();
    int conc_index = lhs.getArrayIndex();
    Object conc_array;
    try {
        conc_array = arrayReference.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    Type arrayType = Type.getType(conc_array.getClass());
    Type elementType = arrayType.getElementType();
    if (isValue(elementType) || elementType.equals(Type.getType(String.class))) {
        Expression<?> symb_value = readResult.getExpression();
        symb_value = castIfNeeded(elementType, symb_value);
        String array_name = arrayReference.getName();
        ReferenceExpression symb_ref = symb_references.get(array_name);
        ReferenceConstant symb_array = (ReferenceConstant) symb_ref;
        env.heap.array_store(conc_array, symb_array, conc_index, symb_value);
    } else {
    /* ignore storing references (we use objects to find them) */
    }
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) ArrayReference(org.evosuite.testcase.variable.ArrayReference) Type(org.objectweb.asm.Type) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 3 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference 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 4 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.

the class TestDefaultValue method testLong.

@Test
public void testLong() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference longArray0 = builder.appendArrayStmt(Long[].class, 10);
    VariableReference long0 = builder.appendNull(Long.class);
    builder.appendAssignment(longArray0, 0, long0);
    builder.appendAssignment(long0, longArray0, 0);
    builder.appendMethod(long0, Long.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 5 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.

the class TestDefaultValue method testFloat.

@Test
public void testFloat() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference floatArray0 = builder.appendArrayStmt(Float[].class, 10);
    VariableReference float0 = builder.appendNull(Float.class);
    builder.appendAssignment(floatArray0, 0, float0);
    builder.appendAssignment(float0, floatArray0, 0);
    builder.appendMethod(float0, Float.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    System.out.println(tc.toCode());
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Aggregations

ArrayReference (org.evosuite.testcase.variable.ArrayReference)20 VariableReference (org.evosuite.testcase.variable.VariableReference)15 Test (org.junit.Test)10 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)8 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)7 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)7 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)6 ArrayList (java.util.ArrayList)3 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)3 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)3 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)3 GenericMethod (org.evosuite.utils.generic.GenericMethod)3 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)2 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)2 NullStatement (org.evosuite.testcase.statements.NullStatement)2 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)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