public ExecutionResult getLastRegressionExecutionResult() { return theSameTestForTheOtherClassLoader.getLastExecutionResult(); }
@Override public boolean doSearch( TestChromosome individual, LocalSearchObjective<TestChromosome> objective) { logger.info("Test before local search: " + individual.getTestCase().toCode()); boolean improved = false; // Only apply local search up to the point where an exception was thrown // TODO: Check whether this conflicts with test expansion int lastPosition = individual.size() - 1; if (individual.getLastExecutionResult() != null && !individual.isChanged()) { Integer lastPos = individual.getLastExecutionResult().getFirstPositionOfThrownException(); if (lastPos != null) lastPosition = lastPos.intValue(); } TestCase test = individual.getTestCase(); // We count down to make the code work when lines are // added during the search (see NullReferenceSearch). for (int i = lastPosition; i >= 0; i--) { if (LocalSearchBudget.getInstance().isFinished()) break; if (objective.isDone()) { break; } if (i >= individual.size()) { logger.warn("Test size decreased unexpectedly during local search, aborting local search"); logger.warn(individual.getTestCase().toCode()); break; } final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); final Statement statement = test.getStatement(i); if (!test.hasReferences(statement.getReturnValue()) && !statement.getReturnClass().equals(targetClass)) { logger.info( "Return value of statement " + i + " is not referenced and not SUT, not doing local search"); continue; } StatementLocalSearch search = StatementLocalSearch.getLocalSearchFor(statement); if (search != null) { logger.info( "Applying local search of type " + search.getClass() + " to statement " + statement + " / " + individual.getTestCase().getStatement(i)); if (search.doSearch(individual, i, (LocalSearchObjective<TestChromosome>) objective)) { improved = true; } // i = s.getPosition(); logger.debug( "Old position was: " + i + ", adjusting to: " + (i + search.getPositionDelta())); i += search.getPositionDelta(); test = individual.getTestCase(); } else { /* * No statement local search has been produced for this * statement. Skipping. */ continue; } } LocalSearchBudget.getInstance().countLocalSearchOnTest(); // logger.warn("Test after local search: " + // individual.getTestCase().toCode()); // Return true iif search was successful return improved; // TODO: Handle arrays in local search // TODO: mutating an int might have an effect on array lengths }
@Override public ExecutionResult getLastExecutionResult() { return theTest.getLastExecutionResult(); }
// 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); Set<DefUseCoverageTestFitness> coveredGoalsSet = new HashSet< DefUseCoverageTestFitness>(); // DefUseExecutionTraceAnalyzer.getCoveredGoals(results); 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; } // 2. Need to execute each definition X times // 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; }