use of org.evosuite.symbolic.BranchCondition 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.symbolic.BranchCondition 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;
}
use of org.evosuite.symbolic.BranchCondition in project evosuite by EvoSuite.
the class TestStringSearch2 method testSolveIndexOfConstant.
@Test
public void testSolveIndexOfConstant() throws SecurityException, NoSuchMethodException {
DefaultTestCase tc = buildTestCase("V*X-:o%tp");
List<BranchCondition> branch_conditions = executeTest(tc);
Collection<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
for (int i = 0; i < branch_conditions.size() - 2; i++) {
BranchCondition b = branch_conditions.get(i);
constraints.addAll(b.getSupportingConstraints());
constraints.add(b.getConstraint());
}
BranchCondition last_branch = branch_conditions.get(branch_conditions.size() - 2);
constraints.addAll(last_branch.getSupportingConstraints());
constraints.add(last_branch.getConstraint().negate());
EvoSuiteSolver solver = new EvoSuiteSolver();
Map<String, Object> solution;
try {
solution = solve(solver, constraints);
assertNotNull(solution);
System.out.println(solution);
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.BranchCondition in project evosuite by EvoSuite.
the class TestStringSearch2 method testCreatePathConstraint.
@Test
public void testCreatePathConstraint() throws SecurityException, NoSuchMethodException {
DefaultTestCase tc = buildTestCase("urn:pBth:/A/B/C/doc.html#gilada");
List<BranchCondition> branch_conditions = executeTest(tc);
assertEquals(11, branch_conditions.size());
}
use of org.evosuite.symbolic.BranchCondition in project evosuite by EvoSuite.
the class TestStringSearch2 method testSolvePathConstraint.
@Test
public void testSolvePathConstraint() throws SecurityException, NoSuchMethodException {
DefaultTestCase tc = buildTestCase("urn:pBth:/A/B/C/doc.html#gilada");
List<BranchCondition> branch_conditions = executeTest(tc);
Collection<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
for (int i = 0; i < branch_conditions.size() - 1; i++) {
BranchCondition b = branch_conditions.get(i);
constraints.addAll(b.getSupportingConstraints());
constraints.add(b.getConstraint());
}
BranchCondition last_branch = branch_conditions.get(branch_conditions.size() - 1);
constraints.addAll(last_branch.getSupportingConstraints());
constraints.add(last_branch.getConstraint().negate());
EvoSuiteSolver solver = new EvoSuiteSolver();
Map<String, Object> solution;
try {
solution = solve(solver, constraints);
assertNotNull(solution);
System.out.println(solution);
} catch (SolverTimeoutException e) {
fail();
}
}
Aggregations