use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class TestCaseReplacer method replaceTest.
/**
* Given a test, create a GA and look for a replacement test
*
* @param test
*/
public TestCase replaceTest(String targetClass, List<TestCase> otherTests, TestCase test) {
// Various environmental setup necessary for EvoSuite
Properties.ALGORITHM = Algorithm.MONOTONICGA;
Properties.STRATEGY = Strategy.ONEBRANCH;
ExecutionTracer.enableTraceCalls();
// Run for 10 generations - adapt as necessary
// Properties.STOPPING_CONDITION = StoppingCondition.MAXGENERATIONS;
// Properties.SEARCH_BUDGET = 20;
// Properties.STOPPING_CONDITION = StoppingCondition.MAXTIME;
// Properties.SEARCH_BUDGET = 20;
// GeneticAlgorithm ga = TestSuiteGenerator.getGeneticAlgorithm(new RandomLengthTestFactory());
// TODO: JM: Needs Testing. Not sure if this is equivalent:
PropertiesTestGAFactory algorithmFactory = new PropertiesTestGAFactory();
GeneticAlgorithm<TestChromosome> ga = algorithmFactory.getSearchAlgorithm();
List<TestFitnessFactory<? extends TestFitnessFunction>> factories = TestSuiteGenerator.getFitnessFactories();
Collection<TestFitnessFunction> fitnessFunctions = new ArrayList<TestFitnessFunction>();
for (TestFitnessFactory<? extends TestFitnessFunction> factory : factories) {
// Set up fitness function for the parsed test case
DifferenceFitnessFunction fitnessFunction = new DifferenceFitnessFunction(test, otherTests, factory);
// ga.setFitnessFunction(fitness);
fitnessFunctions.add(fitnessFunction);
ga.addFitnessFunction(fitnessFunction);
}
// Perform calculation
ga.generateSolution();
// The best individual at the end of the search contains our candidate solution
TestChromosome testChromosome = (TestChromosome) ga.getBestIndividual();
TestCaseMinimizer minimizer = new TestCaseMinimizer(fitnessFunctions);
minimizer.minimize(testChromosome);
System.out.println("Best individual has fitness: " + testChromosome.getFitness());
return testChromosome.getTestCase();
}
use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class DSEStrategy method getGoals.
private List<TestFitnessFunction> getGoals(boolean verbose) {
List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
List<TestFitnessFunction> goals = new ArrayList<>();
if (goalFactories.size() == 1) {
TestFitnessFactory<? extends TestFitnessFunction> factory = goalFactories.iterator().next();
goals.addAll(factory.getCoverageGoals());
if (verbose) {
LoggingUtils.getEvoLogger().info("* Total number of test goals: {}", factory.getCoverageGoals().size());
if (Properties.PRINT_GOALS) {
for (TestFitnessFunction goal : factory.getCoverageGoals()) LoggingUtils.getEvoLogger().info("" + goal.toString());
}
}
} else {
if (verbose) {
LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
}
for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
goals.addAll(goalFactory.getCoverageGoals());
if (verbose) {
LoggingUtils.getEvoLogger().info(" - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
if (Properties.PRINT_GOALS) {
for (TestFitnessFunction goal : goalFactory.getCoverageGoals()) LoggingUtils.getEvoLogger().info("" + goal.toString());
}
}
}
}
return goals;
}
use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class MOSuiteStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
// Set up search algorithm
PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
GeneticAlgorithm<TestSuiteChromosome> algorithm = algorithmFactory.getSearchAlgorithm();
// Override chromosome factory
// TODO handle this better by introducing generics
ChromosomeFactory factory = new RandomLengthTestFactory();
algorithm.setChromosomeFactory(factory);
if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD)
TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(algorithm);
long startTime = System.currentTimeMillis() / 1000;
// What's the search target
List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
List<TestFitnessFunction> fitnessFunctions = new ArrayList<TestFitnessFunction>();
for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
fitnessFunctions.addAll(goalFactory.getCoverageGoals());
}
algorithm.addFitnessFunctions((List) fitnessFunctions);
// if (Properties.SHOW_PROGRESS && !logger.isInfoEnabled())
// FIXME progressMonitor may cause
algorithm.addListener(progressMonitor);
if (Properties.ALGORITHM == Properties.Algorithm.LIPS)
LoggingUtils.getEvoLogger().info("* Total number of test goals for LIPS: {}", fitnessFunctions.size());
else if (Properties.ALGORITHM == Properties.Algorithm.MOSA)
LoggingUtils.getEvoLogger().info("* Total number of test goals for MOSA: {}", fitnessFunctions.size());
if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) || ArrayUtil.contains(Properties.CRITERION, Criterion.ALLDEFS) || ArrayUtil.contains(Properties.CRITERION, Criterion.STATEMENT) || ArrayUtil.contains(Properties.CRITERION, Criterion.RHO) || ArrayUtil.contains(Properties.CRITERION, Criterion.BRANCH) || ArrayUtil.contains(Properties.CRITERION, Criterion.AMBIGUITY))
ExecutionTracer.enableTraceCalls();
algorithm.resetStoppingConditions();
TestSuiteChromosome testSuite = null;
if (!(Properties.STOP_ZERO && fitnessFunctions.isEmpty()) || ArrayUtil.contains(Properties.CRITERION, Criterion.EXCEPTION)) {
// Perform search
LoggingUtils.getEvoLogger().info("* Using seed {}", Randomness.getSeed());
LoggingUtils.getEvoLogger().info("* Starting evolution");
ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
algorithm.generateSolution();
List<TestSuiteChromosome> bestSuites = (List<TestSuiteChromosome>) algorithm.getBestIndividuals();
if (bestSuites.isEmpty()) {
LoggingUtils.getEvoLogger().warn("Could not find any suitable chromosome");
return new TestSuiteChromosome();
} else {
testSuite = bestSuites.get(0);
}
} else {
zeroFitness.setFinished();
testSuite = new TestSuiteChromosome();
for (FitnessFunction<?> ff : testSuite.getFitnessValues().keySet()) {
testSuite.setCoverage(ff, 1.0);
}
}
long endTime = System.currentTimeMillis() / 1000;
// Newline after progress bar
if (Properties.SHOW_PROGRESS)
LoggingUtils.getEvoLogger().info("");
String text = " statements, best individual has fitness: ";
LoggingUtils.getEvoLogger().info("* Search finished after " + (endTime - startTime) + "s and " + algorithm.getAge() + " generations, " + MaxStatementsStoppingCondition.getNumExecutedStatements() + text + testSuite.getFitness());
// Search is finished, send statistics
sendExecutionStatistics();
// We send the info about the total number of coverage goals/targets only after
// the end of the search. This is because the number of coverage targets may vary
// when the criterion Properties.Criterion.EXCEPTION is used (exception coverage
// goal are dynamically added when the generated tests trigger some exceptions
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, algorithm.getFitnessFunctions().size());
return testSuite;
}
use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class TestTestSuiteMinimizer method minimizeSuiteHalfCoverageWithTwoFitnessFunctions.
@Test
public void minimizeSuiteHalfCoverageWithTwoFitnessFunctions() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
testFactory.addConstructor(test, gc, 0, 0);
List<VariableReference> parameters = new ArrayList<VariableReference>();
for (int i = 0; i < 10; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
VariableReference vr = test.addStatement(ips, i + 1);
}
ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
GenericMethod method = new GenericMethod(m, sut);
testFactory.addMethod(test, method, 11, 0);
assertEquals(12, test.size());
TestSuiteChromosome tsc = new TestSuiteChromosome();
tsc.addTest(test);
TestSuiteFitnessFunction branch = new BranchCoverageSuiteFitness();
double previous_branch_fitness = branch.getFitness(tsc);
tsc.setFitness(branch, previous_branch_fitness);
assertEquals(previous_branch_fitness, 2.0, 0.0);
TestSuiteFitnessFunction defuse = new DefUseCoverageSuiteFitness();
double previous_defuse_fitness = defuse.getFitness(tsc);
tsc.setFitness(defuse, previous_defuse_fitness);
assertEquals(previous_defuse_fitness, 0.0, 0.0);
List<TestFitnessFactory<? extends TestFitnessFunction>> factories = new ArrayList<TestFitnessFactory<? extends TestFitnessFunction>>();
factories.add(new BranchCoverageFactory());
factories.add(new DefUseCoverageFactory());
TestSuiteMinimizer minimizer = new TestSuiteMinimizer(factories);
minimizer.minimize(tsc, false);
assertEquals(1, tsc.getTests().size());
assertEquals(3, tsc.getTests().get(0).size());
// assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nflagExample1_0.testMe(int0);\n"));
double branch_fitness = branch.getFitness(tsc);
assertEquals(previous_branch_fitness, branch_fitness, 0.0);
double defuse_fitness = defuse.getFitness(tsc);
assertEquals(previous_defuse_fitness, defuse_fitness, 0.0);
}
use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class OutputCoverageFitnessFunctionSystemTest method testOutputCoverageIgnoreHashCode.
@Test
public void testOutputCoverageIgnoreHashCode() {
EvoSuite evosuite = new EvoSuite();
String targetClass = ClassWithHashCode.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
int goals = 0;
for (TestFitnessFactory ff : TestGenerationStrategy.getFitnessFactories()) goals += ff.getCoverageGoals().size();
Assert.assertEquals("Unexpected number of goals", 2, goals);
Assert.assertEquals("Non-optimal fitness: ", 0.0, best.getFitness(), 0.001);
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Aggregations