@SuppressWarnings("unchecked") private void addDependencies4TryCatch() { logger.debug("Added dependencies for Try-Catch"); for (FitnessFunction<T> ff : this.uncoveredGoals){ if (ff instanceof TryCatchCoverageTestFitness){ TryCatchCoverageTestFitness stmt = (TryCatchCoverageTestFitness) ff; BranchCoverageTestFitness branch = new BranchCoverageTestFitness(stmt.getBranchGoal()); this.dependencies.get(branch).add((FitnessFunction<T>) stmt); } } }
@Override public int compareTo(TestFitnessFunction other) { if (other instanceof BranchCoverageTestFitness) { BranchCoverageTestFitness otherBranchFitness = (BranchCoverageTestFitness) other; return goal.compareTo(otherBranchFitness.goal); } return compareClassName(other); }
private void initializeMaps(Set<FitnessFunction<T>> set){ for (FitnessFunction<T> ff : set) { BranchCoverageTestFitness goal = (BranchCoverageTestFitness) ff; // Skip instrumented branches - we only want real branches if(goal.getBranch() != null) { if(goal.getBranch().isInstrumented()) { continue; } } if (goal.getBranch() == null) { branchlessMethodCoverageMap.put(goal.getClassName() + "." + goal.getMethod(), ff); } else { if (goal.getBranchExpressionValue()) branchCoverageTrueMap.put(goal.getBranch().getActualBranchId(), ff); else branchCoverageFalseMap.put(goal.getBranch().getActualBranchId(), ff); } } }
/** * This methods derive the dependencies between {@link MethodNoExceptionCoverageTestFitness} and branches. * Therefore, it is used to update 'this.dependencies' */ @SuppressWarnings("unchecked") private void addDependencies4MethodsNoException() { logger.debug("Added dependencies for MethodsNoException"); for (BranchCoverageTestFitness branch : this.dependencies.keySet()){ MethodNoExceptionCoverageTestFitness method = new MethodNoExceptionCoverageTestFitness(branch.getClassName(), branch.getMethod()); this.dependencies.get(branch).add((FitnessFunction<T>) method); } }
/** * This methods derive the dependencies between {@link CBranchTestFitness} and branches. * Therefore, it is used to update 'this.dependencies' */ @SuppressWarnings("unchecked") private void addDependencies4CBranch() { logger.debug("Added dependencies for CBranch"); CallGraph callGraph = DependencyAnalysis.getCallGraph(); for (BranchCoverageTestFitness branch : this.dependencies.keySet()) { for (CallContext context : callGraph.getMethodEntryPoint(branch.getClassName(), branch.getMethod())) { CBranchTestFitness cBranch = new CBranchTestFitness(branch.getBranchGoal(), context); this.dependencies.get(branch).add((FitnessFunction<T>) cBranch); logger.debug("Added context branch: " + cBranch.toString()); } } }
@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); } }
Branch branch = ((BranchCoverageTestFitness) fitness).getBranch(); if (branch==null){ this.rootBranches.add(fitness); BranchCoverageTestFitness newFitness = new BranchCoverageTestFitness(goal); graph.addEdge((FitnessFunction<T>) newFitness, fitness); BranchCoverageTestFitness newfitness2 = new BranchCoverageTestFitness(goal2); graph.addEdge((FitnessFunction<T>) newfitness2, fitness);
double newFitness = branchFitness.getFitness(individual, result); if (newFitness == 0.0) { r = 0.0;
@Override public String getTargetMethod() { return getMethod(); }
@Override public String getTargetClass() { return getClassName(); }
@Override public int compareTo(TestFitnessFunction other) { if (other instanceof IBranchTestFitness) { IBranchTestFitness otherBranchFitness = (IBranchTestFitness) other; return branchGoal.compareTo(otherBranchFitness.branchGoal); } else if (other instanceof BranchCoverageTestFitness) { BranchCoverageTestFitness otherBranchFitness = (BranchCoverageTestFitness) other; return branchGoal.compareTo(otherBranchFitness.getBranchGoal()); } return compareClassName(other); // return -1; }
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)); }
@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); } }
/** * This methods derive the dependencies between {@link MethodCoverageTestFitness} and branches. * Therefore, it is used to update 'this.dependencies' */ @SuppressWarnings("unchecked") private void addDependencies4Methods() { logger.debug("Added dependencies for Methods"); for (BranchCoverageTestFitness branch : this.dependencies.keySet()){ MethodCoverageTestFitness method = new MethodCoverageTestFitness(branch.getClassName(), branch.getMethod()); this.dependencies.get(branch).add((FitnessFunction<T>) method); } }
double newFitness = branchFitness.getFitness(individual, result); if (newFitness == 0.0) {
/** * This methods derive the dependencies between {@link org.evosuite.coverage.mutation.StrongMutationTestFitness} and branches. * Therefore, it is used to update 'this.dependencies' */ private void addDependencies4StrongMutation() { logger.debug("Added dependencies for Strong-Mutation"); for (FitnessFunction<T> ff : this.uncoveredGoals){ if (ff instanceof StrongMutationTestFitness){ StrongMutationTestFitness mutation = (StrongMutationTestFitness) ff; Set<BranchCoverageGoal> goals = mutation.getMutation().getControlDependencies(); if (goals.size() == 0){ this.currentGoals.add(ff); } else { for (BranchCoverageGoal goal : goals) { BranchCoverageTestFitness fitness = new BranchCoverageTestFitness(goal); this.dependencies.get(fitness).add(ff); } } } } }
BranchCoverageTestFitness goal = (BranchCoverageTestFitness) ff; if(goal.getBranch() != null) { if(goal.getBranch().isInstrumented()) { continue; if (goal.getBranch() == null) { branchlessMethodCoverageMap.put(goal.getClassName() + "." + goal.getMethod(), ff); } else { if (goal.getBranchExpressionValue()) branchCoverageTrueMap.put(goal.getBranch().getActualBranchId(), ff); else branchCoverageFalseMap.put(goal.getBranch().getActualBranchId(), ff);
/** * This methods derive the dependencies between {@link WeakMutationTestFitness} and branches. * Therefore, it is used to update 'this.dependencies' */ private void addDependencies4WeakMutation() { logger.debug("Added dependencies for Weak-Mutation"); for (FitnessFunction<T> ff : this.uncoveredGoals){ if (ff instanceof WeakMutationTestFitness){ WeakMutationTestFitness mutation = (WeakMutationTestFitness) ff; Set<BranchCoverageGoal> goals = mutation.getMutation().getControlDependencies(); if (goals.size() == 0){ this.currentGoals.add(ff); } else { for (BranchCoverageGoal goal : goals) { BranchCoverageTestFitness fitness = new BranchCoverageTestFitness(goal); this.dependencies.get(fitness).add(ff); } } } } }
/** * 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); } } }
/** * Create a fitness function for branch coverage aimed at covering the root * branch of the given method in the given class. Covering a root branch * means entering the method. * * @param className * a {@link java.lang.String} object. * @param method * a {@link java.lang.String} object. * @return a {@link org.evosuite.coverage.branch.BranchCoverageTestFitness} * object. */ public static BranchCoverageTestFitness createRootBranchTestFitness( String className, String method) { return new BranchCoverageTestFitness(new BranchCoverageGoal(className, method.substring(method.lastIndexOf(".") + 1))); }