use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness.DefUsePairType in project evosuite by EvoSuite.
the class DefUseCoverageSuiteFitness method printCoverage.
/**
* <p>
* printCoverage
* </p>
*/
public static void printCoverage() {
LoggingUtils.getEvoLogger().info("* Time spent optimizing covered goals analysis: " + DefUseExecutionTraceAnalyzer.timeGetCoveredGoals + "ms");
Map<DefUsePairType, Integer> totalGoals = initTotalGoals();
for (DefUsePairType type : DefUseCoverageTestFitness.DefUsePairType.values()) {
LoggingUtils.getEvoLogger().info("* Covered goals of type " + type + ": " + mostCoveredGoals.get(type) + " / " + totalGoals.get(type));
for (DefUseCoverageTestFitness pair : getPairsOfType(type)) {
if (pair.isCovered()) {
LoggingUtils.getEvoLogger().info("*(X) " + pair.toString());
} else {
LoggingUtils.getEvoLogger().info("*( ) " + pair.toString());
}
}
}
LoggingUtils.getEvoLogger().info("* Covered " + countMostCoveredGoals() + "/" + countGoalsIn(totalGoals) + " goals");
}
use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness.DefUsePairType 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;
}
use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness.DefUsePairType in project evosuite by EvoSuite.
the class DefUseCoverageSuiteFitness method getFitnessOld.
/*
* (non-Javadoc)
*
* @see
* org.evosuite.ga.FitnessFunction#getFitness(org.
* evosuite.ga.Chromosome)
*/
/**
* {@inheritDoc}
*/
// @Override
public double getFitnessOld(Chromosome individual) {
logger.trace("Calculating defuse fitness");
TestSuiteChromosome suite = (TestSuiteChromosome) individual;
List<ExecutionResult> results = runTestSuite(suite);
double fitness = 0.0;
if (DefUseCoverageFactory.detectAliasingGoals(results)) {
goals = DefUseCoverageFactory.getDUGoals();
logger.debug("New total number of goals: " + goals.size());
totalGoals = initTotalGoals();
for (DefUsePairType type : totalGoals.keySet()) {
logger.info(type + ":" + totalGoals.get(type));
}
}
Set<DefUseCoverageTestFitness> coveredGoalsSet = DefUseExecutionTraceAnalyzer.getCoveredGoals(results);
initCoverageMaps();
for (DefUseCoverageTestFitness goal : goals) {
if (coveredGoalsSet.contains(goal)) {
goal.setCovered(true);
continue;
}
double goalFitness = 2.0;
for (ExecutionResult result : results) {
TestChromosome tc = new TestChromosome();
tc.setTestCase(result.test);
double resultFitness = goal.getFitness(tc, result);
if (resultFitness < goalFitness)
goalFitness = resultFitness;
if (goalFitness == 0.0) {
result.test.addCoveredGoal(goal);
coveredGoalsSet.add(goal);
goal.setCovered(true);
break;
}
}
fitness += goalFitness;
}
countCoveredGoals(coveredGoalsSet);
trackCoverageStatistics(suite);
updateIndividual(this, suite, 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;
}
use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness.DefUsePairType in project evosuite by EvoSuite.
the class DefUseCoverageSuiteFitness method initTotalGoals.
public static Map<DefUsePairType, Integer> initTotalGoals() {
Map<DefUsePairType, Integer> r = new HashMap<DefUsePairType, Integer>();
// init map
for (DefUsePairType type : DefUseCoverageTestFitness.DefUsePairType.values()) r.put(type, 0);
int num = 0;
// count total goals according to type
for (DefUseCoverageTestFitness goal : DefUseCoverageFactory.getDUGoals()) {
logger.info("Goal " + num);
num++;
r.put(goal.getType(), r.get(goal.getType()) + 1);
}
return r;
}
Aggregations