use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class CoverageAnalysis method analyzeCoverage.
/**
* Identify all JUnit tests starting with the given name prefix, instrument
* and run tests
*/
public static void analyzeCoverage() {
Sandbox.goingToExecuteSUTCode();
TestGenerationContext.getInstance().goingToExecuteSUTCode();
Sandbox.goingToExecuteUnsafeCodeOnSameThread();
ExecutionTracer.setCheckCallerThread(false);
try {
String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
if (Properties.TARGET_CLASS.endsWith(".jar") || Properties.TARGET_CLASS.contains(File.separator)) {
targetClasses = DependencyAnalysis.analyzeTarget(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
} else {
targetClasses.add(Properties.TARGET_CLASS);
DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
}
LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
} catch (Throwable e) {
LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
logger.error("Problem for " + Properties.TARGET_CLASS + ". Full stack:", e);
return;
} finally {
Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
Sandbox.doneWithExecutingSUTCode();
TestGenerationContext.getInstance().doneWithExecutingSUTCode();
}
// TestCluster.getInstance();
List<Class<?>> testClasses = getTestClasses();
LoggingUtils.getEvoLogger().info("* Found " + testClasses.size() + " test class(es)");
if (testClasses.isEmpty())
return;
/*
* sort them in a deterministic way, in case there are
* static state dependencies
*/
sortTestClasses(testClasses);
Class<?>[] tests = testClasses.toArray(new Class<?>[testClasses.size()]);
LoggingUtils.getEvoLogger().info("* Executing test(s)");
if (Properties.SELECTED_JUNIT == null) {
boolean origUseAgent = EvoRunner.useAgent;
boolean origUseClassLoader = EvoRunner.useClassLoader;
try {
// avoid double instrumentation
EvoRunner.useAgent = false;
// avoid double instrumentation
EvoRunner.useClassLoader = false;
List<JUnitResult> results = executeTests(tests);
printReport(results);
} finally {
EvoRunner.useAgent = origUseAgent;
EvoRunner.useClassLoader = origUseClassLoader;
}
} else {
// instead of just running junit tests, carve them
JUnitTestCarvedChromosomeFactory carvedFactory = new JUnitTestCarvedChromosomeFactory(null);
TestSuiteChromosome testSuite = carvedFactory.getCarvedTestSuite();
int goals = 0;
for (Properties.Criterion pc : Properties.CRITERION) {
LoggingUtils.getEvoLogger().info("* Coverage analysis for criterion " + pc);
TestFitnessFactory ffactory = FitnessFunctions.getFitnessFactory(pc);
goals += ffactory.getCoverageGoals().size();
FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(pc);
ffunction.getFitness(testSuite);
CoverageCriteriaAnalyzer.analyzeCoverage(testSuite, pc);
}
// Generate test suite
TestSuiteGenerator.writeJUnitTestsAndCreateResult(testSuite);
StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals);
if (Properties.COVERAGE_MATRIX)
throw new IllegalArgumentException("Coverage matrix not yet available when measuring coverage of a carved test suite");
}
}
use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class StructuralGoalManager method updateCoveredGoals.
protected void updateCoveredGoals(FitnessFunction<T> f, T tc) {
// the next two lines are needed since that coverage information are used
// during EvoSuite post-processing
TestChromosome tch = (TestChromosome) tc;
tch.getTestCase().getCoveredGoals().add((TestFitnessFunction) f);
// update covered targets
boolean toArchive = false;
T best = coveredGoals.get(f);
if (best == null) {
toArchive = true;
coveredGoals.put(f, tc);
uncoveredGoals.remove(f);
currentGoals.remove(f);
} else {
double bestSize = best.size();
double size = tc.size();
if (size < bestSize && size > 1) {
toArchive = true;
coveredGoals.put(f, tc);
archive.get(best).remove(f);
if (archive.get(best).size() == 0)
archive.remove(best);
}
}
// update archive
if (toArchive) {
List<FitnessFunction<T>> coveredTargets = archive.get(tc);
if (coveredTargets == null) {
List<FitnessFunction<T>> list = new ArrayList<FitnessFunction<T>>();
list.add(f);
archive.put(tc, list);
} else {
coveredTargets.add(f);
}
}
}
use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class MIOArchive method mergeArchiveAndSolution.
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public TestSuiteChromosome mergeArchiveAndSolution(Chromosome solution) {
// Deactivate in case a test is executed and would access the archive as this might cause a
// concurrent access
Properties.TEST_ARCHIVE = false;
TestSuiteChromosome mergedSolution = (TestSuiteChromosome) solution.clone();
// to avoid adding the same solution to 'mergedSolution' suite
Set<T> solutionsSampledFromArchive = new LinkedHashSet<T>();
for (F target : this.archive.keySet()) {
// does solution cover target?
if (!target.isCoveredBy(mergedSolution)) {
Population population = this.archive.get(target);
// is there any solution in the archive that covers it?
T t = population.getBestSolutionIfAny();
if (t != null) {
// has t been considered?
if (!solutionsSampledFromArchive.contains(t)) {
solutionsSampledFromArchive.add(t);
T tClone = (T) t.clone();
mergedSolution.addTest(tClone);
}
}
}
}
// re-evaluate merged solution
for (FitnessFunction fitnessFunction : solution.getFitnessValues().keySet()) {
fitnessFunction.getFitness(mergedSolution);
}
// re-active it
Properties.TEST_ARCHIVE = true;
logger.info("Final test suite size from archive: " + mergedSolution);
return mergedSolution;
}
use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class TestDominanceComparator method testDominanceComparatorOneFitness.
@Test
public void testDominanceComparatorOneFitness() {
Problem p = new Booths<NSGAChromosome>();
List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
FitnessFunction<NSGAChromosome> ff = fitnessFunctions.get(0);
NSGAChromosome c1 = new NSGAChromosome();
NSGAChromosome c2 = new NSGAChromosome();
// Set Fitness
c1.setFitness(ff, 0.7);
c2.setFitness(ff, 0.3);
List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
population.add(c1);
population.add(c2);
DominanceComparator dc = new DominanceComparator();
Collections.sort(population, dc);
Assert.assertEquals(population.get(0).getFitness(ff), 0.3, 0.0);
Assert.assertEquals(population.get(1).getFitness(ff), 0.7, 0.0);
}
use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class TestDominanceComparator method testDominanceComparatorSeveralFitnessesDomination.
@Test
public void testDominanceComparatorSeveralFitnessesDomination() {
Problem p = new FON();
List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
FitnessFunction<NSGAChromosome> ff_1 = fitnessFunctions.get(0);
FitnessFunction<NSGAChromosome> ff_2 = fitnessFunctions.get(1);
NSGAChromosome c1 = new NSGAChromosome();
NSGAChromosome c2 = new NSGAChromosome();
// Set Fitness
c1.setFitness(ff_1, 0.7);
c1.setFitness(ff_2, 0.6);
c2.setFitness(ff_1, 0.3);
c2.setFitness(ff_2, 0.5);
List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
population.add(c1);
population.add(c2);
DominanceComparator dc = new DominanceComparator();
Collections.sort(population, dc);
Assert.assertEquals(population.get(0).getFitness(ff_1), 0.3, 0.0);
Assert.assertEquals(population.get(0).getFitness(ff_2), 0.5, 0.0);
Assert.assertEquals(population.get(1).getFitness(ff_1), 0.7, 0.0);
Assert.assertEquals(population.get(1).getFitness(ff_2), 0.6, 0.0);
}
Aggregations