Search in sources :

Example 6 with AbstractTestSuiteChromosome

use of org.evosuite.testsuite.AbstractTestSuiteChromosome in project evosuite by EvoSuite.

the class RegressionTestSuiteChromosome method getTestSuite.

public AbstractTestSuiteChromosome<TestChromosome> getTestSuite() {
    AbstractTestSuiteChromosome<TestChromosome> suite = new TestSuiteChromosome();
    for (TestChromosome regressionTest : tests) {
        RegressionTestChromosome rtc = (RegressionTestChromosome) regressionTest;
        suite.addTest(rtc.getTheTest());
    }
    return suite;
}
Also used : AbstractTestSuiteChromosome(org.evosuite.testsuite.AbstractTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 7 with AbstractTestSuiteChromosome

use of org.evosuite.testsuite.AbstractTestSuiteChromosome in project evosuite by EvoSuite.

the class DefUseCoverageSuiteFitness method getFitnessAlternative.

// Not working yet
// @Override
public double getFitnessAlternative(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
    TestSuiteChromosome suite = (TestSuiteChromosome) individual;
    List<ExecutionResult> results = runTestSuite(suite);
    if (DefUseCoverageFactory.detectAliasingGoals(results)) {
        logger.debug("New total number of goals: " + goals.size());
        totalGoals = initTotalGoals();
        for (DefUsePairType type : totalGoals.keySet()) {
            logger.info(type + ":" + totalGoals.get(type));
        }
    }
    Map<Definition, Set<TestChromosome>> passedDefinitions = new HashMap<Definition, Set<TestChromosome>>();
    Map<Definition, Integer> passedDefinitionCount = new HashMap<Definition, Integer>();
    Map<String, Set<TestChromosome>> executedMethods = new HashMap<String, Set<TestChromosome>>();
    Map<String, Integer> executedMethodsCount = new HashMap<String, Integer>();
    for (Definition def : maxDefinitionCount.keySet()) {
        passedDefinitionCount.put(def, 0);
    }
    for (String methodName : maxMethodCount.keySet()) {
        executedMethodsCount.put(methodName, 0);
    }
    for (TestChromosome test : suite.getTestChromosomes()) {
        ExecutionResult result = test.getLastExecutionResult();
        if (result.hasTimeout()) {
            logger.debug("Skipping test with timeout");
            double fitness = goals.size() * 100;
            updateIndividual(this, individual, fitness);
            suite.setCoverage(this, 0.0);
            logger.debug("Test case has timed out, setting fitness to max value " + fitness);
            return fitness;
        }
        for (Entry<Integer, Integer> entry : result.getTrace().getDefinitionExecutionCount().entrySet()) {
            Definition def = DefUsePool.getDefinitionByDefId(entry.getKey());
            if (def == null) {
                logger.warn("Could not find def " + entry.getKey());
                continue;
            }
            if (!passedDefinitions.containsKey(def))
                passedDefinitions.put(def, new HashSet<TestChromosome>());
            if (!passedDefinitionCount.containsKey(def)) {
                // logger.warn("Weird, definition is not known: " + def);
                passedDefinitionCount.put(def, 0);
            }
            passedDefinitions.get(def).add(test);
            passedDefinitionCount.put(def, passedDefinitionCount.get(def) + entry.getValue());
        }
        for (Entry<String, Integer> entry : result.getTrace().getMethodExecutionCount().entrySet()) {
            if (executedMethodsCount.containsKey(entry.getKey()))
                executedMethodsCount.put(entry.getKey(), executedMethodsCount.get(entry.getKey()) + entry.getValue());
            if (!executedMethods.containsKey(entry.getKey())) {
                executedMethods.put(entry.getKey(), new HashSet<TestChromosome>());
            }
            executedMethods.get(entry.getKey()).add(test);
        }
    /*
			for (Integer id : result.getTrace().getPassedDefIDs()) {
				Definition def = DefUsePool.getDefinitionByDefId(id);
				if (!passedDefinitions.containsKey(def))
					passedDefinitions.put(def, new HashSet<TestChromosome>());

				passedDefinitions.get(def).add(test);
				passedDefinitionCount.put(def, passedDefinitionCount.get(def) + 1);
			}
			*/
    }
    // 1. Need to reach each definition
    double fitness = branchFitness.getFitness(individual);
    // logger.info("Branch fitness: " + fitness);
    // 3. For all covered defs, calculate minimal use distance
    // Set<DefUseCoverageTestFitness> coveredGoalsSet = DefUseExecutionTraceAnalyzer.getCoveredGoals(results);
    // DefUseExecutionTraceAnalyzer.getCoveredGoals(results);
    Set<DefUseCoverageTestFitness> coveredGoalsSet = new HashSet<DefUseCoverageTestFitness>();
    initCoverageMaps();
    Set<Definition> notFullyCoveredDefs = new HashSet<Definition>();
    boolean methodIsNotFullyCovered = false;
    for (DefUseCoverageTestFitness goal : goals) {
        if (coveredGoalsSet.contains(goal)) {
            continue;
        }
        double goalFitness = 2.0;
        Set<TestChromosome> coveringTests = new HashSet<TestChromosome>();
        if (goal.isParameterGoal()) {
            String methodKey = goal.getGoalUse().getClassName() + "." + goal.getGoalUse().getMethodName();
            if (executedMethods.containsKey(methodKey)) {
                coveringTests.addAll(executedMethods.get(methodKey));
            }
        } else {
            if (passedDefinitions.containsKey(goal.getGoalDefinition())) {
                coveringTests.addAll(passedDefinitions.get(goal.getGoalDefinition()));
            }
        }
        if (coveringTests.isEmpty()) {
            logger.debug("No tests cover " + goal);
        } else {
            logger.debug("Checking " + coveringTests.size() + " tests covering " + goal);
        }
        // for (TestChromosome test : passedDefinitions.get(goal.getGoalDefinition())) {
        for (TestChromosome test : coveringTests) {
            // for (TestChromosome test : suite.getTestChromosomes()) {
            ExecutionResult result = test.getLastExecutionResult();
            DefUseFitnessCalculator calculator = new DefUseFitnessCalculator(goal, test, result);
            // double resultFitness = goal.getFitness(test, result);
            double resultFitness = calculator.calculateDUFitness();
            if (resultFitness < goalFitness)
                goalFitness = resultFitness;
            if (goalFitness == 0.0) {
                result.test.addCoveredGoal(goal);
                coveredGoalsSet.add(goal);
                break;
            }
        }
        if (goalFitness > 0.0) {
            if (goal.isParameterGoal())
                notFullyCoveredDefs.add(goal.getGoalDefinition());
            else
                methodIsNotFullyCovered = true;
        }
        fitness += goalFitness;
    }
    // TODO ...unless all defuse pairs are covered?
    for (Entry<Definition, Integer> defCount : maxDefinitionCount.entrySet()) {
        if (notFullyCoveredDefs.contains(defCount.getKey())) {
            int executionCount = passedDefinitionCount.get(defCount.getKey());
            int max = defCount.getValue();
            if (executionCount < max) {
                fitness += normalize(max - executionCount);
            }
        }
    }
    if (methodIsNotFullyCovered) {
        for (Entry<String, Integer> methodCount : maxMethodCount.entrySet()) {
            int executionCount = executedMethodsCount.get(methodCount.getKey());
            int max = methodCount.getValue();
            if (executionCount < max) {
                fitness += normalize(max - executionCount);
            }
        }
    }
    countCoveredGoals(coveredGoalsSet);
    trackCoverageStatistics(suite);
    updateIndividual(this, individual, fitness);
    int coveredGoalCount = countCoveredGoals();
    int totalGoalCount = countTotalGoals();
    if (fitness == 0.0 && coveredGoalCount < totalGoalCount)
        throw new IllegalStateException("Fitness 0 implies 100% coverage " + coveredGoalCount + " / " + totalGoals + " (covered / total)");
    return fitness;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefUsePairType(org.evosuite.coverage.dataflow.DefUseCoverageTestFitness.DefUsePairType) AbstractTestSuiteChromosome(org.evosuite.testsuite.AbstractTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Aggregations

TestChromosome (org.evosuite.testcase.TestChromosome)7 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)7 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)7 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)5 Entry (java.util.Map.Entry)3 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)3 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 DefUsePairType (org.evosuite.coverage.dataflow.DefUseCoverageTestFitness.DefUsePairType)1 ExecutionTrace (org.evosuite.testcase.execution.ExecutionTrace)1