Search in sources :

Example 1 with BranchCoverageFactory

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

the class CBranchFitnessFactory method getCoverageGoals.

/* (non-Javadoc)
	 * @see org.evosuite.coverage.TestFitnessFactory#getCoverageGoals()
	 */
@Override
public List<CBranchTestFitness> getCoverageGoals() {
    // TODO this creates duplicate goals. Momentary fixed using a Set, but it should be optimised
    Set<CBranchTestFitness> goals = new HashSet<>();
    // retrieve set of branches
    BranchCoverageFactory branchFactory = new BranchCoverageFactory();
    List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoals();
    CallGraph callGraph = DependencyAnalysis.getCallGraph();
    // try to find all occurrences of this branch in the call tree
    for (BranchCoverageTestFitness branchGoal : branchGoals) {
        logger.info("Adding context branches for " + branchGoal.toString());
        for (CallContext context : callGraph.getMethodEntryPoint(branchGoal.getClassName(), branchGoal.getMethod())) {
            goals.add(new CBranchTestFitness(branchGoal.getBranchGoal(), context));
        }
    }
    logger.info("Created " + goals.size() + " goals");
    return new ArrayList<CBranchTestFitness>(goals);
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) CallGraph(org.evosuite.setup.callgraph.CallGraph) ArrayList(java.util.ArrayList) CallContext(org.evosuite.setup.CallContext) HashSet(java.util.HashSet)

Example 2 with BranchCoverageFactory

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

the class IBranchFitnessFactory method getCoverageGoals.

/* (non-Javadoc)
	 * @see org.evosuite.coverage.TestFitnessFactory#getCoverageGoals()
	 */
@Override
public List<IBranchTestFitness> getCoverageGoals() {
    // TODO this creates duplicate goals. Momentary fixed using a Set.
    Set<IBranchTestFitness> goals = new HashSet<IBranchTestFitness>();
    // retrieve set of branches
    BranchCoverageFactory branchFactory = new BranchCoverageFactory();
    List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoalsForAllKnownClasses();
    CallGraph callGraph = DependencyAnalysis.getCallGraph();
    // try to find all occurrences of this branch in the call tree
    for (BranchCoverageTestFitness branchGoal : branchGoals) {
        logger.info("Adding context branches for " + branchGoal.toString());
        for (CallContext context : callGraph.getAllContextsFromTargetClass(branchGoal.getClassName(), branchGoal.getMethod())) {
            // if is not possible to reach this branch from the target class, continue.
            if (context.isEmpty())
                continue;
            goals.add(new IBranchTestFitness(branchGoal.getBranchGoal(), context));
        }
    }
    assert (goals.size() >= branchFactory.getCoverageGoals().size());
    logger.info("Created " + goals.size() + " goals");
    return new ArrayList<IBranchTestFitness>(goals);
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) CallGraph(org.evosuite.setup.callgraph.CallGraph) ArrayList(java.util.ArrayList) CallContext(org.evosuite.setup.CallContext) HashSet(java.util.HashSet)

Example 3 with BranchCoverageFactory

use of org.evosuite.coverage.branch.BranchCoverageFactory 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 4 with BranchCoverageFactory

use of org.evosuite.coverage.branch.BranchCoverageFactory 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 5 with BranchCoverageFactory

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

the class TestTestSuiteMinimizer method minimizeSuiteFullCoverageWithTwoFitnessFunctionsMinimizeTestsEnabled.

@Test
public void minimizeSuiteFullCoverageWithTwoFitnessFunctionsMinimizeTestsEnabled() throws ClassNotFoundException, NoSuchFieldException, SecurityException, ConstructionFailedException, NoSuchMethodException {
    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);
    parameters = new ArrayList<VariableReference>();
    for (int i = 12; i < 15; i++) {
        IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i);
        VariableReference vr = test.addStatement(ips, i);
    }
    ct = new ConstructorStatement(test, gc, parameters);
    testFactory.addMethod(test, method, 15, 0);
    assertEquals(16, 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, 0.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, true);
    assertEquals(2, tsc.getTests().size());
    assertEquals(3, tsc.getTests().get(0).size());
    assertEquals(3, tsc.getTests().get(1).size());
    // assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nflagExample1_0.testMe(int0);\n"));
    // assertTrue(tsc.getTests().get(1).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28241;\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)

Aggregations

BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)10 ArrayList (java.util.ArrayList)7 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)7 Test (org.junit.Test)7 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)6 FlagExample1 (com.examples.with.different.packagename.FlagExample1)5 Method (java.lang.reflect.Method)5 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)5 VariableReference (org.evosuite.testcase.variable.VariableReference)5 GenericClass (org.evosuite.utils.generic.GenericClass)5 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)5 GenericMethod (org.evosuite.utils.generic.GenericMethod)5 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)3 BranchCoverageTestFitness (org.evosuite.coverage.branch.BranchCoverageTestFitness)3 DefUseCoverageFactory (org.evosuite.coverage.dataflow.DefUseCoverageFactory)3 DefUseCoverageSuiteFitness (org.evosuite.coverage.dataflow.DefUseCoverageSuiteFitness)3 HashSet (java.util.HashSet)2 CallContext (org.evosuite.setup.CallContext)2 CallGraph (org.evosuite.setup.callgraph.CallGraph)2 TestChromosome (org.evosuite.testcase.TestChromosome)1