Search in sources :

Example 6 with NullStatement

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

the class ReferenceLocalSearch method replaceConstructorParameter.

/**
 * Go through parameters of constructor call and apply local search
 *
 * @param test
 * @param statement
 */
private boolean replaceConstructorParameter(TestChromosome test, ConstructorStatement statement) {
    List<VariableReference> parameters = statement.getParameterReferences();
    if (parameters.isEmpty())
        return false;
    int numParameter = Randomness.nextInt(parameters.size());
    VariableReference parameter = parameters.get(numParameter);
    List<VariableReference> objects = test.getTestCase().getObjects(parameter.getType(), statement.getPosition());
    objects.remove(parameter);
    objects.remove(statement.getReturnValue());
    NullStatement nullStatement = new NullStatement(test.getTestCase(), parameter.getType());
    if (!parameter.isPrimitive())
        objects.add(nullStatement.getReturnValue());
    if (objects.isEmpty())
        return false;
    VariableReference replacement = Randomness.choice(objects);
    if (replacement == nullStatement.getReturnValue()) {
        test.getTestCase().addStatement(nullStatement, statement.getPosition());
    }
    statement.replaceParameterReference(replacement, numParameter);
    test.setChanged(true);
    return false;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) NullStatement(org.evosuite.testcase.statements.NullStatement)

Example 7 with NullStatement

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

the class TestCaseBuilder method appendNull.

public VariableReference appendNull(Type type) {
    NullStatement stmt = new NullStatement(tc, type);
    tc.addStatement(stmt);
    return stmt.getReturnValue();
}
Also used : NullStatement(org.evosuite.testcase.statements.NullStatement)

Example 8 with NullStatement

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

the class NullReferenceSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    NullStatement nullStatement = (NullStatement) test.getTestCase().getStatement(statement);
    TestCase newTest = test.getTestCase();
    TestCase oldTest = newTest.clone();
    ExecutionResult oldResult = test.getLastExecutionResult();
    // double oldFitness = test.getFitness();
    Map<FitnessFunction<?>, Double> oldFitnesses = test.getFitnessValues();
    Map<FitnessFunction<?>, Double> oldLastFitnesses = test.getPreviousFitnessValues();
    try {
        TestFactory.getInstance().attemptGeneration(newTest, nullStatement.getReturnType(), statement);
        if (!objective.hasImproved(test)) {
            test.setTestCase(oldTest);
            test.setLastExecutionResult(oldResult);
            // test.setFitness(oldFitness);
            test.setFitnessValues(oldFitnesses);
            test.setPreviousFitnessValues(oldLastFitnesses);
        } else {
            return true;
        }
    } catch (ConstructionFailedException e) {
    // If we can't construct it, then ignore
    }
    return false;
}
Also used : NullStatement(org.evosuite.testcase.statements.NullStatement) TestCase(org.evosuite.testcase.TestCase) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) FitnessFunction(org.evosuite.ga.FitnessFunction) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 9 with NullStatement

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

Example 10 with NullStatement

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

the class EvoTestCaseCodeGenerator method createCollectionInitStmt.

@Override
public void createCollectionInitStmt(final CaptureLog log, final int logRecNo) {
    try {
        final int oid = log.objectIds.get(logRecNo);
        final Object[] params = log.params.get(logRecNo);
        String collTypeName = log.getTypeName(oid);
        Class<?> collType = getClassForName(collTypeName);
        // -- determine if an alternative collection must be used for code generation
        final boolean isPublic = java.lang.reflect.Modifier.isPublic(collType.getModifiers());
        if (!isPublic || !hasDefaultConstructor(collType)) {
            if (Set.class.isAssignableFrom(collType)) {
                collTypeName = HashSet.class.getName();
                collType = HashSet.class;
            } else if (List.class.isAssignableFrom(collType)) {
                collTypeName = ArrayList.class.getName();
                collType = ArrayList.class;
            } else if (Queue.class.isAssignableFrom(collType)) {
                collTypeName = ArrayDeque.class.getName();
                collType = ArrayDeque.class;
            } else {
                CodeGeneratorException.propagateError("[logRecNo = %s] - collection %s is not supported", logRecNo, collType);
            }
        }
        // -- create code for instantiating collection
        final List<VariableReference> noParams = Collections.emptyList();
        final ConstructorStatement constrStmt = new ConstructorStatement(testCase, new GenericConstructor(collType.getConstructor(new Class<?>[0]), collType), noParams);
        final VariableReference collRef = testCase.addStatement(constrStmt);
        this.oidToVarRefMap.put(oid, collRef);
        // --- fill collection
        MethodStatement methodStmt;
        // is either an oid or null
        Integer argOID;
        ArrayList<VariableReference> paramList;
        Method method;
        for (int i = 0; i < params.length; i++) {
            paramList = new ArrayList<VariableReference>(1);
            argOID = (Integer) params[i];
            if (argOID == null || !this.oidToVarRefMap.containsKey(argOID)) {
                VariableReference var = testCase.addStatement(new NullStatement(testCase, Object.class));
                paramList.add(var);
            } else {
                VariableReference var = this.oidToVarRefMap.get(argOID);
                paramList.add(var);
            }
            method = collType.getMethod("add", Object.class);
            methodStmt = new MethodStatement(testCase, new GenericMethod(method, collType), collRef, paramList);
            testCase.addStatement(methodStmt);
        }
    } catch (final Exception e) {
        CodeGeneratorException.propagateError("[logRecNo = %s] - an unexpected error occurred while creating collection init stmt", logRecNo, e);
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayDeque(java.util.ArrayDeque) NullStatement(org.evosuite.testcase.statements.NullStatement) ArrayList(java.util.ArrayList) List(java.util.List) ResourceList(org.evosuite.classpath.ResourceList) HashSet(java.util.HashSet)

Aggregations

NullStatement (org.evosuite.testcase.statements.NullStatement)12 VariableReference (org.evosuite.testcase.variable.VariableReference)8 ArrayList (java.util.ArrayList)4 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)4 MethodStatement (org.evosuite.testcase.statements.MethodStatement)4 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)3 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)3 Statement (org.evosuite.testcase.statements.Statement)3 Method (java.lang.reflect.Method)2 TestFactory (org.evosuite.testcase.TestFactory)2 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)2 FieldStatement (org.evosuite.testcase.statements.FieldStatement)2 ArrayReference (org.evosuite.testcase.variable.ArrayReference)2 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)2 GenericMethod (org.evosuite.utils.generic.GenericMethod)2 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ResourceList (org.evosuite.classpath.ResourceList)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1