@Override public void addTests(Collection<TestChromosome> tests) { for (TestChromosome test : tests) { test.setChanged(true); addTest(test); } }
private void restore(TestChromosome test, StringPrimitiveStatement p) { p.setValue(new String(oldValue)); test.setLastExecutionResult(oldResult); test.setChanged(oldChanged); }
private void restore(TestChromosome test, StringPrimitiveStatement p) { p.setValue(new String(oldValue)); test.setLastExecutionResult(oldResult); test.setChanged(oldChanged); }
/** * <p> * setTestCase * </p> * * @param testCase * a {@link org.evosuite.testcase.TestCase} object. */ public void setTestCase(TestCase testCase) { test = testCase; clearCachedResults(); clearCachedMutationResults(); setChanged(true); }
/** * Helper function if this is used without a chromosome * * @param result * @return */ public boolean isCovered(ExecutionResult result) { TestChromosome chromosome = new TestChromosome(); chromosome.setTestCase(result.test); chromosome.setLastExecutionResult(result); chromosome.setChanged(false); return isCovered(chromosome, result); }
/** {@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; }
private static void restoreTestCase(TestChromosome c, TestChromosome copy) { c.test = copy.test; c.copyCachedResults(copy); //c.setFitness(copy.getFitness()); c.setFitnessValues(copy.getFitnessValues()); c.setPreviousFitnessValues(copy.getPreviousFitnessValues()); c.setChanged(false); }
/** * Return the number of covered goals * * @param testClass * @param allGoals * @return */ public static Set<TestFitnessFunction> getCoveredGoals(Class<?> testClass, List<TestFitnessFunction> allGoals) { // A dummy Chromosome TestChromosome dummy = new TestChromosome(); dummy.setChanged(false); // Execution result of a dummy Test Case ExecutionResult executionResult = new ExecutionResult(dummy.getTestCase()); Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>(); List<JUnitResult> results = executeTests(testClass); for (JUnitResult testResult : results) { executionResult.setTrace(testResult.getExecutionTrace()); dummy.setLastExecutionResult(executionResult); for(TestFitnessFunction goal : allGoals) { if(coveredGoals.contains(goal)) continue; else if (goal.isCovered(dummy)) coveredGoals.add(goal); } } return coveredGoals; }
private boolean iterate(double delta, double factor, LocalSearchObjective<TestChromosome> objective, TestChromosome test, NumericalPrimitiveStatement<T> p, int statement) { boolean improvement = false; T oldValue = p.getValue(); ExecutionResult oldResult = test.getLastExecutionResult(); logger.info("Trying increment " + delta + " of " + p.getCode()); p.increment(delta); while (objective.hasImproved(test)) { oldValue = p.getValue(); oldResult = test.getLastExecutionResult(); test.setChanged(false); improvement = true; delta = factor * delta; //if (delta > 1) // return improvement; logger.info("Trying increment " + delta + " of " + p.getCode()); p.increment(delta); } p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); return improvement; } }
/** * Try to replace source of field with all possible choices * * @param test * @param statement */ private boolean replaceFieldSource(TestChromosome test, FieldStatement statement) { if (!statement.isStatic()) { VariableReference source = statement.getSource(); List<VariableReference> objects = test.getTestCase().getObjects(source.getType(), statement.getPosition()); objects.remove(source); if (!objects.isEmpty()) { statement.setSource(Randomness.choice(objects)); test.setChanged(true); } } return false; }
private ExecutionResult getExecutionResult(TestChromosome individual) { ExecutionResult origResult = individual.getLastExecutionResult(); if(origResult == null||individual.isChanged()) { origResult = runTest(individual.getTestCase()); individual.setLastExecutionResult(origResult); individual.setChanged(false); } return individual.getLastExecutionResult(); }
/** * Add a method call on the return value of the object at position statement * * @param test * @param statement */ private boolean addCall(TestChromosome test, int statement) { logger.debug("Adding call"); TestFactory factory = TestFactory.getInstance(); Statement theStatement = test.getTestCase().getStatement(statement); VariableReference var = theStatement.getReturnValue(); int oldLength = test.size(); factory.insertRandomCallOnObjectAt(test.getTestCase(), var, statement + 1); test.setChanged(test.size() != oldLength); return false; }
private boolean iterate(long delta, LocalSearchObjective<TestChromosome> objective, TestChromosome test, NumericalPrimitiveStatement<T> p, int statement) { boolean improvement = false; T oldValue = p.getValue(); ExecutionResult oldResult = test.getLastExecutionResult(); p.increment(delta); logger.info("Trying increment " + delta + " of " + p.getCode()); while (objective.hasImproved(test)) { oldValue = p.getValue(); oldResult = test.getLastExecutionResult(); improvement = true; delta = 2 * delta; p.increment(delta); logger.info("Trying increment " + delta + " of " + p.getCode()); } logger.info("No improvement on " + p.getCode()); p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); logger.info("Final value of this iteration: " + p.getValue()); return improvement; }
protected void restore(TestChromosome test) { if (backup == null) return; test.setTestCase(backup.getTestCase().clone()); test.copyCachedResults(backup); //test.setFitness(backup.getFitness()); test.setFitnessValues(backup.getFitnessValues()); test.setPreviousFitnessValues(backup.getPreviousFitnessValues()); test.setChanged(backup.isChanged()); }
/** * <p> * isCovered * </p> * * @param tc * a {@link org.evosuite.testcase.TestChromosome} object. * @return a boolean. */ public boolean isCovered(TestChromosome tc) { if(tc.getTestCase().isGoalCovered(this)){ return true; } ExecutionResult result = tc.getLastExecutionResult(); if (result == null || tc.isChanged()) { result = runTest(tc.test); tc.setLastExecutionResult(result); tc.setChanged(false); } return isCovered(tc, result); }
/** {@inheritDoc} */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) { EnumPrimitiveStatement p = (EnumPrimitiveStatement) test.getTestCase().getStatement(statement); ExecutionResult oldResult = test.getLastExecutionResult(); oldValue = p.getValue(); for (Object value : p.getEnumValues()) { p.setValue(value); if (!objective.hasImproved(test)) { // Restore original p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); } else { logger.debug("Finished local search with result " + p.getCode()); return true; } } return false; }
private static void reinstrument(TestSuiteChromosome testSuite, Properties.Criterion criterion) { if (ArrayUtil.contains(Properties.SECONDARY_OBJECTIVE, Properties.SecondaryObjective.IBRANCH)) { ExecutionTracer.enableContext(); } if (!ExecutionTracer.isTraceCallsEnabled()) { ExecutionTracer.enableTraceCalls(); } testSuite.setChanged(true); for (TestChromosome test : testSuite.getTestChromosomes()) { test.setChanged(true); test.clearCachedResults(); // clears last execution result and last mutation result } Properties.Criterion oldCriterion[] = Arrays.copyOf(Properties.CRITERION, Properties.CRITERION.length); Properties.CRITERION = new Properties.Criterion[]{criterion}; logger.info("Re-instrumenting for criterion: " + criterion); TestGenerationContext.getInstance().resetContext(); // Need to load class explicitly in case there are no test cases. // If there are tests, then this is redundant Properties.getInitializedTargetClass(); // TODO: Now all existing test cases have reflection objects pointing to the wrong classloader logger.info("Changing classloader of test suite for criterion: " + criterion); for (TestChromosome test : testSuite.getTestChromosomes()) { DefaultTestCase dtest = (DefaultTestCase) test.getTestCase(); dtest.changeClassLoader(TestGenerationContext.getInstance().getClassLoaderForSUT()); } Properties.CRITERION = oldCriterion; }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) { PrimitiveStatement<Boolean> p = (PrimitiveStatement<Boolean>) test.getTestCase().getStatement(statement); ExecutionResult oldResult = test.getLastExecutionResult(); oldValue = p.getValue(); p.setValue(!oldValue); if (!objective.hasImproved(test)) { // Restore original p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); return false; } else { return true; } }
@Override public boolean isNotWorse() { ExecutionResult lastResult = individual.getLastExecutionResult(); individual.setChanged(true); individual.getTestCase().clearCoveredGoals(); double newFitness = fitness.getFitness(individual); boolean worse = false; if(fitness.isMaximizationFunction()) { if (newFitness < lastFitness) worse = true; } else { if (newFitness > lastFitness) worse = true; } if (!worse) { lastFitness = newFitness; individual.setFitness(fitness, lastFitness); return true; } else { individual.setFitness(fitness, lastFitness); individual.setLastExecutionResult(lastResult); return false; } } }
/** * {@inheritDoc} * * Create a deep copy of the chromosome */ @Override public Chromosome clone() { TestChromosome c = new TestChromosome(); c.test = test.clone(); c.setFitnessValues(getFitnessValues()); c.setPreviousFitnessValues(getPreviousFitnessValues()); c.copyCachedResults(this); c.setChanged(isChanged()); c.setLocalSearchApplied(hasLocalSearchBeenApplied()); if (Properties.LOCAL_SEARCH_SELECTIVE) { for (TestMutationHistoryEntry mutation : mutationHistory) { if(test.contains(mutation.getStatement())) c.mutationHistory.addMutationEntry(mutation.clone(c.getTestCase())); } } // c.mutationHistory.set(mutationHistory); c.setNumberOfMutations(this.getNumberOfMutations()); c.setNumberOfEvaluations(this.getNumberOfEvaluations()); c.setKineticEnergy(getKineticEnergy()); c.setNumCollisions(getNumCollisions()); return c; }