Search in sources :

Example 6 with ArrayStatement

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

the class LocalSearchArraySystemTest method getArrayTest.

private TestCase getArrayTest(int length) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
    VariableReference arrayVar = test.addStatement(new ArrayStatement(test, int[].class, length));
    for (int i = 0; i < length; i++) {
        // Add value
        VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, 0));
        test.addStatement(new AssignmentStatement(test, new ArrayIndex(test, (ArrayReference) arrayVar, i), intVar));
    }
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int[].class });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { arrayVar }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) GenericClass(org.evosuite.utils.generic.GenericClass) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestFactory(org.evosuite.testcase.TestFactory) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement)

Example 7 with ArrayStatement

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

the class SameTraceObserver method visit.

/* (non-Javadoc)
	 * @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
	 */
/**
 * {@inheritDoc}
 */
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
    // TODO: Only MethodStatement?
    if (statement.isAssignmentStatement())
        return;
    if (statement instanceof PrimitiveStatement<?>)
        return;
    if (statement instanceof ArrayStatement)
        return;
    if (statement instanceof ConstructorStatement)
        return;
    try {
        Object object = var.getObject(scope);
        if (object == null)
            return;
        if (var.isPrimitive())
            return;
        if (var.isString() && Properties.INLINE)
            // After inlining the value of assertions would be different
            return;
        SameTraceEntry entry = new SameTraceEntry(var);
        for (VariableReference other : scope.getElements(var.getType())) {
            if (other == var)
                continue;
            if (other.isPrimitive())
                continue;
            if (other.isWrapperType())
                // Issues with inlining resulting in unstable assertions
                continue;
            Object otherObject = other.getObject(scope);
            if (otherObject == null)
                continue;
            if (otherObject.getClass() != object.getClass())
                continue;
            try {
                logger.debug("Comparison of {} with {}", var, other);
                entry.addEntry(other, object == otherObject);
            } catch (Throwable t) {
                logger.debug("Exception during equals: " + t);
            // ignore?
            }
        }
        trace.addEntry(statement.getPosition(), var, entry);
    } catch (CodeUnderTestException e) {
        logger.debug("", e);
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 8 with ArrayStatement

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

ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)8 VariableReference (org.evosuite.testcase.variable.VariableReference)4 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)3 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)3 ArrayReference (org.evosuite.testcase.variable.ArrayReference)3 TestFactory (org.evosuite.testcase.TestFactory)2 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 NullStatement (org.evosuite.testcase.statements.NullStatement)2 Statement (org.evosuite.testcase.statements.Statement)2 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestCase (org.evosuite.testcase.TestCase)1 TestCaseExpander (org.evosuite.testcase.TestCaseExpander)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1