use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class TestSuiteSerialization method saveTests.
public static boolean saveTests(TestSuiteChromosome ts, File target) throws IllegalArgumentException {
File parent = target.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try (ObjectOutputStream out = new DebuggingObjectOutputStream(new FileOutputStream(target))) {
for (TestChromosome tc : ts.getTestChromosomes()) {
out.writeObject(tc);
}
out.flush();
out.close();
} catch (IOException e) {
logger.error("Failed to open/handle " + target.getAbsolutePath() + " for writing: " + e.getMessage());
return false;
}
return true;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class JUnitTestSuiteChromosomeFactory method getChromosome.
/* (non-Javadoc)
* @see org.evosuite.ga.ChromosomeFactory#getChromosome()
*/
/**
* {@inheritDoc}
*/
@Override
public TestSuiteChromosome getChromosome() {
/*
double P_delta = 0.1d;
double P_clone = 0.1d;
int MAX_CHANGES = 10;
*/
TestSuiteChromosome chromosome = new TestSuiteChromosome(new RandomLengthTestFactory());
chromosome.clearTests();
int numTests = Randomness.nextInt(Properties.MIN_INITIAL_TESTS, Properties.MAX_INITIAL_TESTS + 1);
for (int i = 0; i < numTests; i++) {
TestChromosome test = defaultFactory.getChromosome();
chromosome.addTest(test);
// chromosome.tests.add(test);
}
return chromosome;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class TestSuiteChromosomeFactory method getChromosome.
/**
* {@inheritDoc}
*/
@Override
public TestSuiteChromosome getChromosome() {
TestSuiteChromosome chromosome = new TestSuiteChromosome(testChromosomeFactory);
chromosome.clearTests();
// ((AllMethodsChromosomeFactory)test_factory).clear();
int numTests = Randomness.nextInt(Properties.MIN_INITIAL_TESTS, Properties.MAX_INITIAL_TESTS + 1);
for (int i = 0; i < numTests; i++) {
TestChromosome test = testChromosomeFactory.getChromosome();
chromosome.addTest(test);
// chromosome.tests.add(test);
}
// logger.trace("Generated new test suite:"+chromosome);
return chromosome;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class TestSuiteLocalSearch method applyLocalSearch.
/**
* Decides the type of local search to be applied, and invokes the
* corresponding local search procedure.
*
* @param suite
* the suite to optimise
* @param objective
* the local search objective
*/
private void applyLocalSearch(TestSuiteChromosome suite, LocalSearchObjective<TestSuiteChromosome> objective) {
final LocalSearchSuiteType localSearchType;
localSearchType = chooseLocalSearchSuiteType();
/*
* We make a copy of the original test cases before Local Search
*/
List<TestChromosome> originalTests = new ArrayList<TestChromosome>(suite.getTestChromosomes());
for (final TestChromosome test : originalTests) {
// again
if (test.hasLocalSearchBeenApplied()) {
TestCaseLocalSearch.randomizePrimitives(test.getTestCase());
updateFitness(suite, objective.getFitnessFunctions());
}
if (LocalSearchBudget.getInstance().isFinished()) {
logger.debug("Local search budget used up: " + Properties.LOCAL_SEARCH_BUDGET_TYPE);
break;
}
logger.debug("Local search budget not yet used up");
final double tossCoin = Randomness.nextDouble();
final boolean shouldApplyDSE = localSearchType == LocalSearchSuiteType.ALWAYS_DSE || (localSearchType == LocalSearchSuiteType.DSE_AND_AVM && tossCoin <= Properties.DSE_PROBABILITY);
/*
* We create a cloned test case to play local search with it. This
* resembles the deprecated ensureDoubleExecution
*/
TestChromosome clonedTest = (TestChromosome) test.clone();
suite.addTest(clonedTest);
final int lastIndex = suite.size() - 1;
final boolean improved;
if (shouldApplyDSE) {
improved = applyDSE(suite, lastIndex, clonedTest, objective);
} else {
improved = applyAVM(suite, lastIndex, clonedTest, objective);
}
if (improved) {
updateFitness(suite, objective.getFitnessFunctions());
} else {
// remove cloned test case if there was no improvement
suite.deleteTest(clonedTest);
}
test.setLocalSearchApplied(true);
}
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class TestSuiteLocalSearch method applyAVM.
/**
* Applies AVM on the test case in the suite
*
* @param suite
* @param testIndex
* @param test
* @param localSearchObjective
* @return
*/
private boolean applyAVM(TestSuiteChromosome suite, int testIndex, TestChromosome test, LocalSearchObjective<TestSuiteChromosome> objective) {
logger.debug("Local search on test " + testIndex + ", current fitness: " + suite.getFitness());
final List<FitnessFunction<? extends Chromosome>> fitnessFunctions = objective.getFitnessFunctions();
TestSuiteLocalSearchObjective testCaseLocalSearchObjective = TestSuiteLocalSearchObjective.buildNewTestSuiteLocalSearchObjective(fitnessFunctions, suite, testIndex);
AVMTestCaseLocalSearch testCaselocalSearch = new AVMTestCaseLocalSearch();
boolean improved = testCaselocalSearch.doSearch(test, testCaseLocalSearchObjective);
return improved;
}
Aggregations