Search in sources :

Example 11 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class BIAndRITestSuiteChromosomeFactory 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 chrom = null;
    if (!seeded && geneticAlgorithm != null) {
        seeded = true;
        chrom = (TestSuiteChromosome) geneticAlgorithm.getBestIndividual();
    } else if (geneticAlgorithm != null && Randomness.nextDouble() < Properties.SEED_PROBABILITY) {
        int populationSize = geneticAlgorithm.getPopulation().size();
        chrom = geneticAlgorithm.getPopulation().get(Randomness.nextInt(populationSize));
    }
    if (chrom == null) {
        chrom = defaultFactory.getChromosome();
    }
    return chrom;
}
Also used : TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 12 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class BIMutatedMethodSeedingTestSuiteChromosomeFactory 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 = defaultFactory.getChromosome();
    int numTests = chromosome.getTests().size();
    // reduce seed probablility by number of tests to be generated
    final double SEED_CHANCE = Properties.SEED_PROBABILITY / numTests;
    for (int i = 0; i < numTests; i++) {
        if (bestIndividual != null && Randomness.nextDouble() < SEED_CHANCE && Properties.SEED_MUTATIONS > 0) {
            TestSuiteChromosome bi = bestIndividual.clone();
            int mutations = Randomness.nextInt(Properties.SEED_MUTATIONS) + 1;
            for (int j = 0; j < mutations; j++) {
                bi.mutate();
            }
            int testSize = bi.getTests().size();
            TestCase test = bi.getTests().get(Random.nextInt(testSize));
            if (test != null) {
                List<TestCase> tests = chromosome.getTests();
                tests.remove(i);
                tests.add(i, test);
                chromosome.clearTests();
                for (TestCase t : tests) {
                    chromosome.addTest(t);
                }
            }
        }
    }
    return chromosome;
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 13 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class TestLocalSearchMIMEType method testFitness.

@Test
public void testFitness() throws NoSuchFieldException, SecurityException, NoSuchMethodException, ClassNotFoundException {
    Properties.RESET_STATIC_FINAL_FIELDS = false;
    Properties.TEST_ARCHIVE = false;
    Properties.LOCAL_SEARCH_PROBABILITY = 1.0;
    Properties.LOCAL_SEARCH_RATE = 1;
    Properties.LOCAL_SEARCH_BUDGET_TYPE = Properties.LocalSearchBudgetType.TESTS;
    Properties.LOCAL_SEARCH_BUDGET = 100;
    Properties.DSE_SOLVER = SolverType.EVOSUITE_SOLVER;
    Properties.STOPPING_CONDITION = StoppingCondition.MAXTIME;
    Properties.SEARCH_BUDGET = 120;
    Properties.TARGET_CLASS = MIMEType.class.getName();
    String classPath = ClassPathHandler.getInstance().getTargetProjectClasspath();
    DependencyAnalysis.analyzeClass(MIMEType.class.getName(), Arrays.asList(classPath));
    TestSuiteChromosome suite = new TestSuiteChromosome();
    DefaultTestCase test0 = createTestCase0();
    DefaultTestCase test1 = createTestCase1();
    DefaultTestCase test2 = createTestCase2();
    DefaultTestCase test3 = createTestCase3();
    DefaultTestCase test4 = createTestCase4();
    DefaultTestCase test5 = createTestCase5();
    suite.addTest(test0);
    suite.addTest(test1);
    suite.addTest(test2);
    suite.addTest(test3);
    suite.addTest(test4);
    suite.addTest(test5);
    TestSuiteFitnessFunction lineCoverage = FitnessFunctions.getFitnessFunction(Criterion.LINE);
    TestSuiteFitnessFunction branchCoverage = FitnessFunctions.getFitnessFunction(Criterion.BRANCH);
    TestSuiteFitnessFunction exceptionCoverage = FitnessFunctions.getFitnessFunction(Criterion.EXCEPTION);
    TestSuiteFitnessFunction weakMutationCoverage = FitnessFunctions.getFitnessFunction(Criterion.WEAKMUTATION);
    TestSuiteFitnessFunction outputCoverage = FitnessFunctions.getFitnessFunction(Criterion.OUTPUT);
    TestSuiteFitnessFunction methodCoverage = FitnessFunctions.getFitnessFunction(Criterion.METHOD);
    TestSuiteFitnessFunction methodNoExceptionCoverage = FitnessFunctions.getFitnessFunction(Criterion.METHODNOEXCEPTION);
    TestSuiteFitnessFunction cbranchCoverage = FitnessFunctions.getFitnessFunction(Criterion.CBRANCH);
    List<TestSuiteFitnessFunction> fitnessFunctions = new ArrayList<TestSuiteFitnessFunction>();
    fitnessFunctions.add(lineCoverage);
    fitnessFunctions.add(branchCoverage);
    fitnessFunctions.add(exceptionCoverage);
    fitnessFunctions.add(weakMutationCoverage);
    fitnessFunctions.add(outputCoverage);
    fitnessFunctions.add(methodCoverage);
    fitnessFunctions.add(methodNoExceptionCoverage);
    fitnessFunctions.add(cbranchCoverage);
    for (TestSuiteFitnessFunction ff : fitnessFunctions) {
        suite.addFitness(ff);
    }
    for (TestSuiteFitnessFunction ff : fitnessFunctions) {
        double oldFitness = ff.getFitness(suite);
        System.out.println(ff.toString() + "->" + oldFitness);
    }
    double oldFitness = suite.getFitness();
    System.out.println("oldFitness->" + oldFitness);
    System.out.println("oldSize->" + suite.getTests().size());
    DefaultLocalSearchObjective objective = new DefaultLocalSearchObjective<>();
    for (TestSuiteFitnessFunction ff : fitnessFunctions) {
        objective.addFitnessFunction(ff);
    }
    boolean hasImproved = suite.localSearch(objective);
    System.out.println("hasImproved=" + hasImproved);
    for (TestSuiteFitnessFunction ff : fitnessFunctions) {
        double newFitness = ff.getFitness(suite);
        System.out.println(ff.toString() + "->" + newFitness);
    }
    double newFitness = suite.getFitness();
    System.out.println("newFitness->" + newFitness);
    System.out.println("newSize->" + suite.getTests().size());
    assertTrue(newFitness <= oldFitness);
}
Also used : MIMEType(com.examples.with.different.packagename.concolic.MIMEType) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) ArrayList(java.util.ArrayList) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 14 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class TestDSETestCaseLocalSearch method testCVC4Solver.

@Test
public void testCVC4Solver() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    String cvc4_path = System.getenv("cvc4_path");
    if (cvc4_path != null) {
        Properties.CVC4_PATH = cvc4_path;
    }
    Assume.assumeTrue(Properties.CVC4_PATH != null);
    Properties.DSE_SOLVER = Properties.SolverType.CVC4_SOLVER;
    Properties.CRITERION = new Properties.Criterion[] { Criterion.BRANCH };
    Properties.TARGET_CLASS = Foo.class.getName();
    TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    suite.addFitness(branchCoverageSuiteFitness);
    branchCoverageSuiteFitness.getFitness(suite);
    // no goals covered yet
    int coveredGoals0 = suite.getNumOfCoveredGoals();
    int notCoveredGoals0 = suite.getNumOfNotCoveredGoals();
    assertEquals(0, coveredGoals0);
    assertNotEquals(0, notCoveredGoals0);
    DefaultTestCase testCase0 = buildTestCase0();
    TestChromosome testChromosome0 = new TestChromosome();
    testChromosome0.setTestCase(testCase0);
    suite.addTest(testChromosome0);
    double fitnessBeforeLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsBeforeLocalSearch = suite.getNumOfCoveredGoals();
    // some goal was covered
    assertTrue(coveredGoalsBeforeLocalSearch > 0);
    DefaultTestCase duplicatedTestCase0 = buildTestCase0();
    TestChromosome duplicatedTestChromosome0 = new TestChromosome();
    duplicatedTestChromosome0.setTestCase(duplicatedTestCase0);
    suite.addTest(duplicatedTestChromosome0);
    TestSuiteLocalSearchObjective localSearchObjective = TestSuiteLocalSearchObjective.buildNewTestSuiteLocalSearchObjective(Collections.singletonList(branchCoverageSuiteFitness), suite, 1);
    DSETestCaseLocalSearch localSearch = new DSETestCaseLocalSearch();
    boolean improved = localSearch.doSearch(duplicatedTestChromosome0, localSearchObjective);
    assertTrue(improved);
    double fitnessAfterLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsAfterLocalSearch = suite.getNumOfCoveredGoals();
    assertTrue(fitnessAfterLocalSearch < fitnessBeforeLocalSearch);
    assertTrue(coveredGoalsAfterLocalSearch > coveredGoalsBeforeLocalSearch);
}
Also used : TestSuiteLocalSearchObjective(org.evosuite.testsuite.localsearch.TestSuiteLocalSearchObjective) Foo(com.examples.with.different.packagename.symbolic.Foo) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 15 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class TestDSETestSuiteCookie method testCVC4Solver.

@Test
public void testCVC4Solver() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    String cvc4_path = System.getenv("cvc4_path");
    if (cvc4_path != null) {
        Properties.CVC4_PATH = cvc4_path;
    }
    Assume.assumeTrue(Properties.CVC4_PATH != null);
    Properties.DSE_SOLVER = Properties.SolverType.CVC4_SOLVER;
    Properties.CRITERION = new Properties.Criterion[] { Criterion.BRANCH };
    Properties.TARGET_CLASS = Foo.class.getName();
    TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    suite.addFitness(branchCoverageSuiteFitness);
    branchCoverageSuiteFitness.getFitness(suite);
    // no goals covered yet
    int coveredGoals0 = suite.getNumOfCoveredGoals();
    int notCoveredGoals0 = suite.getNumOfNotCoveredGoals();
    assertEquals(0, coveredGoals0);
    assertNotEquals(0, notCoveredGoals0);
    DefaultTestCase testCase0 = buildTestCase0();
    TestChromosome testChromosome0 = new TestChromosome();
    testChromosome0.setTestCase(testCase0);
    suite.addTest(testChromosome0);
    double fitnessBeforeLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsBeforeLocalSearch = suite.getNumOfCoveredGoals();
    // some goal was covered
    assertTrue(coveredGoalsBeforeLocalSearch > 0);
    LocalSearchObjective<TestSuiteChromosome> localSearchObjective = new DefaultLocalSearchObjective<>();
    localSearchObjective.addFitnessFunction(branchCoverageSuiteFitness);
    boolean improved;
    do {
        TestSuiteLocalSearch localSearch = new TestSuiteLocalSearch();
        improved = localSearch.doSearch(suite, localSearchObjective);
    } while (improved);
    double fitnessAfterLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsAfterLocalSearch = suite.getNumOfCoveredGoals();
    assertTrue(fitnessAfterLocalSearch < fitnessBeforeLocalSearch);
    assertTrue(coveredGoalsAfterLocalSearch > coveredGoalsBeforeLocalSearch);
    int finalSuiteSize = suite.size();
    assertTrue(coveredGoalsAfterLocalSearch == 8);
    assertTrue(finalSuiteSize >= 5);
}
Also used : Foo(com.examples.with.different.packagename.symbolic.Foo) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Aggregations

TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)536 Test (org.junit.Test)430 EvoSuite (org.evosuite.EvoSuite)392 Properties (org.evosuite.Properties)78 OutputVariable (org.evosuite.statistics.OutputVariable)50 GenericSUTString (com.examples.with.different.packagename.generic.GenericSUTString)49 TestChromosome (org.evosuite.testcase.TestChromosome)47 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)44 TestCase (org.evosuite.testcase.TestCase)43 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)30 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)22 Ignore (org.junit.Ignore)19 ArrayList (java.util.ArrayList)17 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)17 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)14 CheapPurityAnalyzer (org.evosuite.assertion.CheapPurityAnalyzer)13 LineCoverageSuiteFitness (org.evosuite.coverage.line.LineCoverageSuiteFitness)13 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)13 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)10 Method (java.lang.reflect.Method)9