Search in sources :

Example 16 with ArrayReference

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

the class TestDefaultValue method testByte.

@Test
public void testByte() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference integerArray0 = builder.appendArrayStmt(Byte[].class, 10);
    VariableReference integer0 = builder.appendNull(Byte.class);
    builder.appendAssignment(integerArray0, 0, integer0);
    builder.appendAssignment(integer0, integerArray0, 0);
    builder.appendMethod(integer0, Byte.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 17 with ArrayReference

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

the class ArrayStatement method mutate.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.AbstractStatement#mutate(org.evosuite.testcase.TestCase, org.evosuite.testcase.AbstractTestFactory)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean mutate(TestCase test, TestFactory factory) {
    int maxAssignment = 0;
    for (Statement statement : test) {
        for (VariableReference var : statement.getVariableReferences()) {
            if (var.getAdditionalVariableReference() == this.retval) {
                VariableReference currentVar = var;
                while (currentVar instanceof FieldReference) {
                    currentVar = ((FieldReference) currentVar).getSource();
                }
                if (!(currentVar instanceof ArrayIndex)) {
                    LoggingUtils.getEvoLogger().error("Found assignment to array without ArrayIndex:");
                    LoggingUtils.getEvoLogger().error(test.toCode());
                    LoggingUtils.getEvoLogger().error(statement.getPosition() + ", " + statement.getCode());
                }
                ArrayIndex index = (ArrayIndex) currentVar;
                maxAssignment = Math.max(maxAssignment, index.getArrayIndex());
            }
        }
    }
    int dim = 0;
    if (lengths.length > 1) {
        dim = Randomness.nextInt(lengths.length - 1);
    }
    int newLength = lengths[dim];
    while (newLength == lengths[dim]) {
        if (Randomness.nextDouble() <= Properties.RANDOM_PERTURBATION) {
            newLength = Randomness.nextInt(maxAssignment, Math.max(maxAssignment + 1, Properties.MAX_ARRAY)) + 1;
        } else {
            int max = Math.min(Math.abs(lengths[dim] - maxAssignment - 1), Properties.MAX_DELTA);
            if (max > 0)
                newLength = lengths[dim] + Randomness.nextInt(2 * max) - max;
            else
                newLength = lengths[dim] + Randomness.nextInt(Properties.MAX_DELTA);
        }
    }
    // TODO: Need to make sure this doesn't happen by construction
    if (newLength <= 0)
        newLength = 1;
    lengths[dim] = newLength;
    ((ArrayReference) retval).setLengths(lengths);
    return true;
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 18 with ArrayReference

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

the class MethodStatement method copy.

/**
 * {@inheritDoc}
 */
@Override
public Statement copy(TestCase newTestCase, int offset) {
    ArrayList<VariableReference> newParams = new ArrayList<VariableReference>();
    for (VariableReference r : parameters) {
        newParams.add(r.copy(newTestCase, offset));
    }
    MethodStatement m;
    if (isStatic()) {
        // FIXXME: If callee is an array index, this will return an invalid
        // copy of the cloned variable!
        m = new MethodStatement(newTestCase, method.copy(), null, newParams);
    } else {
        VariableReference newCallee = callee.copy(newTestCase, offset);
        m = new MethodStatement(newTestCase, method.copy(), newCallee, newParams);
    }
    if (retval instanceof ArrayReference && !(m.getReturnValue() instanceof ArrayReference)) {
        // logger.info("Copying array retval: " + retval.getGenericClass());
        // assert (retval.getGenericClass() != null);
        // assert (retval.getGenericClass().isArray()) : method.toString();
        ArrayReference newRetVal = new ArrayReference(newTestCase, retval.getGenericClass(), ((ArrayReference) retval).getArrayLength());
        m.setRetval(newRetVal);
    }
    // Actual type may have changed, e.g. subtype
    m.getReturnValue().setType(retval.getType());
    return m;
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList)

Example 19 with ArrayReference

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

the class SymbolicObserver method after.

private void after(ArrayStatement s, Scope scope) {
    try {
        ArrayReference arrayRef = (ArrayReference) s.getReturnValue();
        Object conc_array;
        conc_array = arrayRef.getObject(scope);
        if (arrayRef.getArrayDimensions() == 1) {
            int length = arrayRef.getArrayLength();
            IntegerConstant lengthExpr = ExpressionFactory.buildNewIntegerConstant(length);
            Class<?> component_class = arrayRef.getComponentClass();
            env.topFrame().operandStack.pushBv32(lengthExpr);
            if (component_class.equals(int.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_INT);
            } else if (component_class.equals(char.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_CHAR);
            } else if (component_class.equals(short.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_SHORT);
            } else if (component_class.equals(byte.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_BYTE);
            } else if (component_class.equals(float.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_FLOAT);
            } else if (component_class.equals(long.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_LONG);
            } else if (component_class.equals(boolean.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_BOOLEAN);
            } else if (component_class.equals(double.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_DOUBLE);
            } else {
                // push arguments
                String componentTypeName = component_class.getName().replace('.', '/');
                VM.ANEWARRAY(length, componentTypeName);
            }
        } else {
            // push dimensions
            List<Integer> dimensions = arrayRef.getLengths();
            for (int i = 0; i < arrayRef.getArrayDimensions(); i++) {
                int length = dimensions.get(i);
                IntegerConstant lengthExpr = ExpressionFactory.buildNewIntegerConstant(length);
                env.topFrame().operandStack.pushBv32(lengthExpr);
            }
            String arrayTypeDesc = Type.getDescriptor(conc_array.getClass());
            VM.MULTIANEWARRAY(arrayTypeDesc, arrayRef.getArrayDimensions());
        }
        ReferenceConstant symb_array = (ReferenceConstant) env.topFrame().operandStack.popRef();
        env.heap.initializeReference(conc_array, symb_array);
        String varRef_name = arrayRef.getName();
        symb_references.put(varRef_name, symb_array);
    } catch (CodeUnderTestException e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) ArrayReference(org.evosuite.testcase.variable.ArrayReference) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 20 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference 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());
    }
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) NullStatement(org.evosuite.testcase.statements.NullStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

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