Search in sources :

Example 1 with NullStatement

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

the class EvoTestCaseCodeGenerator method getArguments.

private ArrayList<VariableReference> getArguments(final Object[] methodArgs, final Class<?>[] methodParamTypeClasses) throws IllegalArgumentException {
    ArrayList<VariableReference> args = new ArrayList<VariableReference>();
    // is either an oid or null
    Integer argOID;
    for (int i = 0; i < methodArgs.length; i++) {
        argOID = (Integer) methodArgs[i];
        if (argOID == null) {
            args.add(testCase.addStatement(new NullStatement(testCase, methodParamTypeClasses[i])));
        } else {
            VariableReference ref = this.oidToVarRefMap.get(argOID);
            if (ref == null) {
                throw new RuntimeException("VariableReference is null for argOID " + argOID + "; have oids: " + this.oidToVarRefMap.keySet());
            } else {
                args.add(ref);
            }
        }
    }
    return args;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) NullStatement(org.evosuite.testcase.statements.NullStatement) ArrayList(java.util.ArrayList)

Example 2 with NullStatement

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

the class EvoTestCaseCodeGenerator method createFieldWriteAccessStmt.

@Override
public void createFieldWriteAccessStmt(CaptureLog log, int logRecNo) {
    // assumption: all necessary statements are created and there is one variable for each referenced object
    final Object[] methodArgs = log.params.get(logRecNo);
    final int oid = log.objectIds.get(logRecNo);
    final int captureId = log.captureIds.get(logRecNo);
    final String fieldName = log.getNameOfAccessedFields(captureId);
    final String typeName = log.getTypeName(oid);
    try {
        final Class<?> type = getClassForName(typeName);
        final String fieldDesc = log.descList.get(logRecNo);
        final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
        final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type), this.oidToVarRefMap.get(oid));
        final AssignmentStatement assignment;
        final Integer arg = (Integer) methodArgs[0];
        if (arg == null) {
            final NullStatement nullStmt = new NullStatement(testCase, fieldType);
            final VariableReference nullReference = testCase.addStatement(nullStmt);
            assignment = new AssignmentStatement(testCase, targetFieldRef, nullReference);
        } else {
            assignment = new AssignmentStatement(testCase, targetFieldRef, this.oidToVarRefMap.get(arg));
        }
        final VariableReference varRef = testCase.addStatement(assignment);
        logger.debug("Adding assignment statement: " + assignment.getCode());
        if (arg != null) {
            this.oidToVarRefMap.put(arg, varRef);
        }
    } catch (final Exception e) {
        CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating field write access stmt. Log: %s", logRecNo, log);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) NullStatement(org.evosuite.testcase.statements.NullStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 3 with NullStatement

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

the class EvoTestCaseCodeGenerator method createMapInitStmt.

@Override
public void createMapInitStmt(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)) {
            collType = HashMap.class;
        }
        // -- 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 = new ArrayList<VariableReference>();
        for (int i = 0; i < params.length; i++) {
            argOID = (Integer) params[i];
            if (argOID == null) {
                paramList.add(testCase.addStatement(new NullStatement(testCase, Object.class)));
            } else {
                paramList.add(this.oidToVarRefMap.get(argOID));
            }
            if (i % 2 == 1) {
                final Method method = collType.getMethod("put", Object.class, Object.class);
                replaceNullWithNullReferences(paramList, Object.class, Object.class);
                methodStmt = new MethodStatement(testCase, new GenericMethod(method, collType), collRef, paramList);
                testCase.addStatement(methodStmt);
                paramList = new ArrayList<VariableReference>(2);
            }
        }
    } catch (final Exception e) {
        CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating map init stmt", logRecNo);
    }
}
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) NullStatement(org.evosuite.testcase.statements.NullStatement)

Example 4 with NullStatement

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

the class ArrayLocalSearch method stripAssignments.

private int stripAssignments(ArrayStatement statement, TestChromosome test, LocalSearchObjective<TestChromosome> objective) {
    int difference = 0;
    ArrayReference arrRef = (ArrayReference) statement.getReturnValue();
    TestFactory factory = TestFactory.getInstance();
    for (int position = test.size() - 1; position > statement.getPosition(); position--) {
        logger.debug("Current delete position: " + position);
        if (test.getTestCase().getStatement(position) instanceof AssignmentStatement) {
            logger.debug("Is assignment statement");
            AssignmentStatement assignment = (AssignmentStatement) test.getTestCase().getStatement(position);
            Statement valueStatement = test.getTestCase().getStatement(assignment.getValue().getStPosition());
            if (assignment.getReturnValue().getAdditionalVariableReference() == arrRef) {
                int currentDelta = 0;
                int differenceDelta = 0;
                logger.debug("Assigns to target array. Checking if we can remove it without worsening fitness");
                backup(test);
                try {
                    factory.deleteStatement(test.getTestCase(), position);
                    if (valueStatement instanceof PrimitiveStatement || valueStatement instanceof NullStatement) {
                        if (!test.getTestCase().hasReferences(valueStatement.getReturnValue())) {
                            if (valueStatement.getPosition() < statement.getPosition())
                                differenceDelta = 1;
                            currentDelta = 1;
                            logger.debug("Deleting primitive statement assigned to this array at " + valueStatement.getPosition());
                            factory.deleteStatement(test.getTestCase(), valueStatement.getPosition());
                        }
                    }
                    if (!objective.hasNotWorsened(test)) {
                        logger.debug("Fitness has decreased, so restoring test");
                        restore(test);
                        currentDelta = 0;
                        differenceDelta = 0;
                    }
                } catch (ConstructionFailedException e) {
                    currentDelta = 0;
                    differenceDelta = 0;
                    restore(test);
                }
                position -= currentDelta;
                difference += differenceDelta;
            }
        }
    }
    return difference;
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestFactory(org.evosuite.testcase.TestFactory) Statement(org.evosuite.testcase.statements.Statement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 5 with NullStatement

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

the class ReferenceLocalSearch method replace.

/**
 * Replace the call with a completely different call
 *
 * @param test
 * @param statement
 * @return
 */
private boolean replace(TestChromosome test, int statement) {
    logger.debug("Replacing call");
    TestFactory factory = TestFactory.getInstance();
    Statement theStatement = test.getTestCase().getStatement(statement);
    VariableReference var = theStatement.getReturnValue();
    int oldLength = test.size();
    try {
        VariableReference replacement = null;
        if (Randomness.nextDouble() < Properties.NULL_PROBABILITY) {
            NullStatement nullStatement = new NullStatement(test.getTestCase(), var.getType());
            replacement = test.getTestCase().addStatement(nullStatement, statement);
        } else if (!var.isPrimitive()) {
            // Test cluster does not keep track of generators for primitives
            replacement = factory.createObject(test.getTestCase(), var.getType(), statement, 0, null);
        }
        if (replacement != null) {
            int oldStatement = statement + (test.size() - oldLength);
            for (int i = oldStatement + 1; i < test.size(); i++) {
                test.getTestCase().getStatement(i).replace(var, replacement);
            }
            factory.deleteStatement(test.getTestCase(), oldStatement);
            test.setChanged(true);
        }
    } catch (ConstructionFailedException e) {
        if (test.size() < oldLength) {
            restore(test);
        }
        test.setChanged(test.size() != oldLength);
    }
    return false;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) NullStatement(org.evosuite.testcase.statements.NullStatement) TestFactory(org.evosuite.testcase.TestFactory) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

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