use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class AllMethodsTestChromosomeFactory method getChromosome.
/**
* {@inheritDoc}
*
* Generate a random chromosome
*/
@Override
public TestChromosome getChromosome() {
TestChromosome c = new TestChromosome();
c.setTestCase(getRandomTestCase(Properties.CHROMOSOME_LENGTH));
return c;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactory method getChromosome.
@Override
public TestChromosome getChromosome() {
final int N_mutations = Properties.SEED_MUTATIONS;
final double P_clone = Properties.SEED_CLONE;
double r = Randomness.nextDouble();
if (r >= P_clone || junitTests.isEmpty()) {
logger.debug("Using random test");
return defaultFactory.getChromosome();
}
// Cloning
logger.info("Cloning user test");
TestCase test = Randomness.choice(junitTests);
TestChromosome chromosome = new TestChromosome();
chromosome.setTestCase(test.clone());
if (N_mutations > 0) {
int numMutations = Randomness.nextInt(N_mutations);
logger.debug("Mutations: " + numMutations);
// Delta
for (int i = 0; i < numMutations; i++) {
chromosome.mutate();
}
}
return chromosome;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class DeprecatedTestSuiteDSE method calculateUncoveredBranches.
/**
* Iterate over path constraints to identify those which map to branches
* that are only covered one way
*/
private void calculateUncoveredBranches() {
unsolvedBranchConditions.clear();
if (Properties.DSE_NEGATE_ALL_CONDITIONS == true) {
for (TestChromosome testChromosome : pathConditions.keySet()) {
final List<BranchCondition> pathCondition = pathConditions.get(testChromosome);
for (BranchCondition branch : pathCondition) {
if (!unsolvableBranchConditions.contains(branch)) {
unsolvedBranchConditions.add(new TestBranchPair(testChromosome, pathCondition, branch));
}
}
}
} else {
Map<String, Map<Comparator, Set<TestBranchPair>>> solvedConstraints = new HashMap<String, Map<Comparator, Set<TestBranchPair>>>();
for (TestChromosome test : pathConditions.keySet()) {
final List<BranchCondition> pathCondition = pathConditions.get(test);
for (BranchCondition branch : pathCondition) {
if (unsolvableBranchConditions.contains(branch))
continue;
String index = getBranchIndex(branch);
if (!solvedConstraints.containsKey(index))
solvedConstraints.put(index, new HashMap<Comparator, Set<TestBranchPair>>());
Constraint<?> c = branch.getConstraint();
if (!solvedConstraints.get(index).containsKey(c.getComparator()))
solvedConstraints.get(index).put(c.getComparator(), new HashSet<TestBranchPair>());
solvedConstraints.get(index).get(c.getComparator()).add(new TestBranchPair(test, pathCondition, branch));
}
}
for (String index : solvedConstraints.keySet()) {
if (solvedConstraints.get(index).size() == 1) {
Set<TestBranchPair> branches = solvedConstraints.get(index).values().iterator().next();
unsolvedBranchConditions.addAll(branches);
}
}
logger.info("Update set of unsolved branch conditions to " + unsolvedBranchConditions.size());
if (Properties.DSE_RANK_BRANCH_CONDITIONS == false) {
Randomness.shuffle((ArrayList<TestBranchPair>) unsolvedBranchConditions);
}
}
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class DeprecatedTestSuiteDSE method getCoveredBranches.
/**
* Returns the set covered branches by this suite
*
* @param suite
* @return
*/
private static Set<Branch> getCoveredBranches(TestSuiteChromosome suite) {
final Set<Branch> suiteCoveredBranches = new HashSet<Branch>();
for (TestChromosome test : suite.getTestChromosomes()) {
final Set<Branch> testCoveredBranches = getCoveredBranches(test);
suiteCoveredBranches.addAll(testCoveredBranches);
}
return suiteCoveredBranches;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class DeprecatedTestSuiteDSE method applyDSE0.
/**
* Attempt to negate individual branches until budget is used up, or there
* are no further branches to negate
*
* @param individual
*/
public boolean applyDSE0(TestSuiteChromosome individual) {
logger.info("[DSE] Current test suite: " + individual.toString());
boolean wasSuccess = false;
// expansion already happens as part of LS
// TestSuiteChromosome expandedTests = expandTestSuite(individual);
TestSuiteChromosome expandedTests = individual.clone();
createPathConstraints(expandedTests);
// fitness.getFitness(expandedTests);
double originalFitness = getFitness(individual);
while (hasNextBranchCondition() && !LocalSearchBudget.getInstance().isFinished()) {
logger.info("Branches remaining: " + unsolvedBranchConditions.size());
TestBranchPair next = getNextBranchCondition();
BranchCondition branch = next.branch;
List<BranchCondition> pathCondition = next.pathCondition;
List<Constraint<?>> reachingConstraints = new LinkedList<Constraint<?>>();
for (BranchCondition b : pathCondition) {
reachingConstraints.addAll(b.getSupportingConstraints());
if (b == branch) {
break;
}
reachingConstraints.add(b.getConstraint());
}
List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
TestCase newTest = negateCondition(new HashSet<Constraint<?>>(reachingConstraints), branch.getConstraint(), next.test.getTestCase());
if (newTest != null) {
logger.info("Found new test: " + newTest.toCode());
TestChromosome newTestChromosome = new TestChromosome();
newTestChromosome.setTestCase(newTest);
expandedTests.addTest(newTestChromosome);
if (Properties.DSE_KEEP_ALL_TESTS) {
updatePathConstraints(newTestChromosome);
calculateUncoveredBranches();
individual.addTest(newTest);
wasSuccess = true;
} else {
if (getFitness(expandedTests) < originalFitness) {
logger.info("New test improves fitness to {}", getFitness(expandedTests));
DSEStats.getInstance().reportNewTestUseful();
wasSuccess = true;
// no need to clone so we can keep executionresult
updatePathConstraints(newTestChromosome);
calculateUncoveredBranches(newTestChromosome);
individual.addTest(newTest);
originalFitness = getFitness(expandedTests);
// TODO: Cancel on fitness 0 - would need to know if
// ZeroFitness is a stopping condition
} else {
logger.info("New test does not improve fitness");
DSEStats.getInstance().reportNewTestUnuseful();
expandedTests.deleteTest(newTest);
}
}
success++;
} else {
unsolvableBranchConditions.add(branch);
failed++;
logger.info("Failed to find new test.");
}
}
logger.info("Finished DSE");
// Ensure fitness values are up to date.
getFitness(individual);
LocalSearchBudget.getInstance().countLocalSearchOnTestSuite();
return wasSuccess;
}
Aggregations