Search in sources :

Example 16 with BranchCoverageSuiteFitness

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

the class MockJOptionPaneShowMessageDialogTest method testShowMessageDialog2.

@Test
public void testShowMessageDialog2() throws Exception {
    TestSuiteChromosome suite = new TestSuiteChromosome();
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t0 = buildTestCase2TrueBranch(cl);
    TestCase t1 = buildTestCase2FalseBranch(cl);
    suite.addTest(t0);
    suite.addTest(t1);
    BranchCoverageSuiteFitness ff = new BranchCoverageSuiteFitness(cl);
    ff.getFitness(suite);
    Set<TestFitnessFunction> coveredGoals = suite.getCoveredGoals();
    Assert.assertEquals(3, coveredGoals.size());
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 17 with BranchCoverageSuiteFitness

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

the class MockJOptionPaneTest method testInlinerBug.

@Test
public void testInlinerBug() throws Exception {
    Properties.TIMEOUT = Integer.MAX_VALUE;
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t0 = buildTestCase0(cl);
    TestCase t1 = buildTestCase1(cl);
    TestSuiteChromosome suite = new TestSuiteChromosome();
    suite.addTest(t0);
    suite.addTest(t1);
    System.out.println(suite.toString());
    BranchCoverageSuiteFitness ff = new BranchCoverageSuiteFitness(cl);
    ff.getFitness(suite);
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(suite);
    System.out.println(suite.toString());
    List<ExecutionResult> execResults = suite.getLastExecutionResults();
    assertEquals(2, execResults.size());
    ExecutionResult r1 = execResults.get(0);
    ExecutionResult r2 = execResults.get(1);
    r1.calledReflection();
    r2.calledReflection();
}
Also used : ConstantInliner(org.evosuite.testcase.ConstantInliner) TestCase(org.evosuite.testcase.TestCase) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 18 with BranchCoverageSuiteFitness

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

the class CompositionalFitnessSystemTest method testCompositionalGetFitnessForTwoFunctions.

@Test
public void testCompositionalGetFitnessForTwoFunctions() {
    TestSuiteChromosome c = new TestSuiteChromosome();
    LineCoverageSuiteFitness f1 = new LineCoverageSuiteFitness();
    c.addFitness(f1);
    c.setFitness(f1, ANY_DOUBLE_1);
    BranchCoverageSuiteFitness f2 = new BranchCoverageSuiteFitness();
    c.addFitness(f2);
    c.setFitness(f2, ANY_DOUBLE_2);
    assertEquals(ANY_DOUBLE_1 + ANY_DOUBLE_2, c.getFitness(), 0.001);
}
Also used : LineCoverageSuiteFitness(org.evosuite.coverage.line.LineCoverageSuiteFitness) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 19 with BranchCoverageSuiteFitness

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

the class Issre13SystemTest method testOnSpecificTest.

@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
    // force using DSE
    Properties.DSE_PROBABILITY = 1.0;
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    // String string0 = "baz5";
    VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
    // DseFoo dseFoo0 = new DseFoo();
    GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
    ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
    VariableReference fooVar = test.addStatement(fooConstructorStatement);
    Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
    GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    // DseBar dseBar0 = new DseBar(string0);
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
    VariableReference callee = test.addStatement(constructorStatement);
    // dseBar0.coverMe(dseFoo0);
    Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
    test.addStatement(ms);
    System.out.println(test);
    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);
    System.out.println("Test suite: " + suite);
    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 : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) DseBar(com.examples.with.different.packagename.localsearch.DseBar) TestSuiteLocalSearch(org.evosuite.testsuite.localsearch.TestSuiteLocalSearch) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) GenericClass(org.evosuite.utils.generic.GenericClass) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DseFoo(com.examples.with.different.packagename.localsearch.DseFoo) Test(org.junit.Test)

Example 20 with BranchCoverageSuiteFitness

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

the class LocalSearchArraySystemTest method runArrayExample.

private void runArrayExample(int length, double expectedFitness) throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    TestCase test = getArrayTest(length);
    System.out.println("Test: " + test.toCode());
    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(expectedFitness, 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)

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