/** * Ensure that all branches are executed twice */ private void restoreBranchCoverage(TestSuiteChromosome individual, TestSuiteFitnessFunction objective) { logger.debug("Adding branches already covered previously"); BranchCoverageMap branchMap = BranchCoverageMap.getInstance(); Set<Integer> uncoveredTrueBranches = new LinkedHashSet<Integer>(branchMap.getCoveredTrueBranches()); Set<Integer> uncoveredFalseBranches = new LinkedHashSet<Integer>(branchMap.getCoveredFalseBranches()); uncoveredTrueBranches.removeAll(getCoveredTrueBranches(individual)); uncoveredFalseBranches.removeAll(getCoveredFalseBranches(individual)); for (Integer branchId : uncoveredTrueBranches) { individual.addTest(branchMap.getTestCoveringTrue(branchId).clone()); } for (Integer branchId : uncoveredFalseBranches) { individual.addTest(branchMap.getTestCoveringFalse(branchId).clone()); } }
protected boolean deleteStatement(TestFactory testFactory, int num) { try { TestCase copy = test.clone(); mutationHistory.addMutationEntry(new TestMutationHistoryEntry( TestMutationHistoryEntry.TestMutation.DELETION)); boolean modified = testFactory.deleteStatementGracefully(copy, num); test = copy; return modified; } catch (ConstructionFailedException e) { logger.warn("Deletion of statement failed: " + test.getStatement(num).getCode()); logger.warn(test.toCode()); return false; //modifications were on copy } }
@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; }
@SuppressWarnings({ "rawtypes", "unchecked" }) public static TestCase updateTest(TestCase test, Map<String, Object> values) { TestCase newTest = test.clone(); newTest.clearCoveredGoals();
TestCase clonedTestCase = currentTestCase.clone();
TestCase newTest = test.clone();
DSEStats.getInstance().reportNewSAT(); TestCase newTest = test.clone();
/** * <p> * Constructor for ContractViolation. * </p> * * @param contract * a {@link org.evosuite.contracts.Contract} object. * @param statement * a {@link org.evosuite.testcase.statements.Statement} object. * @param exception * a {@link java.lang.Throwable} object. */ public ContractViolation(Contract contract, Statement statement, Throwable exception, VariableReference... variables) { this.contract = contract; this.test = statement.getTestCase().clone(); this.test.chop(statement.getPosition() + 1); ((DefaultTestCase) this.test).setFailing(true); this.statement = this.test.getStatement(statement.getPosition()); for (VariableReference var : variables) { this.variables.add(var.clone(this.test)); } this.exception = exception; }
public TestCase expandTestCase(TestCase test) { TestCase expandedTest = test.clone(); // Deactivated for now - only needed in NL branch // createConcretePrimitives(expandedTest); while (currentPosition < expandedTest.size()) { Statement statement = expandedTest.getStatement(currentPosition); if (statement instanceof MethodStatement) { visitMethodStatement(expandedTest, (MethodStatement) statement); } else if (statement instanceof ConstructorStatement) { visitConstructorStatement(expandedTest, (ConstructorStatement) statement); } else if (statement instanceof ArrayStatement) { visitArrayStatement(expandedTest, ((ArrayStatement) statement)); } else if (statement instanceof AssignmentStatement) { visitAssignmentStatement(expandedTest, ((AssignmentStatement) statement)); } currentPosition++; } return expandedTest; }
protected void restore(TestChromosome test) { if (backup == null) return; test.setTestCase(backup.getTestCase().clone()); test.copyCachedResults(backup); //test.setFitness(backup.getFitness()); test.setFitnessValues(backup.getFitnessValues()); test.setPreviousFitnessValues(backup.getPreviousFitnessValues()); test.setChanged(backup.isChanged()); }
LocalSearchObjective<TestChromosome> objective) { TestCase slice = test.getTestCase().clone(); int newPos = slice.sliceFor(slice.getStatement(statement).getReturnValue()); TestCase oldTest = test.getTestCase();
DefaultTestCase clone_test_case = (DefaultTestCase) test.getTestCase().clone(); final PathCondition collectedPathCondition = ConcolicExecution.executeConcolic(clone_test_case);
TestCase newTest = test.getTestCase().clone(); TestCase expandedTest = expandTestCase(newTest); TestChromosome expandedTestChromosome = newTestSuite.addTest(expandedTest);
/** * Convert a test suite to a pool * * @param testSuite */ public static ObjectPool getPoolFromTestSuite(TestSuiteChromosome testSuite) { ObjectPool pool = new ObjectPool(); for (TestChromosome testChromosome : testSuite.getTestChromosomes()) { TestCase test = testChromosome.getTestCase().clone(); test.removeAssertions(); /* if (testChromosome.hasException()) { // No code including or after an exception should be in the pool Integer pos = testChromosome.getLastExecutionResult().getFirstPositionOfThrownException(); if (pos != null) { test.chop(pos); } else { test.chop(test.size() - 1); } } */ Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (!testChromosome.hasException() && test.hasObject(targetClass, test.size())) { pool.addSequence(new GenericClass(targetClass), test); } } return pool; }
/** * Method used to mutate an offspring. * * @param offspring * @param parent */ private void mutate(T offspring, T parent) { offspring.mutate(); TestChromosome tch = (TestChromosome) offspring; if (!offspring.isChanged()) { // if offspring is not changed, we try to mutate it once again offspring.mutate(); } if (!this.hasMethodCall(offspring)) { tch.setTestCase(((TestChromosome) parent).getTestCase().clone()); boolean changed = tch.mutationInsert(); if (changed) { for (Statement s : tch.getTestCase()) { s.isValid(); } } offspring.setChanged(changed); } this.notifyMutation(offspring); }
@Override public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) { NullStatement nullStatement = (NullStatement) test.getTestCase().getStatement(statement); TestCase newTest = test.getTestCase(); TestCase oldTest = newTest.clone(); ExecutionResult oldResult = test.getLastExecutionResult(); //double oldFitness = test.getFitness(); Map<FitnessFunction<?>, Double> oldFitnesses = test.getFitnessValues(); Map<FitnessFunction<?>, Double> oldLastFitnesses = test.getPreviousFitnessValues(); try { TestFactory.getInstance().attemptGeneration(newTest, nullStatement.getReturnType(), statement); if (!objective.hasImproved(test)) { test.setTestCase(oldTest); test.setLastExecutionResult(oldResult); //test.setFitness(oldFitness); test.setFitnessValues(oldFitnesses); test.setPreviousFitnessValues(oldLastFitnesses); } else { return true; } } catch (ConstructionFailedException e) { // If we can't construct it, then ignore } return false; }
TestCase slice = test.getTestCase().clone(); int newPos = slice.sliceFor(slice.getStatement(statement).getReturnValue()); TestCase oldTest = test.getTestCase();
/** * {@inheritDoc} * * Create a deep copy of the chromosome */ @Override public Chromosome clone() { TestChromosome c = new TestChromosome(); c.test = test.clone(); c.setFitnessValues(getFitnessValues()); c.setPreviousFitnessValues(getPreviousFitnessValues()); c.copyCachedResults(this); c.setChanged(isChanged()); c.setLocalSearchApplied(hasLocalSearchBeenApplied()); if (Properties.LOCAL_SEARCH_SELECTIVE) { for (TestMutationHistoryEntry mutation : mutationHistory) { if(test.contains(mutation.getStatement())) c.mutationHistory.addMutationEntry(mutation.clone(c.getTestCase())); } } // c.mutationHistory.set(mutationHistory); c.setNumberOfMutations(this.getNumberOfMutations()); c.setNumberOfEvaluations(this.getNumberOfEvaluations()); c.setKineticEnergy(getKineticEnergy()); c.setNumCollisions(getNumCollisions()); return c; }