@Override public List<IBranchTestFitness> getCoverageGoals() { //TODO this creates duplicate goals. Momentary fixed using a Set. Set<IBranchTestFitness> goals = new HashSet<IBranchTestFitness>(); // retrieve set of branches BranchCoverageFactory branchFactory = new BranchCoverageFactory(); List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoalsForAllKnownClasses(); CallGraph callGraph = DependencyAnalysis.getCallGraph(); // try to find all occurrences of this branch in the call tree for (BranchCoverageTestFitness branchGoal : branchGoals) { logger.info("Adding context branches for " + branchGoal.toString()); for (CallContext context : callGraph.getAllContextsFromTargetClass(branchGoal.getClassName(), branchGoal.getMethod())) { //if is not possible to reach this branch from the target class, continue. if(context.isEmpty()) continue; goals.add(new IBranchTestFitness(branchGoal.getBranchGoal(), context)); } } assert(goals.size()>=branchFactory.getCoverageGoals().size()); logger.info("Created " + goals.size() + " goals"); return new ArrayList<IBranchTestFitness>(goals); } }
/** {@inheritDoc} */ @Override public List<BranchCoverageTestFitness> getCoverageGoals() { return computeCoverageGoals(true); }
private void setupDependencies(BytecodeInstruction goalInstruction) { this.goalInstruction = goalInstruction; Set<ControlDependency> cds = goalInstruction.getControlDependencies(); for (ControlDependency cd : cds) { BranchCoverageTestFitness fitness = BranchCoverageFactory.createBranchCoverageTestFitness(cd); this.branchFitnesses.add(fitness); } if (goalInstruction.isRootBranchDependent()) this.branchFitnesses.add(BranchCoverageFactory.createRootBranchTestFitness(goalInstruction)); if (cds.isEmpty() && !goalInstruction.isRootBranchDependent()) throw new IllegalStateException( "expect control dependencies to be empty only for root dependent instructions: " + toString()); if (this.branchFitnesses.isEmpty()) throw new IllegalStateException( "an instruction is at least on the root branch of it's method"); }
public BranchFitnessGraph getControlDepencies4Branches(List<FitnessFunction<T>> fitnessFunctions){ Set<FitnessFunction<T>> setOfBranches = new LinkedHashSet<FitnessFunction<T>>(); this.dependencies = new LinkedHashMap(); List<BranchCoverageTestFitness> branches = new BranchCoverageFactory().getCoverageGoals(); for (BranchCoverageTestFitness branch : branches){ setOfBranches.add((FitnessFunction<T>) branch); this.dependencies.put(branch, new LinkedHashSet<FitnessFunction<T>>()); } // initialize the maps this.initializeMaps(setOfBranches); return new BranchFitnessGraph<T, FitnessFunction<T>>(setOfBranches); } }
@Test public void minimizeEmptySuite() throws ClassNotFoundException { DefaultTestCase test = new DefaultTestCase(); TestSuiteChromosome tsc = new TestSuiteChromosome(); tsc.addTest(test); TestSuiteFitnessFunction ff = new BranchCoverageSuiteFitness(); double previous_fitness = ff.getFitness(tsc); tsc.setFitness(ff, previous_fitness); assertEquals(0.0, previous_fitness, 0.0); TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory()); minimizer.minimize(tsc, false); assertEquals(0, tsc.getTests().size()); double fitness = ff.getFitness(tsc); assertEquals(previous_fitness, fitness, 0.0); }
if(limitToCUT && !isCUT(className)) continue; goals.add(createRootBranchTestFitness(className, method)); methodName)) { if(!b.isInstrumented()) { goals.add(createBranchCoverageTestFitness(b, true)); goals.add(createBranchCoverageTestFitness(b, false));
/** * Create a fitness function for branch coverage aimed at executing the * given ControlDependency. * * @param cd * a {@link org.evosuite.graphs.cfg.ControlDependency} object. * @return a {@link org.evosuite.coverage.branch.BranchCoverageTestFitness} * object. */ public static BranchCoverageTestFitness createBranchCoverageTestFitness( ControlDependency cd) { return createBranchCoverageTestFitness(cd.getBranch(), cd.getBranchExpressionValue()); }
/** * Convenience method calling createRootBranchTestFitness(class,method) with * the respective class and method of the given BytecodeInstruction. * * @param instruction * a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object. * @return a {@link org.evosuite.coverage.branch.BranchCoverageTestFitness} * object. */ public static BranchCoverageTestFitness createRootBranchTestFitness( BytecodeInstruction instruction) { if (instruction == null) throw new IllegalArgumentException("null given"); return createRootBranchTestFitness(instruction.getClassName(), instruction.getMethodName()); } }
return new DefUseCoverageFactory(); case BRANCH: return new BranchCoverageFactory(); case CBRANCH: return new CBranchFitnessFactory(); logger.warn("No TestFitnessFactory defined for " + crit + " using default one (BranchCoverageFactory)"); return new BranchCoverageFactory();
@Override public List<CBranchTestFitness> getCoverageGoals() { //TODO this creates duplicate goals. Momentary fixed using a Set, but it should be optimised Set<CBranchTestFitness> goals = new HashSet<>(); // retrieve set of branches BranchCoverageFactory branchFactory = new BranchCoverageFactory(); List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoals(); CallGraph callGraph = DependencyAnalysis.getCallGraph(); // try to find all occurrences of this branch in the call tree for (BranchCoverageTestFitness branchGoal : branchGoals) { logger.info("Adding context branches for " + branchGoal.toString()); for (CallContext context : callGraph.getMethodEntryPoint(branchGoal.getClassName(), branchGoal.getMethod())) { goals.add(new CBranchTestFitness(branchGoal.getBranchGoal(), context)); } } logger.info("Created " + goals.size() + " goals"); return new ArrayList<CBranchTestFitness>(goals); } }
/** * This methods derive the dependencies between {@link LineCoverageTestFitness} and branches. * Therefore, it is used to update 'this.dependencies' */ private void addDependencies4Line() { logger.debug("Added dependencies for Lines"); for (FitnessFunction<T> ff : this.uncoveredGoals){ if (ff instanceof LineCoverageTestFitness){ LineCoverageTestFitness line = (LineCoverageTestFitness) ff; ClassLoader loader = TestGenerationContext.getInstance().getClassLoaderForSUT(); BytecodeInstructionPool pool = BytecodeInstructionPool.getInstance(loader); BytecodeInstruction instruction = pool.getFirstInstructionAtLineNumber(line.getClassName(), line.getMethod(), line.getLine()); Set<ControlDependency> cds = instruction.getControlDependencies(); if(cds.size() == 0) this.currentGoals.add(ff); else { for (ControlDependency cd : cds) { BranchCoverageTestFitness fitness = BranchCoverageFactory.createBranchCoverageTestFitness(cd); this.dependencies.get(fitness).add(ff); } } } } }
@Test public void minimizeSuiteOnlyWithVariables() { DefaultTestCase test = new DefaultTestCase(); for (int i = 0; i < 10; i++) { IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i); test.addStatement(ips); } assertEquals(10, test.size()); TestSuiteChromosome tsc = new TestSuiteChromosome(); tsc.addTest(test); TestSuiteFitnessFunction ff = new BranchCoverageSuiteFitness(); double previous_fitness = ff.getFitness(tsc); tsc.setFitness(ff, previous_fitness); assertEquals(0.0, previous_fitness, 0.0); TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory()); minimizer.minimize(tsc, false); assertEquals(0, tsc.getTests().size()); double fitness = ff.getFitness(tsc); assertEquals(previous_fitness, fitness, 0.0); }
/** * Initialize the set of known coverage goals */ protected void determineCoverageGoals(boolean updateArchive) { List<BranchCoverageTestFitness> goals = new BranchCoverageFactory().getCoverageGoals(); for (BranchCoverageTestFitness goal : goals) { // Skip instrumented branches - we only want real branches if(goal.getBranch() != null) { if(goal.getBranch().isInstrumented()) { continue; } } if(updateArchive && Properties.TEST_ARCHIVE) Archive.getArchiveInstance().addTarget(goal); if (goal.getBranch() == null) { branchlessMethodCoverageMap.put(goal.getClassName() + "." + goal.getMethod(), goal); } else { branchesId.add(goal.getBranch().getActualBranchId()); if (goal.getBranchExpressionValue()) branchCoverageTrueMap.put(goal.getBranch().getActualBranchId(), goal); else branchCoverageFalseMap.put(goal.getBranch().getActualBranchId(), goal); } } }
private void setupDependencies() { goalInstruction = BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getFirstInstructionAtLineNumber(className, methodName, line); if(goalInstruction == null) return; Set<ControlDependency> cds = goalInstruction.getControlDependencies(); for (ControlDependency cd : cds) { BranchCoverageTestFitness fitness = BranchCoverageFactory.createBranchCoverageTestFitness(cd); branchFitnesses.add(fitness); } if (goalInstruction.isRootBranchDependent()) branchFitnesses.add(BranchCoverageFactory.createRootBranchTestFitness(goalInstruction)); if (cds.isEmpty() && !goalInstruction.isRootBranchDependent()) throw new IllegalStateException( "expect control dependencies to be empty only for root dependent instructions: " + toString()); if (branchFitnesses.isEmpty()) throw new IllegalStateException( "an instruction is at least on the root branch of it's method"); branchFitnesses.sort((a,b) -> a.compareTo(b)); }
private void addDependencies4Output() { logger.debug("Added dependencies for Output"); for (FitnessFunction<T> ff : this.uncoveredGoals){ if (ff instanceof OutputCoverageTestFitness){ OutputCoverageTestFitness output = (OutputCoverageTestFitness) ff; ClassLoader loader = TestGenerationContext.getInstance().getClassLoaderForSUT(); BytecodeInstructionPool pool = BytecodeInstructionPool.getInstance(loader); if (pool.getInstructionsIn(output.getClassName(), output.getMethod()) == null) continue; for (BytecodeInstruction instruction : pool.getInstructionsIn(output.getClassName(), output.getMethod())) { if (instruction.getBasicBlock() != null){ Set<ControlDependency> cds = instruction.getBasicBlock().getControlDependencies(); if (cds.size()==0){ this.currentGoals.add(ff); } else { for (ControlDependency cd : cds) { BranchCoverageTestFitness fitness = BranchCoverageFactory.createBranchCoverageTestFitness(cd); this.dependencies.get(fitness).add(ff); } } } } } } }
public List<BranchCoverageTestFitness> getCoverageGoalsForAllKnownClasses() { return computeCoverageGoals(false); }
assertEquals(2.0, previous_fitness, 0.0); TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory()); minimizer.minimize(tsc, false); assertEquals(1, tsc.getTests().size());
/** * This methods derive the dependencies between {@link InputCoverageTestFitness} and branches. * Therefore, it is used to update 'this.dependencies' */ private void addDependencies4Input() { logger.debug("Added dependencies for Input"); for (FitnessFunction<T> ff : this.uncoveredGoals){ if (ff instanceof InputCoverageTestFitness){ InputCoverageTestFitness input = (InputCoverageTestFitness) ff; ClassLoader loader = TestGenerationContext.getInstance().getClassLoaderForSUT(); BytecodeInstructionPool pool = BytecodeInstructionPool.getInstance(loader); if (pool.getInstructionsIn(input.getClassName(), input.getMethod()) == null) continue; for (BytecodeInstruction instruction : pool.getInstructionsIn(input.getClassName(), input.getMethod())) { if (instruction.getBasicBlock() != null){ Set<ControlDependency> cds = instruction.getBasicBlock().getControlDependencies(); if (cds.size()==0){ this.currentGoals.add(ff); } else { for (ControlDependency cd : cds) { BranchCoverageTestFitness fitness = BranchCoverageFactory.createBranchCoverageTestFitness(cd); this.dependencies.get(fitness).add(ff); } } } } } } }
factories.add(new BranchCoverageFactory());
assertEquals(0.0, previous_fitness, 0.0); TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory()); minimizer.minimize(tsc, false); assertEquals(1, tsc.getTests().size());