Search in sources :

Example 6 with ArrayIndex

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

the class TestConstantInliner method testNumericArrayIndexInlining.

@Test
public void testNumericArrayIndexInlining() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    PrimitiveStatement<?> primitiveStatement = PrimitiveStatement.getPrimitiveStatement(test, int.class);
    VariableReference intVar = test.addStatement(primitiveStatement);
    ArrayStatement as = new ArrayStatement(test, int[].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, intVar));
    test.addStatement(new AssignmentStatement(test, ai1, intVar));
    test.addStatement(new AssignmentStatement(test, ai2, intVar));
    ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(TrivialInt.class.getConstructor(), TrivialInt.class), new ArrayList<>());
    VariableReference sut = test.addStatement(sutCS);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(ai0);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    parameters = new ArrayList<>();
    parameters.add(ai1);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    parameters = new ArrayList<>();
    parameters.add(ai2);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    System.out.println(test.toCode());
    assertFalse(code.contains("trivialInt0.testMe(int"));
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) TrivialInt(com.examples.with.different.packagename.TrivialInt) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Test(org.junit.Test)

Example 7 with ArrayIndex

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

the class InputObserver method afterStatement.

/* (non-Javadoc)
     * @see org.evosuite.testcase.ExecutionObserver#afterStatement(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
     */
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    if (statement instanceof EntityWithParametersStatement) {
        EntityWithParametersStatement parameterisedStatement = (EntityWithParametersStatement) statement;
        List<VariableReference> parRefs = parameterisedStatement.getParameterReferences();
        List<Object> argObjects = new ArrayList<>(parRefs.size());
        for (VariableReference parRef : parRefs) {
            Object parObject = null;
            try {
                if (parRef instanceof ArrayIndex || parRef instanceof FieldReference) {
                    parObject = parRef.getObject(scope);
                } else if (parRef instanceof ConstantValue) {
                    parObject = ((ConstantValue) parRef).getValue();
                } else {
                    parObject = parRef.getObject(scope);
                }
            } catch (CodeUnderTestException e) {
                e.printStackTrace();
            }
            argObjects.add(parObject);
        }
        assert parRefs.size() == argObjects.size();
        String className = parameterisedStatement.getDeclaringClassName();
        String methodDesc = parameterisedStatement.getDescriptor();
        String methodName = parameterisedStatement.getMethodName();
        inputCoverage.put(statement.getPosition(), InputCoverageGoal.createCoveredGoalsFromParameters(className, methodName, methodDesc, argObjects));
    // argumentsValues.put((EntityWithParametersStatement) statement, argObjects);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ConstantValue(org.evosuite.testcase.variable.ConstantValue) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 8 with ArrayIndex

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

the class ArrayStatement method isValid.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.StatementInterface#isValid()
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean isValid() {
    int maxAssignment = 0;
    for (Statement statement : this.tc) {
        for (VariableReference var : statement.getVariableReferences()) {
            if (var.getAdditionalVariableReference() == this.retval) {
                VariableReference currentVar = var;
                while (currentVar instanceof FieldReference) {
                    currentVar = ((FieldReference) currentVar).getSource();
                }
                ArrayIndex index = (ArrayIndex) currentVar;
                maxAssignment = Math.max(maxAssignment, index.getArrayIndex());
            }
        }
    }
    if (maxAssignment > lengths[0]) {
        logger.warn("Max assignment = " + maxAssignment + ", length = " + lengths[0]);
        return false;
    }
    return super.isValid();
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 9 with ArrayIndex

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

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

the class EntityWithParametersStatement method getUniqueVariableReferences.

@Override
public List<VariableReference> getUniqueVariableReferences() {
    List<VariableReference> references = new ArrayList<>();
    references.add(retval);
    references.addAll(parameters);
    for (VariableReference param : parameters) {
        if (param instanceof ArrayIndex)
            references.add(((ArrayIndex) param).getArray());
    }
    return references;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

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