Search in sources :

Example 51 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class AllMethodsTestChromosomeFactory method getChromosome.

/**
 * {@inheritDoc}
 *
 * Generate a random chromosome
 */
@Override
public TestChromosome getChromosome() {
    TestChromosome c = new TestChromosome();
    c.setTestCase(getRandomTestCase(Properties.CHROMOSOME_LENGTH));
    return c;
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome)

Example 52 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactory method getChromosome.

@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;
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 53 with TestChromosome

use of org.evosuite.testcase.TestChromosome 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);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) BranchCondition(org.evosuite.symbolic.BranchCondition) Comparator(org.evosuite.symbolic.expr.Comparator) TestChromosome(org.evosuite.testcase.TestChromosome) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 54 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class DeprecatedTestSuiteDSE method getCoveredBranches.

/**
 * 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;
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 55 with TestChromosome

use of org.evosuite.testcase.TestChromosome 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;
}
Also used : Constraint(org.evosuite.symbolic.expr.Constraint) TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BranchCondition(org.evosuite.symbolic.BranchCondition) TestChromosome(org.evosuite.testcase.TestChromosome) LinkedList(java.util.LinkedList)

Aggregations

TestChromosome (org.evosuite.testcase.TestChromosome)128 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)47 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)33 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)22 ArrayList (java.util.ArrayList)17 TestCase (org.evosuite.testcase.TestCase)17 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)16 HashSet (java.util.HashSet)15 Properties (org.evosuite.Properties)15 Test (org.junit.Test)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)14 HashMap (java.util.HashMap)11 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)10 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)8 LinkedHashMap (java.util.LinkedHashMap)7 Foo (com.examples.with.different.packagename.symbolic.Foo)6 LinkedHashSet (java.util.LinkedHashSet)6 Set (java.util.Set)6 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 Map (java.util.Map)5