Search in sources :

Example 11 with AssignmentStatement

use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.

the class ContractViolation method same.

/**
 * Determine if we have already seen an instance of this violation
 *
 * @param other
 *            a {@link org.evosuite.contracts.ContractViolation} object.
 * @return a boolean.
 */
public boolean same(ContractViolation other) {
    // Same contract?
    if (!contract.getClass().equals(other.contract.getClass()))
        return false;
    // Same type of statement?
    if (!statement.getClass().equals(other.statement.getClass()))
        return false;
    // Same exception type?
    if (exception != null && other.exception != null) {
        if (!exception.getClass().equals(other.exception.getClass()))
            return false;
    }
    // Same method call / constructor?
    if (statement instanceof MethodStatement) {
        MethodStatement ms1 = (MethodStatement) statement;
        MethodStatement ms2 = (MethodStatement) other.statement;
        if (ms1.getMethod().getMethod().equals(ms2.getMethod().getMethod())) {
            return true;
        }
    } else if (statement instanceof ConstructorStatement) {
        ConstructorStatement ms1 = (ConstructorStatement) statement;
        ConstructorStatement ms2 = (ConstructorStatement) other.statement;
        if (ms1.getConstructor().getConstructor().equals(ms2.getConstructor().getConstructor())) {
            return true;
        }
    } else if (statement instanceof AssignmentStatement) {
        VariableReference var1 = statement.getReturnValue();
        VariableReference var2 = other.statement.getReturnValue();
        if (var1 instanceof FieldReference && var2 instanceof FieldReference) {
            if (((FieldReference) var1).getField().getField().equals(((FieldReference) var2).getField().getField()))
                return true;
        }
    }
    return false;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement)

Example 12 with AssignmentStatement

use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.

the class DowncastTest method testFieldReferenceDoesNotNeedDowncast.

@Test
public void testFieldReferenceDoesNotNeedDowncast() throws NoSuchMethodException, NoSuchFieldException {
    TestCaseBuilder builder = new TestCaseBuilder();
    VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor());
    VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getAbstractFoo"));
    // This would be set during execution
    num0.setType(ConcreteSubclass.class);
    VariableReference bool0 = builder.appendBooleanPrimitive(true);
    DefaultTestCase test = builder.getDefaultTestCase();
    FieldReference fr = new FieldReference(test, new GenericField(AbstractSuperclass.class.getField("fieldInAbstractClass"), AbstractSuperclass.class), num0);
    AssignmentStatement statement = new AssignmentStatement(test, fr, bool0);
    test.addStatement(statement);
    test.removeDownCasts();
    System.out.println(test);
    FieldReference fr2 = (FieldReference) test.getStatement(3).getReturnValue();
    assertEquals(AbstractSuperclass.class, fr2.getSource().getVariableClass());
}
Also used : DowncastExample(com.examples.with.different.packagename.test.DowncastExample) AbstractSuperclass(com.examples.with.different.packagename.test.AbstractSuperclass) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 13 with AssignmentStatement

use of org.evosuite.testcase.statements.AssignmentStatement 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

AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)13 VariableReference (org.evosuite.testcase.variable.VariableReference)10 FieldReference (org.evosuite.testcase.variable.FieldReference)7 GenericField (org.evosuite.utils.generic.GenericField)6 MethodStatement (org.evosuite.testcase.statements.MethodStatement)5 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)3 NullStatement (org.evosuite.testcase.statements.NullStatement)3 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)3 Statement (org.evosuite.testcase.statements.Statement)3 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)3 Test (org.junit.Test)3 DowncastExample (com.examples.with.different.packagename.test.DowncastExample)2 Method (java.lang.reflect.Method)2 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)2 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)2 TestFactory (org.evosuite.testcase.TestFactory)2 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)2 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 ArrayReference (org.evosuite.testcase.variable.ArrayReference)2 GenericClass (org.evosuite.utils.generic.GenericClass)2