Search in sources :

Example 46 with TestChromosome

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

the class TestSuiteSerialization method saveTests.

public static boolean saveTests(TestSuiteChromosome ts, File target) throws IllegalArgumentException {
    File parent = target.getParentFile();
    if (!parent.exists()) {
        parent.mkdirs();
    }
    try (ObjectOutputStream out = new DebuggingObjectOutputStream(new FileOutputStream(target))) {
        for (TestChromosome tc : ts.getTestChromosomes()) {
            out.writeObject(tc);
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        logger.error("Failed to open/handle " + target.getAbsolutePath() + " for writing: " + e.getMessage());
        return false;
    }
    return true;
}
Also used : DebuggingObjectOutputStream(org.evosuite.utils.DebuggingObjectOutputStream) DebuggingObjectOutputStream(org.evosuite.utils.DebuggingObjectOutputStream) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 47 with TestChromosome

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

the class JUnitTestSuiteChromosomeFactory method getChromosome.

/* (non-Javadoc)
	 * @see org.evosuite.ga.ChromosomeFactory#getChromosome()
	 */
/**
 * {@inheritDoc}
 */
@Override
public TestSuiteChromosome getChromosome() {
    /*
		double P_delta = 0.1d;
		double P_clone = 0.1d;
		int MAX_CHANGES = 10;
		*/
    TestSuiteChromosome chromosome = new TestSuiteChromosome(new RandomLengthTestFactory());
    chromosome.clearTests();
    int numTests = Randomness.nextInt(Properties.MIN_INITIAL_TESTS, Properties.MAX_INITIAL_TESTS + 1);
    for (int i = 0; i < numTests; i++) {
        TestChromosome test = defaultFactory.getChromosome();
        chromosome.addTest(test);
    // chromosome.tests.add(test);
    }
    return chromosome;
}
Also used : TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 48 with TestChromosome

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

the class TestSuiteChromosomeFactory method getChromosome.

/**
 * {@inheritDoc}
 */
@Override
public TestSuiteChromosome getChromosome() {
    TestSuiteChromosome chromosome = new TestSuiteChromosome(testChromosomeFactory);
    chromosome.clearTests();
    // ((AllMethodsChromosomeFactory)test_factory).clear();
    int numTests = Randomness.nextInt(Properties.MIN_INITIAL_TESTS, Properties.MAX_INITIAL_TESTS + 1);
    for (int i = 0; i < numTests; i++) {
        TestChromosome test = testChromosomeFactory.getChromosome();
        chromosome.addTest(test);
    // chromosome.tests.add(test);
    }
    // logger.trace("Generated new test suite:"+chromosome);
    return chromosome;
}
Also used : TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 49 with TestChromosome

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

the class TestSuiteLocalSearch method applyLocalSearch.

/**
 * Decides the type of local search to be applied, and invokes the
 * corresponding local search procedure.
 *
 * @param suite
 *            the suite to optimise
 * @param objective
 *            the local search objective
 */
private void applyLocalSearch(TestSuiteChromosome suite, LocalSearchObjective<TestSuiteChromosome> objective) {
    final LocalSearchSuiteType localSearchType;
    localSearchType = chooseLocalSearchSuiteType();
    /*
		 * We make a copy of the original test cases before Local Search
		 */
    List<TestChromosome> originalTests = new ArrayList<TestChromosome>(suite.getTestChromosomes());
    for (final TestChromosome test : originalTests) {
        // again
        if (test.hasLocalSearchBeenApplied()) {
            TestCaseLocalSearch.randomizePrimitives(test.getTestCase());
            updateFitness(suite, objective.getFitnessFunctions());
        }
        if (LocalSearchBudget.getInstance().isFinished()) {
            logger.debug("Local search budget used up: " + Properties.LOCAL_SEARCH_BUDGET_TYPE);
            break;
        }
        logger.debug("Local search budget not yet used up");
        final double tossCoin = Randomness.nextDouble();
        final boolean shouldApplyDSE = localSearchType == LocalSearchSuiteType.ALWAYS_DSE || (localSearchType == LocalSearchSuiteType.DSE_AND_AVM && tossCoin <= Properties.DSE_PROBABILITY);
        /*
			 * We create a cloned test case to play local search with it. This
			 * resembles the deprecated ensureDoubleExecution
			 */
        TestChromosome clonedTest = (TestChromosome) test.clone();
        suite.addTest(clonedTest);
        final int lastIndex = suite.size() - 1;
        final boolean improved;
        if (shouldApplyDSE) {
            improved = applyDSE(suite, lastIndex, clonedTest, objective);
        } else {
            improved = applyAVM(suite, lastIndex, clonedTest, objective);
        }
        if (improved) {
            updateFitness(suite, objective.getFitnessFunctions());
        } else {
            // remove cloned test case if there was no improvement
            suite.deleteTest(clonedTest);
        }
        test.setLocalSearchApplied(true);
    }
}
Also used : ArrayList(java.util.ArrayList) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 50 with TestChromosome

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

the class TestSuiteLocalSearch method applyAVM.

/**
 * Applies AVM on the test case in the suite
 *
 * @param suite
 * @param testIndex
 * @param test
 * @param localSearchObjective
 * @return
 */
private boolean applyAVM(TestSuiteChromosome suite, int testIndex, TestChromosome test, LocalSearchObjective<TestSuiteChromosome> objective) {
    logger.debug("Local search on test " + testIndex + ", current fitness: " + suite.getFitness());
    final List<FitnessFunction<? extends Chromosome>> fitnessFunctions = objective.getFitnessFunctions();
    TestSuiteLocalSearchObjective testCaseLocalSearchObjective = TestSuiteLocalSearchObjective.buildNewTestSuiteLocalSearchObjective(fitnessFunctions, suite, testIndex);
    AVMTestCaseLocalSearch testCaselocalSearch = new AVMTestCaseLocalSearch();
    boolean improved = testCaselocalSearch.doSearch(test, testCaseLocalSearchObjective);
    return improved;
}
Also used : Chromosome(org.evosuite.ga.Chromosome) TestChromosome(org.evosuite.testcase.TestChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) AVMTestCaseLocalSearch(org.evosuite.testcase.localsearch.AVMTestCaseLocalSearch) FitnessFunction(org.evosuite.ga.FitnessFunction) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

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