Search in sources :

Example 1 with LineCoverageSuiteFitness

use of org.evosuite.coverage.line.LineCoverageSuiteFitness in project evosuite by EvoSuite.

the class ChangeMutationSystemTest method testTwoIntsLineCoverage.

@Test
public void testTwoIntsLineCoverage() throws NoSuchMethodException, SecurityException, ClassNotFoundException, ConstructionFailedException {
    Properties.TARGET_CLASS = IntExampleWithNoElse.class.getCanonicalName();
    TestChromosome test1 = new TestChromosome();
    test1.setTestCase(getTwoIntTest(1000, 100));
    TestChromosome test2 = new TestChromosome();
    test2.setTestCase(getTwoIntTest(0, 0));
    TestSuiteChromosome suite = new TestSuiteChromosome();
    LineCoverageSuiteFitness fitness = new LineCoverageSuiteFitness();
    suite.addTest(test1);
    suite.addTest(test2);
    Properties.P_TEST_CHANGE = 1.0;
    Properties.P_TEST_DELETE = 0.0;
    Properties.P_TEST_INSERT = 0.0;
    Properties.PRIMITIVE_POOL = 0.0;
    double oldFitness = fitness.getFitness(suite);
    int notChanged = 0;
    for (int i = 0; i < 10000; i++) {
        TestChromosome testNew = (TestChromosome) test1.clone();
        testNew.mutate();
        if (testNew.isChanged()) {
            suite.deleteTest(test1);
            suite.addTest(testNew);
            double newFitness = fitness.getFitness(suite);
            if (newFitness < oldFitness) {
                test1 = testNew;
                oldFitness = newFitness;
                System.out.println("" + i + ":" + ((IntPrimitiveStatement) test1.getTestCase().getStatement(1)).getValue());
                System.out.println("    " + ((IntPrimitiveStatement) test1.getTestCase().getStatement(2)).getValue());
                if (newFitness == 0.0) {
                    System.out.println("Iterations: " + i);
                    System.out.println("Not changed: " + notChanged);
                    break;
                }
            } else {
                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 : LineCoverageSuiteFitness(org.evosuite.coverage.line.LineCoverageSuiteFitness) IntExampleWithNoElse(com.examples.with.different.packagename.coverage.IntExampleWithNoElse) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 2 with LineCoverageSuiteFitness

use of org.evosuite.coverage.line.LineCoverageSuiteFitness in project evosuite by EvoSuite.

the class CompositionalFitnessSystemTest method testCompositionalGetFitnessForOneFunction.

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

Example 3 with LineCoverageSuiteFitness

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

use of org.evosuite.coverage.line.LineCoverageSuiteFitness in project evosuite by EvoSuite.

the class ChromosomeTest method testCompositionalGetFitnessForOneFunction.

@Test
public void testCompositionalGetFitnessForOneFunction() {
    Properties.ALGORITHM = Algorithm.MONOTONIC_GA;
    TestSuiteChromosome c = new TestSuiteChromosome();
    LineCoverageSuiteFitness f1 = new LineCoverageSuiteFitness();
    c.addFitness(f1);
    c.setFitness(f1, ANY_DOUBLE_1);
    assertEquals(ANY_DOUBLE_1, c.getFitness(), 0.001);
}
Also used : LineCoverageSuiteFitness(org.evosuite.coverage.line.LineCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 5 with LineCoverageSuiteFitness

use of org.evosuite.coverage.line.LineCoverageSuiteFitness in project evosuite by EvoSuite.

the class SPEA2Test method testComputeStrength.

@Test
public void testComputeStrength() {
    List<Chromosome> population = new ArrayList<Chromosome>();
    BranchCoverageSuiteFitness branch = new BranchCoverageSuiteFitness();
    LineCoverageSuiteFitness line = new LineCoverageSuiteFitness();
    // dominates all the other two chromosomes
    TestSuiteChromosome t1 = new TestSuiteChromosome();
    t1.setFitness(branch, 0.0);
    t1.setFitness(line, 0.0);
    population.add(t1);
    // t2 only dominates t3
    TestSuiteChromosome t2 = new TestSuiteChromosome();
    t2.setFitness(branch, 0.5);
    t2.setFitness(line, 0.5);
    population.add(t2);
    // t3 is dominated by all chromosomes
    TestSuiteChromosome t3 = new TestSuiteChromosome();
    t3.setFitness(branch, 1.0);
    t3.setFitness(line, 1.0);
    population.add(t3);
    SPEA2<Chromosome> algorithm = new SPEA2<Chromosome>(null);
    algorithm.computeStrength(population);
    assertEquals(0.36, t1.getDistance(), 0.01);
    assertEquals(2.36, t2.getDistance(), 0.01);
    assertEquals(3.36, t3.getDistance(), 0.01);
    // invert order of chromosomes
    population.clear();
    population.add(t3);
    population.add(t2);
    population.add(t1);
    algorithm.computeStrength(population);
    assertEquals(0.36, t1.getDistance(), 0.01);
    assertEquals(2.36, t2.getDistance(), 0.01);
    assertEquals(3.36, t3.getDistance(), 0.01);
}
Also used : LineCoverageSuiteFitness(org.evosuite.coverage.line.LineCoverageSuiteFitness) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) ArrayList(java.util.ArrayList) Chromosome(org.evosuite.ga.Chromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Aggregations

LineCoverageSuiteFitness (org.evosuite.coverage.line.LineCoverageSuiteFitness)12 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)12 Test (org.junit.Test)12 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)6 Chromosome (org.evosuite.ga.Chromosome)5 ArrayList (java.util.ArrayList)3 IntExampleWithNoElse (com.examples.with.different.packagename.coverage.IntExampleWithNoElse)1 OnlyBranchCoverageSuiteFitness (org.evosuite.coverage.branch.OnlyBranchCoverageSuiteFitness)1 ExceptionCoverageSuiteFitness (org.evosuite.coverage.exception.ExceptionCoverageSuiteFitness)1 OutputCoverageSuiteFitness (org.evosuite.coverage.io.output.OutputCoverageSuiteFitness)1 MethodNoExceptionCoverageSuiteFitness (org.evosuite.coverage.method.MethodNoExceptionCoverageSuiteFitness)1 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)1