Search in sources :

Example 1 with NumericalPrimitiveStatement

use of org.evosuite.testcase.statements.numeric.NumericalPrimitiveStatement in project evosuite by EvoSuite.

the class IntegerLocalSearch 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) {
    boolean improved = false;
    TestCase slice = test.getTestCase().clone();
    int newPos = slice.sliceFor(slice.getStatement(statement).getReturnValue());
    TestCase oldTest = test.getTestCase();
    test.setTestCase(slice);
    test.setChanged(true);
    /**
     * Commenting the call to getCopyForTest(). It seems unusual that fitness
     * should be considered on a new test suite instead of the original objective
     */
    // objective = ((TestSuiteLocalSearchObjective)objective).getCopyForTest(test);
    int oldStatement = statement;
    statement = newPos;
    NumericalPrimitiveStatement<T> p = (NumericalPrimitiveStatement<T>) test.getTestCase().getStatement(statement);
    ExecutionResult oldResult = test.getLastExecutionResult();
    oldValue = p.getValue();
    logger.info("Applying search to: " + p.getCode());
    boolean done = false;
    while (!done) {
        done = true;
        // Try +1
        p.increment(1);
        logger.info("Trying increment of " + p.getCode());
        if (objective.hasImproved(test)) {
            done = false;
            improved = true;
            iterate(2, objective, test, p, statement);
            oldValue = p.getValue();
            oldResult = test.getLastExecutionResult();
        } else {
            // Restore original, try -1
            p.setValue(oldValue);
            test.setLastExecutionResult(oldResult);
            test.setChanged(false);
            p.increment(-1);
            logger.info("Trying decrement of " + p.getCode());
            if (objective.hasImproved(test)) {
                improved = true;
                done = false;
                iterate(-2, objective, test, p, statement);
                oldValue = p.getValue();
                oldResult = test.getLastExecutionResult();
            } else {
                p.setValue(oldValue);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
            }
        }
    }
    if (improved) {
        NumericalPrimitiveStatement<T> ps = (NumericalPrimitiveStatement<T>) oldTest.getStatement(oldStatement);
        ps.setValue(p.getValue());
    }
    test.setChanged(true);
    test.setTestCase(oldTest);
    logger.info("Finished local search with result " + p.getCode());
    return improved;
}
Also used : TestCase(org.evosuite.testcase.TestCase) NumericalPrimitiveStatement(org.evosuite.testcase.statements.numeric.NumericalPrimitiveStatement) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 2 with NumericalPrimitiveStatement

use of org.evosuite.testcase.statements.numeric.NumericalPrimitiveStatement in project evosuite by EvoSuite.

the class FloatLocalSearch 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) {
    TestCase slice = test.getTestCase().clone();
    int newPos = slice.sliceFor(slice.getStatement(statement).getReturnValue());
    TestCase oldTest = test.getTestCase();
    test.setTestCase(slice);
    test.setChanged(true);
    /**
     * Commenting the call to getCopyForTest(). It seems unusual that fitness
     * should be considered on a new test suite instead of the original objective
     */
    // objective = ((TestSuiteLocalSearchObjective)objective).getCopyForTest(test);
    int oldStatement = statement;
    statement = newPos;
    boolean improved = false;
    NumericalPrimitiveStatement<T> p = (NumericalPrimitiveStatement<T>) test.getTestCase().getStatement(statement);
    double value = p.getValue().doubleValue();
    if (Double.isInfinite(value) || Double.isNaN(value)) {
        return false;
    }
    logger.info("Applying search to: " + p.getCode());
    int change = doSearch(test, statement, objective, 1.0, 2, p);
    if (change != 0)
        improved = true;
    else {
        // if(change == 0) {
        // Only apply search after the comma if the fitness was affected by the first part of the search
        logger.info("Stopping search as variable doesn't influence fitness");
        test.setTestCase(oldTest);
        return improved;
    }
    logger.info("Checking after the comma: " + p.getCode());
    int maxPrecision = p.getValue().getClass().equals(Float.class) ? 7 : 15;
    for (int precision = 1; precision <= maxPrecision; precision++) {
        if (LocalSearchBudget.getInstance().isFinished())
            break;
        roundPrecision(test, objective, precision, p);
        logger.debug("Current precision: " + precision);
        change = doSearch(test, statement, objective, Math.pow(10.0, -precision), 2, p);
        if (change < 0)
            improved = true;
    }
    logger.debug("Finished local search with result " + p.getCode());
    if (improved) {
        NumericalPrimitiveStatement<T> ps = (NumericalPrimitiveStatement<T>) oldTest.getStatement(oldStatement);
        ps.setValue(p.getValue());
    }
    test.setChanged(true);
    test.setTestCase(oldTest);
    return improved;
}
Also used : TestCase(org.evosuite.testcase.TestCase) NumericalPrimitiveStatement(org.evosuite.testcase.statements.numeric.NumericalPrimitiveStatement)

Aggregations

TestCase (org.evosuite.testcase.TestCase)2 NumericalPrimitiveStatement (org.evosuite.testcase.statements.numeric.NumericalPrimitiveStatement)2 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1