Search in sources :

Example 1 with NullReference

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

the class EvoTestCaseCodeGenerator method replaceNullWithNullReferences.

private void replaceNullWithNullReferences(List<VariableReference> paramList, Class<?>... paramTypes) {
    CodeGeneratorException.check(paramList.size() == paramTypes.length, "[paramList = %s, paramTypes] - number of params does not correspond number of paramTypes", paramList, Arrays.toString(paramTypes));
    Object v;
    for (int j = 0; j < paramList.size(); j++) {
        v = paramList.get(j);
        if (v == null) {
            paramList.set(j, new NullReference(testCase, paramTypes[j]));
        }
    }
}
Also used : NullReference(org.evosuite.testcase.variable.NullReference)

Example 2 with NullReference

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

the class ParameterLocalSearch method doSearch.

/**
 * Go through parameters of constructor call and apply local search
 *
 * @param test
 * @param statement
 * @param objective
 */
private boolean doSearch(TestChromosome test, ConstructorStatement statement, LocalSearchObjective<TestChromosome> objective) {
    int numParameter = 0;
    boolean hasImproved = false;
    for (VariableReference parameter : statement.getParameterReferences()) {
        // First try null
        statement.replaceParameterReference(new NullReference(test.getTestCase(), parameter.getType()), numParameter);
        // Else try all other values available in the test
        if (!objective.hasImproved(test)) {
            statement.replaceParameterReference(parameter, numParameter);
            boolean done = false;
            List<VariableReference> objects = test.getTestCase().getObjects(parameter.getType(), statement.getPosition());
            objects.remove(parameter);
            for (VariableReference replacement : objects) {
                statement.replaceParameterReference(replacement, numParameter);
                if (objective.hasImproved(test)) {
                    done = true;
                    hasImproved = true;
                    break;
                }
            }
            if (!done)
                statement.replaceParameterReference(parameter, numParameter);
        } else {
            hasImproved = true;
        }
        numParameter++;
    }
    return hasImproved;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) NullReference(org.evosuite.testcase.variable.NullReference)

Example 3 with NullReference

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

the class RandomInsertion method selectRandomVariableForCall.

private VariableReference selectRandomVariableForCall(TestCase test, int position) {
    if (test.isEmpty() || position == 0)
        return null;
    List<VariableReference> allVariables = test.getObjects(position);
    List<VariableReference> candidateVariables = new ArrayList<>();
    for (VariableReference var : allVariables) {
        if (!(var instanceof NullReference) && !var.isVoid() && !var.getGenericClass().isObject() && !(test.getStatement(var.getStPosition()) instanceof PrimitiveStatement) && !var.isPrimitive() && (test.hasReferences(var) || var.getVariableClass().equals(Properties.getInitializedTargetClass())) && // do not directly call methods on mock objects
        !(test.getStatement(var.getStPosition()) instanceof FunctionalMockStatement)) {
            candidateVariables.add(var);
        }
    }
    if (candidateVariables.isEmpty()) {
        return null;
    } else if (Properties.SORT_OBJECTS) {
        candidateVariables = candidateVariables.stream().sorted(Comparator.comparingInt(item -> item.getDistance())).collect(Collectors.toList());
        return ListUtil.selectRankBiased(candidateVariables);
    } else {
        return Randomness.choice(candidateVariables);
    }
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) NullReference(org.evosuite.testcase.variable.NullReference) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement)

Example 4 with NullReference

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

the class EntityWithParametersStatement method mutateParameter.

protected boolean mutateParameter(TestCase test, int numParameter) {
    // replace a parameter
    VariableReference parameter = parameters.get(numParameter);
    List<VariableReference> objects = test.getObjects(parameter.getType(), getPosition());
    objects.remove(parameter);
    objects.remove(getReturnValue());
    NullStatement nullStatement = new NullStatement(test, parameter.getType());
    Statement copy = null;
    // check if NULL is a valid option
    Constraints constraint = getConstraints();
    boolean avoidNull = constraint != null && constraint.noNullInputs();
    if (Properties.HONOUR_DATA_ANNOTATIONS && (numParameter < parameterAnnotations.length)) {
        if (GenericUtils.isAnnotationTypePresent(parameterAnnotations[numParameter], GenericUtils.NONNULL)) {
            avoidNull = true;
        }
    }
    if (avoidNull) {
        // be sure to remove all references pointing to NULL
        Iterator<VariableReference> iter = objects.iterator();
        while (iter.hasNext()) {
            VariableReference ref = iter.next();
            if (ref instanceof NullReference) {
                iter.remove();
            }
        }
    } else {
        // If it's not a primitive, then changing to null is also an option
        if (!parameter.isPrimitive()) {
            objects.add(nullStatement.getReturnValue());
        }
    }
    // we consider adding an instance
    if (getNumParametersOfType(parameter.getVariableClass()) + 1 < objects.size()) {
        Statement originalStatement = test.getStatement(parameter.getStPosition());
        copy = originalStatement.clone(test);
        if (originalStatement instanceof PrimitiveStatement<?>) {
            ((PrimitiveStatement<?>) copy).delta();
        }
        objects.add(copy.getReturnValue());
    }
    if (objects.isEmpty())
        return false;
    VariableReference replacement = Randomness.choice(objects);
    if (replacement == nullStatement.getReturnValue()) {
        test.addStatement(nullStatement, getPosition());
    } else if (copy != null && replacement == copy.getReturnValue()) {
        test.addStatement(copy, getPosition());
    }
    replaceParameterReference(replacement, numParameter);
    return true;
}
Also used : Constraints(org.evosuite.runtime.annotation.Constraints) VariableReference(org.evosuite.testcase.variable.VariableReference) NullReference(org.evosuite.testcase.variable.NullReference)

Example 5 with NullReference

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

the class ParameterLocalSearch method doSearch.

/**
 * Go through parameters of method call and apply local search
 *
 * @param test
 * @param statement
 * @param objective
 */
private boolean doSearch(TestChromosome test, MethodStatement statement, LocalSearchObjective<TestChromosome> objective) {
    logger.info("Original test: " + test.getTestCase().toCode());
    boolean hasImproved = false;
    if (!statement.isStatic()) {
        logger.info("Replacing callee");
        VariableReference callee = statement.getCallee();
        List<VariableReference> objects = test.getTestCase().getObjects(callee.getType(), statement.getPosition());
        objects.remove(callee);
        boolean done = false;
        for (VariableReference replacement : objects) {
            statement.setCallee(replacement);
            if (objective.hasImproved(test)) {
                done = true;
                hasImproved = true;
                backup(test, statement);
                break;
            } else {
                logger.info("Undoing change");
                restore(test, statement);
            }
        }
        if (!done)
            statement.setCallee(callee);
    }
    int numParameter = 0;
    for (VariableReference parameter : statement.getParameterReferences()) {
        logger.info("Replacing parameter " + numParameter);
        // First try null
        statement.replaceParameterReference(new NullReference(test.getTestCase(), parameter.getType()), numParameter);
        logger.info("Resulting test: " + test.getTestCase().toCode());
        // Else try all other values available in the test
        if (!objective.hasImproved(test)) {
            logger.info("Undoing change");
            restore(test, statement);
            statement.replaceParameterReference(parameter, numParameter);
            boolean done = false;
            List<VariableReference> objects = test.getTestCase().getObjects(parameter.getType(), statement.getPosition());
            objects.remove(parameter);
            for (VariableReference replacement : objects) {
                statement.replaceParameterReference(replacement, numParameter);
                logger.info("Resulting test: " + test.getTestCase().toCode());
                if (objective.hasImproved(test)) {
                    backup(test, statement);
                    hasImproved = true;
                    done = true;
                    break;
                } else {
                    logger.info("Undoing change");
                    restore(test, statement);
                }
            }
            if (!done)
                statement.replaceParameterReference(parameter, numParameter);
        } else {
            hasImproved = true;
        }
        numParameter++;
    }
    return hasImproved;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) NullReference(org.evosuite.testcase.variable.NullReference)

Aggregations

NullReference (org.evosuite.testcase.variable.NullReference)7 VariableReference (org.evosuite.testcase.variable.VariableReference)6 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)3 LinkedHashSet (java.util.LinkedHashSet)1 Constraints (org.evosuite.runtime.annotation.Constraints)1 FunctionalMockStatement (org.evosuite.testcase.statements.FunctionalMockStatement)1