/** * {@inheritDoc} * * Replace chromosome at position */ @SuppressWarnings("unchecked") @Override public void crossOver(Chromosome other, int position) throws ConstructionFailedException { if (!(other instanceof AbstractTestSuiteChromosome<?>)) { throw new IllegalArgumentException( "AbstractTestSuiteChromosome.crossOver() called with parameter of unsupported type " + other.getClass()); } AbstractTestSuiteChromosome<T> chromosome = (AbstractTestSuiteChromosome<T>) other; T otherTest = chromosome.tests.get(position); T clonedTest = (T) otherTest.clone(); tests.add(clonedTest); this.setChanged(true); }
/** * <p> * runTestSuite * </p> * * @param suite * a {@link org.evosuite.testsuite.AbstractTestSuiteChromosome} * object. * @return a {@link java.util.List} object. */ protected List<ExecutionResult> runTestSuite( AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) { List<ExecutionResult> results = new ArrayList<ExecutionResult>(); for (ExecutableChromosome chromosome : suite.getTestChromosomes()) { // Only execute test if it hasn't been changed if (chromosome.isChanged() || chromosome.getLastExecutionResult() == null) { ExecutionResult result = chromosome.executeForFitnessFunction(this); if (result != null) { results.add(result); chromosome.setLastExecutionResult(result); // .clone(); chromosome.setChanged(false); } } else { results.add(chromosome.getLastExecutionResult()); } } suite.setChanged(false); return results; }
private void backup(ExecutableChromosome test, Statement p) { oldResult = test.getLastExecutionResult(); oldChanged = test.isChanged(); }
private void restore(ExecutableChromosome test, Statement p) { test.setLastExecutionResult(oldResult); test.setChanged(oldChanged); }
test.setChanged(true); test.clearCachedResults();
public List<ExecutionResult> getLastExecutionResults() { return tests.stream().map(t -> t.getLastExecutionResult()).collect(Collectors.toList()); }
/** {@inheritDoc} */ @Override public void setChanged(boolean changed) { super.setChanged(changed); if (changed) { clearCachedResults(); } }
/** {@inheritDoc} */ @Override public int compareTo(Chromosome o) { int result = super.compareTo(o); if (result != 0) { return result; } // make this deliberately not 0 // because then ordering of results will be random // among tests of equal fitness if (o instanceof TestChromosome) { return test.toCode().compareTo(((TestChromosome) o).test.toCode()); } return result; }
/** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof TestSuiteChromosome)) return false; TestSuiteChromosome other = (TestSuiteChromosome) obj; if (other.size() != size()) return false; for (int i = 0; i < size(); i++) { if (!tests.get(i).equals(other.tests.get(i))) return false; } return true; }
double fit = b ? N-i : i; ind.addFitness(null, fit);
private int getNumExceptions(TestSuiteChromosome chromosome) { int sum = 0; for (ExecutableChromosome test : chromosome.getTestChromosomes()) { if (test.getLastExecutionResult() != null) sum += test.getLastExecutionResult().getNumberOfThrownExceptions(); } return sum; }
/** * {@inheritDoc} * * Keep up to position1, append copy of other from position2 on */ @SuppressWarnings("unchecked") @Override public void crossOver(Chromosome other, int position1, int position2) throws ConstructionFailedException { if (!(other instanceof AbstractTestSuiteChromosome<?>)) { throw new IllegalArgumentException( "AbstractTestSuiteChromosome.crossOver() called with parameter of unsupported type " + other.getClass()); } AbstractTestSuiteChromosome<T> chromosome = (AbstractTestSuiteChromosome<T>) other; while (tests.size() > position1) { tests.remove(position1); } for (int num = position2; num < other.size(); num++) { T otherTest = chromosome.tests.get(num); T clonedTest = (T) otherTest.clone(); tests.add(clonedTest); } this.setChanged(true); }
/** * Creates a deep copy of source. * * @param source a {@link org.evosuite.testsuite.AbstractTestSuiteChromosome} object. */ @SuppressWarnings("unchecked") protected AbstractTestSuiteChromosome(AbstractTestSuiteChromosome<T> source) { this(source.testChromosomeFactory); for (T test : source.tests) { addTest((T) test.clone()); } //this.setFitness(source.getFitness()); this.setFitnessValues(source.getFitnessValues()); this.setPreviousFitnessValues(source.getPreviousFitnessValues()); this.setChanged(source.isChanged()); this.setCoverageValues(source.getCoverageValues()); this.setNumsOfCoveredGoals(source.getNumsOfCoveredGoals()); this.setNumsOfNotCoveredGoals(source.getNumsNotCoveredGoals()); this.setNumberOfMutations(source.getNumberOfMutations()); this.setNumberOfEvaluations(source.getNumberOfEvaluations()); this.setKineticEnergy(source.getKineticEnergy()); this.setNumCollisions(source.getNumCollisions()); }