/** * Creates a new <code>TestSuiteLocalSearchObjective</code> for a given list * of fitness functions, a test suite and a <code>testIndex</code> for * replacing an optimised test case (i.e. a test case over which was applied * local search) * * @param fitness * the list of fitness functions to be used on the modified test * suite * @param suite * the original test suite * @param index * the index (between 0 and the suite length) that will be * replaced with a new test case * @return */ public static TestSuiteLocalSearchObjective buildNewTestSuiteLocalSearchObjective( List<FitnessFunction<? extends Chromosome>> fitness, TestSuiteChromosome suite, int index) { List<TestSuiteFitnessFunction> ffs = new ArrayList<>(); for (FitnessFunction<? extends Chromosome> ff : fitness) { TestSuiteFitnessFunction tff = (TestSuiteFitnessFunction) ff; ffs.add(tff); } return new TestSuiteLocalSearchObjective(ffs, suite, index); }
/** * Replaces the test case at position <code>testIndex</code> with the passed * TestChromosome. */ @Override public boolean hasImproved(TestChromosome testCase) { return hasChanged(testCase) < 0; }
/** * Creates a Local Search objective for a TestCase that will be optimized * using a containing TestSuite to measure the changes in fitness values. * * @param fitness * the list of fitness functions to use to compute the fitness of * the TestSuiteChromosome * @param suite * a TestSuite chromosome that will be subjected to local search * @param index * a test index (between 0 and the test suite length) that will * be used to modify the testchromosome each time a changed has * been applied. */ private TestSuiteLocalSearchObjective(List<TestSuiteFitnessFunction> fitness, TestSuiteChromosome suite, int index) { this.fitnessFunctions.addAll(fitness); this.suite = suite; this.testIndex = index; for (TestSuiteFitnessFunction ff : fitness) { if (ff.isMaximizationFunction()) isMaximization = true; else isMaximization = false; break; } updateLastFitness(); updateLastCoverage(); }
/** * Overrides the test case at position <code>testIndex</code> with the * individual. It returns <code>-1</code> if the new fitness has improved, * <code>1</code> if the fitness has worsened or <code>0</code> if the * fitness has not changed. */ @Override public int hasChanged(TestChromosome testCase) { testCase.setChanged(true); suite.setTestChromosome(testIndex, testCase); LocalSearchBudget.getInstance().countFitnessEvaluation(); for (TestSuiteFitnessFunction fitnessFunction : fitnessFunctions) fitnessFunction.getFitness(suite); double newFitness = suite.getFitness(); if (isFitnessBetter(newFitness, lastFitnessSum)) { logger.info("Local search improved fitness from " + lastFitnessSum + " to " + newFitness); updateLastFitness(); updateLastCoverage(); return -1; } else if (isFitnessWorse(newFitness, lastFitnessSum)) { logger.info("Local search worsened fitness from " + lastFitnessSum + " to " + newFitness); suite.setFitnessValues(lastFitness); suite.setCoverageValues(lastCoverage); return 1; } else { logger.info("Local search did not change fitness of " + lastFitnessSum); updateLastCoverage(); return 0; } }
/** * Applies DSE on the test case of the suite * * @param suite * @param testIndex * @param test * @param objective * @return */ private boolean applyDSE(TestSuiteChromosome suite, int testIndex, TestChromosome test, LocalSearchObjective<TestSuiteChromosome> objective) { TestSuiteLocalSearchObjective testSuiteObject = TestSuiteLocalSearchObjective .buildNewTestSuiteLocalSearchObjective(objective.getFitnessFunctions(), suite, testIndex); DSETestCaseLocalSearch dseTestCaseLocalSearch = new DSETestCaseLocalSearch(suite); boolean improved = dseTestCaseLocalSearch.doSearch(test, testSuiteObject); return improved; }
/** * Applies AVM on the test case in the suite * * @param suite * @param testIndex * @param test * @param localSearchObjective * @return */ private boolean applyAVM(TestSuiteChromosome suite, int testIndex, TestChromosome test, LocalSearchObjective<TestSuiteChromosome> objective) { logger.debug("Local search on test " + testIndex + ", current fitness: " + suite.getFitness()); final List<FitnessFunction<? extends Chromosome>> fitnessFunctions = objective.getFitnessFunctions(); TestSuiteLocalSearchObjective testCaseLocalSearchObjective = TestSuiteLocalSearchObjective .buildNewTestSuiteLocalSearchObjective(fitnessFunctions, suite, testIndex); AVMTestCaseLocalSearch testCaselocalSearch = new AVMTestCaseLocalSearch(); boolean improved = testCaselocalSearch.doSearch(test, testCaseLocalSearchObjective); return improved; }
.buildNewTestSuiteLocalSearchObjective(Collections.singletonList(branchCoverageSuiteFitness), suite, 1);
/** * Returns true if by replacing the test case at position * <code>testIndex</code> with the argument <code>testCase</code>, the * resulting test suite has not worsened the fitness */ @Override public boolean hasNotWorsened(TestChromosome testCase) { return hasChanged(testCase) < 1; }
.buildNewTestSuiteLocalSearchObjective(Collections.singletonList(branchCoverageSuiteFitness), suite, 1);