use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class PropertiesNoveltySearchFactory method getSearchAlgorithm.
@Override
public // public GeneticAlgorithm<TestChromosome> getSearchAlgorithm() {
NoveltySearch<TestChromosome> getSearchAlgorithm() {
ChromosomeFactory<TestChromosome> factory = getChromosomeFactory();
NoveltySearch<TestChromosome> ga = new NoveltySearch<>(factory);
if (Properties.NEW_STATISTICS)
ga.addListener(new StatisticsListener());
// How to select candidates for reproduction
SelectionFunction<TestChromosome> selectionFunction = getSelectionFunction();
selectionFunction.setMaximize(false);
ga.setSelectionFunction(selectionFunction);
// When to stop the search
StoppingCondition stopping_condition = getStoppingCondition();
ga.setStoppingCondition(stopping_condition);
// ga.addListener(stopping_condition);
if (Properties.STOP_ZERO) {
ga.addStoppingCondition(new ZeroFitnessStoppingCondition());
}
if (!(stopping_condition instanceof MaxTimeStoppingCondition)) {
ga.addStoppingCondition(new GlobalTimeStoppingCondition());
}
if (ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.STRONGMUTATION)) {
if (Properties.STRATEGY == Properties.Strategy.ONEBRANCH)
ga.addStoppingCondition(new MutationTimeoutStoppingCondition());
else
ga.addListener(new MutationTestPool());
// } else if (Properties.CRITERION == Criterion.DEFUSE) {
// if (Properties.STRATEGY == Strategy.EVOSUITE)
// ga.addListener(new DefUseTestPool());
}
ga.resetStoppingConditions();
ga.setPopulationLimit(getPopulationLimit());
// How to cross over
CrossOverFunction crossover_function = getCrossoverFunction();
ga.setCrossOverFunction(crossover_function);
if (Properties.CHECK_BEST_LENGTH) {
RelativeSuiteLengthBloatControl bloat_control = new org.evosuite.testsuite.RelativeSuiteLengthBloatControl();
ga.addBloatControl(bloat_control);
ga.addListener(bloat_control);
}
// ga.addBloatControl(new MaxLengthBloatControl());
TestCaseSecondaryObjective.setSecondaryObjectives();
if (Properties.DYNAMIC_LIMIT) {
// max_s = GAProperties.generations * getBranches().size();
// TODO: might want to make this dependent on the selected coverage
// criterion
// TODO also, question: is branchMap.size() really intended here?
// I think BranchPool.getBranchCount() was intended
Properties.SEARCH_BUDGET = Properties.SEARCH_BUDGET * (BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getNumBranchlessMethods(Properties.TARGET_CLASS) + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchCountForClass(Properties.TARGET_CLASS) * 2);
stopping_condition.setLimit(Properties.SEARCH_BUDGET);
logger.info("Setting dynamic length limit to " + Properties.SEARCH_BUDGET);
}
if (Properties.LOCAL_SEARCH_RESTORE_COVERAGE) {
org.evosuite.ga.metaheuristics.SearchListener map = BranchCoverageMap.getInstance();
ga.addListener(map);
}
if (Properties.SHUTDOWN_HOOK) {
// ShutdownTestWriter writer = new
// ShutdownTestWriter(Thread.currentThread());
ShutdownTestWriter writer = new ShutdownTestWriter();
ga.addStoppingCondition(writer);
RMIStoppingCondition rmi = RMIStoppingCondition.getInstance();
ga.addStoppingCondition(rmi);
if (Properties.STOPPING_PORT != -1) {
SocketStoppingCondition ss = new SocketStoppingCondition();
ss.accept();
ga.addStoppingCondition(ss);
}
// Runtime.getRuntime().addShutdownHook(writer);
Signal.handle(new Signal("INT"), writer);
}
ga.addListener(new ResourceController());
return ga;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class RandomTestStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
LoggingUtils.getEvoLogger().info("* Using random test generation");
List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
TestSuiteChromosome suite = new TestSuiteChromosome();
for (TestSuiteFitnessFunction fitnessFunction : fitnessFunctions) suite.addFitness(fitnessFunction);
List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>();
LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
goals.addAll(goalFactory.getCoverageGoals());
LoggingUtils.getEvoLogger().info(" - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
}
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
if (!canGenerateTestsForSUT()) {
LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
return new TestSuiteChromosome();
}
ChromosomeFactory<TestChromosome> factory = getChromosomeFactory();
StoppingCondition stoppingCondition = getStoppingCondition();
for (FitnessFunction<?> fitness_function : fitnessFunctions) ((TestSuiteFitnessFunction) fitness_function).getFitness(suite);
ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
int number_generations = 0;
while (!isFinished(suite, stoppingCondition)) {
number_generations++;
TestChromosome test = factory.getChromosome();
TestSuiteChromosome clone = suite.clone();
clone.addTest(test);
for (FitnessFunction<?> fitness_function : fitnessFunctions) {
((TestSuiteFitnessFunction) fitness_function).getFitness(clone);
logger.debug("Old fitness: {}, new fitness: {}", suite.getFitness(), clone.getFitness());
}
if (clone.compareTo(suite) < 0) {
suite = clone;
StatisticsSender.executedAndThenSendIndividualToMaster(clone);
}
}
// statistics.searchFinished(suiteGA);
LoggingUtils.getEvoLogger().info("* Search Budget:");
LoggingUtils.getEvoLogger().info("\t- " + stoppingCondition.toString());
// In the GA, these statistics are sent via the SearchListener when notified about the GA completing
// Search is finished, send statistics
sendExecutionStatistics();
// TODO: Check this: Fitness_Evaluations = getNumExecutedTests?
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Fitness_Evaluations, MaxTestsStoppingCondition.getNumExecutedTests());
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Generations, number_generations);
return suite;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class EntBugTestStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
// Set up search algorithm
LoggingUtils.getEvoLogger().info("* Setting up search algorithm for individual test generation (ASE'13)");
ExecutionTracer.enableTraceCalls();
// Set up genetic algorithm
PropertiesTestGAFactory factory = new PropertiesTestGAFactory();
GeneticAlgorithm<TestChromosome> ga = factory.getSearchAlgorithm();
if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD) {
TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(ga);
}
// What's the search target
RhoCoverageFactory rhoFactory = (RhoCoverageFactory) FitnessFunctions.getFitnessFactory(Properties.Criterion.RHO);
RhoCoverageTestFitness rhoTestFitnessFunction = new RhoCoverageTestFitness();
ga.addFitnessFunction(rhoTestFitnessFunction);
// Goals
List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>(rhoFactory.getCoverageGoals());
LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
LoggingUtils.getEvoLogger().info(" - Rho " + goals.size());
double previous_fitness = RhoCoverageFactory.getRho();
double best_fitness = 0.0;
int number_of_generations = (int) (Properties.SEARCH_BUDGET / 10);
// Properties.SEARCH_BUDGET = 10; // 10 seconds for each generation
TestSuiteChromosome bests = new TestSuiteChromosome();
while (number_of_generations > 0) {
LoggingUtils.getEvoLogger().info(" * iteration(" + number_of_generations + ")");
// 10 seconds for each generation
// FIXME: should be a parameter?
ga.setStoppingConditionLimit(10);
ga.resetStoppingConditions();
ga.clearPopulation();
// ga.setChromosomeFactory(getDefaultChromosomeFactory()); // not in the original version
ga.generateSolution();
number_of_generations--;
TestChromosome best = (TestChromosome) ga.getBestIndividual();
if (best.getLastExecutionResult() == null) {
// some timeout?
continue;
}
best_fitness = best.getFitness(rhoTestFitnessFunction);
if (// we've found a better test case
(best_fitness < previous_fitness) || // or this new test case is not so bad (i.e., < Properties.EPSON)
(best_fitness <= Properties.EPSON)) {
// GOOD
LoggingUtils.getEvoLogger().info(" * new best (previous fitness: " + previous_fitness + " | best_fitness: " + best_fitness + ")");
ExecutionResult exec = best.getLastExecutionResult();
ExecutionTrace trace = exec.getTrace();
Set<Integer> testCoverage = trace.getCoveredLines();
LoggingUtils.getEvoLogger().info(" * new test case added " + testCoverage.toString());
rhoTestFitnessFunction.incrementNumber_of_Ones(testCoverage.size());
rhoTestFitnessFunction.incrementNumber_of_Test_Cases();
rhoTestFitnessFunction.addTestCoverage(testCoverage);
bests.addTest(best);
// update global fitness
previous_fitness = best_fitness;
} else {
// BAD
LoggingUtils.getEvoLogger().info(" * new test case ignored (previous fitness: " + previous_fitness + " | best_fitness: " + best_fitness + ")");
}
}
LoggingUtils.getEvoLogger().info("* Search finished after, best individual has fitness " + best_fitness);
LoggingUtils.getEvoLogger().info("Resulting test suite: " + bests.size() + " tests, length " + bests.totalLengthOfTestCases());
return bests;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class IndividualTestStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
// Set up search algorithm
LoggingUtils.getEvoLogger().info("* Setting up search algorithm for individual test generation");
ExecutionTracer.enableTraceCalls();
PropertiesTestGAFactory factory = new PropertiesTestGAFactory();
List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
long start_time = System.currentTimeMillis() / 1000;
// Get list of goals
List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
// long goalComputationStart = System.currentTimeMillis();
List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>();
LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
goals.addAll(goalFactory.getCoverageGoals());
LoggingUtils.getEvoLogger().info(" - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
}
if (!canGenerateTestsForSUT()) {
LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
return new TestSuiteChromosome();
}
// Need to shuffle goals because the order may make a difference
if (Properties.SHUFFLE_GOALS) {
// LoggingUtils.getEvoLogger().info("* Shuffling goals");
Randomness.shuffle(goals);
}
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
LoggingUtils.getEvoLogger().info("* Total number of test goals: " + goals.size());
// Bootstrap with random testing to cover easy goals
// statistics.searchStarted(suiteGA);
StoppingCondition stoppingCondition = getStoppingCondition();
// FIXME: just one fitness and one factory?!
TestSuiteChromosome suite = (TestSuiteChromosome) bootstrapRandomSuite(fitnessFunctions.get(0), goalFactories.get(0));
Set<Integer> covered = new HashSet<Integer>();
int covered_goals = 0;
int num = 0;
for (TestFitnessFunction fitness_function : goals) {
if (fitness_function.isCoveredBy(suite)) {
covered.add(num);
covered_goals++;
}
num++;
}
if (covered_goals > 0)
LoggingUtils.getEvoLogger().info("* Random bootstrapping covered " + covered_goals + " test goals");
int total_goals = goals.size();
if (covered_goals == total_goals)
zeroFitness.setFinished();
int current_budget = 0;
long total_budget = Properties.SEARCH_BUDGET;
LoggingUtils.getEvoLogger().info("* Budget: " + NumberFormat.getIntegerInstance().format(total_budget));
while (current_budget < total_budget && covered_goals < total_goals && !globalTime.isFinished() && !ShutdownTestWriter.isInterrupted()) {
long budget = (total_budget - current_budget) / (total_goals - covered_goals);
logger.info("Budget: " + budget + "/" + (total_budget - current_budget));
logger.info("Statements: " + current_budget + "/" + total_budget);
logger.info("Goals covered: " + covered_goals + "/" + total_goals);
stoppingCondition.setLimit(budget);
num = 0;
// //MaxStatementsStoppingCondition.getNumExecutedStatements();
for (TestFitnessFunction fitnessFunction : goals) {
if (covered.contains(num)) {
num++;
continue;
}
GeneticAlgorithm<TestChromosome> ga = factory.getSearchAlgorithm();
if (Properties.PRINT_CURRENT_GOALS)
LoggingUtils.getEvoLogger().info("* Searching for goal " + num + ": " + fitnessFunction.toString());
logger.info("Goal " + num + "/" + (total_goals - covered_goals) + ": " + fitnessFunction);
if (ShutdownTestWriter.isInterrupted()) {
num++;
continue;
}
if (globalTime.isFinished()) {
LoggingUtils.getEvoLogger().info("Skipping goal because time is up");
num++;
continue;
}
// FitnessFunction fitness_function = new
ga.addFitnessFunction(fitnessFunction);
// Perform search
logger.info("Starting evolution for goal " + fitnessFunction);
ga.generateSolution();
if (ga.getBestIndividual().getFitness() == 0.0) {
if (Properties.PRINT_COVERED_GOALS)
// : " +
LoggingUtils.getEvoLogger().info("* Covered!");
// fitness_function.toString());
logger.info("Found solution, adding to test suite at " + MaxStatementsStoppingCondition.getNumExecutedStatements());
TestChromosome best = (TestChromosome) ga.getBestIndividual();
best.getTestCase().addCoveredGoal(fitnessFunction);
suite.addTest(best);
// Calculate and keep track of overall fitness
for (TestSuiteFitnessFunction fitness_function : fitnessFunctions) fitness_function.getFitness(suite);
covered_goals++;
covered.add(num);
// experiment:
if (Properties.SKIP_COVERED) {
Set<Integer> additional_covered_nums = getAdditionallyCoveredGoals(goals, covered, best);
// LoggingUtils.getEvoLogger().info("Additionally covered: "+additional_covered_nums.size());
for (Integer covered_num : additional_covered_nums) {
covered_goals++;
covered.add(covered_num);
}
}
} else {
logger.info("Found no solution for " + fitnessFunction + " at " + MaxStatementsStoppingCondition.getNumExecutedStatements());
}
// statistics.iteration(suiteGA);
if (Properties.REUSE_BUDGET)
current_budget += stoppingCondition.getCurrentValue();
else
current_budget += budget + 1;
// print console progress bar
if (Properties.SHOW_PROGRESS && !(Properties.PRINT_COVERED_GOALS || Properties.PRINT_CURRENT_GOALS)) {
double percent = current_budget;
percent = percent / total_budget * 100;
double coverage = covered_goals;
coverage = coverage / total_goals * 100;
// ConsoleProgressBar.printProgressBar((int) percent, (int)
// coverage);
}
if (current_budget > total_budget)
break;
num++;
// break;
}
}
if (Properties.SHOW_PROGRESS)
LoggingUtils.getEvoLogger().info("");
// for testing purposes
if (globalTime.isFinished())
LoggingUtils.getEvoLogger().info("! Timeout reached");
if (current_budget >= total_budget)
LoggingUtils.getEvoLogger().info("! Budget exceeded");
else
LoggingUtils.getEvoLogger().info("* Remaining budget: " + (total_budget - current_budget));
// stoppingCondition.setLimit(Properties.SEARCH_BUDGET);
// stoppingCondition.forceCurrentValue(current_budget);
// suiteGA.setStoppingCondition(stopping_condition);
// suiteGA.addStoppingCondition(global_time);
// printBudget(suiteGA);
int c = 0;
int uncovered_goals = total_goals - covered_goals;
if (uncovered_goals < 10)
for (TestFitnessFunction goal : goals) {
if (!covered.contains(c)) {
LoggingUtils.getEvoLogger().info("! Unable to cover goal " + c + " " + goal.toString());
}
c++;
}
else
LoggingUtils.getEvoLogger().info("! #Goals that were not covered: " + uncovered_goals);
// statistics.searchFinished(suiteGA);
long end_time = System.currentTimeMillis() / 1000;
LoggingUtils.getEvoLogger().info("* Search finished after " + (end_time - start_time) + "s, " + current_budget + " statements, best individual has fitness " + suite.getFitness());
// Search is finished, send statistics
sendExecutionStatistics();
LoggingUtils.getEvoLogger().info("* Covered " + covered_goals + "/" + goals.size() + " goals");
logger.info("Resulting test suite: " + suite.size() + " tests, length " + suite.totalLengthOfTestCases());
return suite;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class NoveltyStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
// Set up search algorithm
LoggingUtils.getEvoLogger().info("* Setting up search algorithm for novelty search");
PropertiesNoveltySearchFactory algorithmFactory = new PropertiesNoveltySearchFactory();
NoveltySearch<TestChromosome> algorithm = algorithmFactory.getSearchAlgorithm();
if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD)
TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(algorithm);
long startTime = System.currentTimeMillis() / 1000;
// What's the search target
List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
NoveltyFitnessEvaluationListener listener = new NoveltyFitnessEvaluationListener(fitnessFunctions);
algorithm.addListener(listener);
algorithm.setNoveltyFunction(new BranchNoveltyFunction());
if (Properties.TRACK_DIVERSITY)
algorithm.addListener(new DiversityObserver());
if (ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.DEFUSE) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.ALLDEFS) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.STATEMENT) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.RHO) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.AMBIGUITY))
ExecutionTracer.enableTraceCalls();
// TODO: why it was only if "analyzing"???
// if (analyzing)
algorithm.resetStoppingConditions();
List<TestFitnessFunction> goals = getGoals(true);
if (!canGenerateTestsForSUT()) {
LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
return new TestSuiteChromosome();
}
/*
* Proceed with search if CRITERION=EXCEPTION, even if goals is empty
*/
TestSuiteChromosome testSuite = null;
if (!(Properties.STOP_ZERO && goals.isEmpty()) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.EXCEPTION)) {
// Perform search
LoggingUtils.getEvoLogger().info("* Using seed {}", Randomness.getSeed());
LoggingUtils.getEvoLogger().info("* Starting evolution");
ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
algorithm.generateSolution();
testSuite = Archive.getArchiveInstance().mergeArchiveAndSolution(new TestSuiteChromosome());
} else {
zeroFitness.setFinished();
testSuite = new TestSuiteChromosome();
for (FitnessFunction<?> ff : fitnessFunctions) {
testSuite.setCoverage(ff, 1.0);
}
}
long endTime = System.currentTimeMillis() / 1000;
// recalculated now after the search, eg to handle exception fitness
goals = getGoals(false);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
// Newline after progress bar
if (Properties.SHOW_PROGRESS)
LoggingUtils.getEvoLogger().info("");
if (!Properties.IS_RUNNING_A_SYSTEM_TEST) {
// avoid printing time related info in system tests due to lack of determinism
LoggingUtils.getEvoLogger().info("* Search finished after " + (endTime - startTime) + "s and " + algorithm.getAge() + " generations, " + MaxStatementsStoppingCondition.getNumExecutedStatements() + " statements, best individual has fitness: " + testSuite.getFitness());
}
// Search is finished, send statistics
sendExecutionStatistics();
return testSuite;
}
Aggregations