/** * Create path constraints for all tests in a test suite * * @param testSuite */ private void createPathConstraints(TestSuiteChromosome testSuite) { for (TestChromosome test : testSuite.getTestChromosomes()) { updatePathConstraints(test); } calculateUncoveredBranches(); }
/** * Get a new candidate for negation * * @return */ private TestBranchPair getNextBranchCondition() { TestBranchPair pair; pair = getNextTestBranchPair(); if (Properties.DSE_NEGATE_ALL_CONDITIONS == true) { return pair; } String index = getBranchIndex(pair.branch); if (!unsolvedBranchConditions.isEmpty()) { while (solutionAttempts.containsKey(index) && solutionAttempts.get(index) >= Properties.CONSTRAINT_SOLUTION_ATTEMPTS && !unsolvedBranchConditions.isEmpty()) { logger.info("Reached maximum number of attempts for branch " + index); pair = getNextTestBranchPair(); index = getBranchIndex(pair.branch); } } if (!solutionAttempts.containsKey(index)) solutionAttempts.put(index, 1); else solutionAttempts.put(index, solutionAttempts.get(index) + 1); return pair; }
private boolean reachesUncoveredBranch(TestChromosome test, Set<Branch> uncoveredBranches) { Set<Branch> testCoveredBranches = getCoveredBranches(test); for (Branch b : testCoveredBranches) { Branch negate = b.negate(); if (uncoveredBranches.contains(negate)) { return true; } } return false; }
createPathConstraints(expandedTests); double originalFitness = getFitness(individual); while (hasNextBranchCondition() && !LocalSearchBudget.getInstance().isFinished()) { logger.info("Branches remaining: " + unsolvedBranchConditions.size()); TestBranchPair next = getNextBranchCondition(); BranchCondition branch = next.branch; TestCase newTest = negateCondition(new HashSet<Constraint<?>>(reachingConstraints), branch.getConstraint(), next.test.getTestCase()); 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; updatePathConstraints(newTestChromosome); calculateUncoveredBranches(newTestChromosome); individual.addTest(newTest); originalFitness = getFitness(expandedTests);
constraints = reduce(constraints); logger.info("Reduced constraints from " + size + " to " + constraints.size()); PrimitiveStatement p = getStatement(newTest, name); if (p.getValue().getClass().equals(Character.class)) p.setValue((char) value.intValue()); } else if (val instanceof String) { String name = ((String) key).replace("__SYM", ""); PrimitiveStatement p = getStatement(newTest, name); Double value = (Double) val; String name = ((String) key).replace("__SYM", ""); PrimitiveStatement p = getStatement(newTest, name);
/** * Determine the set of variable referenced by this constraint * * @param constraint * @return */ private Set<Variable<?>> getVariables(Constraint<?> constraint) { Set<Variable<?>> variables = new HashSet<Variable<?>>(); getVariables(constraint.getLeftOperand(), variables); getVariables(constraint.getRightOperand(), variables); return variables; }
private void calculateUncoveredBranches(TestChromosome newTestChromosome) { if (Properties.DSE_NEGATE_ALL_CONDITIONS == true) { final List<BranchCondition> pathCondition = pathConditions.get(newTestChromosome); for (BranchCondition targetBranchCondition : pathCondition) { if (!unsolvableBranchConditions.contains(targetBranchCondition)) { unsolvedBranchConditions .add(new TestBranchPair(newTestChromosome, pathCondition, targetBranchCondition)); } } } else { calculateUncoveredBranches(); } }
continue; String index = getBranchIndex(branch); if (!solvedConstraints.containsKey(index)) solvedConstraints.put(index, new HashMap<Comparator, Set<TestBranchPair>>());
/** * Apply cone of influence reduction to constraints with respect to the last * constraint in the list * * @param constraints * @return */ private List<Constraint<?>> reduce(List<Constraint<?>> constraints) { Constraint<?> target = constraints.get(constraints.size() - 1); Set<Variable<?>> dependencies = getVariables(target); LinkedList<Constraint<?>> coi = new LinkedList<Constraint<?>>(); if (dependencies.size() <= 0) return coi; coi.add(target); for (int i = constraints.size() - 2; i >= 0; i--) { Constraint<?> constraint = constraints.get(i); Set<Variable<?>> variables = getVariables(constraint); for (Variable<?> var : dependencies) { if (variables.contains(var)) { dependencies.addAll(variables); coi.addFirst(constraint); break; } } } return coi; }
/** * 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; }