Search in sources :

Example 6 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class TestTestSuiteMinimizer method minimizeSuiteHalfCoverageWithTwoFitnessFunctions.

@Test
public void minimizeSuiteHalfCoverageWithTwoFitnessFunctions() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    testFactory.addConstructor(test, gc, 0, 0);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    for (int i = 0; i < 10; i++) {
        IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
        VariableReference vr = test.addStatement(ips, i + 1);
    }
    ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
    GenericMethod method = new GenericMethod(m, sut);
    testFactory.addMethod(test, method, 11, 0);
    assertEquals(12, test.size());
    TestSuiteChromosome tsc = new TestSuiteChromosome();
    tsc.addTest(test);
    TestSuiteFitnessFunction branch = new BranchCoverageSuiteFitness();
    double previous_branch_fitness = branch.getFitness(tsc);
    tsc.setFitness(branch, previous_branch_fitness);
    assertEquals(previous_branch_fitness, 2.0, 0.0);
    TestSuiteFitnessFunction defuse = new DefUseCoverageSuiteFitness();
    double previous_defuse_fitness = defuse.getFitness(tsc);
    tsc.setFitness(defuse, previous_defuse_fitness);
    assertEquals(previous_defuse_fitness, 0.0, 0.0);
    List<TestFitnessFactory<? extends TestFitnessFunction>> factories = new ArrayList<TestFitnessFactory<? extends TestFitnessFunction>>();
    factories.add(new BranchCoverageFactory());
    factories.add(new DefUseCoverageFactory());
    TestSuiteMinimizer minimizer = new TestSuiteMinimizer(factories);
    minimizer.minimize(tsc, false);
    assertEquals(1, tsc.getTests().size());
    assertEquals(3, tsc.getTests().get(0).size());
    // assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nflagExample1_0.testMe(int0);\n"));
    double branch_fitness = branch.getFitness(tsc);
    assertEquals(previous_branch_fitness, branch_fitness, 0.0);
    double defuse_fitness = defuse.getFitness(tsc);
    assertEquals(previous_defuse_fitness, defuse_fitness, 0.0);
}
Also used : DefUseCoverageSuiteFitness(org.evosuite.coverage.dataflow.DefUseCoverageSuiteFitness) ArrayList(java.util.ArrayList) GenericMethod(org.evosuite.utils.generic.GenericMethod) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) DefUseCoverageFactory(org.evosuite.coverage.dataflow.DefUseCoverageFactory) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) FlagExample1(com.examples.with.different.packagename.FlagExample1) Test(org.junit.Test)

Example 7 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class TestTestSuiteMinimizer method minimizeSuiteOnlyWithVariables.

@Test
public void minimizeSuiteOnlyWithVariables() {
    DefaultTestCase test = new DefaultTestCase();
    for (int i = 0; i < 10; i++) {
        IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i);
        test.addStatement(ips);
    }
    assertEquals(10, test.size());
    TestSuiteChromosome tsc = new TestSuiteChromosome();
    tsc.addTest(test);
    TestSuiteFitnessFunction ff = new BranchCoverageSuiteFitness();
    double previous_fitness = ff.getFitness(tsc);
    tsc.setFitness(ff, previous_fitness);
    assertEquals(previous_fitness, 0.0, 0.0);
    TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory());
    minimizer.minimize(tsc, false);
    assertEquals(0, tsc.getTests().size());
    double fitness = ff.getFitness(tsc);
    assertEquals(previous_fitness, fitness, 0.0);
}
Also used : BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 8 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class TestDSETestSuiteFlag 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 = Flag.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);
    branchCoverageSuiteFitness.getFitness(suite);
    DefaultTestCase testCase1 = buildTestCase1();
    TestChromosome testChromosome1 = new TestChromosome();
    testChromosome1.setTestCase(testCase1);
    suite.addTest(testChromosome1);
    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();
    /*
		 * Check at least 6 goals were covered
		 */
    assertTrue(coveredGoalsAfterLocalSearch >= 6);
    assertTrue(finalSuiteSize > 1);
}
Also used : BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) Flag(com.examples.with.different.packagename.ncs.Flag) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 9 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class LocalSearchNumericSystemTest method runDoubleExample.

private void runDoubleExample(double x, double y) throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    TestCase test = getDoubleTest(x, y);
    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
    BranchCoverageMap.getInstance().searchStarted(null);
    assertEquals(4.0, fitness.getFitness(suite), 0.1F);
    suite.addTest(test);
    assertEquals(1.0, fitness.getFitness(suite), 0.1F);
    TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
    LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
    localObjective.addFitnessFunction(fitness);
    localSearch.doSearch(suite, localObjective);
    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals(0.0, fitness.getFitness(suite), 0.1F);
    BranchCoverageMap.getInstance().searchFinished(null);
}
Also used : TestSuiteLocalSearch(org.evosuite.testsuite.localsearch.TestSuiteLocalSearch) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 10 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class DeleteMutationSystemTest method testSimpleInt.

@Test
public void testSimpleInt() throws NoSuchMethodException, SecurityException, ClassNotFoundException, ConstructionFailedException {
    Properties.TARGET_CLASS = TrivialInt.class.getCanonicalName();
    TestChromosome test1 = new TestChromosome();
    test1.setTestCase(getIntTest(2938, -1000000));
    TestChromosome test2 = (TestChromosome) test1.clone();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
    assertEquals(4.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test1);
    assertEquals(1.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test2);
    // 0.99...
    assertEquals(1.0, fitness.getFitness(suite), 0.01F);
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Method m = sut.getMethod("testMe", new Class<?>[] { int.class });
    GenericMethod method = new GenericMethod(m, sut);
    TestCluster.getInstance().addTestCall(method);
    Properties.P_TEST_CHANGE = 0.0;
    Properties.P_TEST_DELETE = 1.0;
    Properties.P_TEST_INSERT = 0.0;
    double oldFitness = suite.getFitness();
    int notChanged = 0;
    for (int i = 0; i < 100; i++) {
        TestChromosome testNew = (TestChromosome) test1.clone();
        testNew.mutate();
        if (testNew.isChanged()) {
            suite.deleteTest(test1);
            suite.addTest(testNew);
            System.out.println(testNew.getTestCase().toCode());
            double newFitness = fitness.getFitness(suite);
            if (newFitness < oldFitness) {
                System.out.println("Improved: " + newFitness);
                test1 = testNew;
                oldFitness = newFitness;
                System.out.println("" + i + ":" + ((IntPrimitiveStatement) test1.getTestCase().getStatement(1)).getValue());
                if (newFitness == 0.0) {
                    System.out.println("Iterations: " + i);
                    System.out.println("Not changed: " + notChanged);
                    break;
                }
            } else {
                System.out.println("Not improved: " + newFitness);
                suite.deleteTest(testNew);
                suite.addTest(test1);
                fitness.getFitness(suite);
            }
        } else {
            notChanged++;
        }
    }
    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals(0.0, fitness.getFitness(suite), 0.1F);
}
Also used : TrivialInt(com.examples.with.different.packagename.TrivialInt) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Aggregations

BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)52 Test (org.junit.Test)45 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)18 TestCase (org.evosuite.testcase.TestCase)17 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)16 TestChromosome (org.evosuite.testcase.TestChromosome)14 Properties (org.evosuite.Properties)12 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)12 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)12 Method (java.lang.reflect.Method)11 GenericMethod (org.evosuite.utils.generic.GenericMethod)11 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)10 ArrayList (java.util.ArrayList)8 BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)7 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)7 VariableReference (org.evosuite.testcase.variable.VariableReference)7 GenericClass (org.evosuite.utils.generic.GenericClass)7 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)7 Foo (com.examples.with.different.packagename.symbolic.Foo)6