Search in sources :

Example 6 with NullReference

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

the class LegacyInsertion method selectVariableForCall.

/**
 * Randomly select one of the variables in the test defined up to position
 * to insert a call for
 *
 * @param test
 * @param position
 * @return
 */
private VariableReference selectVariableForCall(TestCase test, int position) {
    if (test.isEmpty() || position == 0)
        return null;
    double sum = 0.0;
    for (int i = 0; i < position; i++) {
        // sum += 1d / (10 * test.getStatement(i).getReturnValue().getDistance() + 1d);
        sum += 1d / (test.getStatement(i).getReturnValue().getDistance() + 1d);
        if (logger.isDebugEnabled()) {
            logger.debug(test.getStatement(i).getCode() + ": Distance = " + test.getStatement(i).getReturnValue().getDistance());
        }
    }
    double rnd = Randomness.nextDouble() * sum;
    for (int i = 0; i < position; i++) {
        double dist = 1d / (test.getStatement(i).getReturnValue().getDistance() + 1d);
        if (dist >= rnd && !(test.getStatement(i).getReturnValue() instanceof NullReference) && !(test.getStatement(i).getReturnValue().isPrimitive()) && !(test.getStatement(i).getReturnValue().isVoid()) && !(test.getStatement(i) instanceof PrimitiveStatement))
            return test.getStatement(i).getReturnValue();
        else
            rnd = rnd - dist;
    }
    if (position > 0)
        position = Randomness.nextInt(position);
    VariableReference var = test.getStatement(position).getReturnValue();
    if (!(var instanceof NullReference) && !var.isVoid() && !(test.getStatement(position) instanceof PrimitiveStatement) && !var.isPrimitive())
        return var;
    else
        return null;
}
Also used : PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) NullReference(org.evosuite.testcase.variable.NullReference)

Example 7 with NullReference

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

the class LegacyInsertion method selectRandomVariableForCall.

private VariableReference selectRandomVariableForCall(TestCase test, int position) {
    if (test.isEmpty() || position == 0)
        return null;
    List<VariableReference> allVariables = test.getObjects(position);
    Set<VariableReference> candidateVariables = new LinkedHashSet<VariableReference>();
    for (VariableReference var : allVariables) {
        if (!(var instanceof NullReference) && !var.isVoid() && !(test.getStatement(var.getStPosition()) instanceof PrimitiveStatement) && !var.isPrimitive())
            candidateVariables.add(var);
    }
    if (candidateVariables.isEmpty()) {
        return null;
    } else {
        VariableReference choice = Randomness.choice(candidateVariables);
        return choice;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VariableReference(org.evosuite.testcase.variable.VariableReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) 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