Search in sources :

Example 11 with PrimitiveStatement

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

the class EvoTestCaseCodeGenerator method createPlainInitStmt.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void createPlainInitStmt(CaptureLog log, int logRecNo) {
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int oid = log.objectIds.get(logRecNo);
    if (this.oidToVarRefMap.containsKey(oid)) {
        // TODO this might happen because of Integer.valueOf(), for example. . Is this approach ok?
        return;
    }
    final String type = log.getTypeName(oid);
    final Object value = log.params.get(logRecNo)[0];
    final VariableReference varRef;
    if (value instanceof Class) {
        // final PrimitiveStatement cps = ClassPrimitiveStatement.getPrimitiveStatement(testCase, getClassForName(type));
        final PrimitiveStatement cps = new ClassPrimitiveStatement(testCase, getClassForName(type));
        cps.setValue(value);
        varRef = testCase.addStatement(cps);
    } else {
        final PrimitiveStatement primitiveValue = PrimitiveStatement.getPrimitiveStatement(testCase, getClassForName(type));
        primitiveValue.setValue(value);
        varRef = testCase.addStatement(primitiveValue);
    }
    this.oidToVarRefMap.put(oid, varRef);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) ClassPrimitiveStatement(org.evosuite.testcase.statements.ClassPrimitiveStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ImmutableStringPrimitiveStatement(org.evosuite.testcase.statements.ImmutableStringPrimitiveStatement) ClassPrimitiveStatement(org.evosuite.testcase.statements.ClassPrimitiveStatement)

Example 12 with PrimitiveStatement

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

the class ConcreteValueObserver 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) {
    int numStatement = statement.getPosition();
    VariableReference returnValue = statement.getReturnValue();
    if (!returnValue.isPrimitive()) {
        // Only interested in primitive values
        return;
    }
    TestCase test = super.getCurrentTest();
    if (test.getStatement(returnValue.getStPosition()) instanceof PrimitiveStatement<?>) {
        // Don't need to collect primitive statement values
        return;
    }
    try {
        Object object = statement.getReturnValue().getObject(scope);
        concreteValues.put(numStatement, object);
    } catch (CodeUnderTestException e) {
    // Ignore
    }
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 13 with PrimitiveStatement

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

the class ConstantInliner method removeUnusedVariables.

/**
 * Remove all unreferenced variables
 *
 * @param t
 *            The test case
 * @return True if something was deleted
 */
public boolean removeUnusedVariables(TestCase t) {
    List<Integer> toDelete = new ArrayList<Integer>();
    boolean hasDeleted = false;
    int num = 0;
    for (Statement s : t) {
        if (s instanceof PrimitiveStatement) {
            VariableReference var = s.getReturnValue();
            if (!t.hasReferences(var)) {
                toDelete.add(num);
                hasDeleted = true;
            }
        }
        num++;
    }
    Collections.sort(toDelete, Collections.reverseOrder());
    for (Integer position : toDelete) {
        t.remove(position);
    }
    return hasDeleted;
}
Also used : PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ArrayList(java.util.ArrayList)

Example 14 with PrimitiveStatement

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

the class BooleanLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    PrimitiveStatement<Boolean> p = (PrimitiveStatement<Boolean>) test.getTestCase().getStatement(statement);
    ExecutionResult oldResult = test.getLastExecutionResult();
    oldValue = p.getValue();
    p.setValue(!oldValue);
    if (!objective.hasImproved(test)) {
        // Restore original
        p.setValue(oldValue);
        test.setLastExecutionResult(oldResult);
        test.setChanged(false);
        return false;
    } else {
        return true;
    }
}
Also used : PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 15 with PrimitiveStatement

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

Aggregations

PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)16 VariableReference (org.evosuite.testcase.variable.VariableReference)11 Statement (org.evosuite.testcase.statements.Statement)6 MethodStatement (org.evosuite.testcase.statements.MethodStatement)5 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)4 TestCase (org.evosuite.testcase.TestCase)3 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)3 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)3 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)3 NullReference (org.evosuite.testcase.variable.NullReference)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 ClassPrimitiveStatement (org.evosuite.testcase.statements.ClassPrimitiveStatement)2 ImmutableStringPrimitiveStatement (org.evosuite.testcase.statements.ImmutableStringPrimitiveStatement)2 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1