Search in sources :

Example 86 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class DefUseExecutionTraceAnalyzer method getCoveredGoals.

/**
 * <p>
 * getCoveredGoals
 * </p>
 *
 * @param results
 *            a {@link java.util.List} object.
 * @return a {@link java.util.Set} object.
 */
public static Set<DefUseCoverageTestFitness> getCoveredGoals(List<ExecutionResult> results) {
    // did an experiment here: subject ncs.Bessj
    // so as it turns out the getCoveredGoals() might have to run through up
    // to 50k entry big duTraces and thus take up another 20-25% memory, but
    // if you disable this you have to take more time in single fitness
    // calculations and gain worse coverage / less statements per time
    // so for ncs.Bessj with this disabled:
    /*
		 * ida@ubuntu:~/Bachelor/again/evosuite/examples/ncs$ ^C
		 * ida@ubuntu:~/Bachelor/again/evosuite/examples/ncs$ ../../EvoSuite
		 * -generateSuite -criterion defuse -class ncs.Bessj Generating tests
		 * for class ncs.Bessj Test criterion: All DU Pairs Setting up search
		 * algorithm for whole suite generation Goal computation took: 41ms
		 * Starting evolution Alternative fitness calculation disabled!
		 * [Progress:=====> 19%] [Cov:========================> 71%] Search
		 * finished after 600s and 6 generations, 19420 statements, best
		 * individual has fitness 46.49999999592326 Minimizing result Generated
		 * 4 tests with total length 17 Resulting TestSuite's coverage: 71%
		 * GA-Budget: - ShutdownTestWriter : 0 / 0 - GlobalTime : 602 / 600
		 * Finished! - ZeroFitness : 46 / 0 - MaxStatements : 20.881 / 100.000
		 * Covered 92/129 goals Time spent optimizing covered goals analysis:
		 * 0ms Time spent executing tests: 59980ms Writing JUnit test cases to
		 * evosuite-tests/DEFUSE Time spent calculating single fitnesses:
		 * 540751ms Done!
		 */
    // and enabled:
    /*
		 * ida@ubuntu:~/Bachelor/again/evosuite/examples/ncs$ ../../EvoSuite
		 * -generateSuite -criterion defuse -class ncs.Bessj Generating tests
		 * for class ncs.Bessj Test criterion: All DU Pairs Setting up search
		 * algorithm for whole suite generation Goal computation took: 42ms
		 * Starting evolution Alternative fitness calculation disabled!
		 * [Progress:=======> 25%] [Cov:========================> 71%] Search
		 * finished after 600s and 11 generations, 25732 statements, best
		 * individual has fitness 46.49999999553073 Minimizing result Generated
		 * 4 tests with total length 19 Resulting TestSuite's coverage: 71%
		 * GA-Budget: - ShutdownTestWriter : 0 / 0 - MaxStatements : 28.030 /
		 * 100.000 - ZeroFitness : 46 / 0 - GlobalTime : 604 / 600 Finished!
		 * Covered 92/129 goals Time spent optimizing covered goals analysis:
		 * 414365ms Time spent executing tests: 87669ms Writing JUnit test cases
		 * to evosuite-tests/DEFUSE Time spent calculating single fitnesses:
		 * 100324ms Done!
		 */
    // so we have 25% more executed statements, which means this will stay
    // enabled
    // System.out.println("start");
    long start = System.currentTimeMillis();
    Set<DefUseCoverageTestFitness> r = new HashSet<DefUseCoverageTestFitness>();
    for (ExecutionResult result : results) {
        Set<DefUseCoverageTestFitness> goals = getCoveredGoals(result);
        r.addAll(goals);
    }
    timeGetCoveredGoals += System.currentTimeMillis() - start;
    return r;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) HashSet(java.util.HashSet)

Example 87 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class TestFitnessFunction method getFitness.

/**
 * {@inheritDoc}
 */
@Override
public double getFitness(TestChromosome individual) {
    logger.trace("Executing test case on original");
    ExecutionResult origResult = individual.getLastExecutionResult();
    if (origResult == null || individual.isChanged()) {
        origResult = runTest(individual.test);
        individual.setLastExecutionResult(origResult);
        individual.setChanged(false);
    }
    double fitness = getFitness(individual, origResult);
    updateIndividual(this, individual, fitness);
    return fitness;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 88 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class DSETestGenerator method generateNewTest.

/**
 * Applies DSE to the passed test using as symbolic variables only those
 * that are declared in the set of statement indexes. The objective is used
 * to detect if the DSE has improved the fitness.
 *
 * @param test
 *            the test case to be used as parameterised unit test
 *
 * @param statementIndexes
 *            a set with statement indexes with primitive value declarations
 *            that can be used as symbolic variables. This set must be
 *            non-empty.
 *
 * @param objective
 *            the local search objective to measure fitness improvement.
 */
public TestChromosome generateNewTest(final TestChromosome test, Set<Integer> statementIndexes, LocalSearchObjective<TestChromosome> objective) {
    logger.info("APPLYING DSE EEEEEEEEEEEEEEEEEEEEEEE");
    logger.info(test.getTestCase().toCode());
    logger.info("Starting concolic execution");
    // Backup copy
    // test.getMutationHistory().clear();
    // I am not sure what is the purpose of this
    test.clone();
    DefaultTestCase clone_test_case = (DefaultTestCase) test.getTestCase().clone();
    List<BranchCondition> branchConditions = ConcolicExecution.executeConcolic(clone_test_case);
    final PathCondition collectedPathCondition = new PathCondition(branchConditions);
    logger.info("Done concolic execution");
    if (collectedPathCondition.isEmpty()) {
        return null;
    }
    for (BranchCondition c : collectedPathCondition.getBranchConditions()) {
        logger.info(" -> " + c.getConstraint());
    }
    Set<VariableReference> symbolicVariables = new HashSet<VariableReference>();
    for (Integer position : statementIndexes) {
        final VariableReference variableReference = test.getTestCase().getStatement(position).getReturnValue();
        symbolicVariables.add(variableReference);
    }
    logger.info("Checking {} conditions", collectedPathCondition.size());
    List<Integer> conditionIndexesNotCoveredTwoWays = computeConditionIndexesNotCoveredTwoWays(test, collectedPathCondition);
    // 
    for (int conditionIndex = 0; conditionIndex < collectedPathCondition.size(); conditionIndex++) {
        BranchCondition condition = collectedPathCondition.get(conditionIndex);
        if (LocalSearchBudget.getInstance().isFinished()) {
            logger.debug("Local search budget used up: " + Properties.LOCAL_SEARCH_BUDGET_TYPE);
            break;
        }
        logger.debug("Local search budget not yet used up");
        if (!conditionIndexesNotCoveredTwoWays.contains(conditionIndex)) {
            // skip branches covered two ways
            continue;
        }
        logger.info("Current condition: " + conditionIndex + "/" + collectedPathCondition.size() + ": " + condition.getConstraint());
        // Determine if this a branch condition depending on the target
        // statement
        Constraint<?> currentConstraint = condition.getConstraint();
        if (!isRelevant(currentConstraint, symbolicVariables)) {
            // if(!isRelevant(currentConstraint, test.getTestCase(),
            // statement)) {
            logger.info("Is not relevant for " + symbolicVariables);
            continue;
        }
        logger.info("Is relevant for " + symbolicVariables);
        List<Constraint<?>> query = buildQuery(collectedPathCondition, conditionIndex);
        logger.info("Trying to solve: ");
        for (Constraint<?> c : query) {
            logger.info("  " + c);
        }
        DSEStats.getInstance().reportNewConstraints(query);
        // Get solution
        Solver solver = SolverFactory.getInstance().buildNewSolver();
        long startSolvingTime = System.currentTimeMillis();
        SolverCache solverCache = SolverCache.getInstance();
        SolverResult solverResult = solverCache.solve(solver, query);
        long estimatedSolvingTime = System.currentTimeMillis() - startSolvingTime;
        DSEStats.getInstance().reportNewSolvingTime(estimatedSolvingTime);
        if (solverResult == null) {
            logger.info("Found no result");
        } else if (solverResult.isUNSAT()) {
            logger.info("Found UNSAT result");
            DSEStats.getInstance().reportNewUNSAT();
        } else {
            logger.info("Found SAT result");
            DSEStats.getInstance().reportNewSAT();
            Map<String, Object> model = solverResult.getModel();
            TestCase oldTest = test.getTestCase();
            ExecutionResult oldResult = test.getLastExecutionResult().clone();
            TestCase newTest = updateTest(oldTest, model);
            logger.info("New test: " + newTest.toCode());
            test.setTestCase(newTest);
            // test.clearCachedMutationResults(); // TODO Mutation
            test.clearCachedResults();
            if (objective.hasImproved(test)) {
                DSEStats.getInstance().reportNewTestUseful();
                logger.info("Solution improves fitness, finishing DSE");
                /* new test was created */
                return test;
            } else {
                DSEStats.getInstance().reportNewTestUnuseful();
                test.setTestCase(oldTest);
                // FIXXME: How can this be null?
                if (oldResult != null)
                    test.setLastExecutionResult(oldResult);
            // TODO Mutation
            }
        }
    }
    /* no new test was created */
    return null;
}
Also used : PathCondition(org.evosuite.symbolic.PathCondition) Solver(org.evosuite.symbolic.solver.Solver) VariableReference(org.evosuite.testcase.variable.VariableReference) Constraint(org.evosuite.symbolic.expr.Constraint) SolverResult(org.evosuite.symbolic.solver.SolverResult) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCondition(org.evosuite.symbolic.BranchCondition) SolverCache(org.evosuite.symbolic.solver.SolverCache) Constraint(org.evosuite.symbolic.expr.Constraint) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Map(java.util.Map) HashSet(java.util.HashSet)

Example 89 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class DSETestGenerator method isCoveredTwoWays.

/**
 * Returns if the true and false branches for this were already covered. If
 * the test case belongs to a whole test suite, then the coverage of the
 * test suite is used, otherwise the single test case is used.
 *
 * @param className
 * @param methodName
 * @param branchIndex
 * @return
 */
private boolean isCoveredTwoWays(TestChromosome test, int branchIndex) {
    Set<Integer> trueIndexes = new HashSet<Integer>();
    Set<Integer> falseIndexes = new HashSet<Integer>();
    if (suite != null) {
        for (ExecutionResult execResult : this.suite.getLastExecutionResults()) {
            Set<Integer> trueIndexesInTrace = execResult.getTrace().getCoveredTrueBranches();
            Set<Integer> falseIndexesInTrace = execResult.getTrace().getCoveredFalseBranches();
            trueIndexes.addAll(trueIndexesInTrace);
            falseIndexes.addAll(falseIndexesInTrace);
        }
    } else {
        ExecutionResult execResult = test.getLastExecutionResult();
        Set<Integer> trueIndexesInTest = execResult.getTrace().getCoveredTrueBranches();
        Set<Integer> falseIndexesInTest = execResult.getTrace().getCoveredFalseBranches();
        trueIndexes.addAll(trueIndexesInTest);
        falseIndexes.addAll(falseIndexesInTest);
    }
    final boolean trueIsCovered = trueIndexes.contains(branchIndex);
    final boolean falseIsCovered = falseIndexes.contains(branchIndex);
    return trueIsCovered && falseIsCovered;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) HashSet(java.util.HashSet)

Example 90 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class FloatLocalSearch method roundPrecision.

@SuppressWarnings("unchecked")
private boolean roundPrecision(TestChromosome test, LocalSearchObjective<TestChromosome> objective, int precision, NumericalPrimitiveStatement<T> p) {
    double value = p.getValue().doubleValue();
    if (Double.isInfinite(value) || Double.isNaN(value)) {
        return false;
    }
    BigDecimal bd = new BigDecimal(value).setScale(precision, RoundingMode.HALF_EVEN);
    if (bd.doubleValue() == value) {
        return false;
    }
    double newValue = bd.doubleValue();
    oldValue = p.getValue();
    ExecutionResult oldResult = test.getLastExecutionResult();
    if (p.getValue().getClass().equals(Float.class))
        p.setValue((T) (new Float(newValue)));
    else
        p.setValue((T) (new Double(newValue)));
    logger.info("Trying to chop precision " + precision + ": " + value + " -> " + newValue);
    if (objective.hasNotWorsened(test)) {
        return true;
    } else {
        logger.info("Restoring old value: " + value);
        p.setValue(oldValue);
        test.setLastExecutionResult(oldResult);
        test.setChanged(false);
        return false;
    }
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) BigDecimal(java.math.BigDecimal)

Aggregations

ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)93 TestChromosome (org.evosuite.testcase.TestChromosome)33 HashSet (java.util.HashSet)15 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)15 ArrayList (java.util.ArrayList)11 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)10 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 TestCase (org.evosuite.testcase.TestCase)9 VariableReference (org.evosuite.testcase.variable.VariableReference)9 Test (org.junit.Test)9 LinkedHashSet (java.util.LinkedHashSet)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)7 ArrayReference (org.evosuite.testcase.variable.ArrayReference)7 Set (java.util.Set)6 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)6 Map (java.util.Map)4 Entry (java.util.Map.Entry)3 ExecutionTrace (org.evosuite.testcase.execution.ExecutionTrace)3