Search in sources :

Example 21 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome 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 22 with TestSuiteChromosome

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

the class JUnitTestCarvedChromosomeFactorySystemTest method testDifficultClassWithWrongTestFails.

// EvoSuite may also cover it without seeding now.
@Ignore
@Test
public void testDifficultClassWithWrongTestFails() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DifficultClassWithoutCarving.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.TEST_FACTORY = TestFactory.JUNIT;
    Properties.SELECTED_JUNIT = DifficultClassTest.class.getCanonicalName();
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertTrue("Did not expect optimal coverage: ", best.getCoverage() < 1d);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with TestSuiteChromosome

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

the class DefUseAnalysisSystemTest method testSimpleExample.

@Test
public void testSimpleExample() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DefUseExample1.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    // Need to deactivate assertions, otherwise classloader is chanaged
    // and DefUseCoverageFactory is reset
    Properties.ASSERTIONS = false;
    // Properties.ANALYSIS_CRITERIA = "Branch,DefUse";
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertEquals(0, DefUseCoverageFactory.getInterMethodGoalsCount());
    Assert.assertEquals(0, DefUseCoverageFactory.getIntraClassGoalsCount());
    Assert.assertEquals(1, DefUseCoverageFactory.getParamGoalsCount());
    Assert.assertEquals(3, DefUseCoverageFactory.getIntraMethodGoalsCount());
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 24 with TestSuiteChromosome

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

the class DefUseAnalysisSystemTest method testGCDExample.

@Test
public void testGCDExample() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = GCD.class.getCanonicalName();
    Properties.ASSERTIONS = false;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    System.out.println("Def: " + DefUsePool.getDefCounter());
    // DefUseCoverageFactory.computeGoals();
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertEquals(0, DefUseCoverageFactory.getInterMethodGoalsCount());
    Assert.assertEquals(0, DefUseCoverageFactory.getIntraClassGoalsCount());
    // 3 or 4?
    Assert.assertEquals(4, DefUseCoverageFactory.getParamGoalsCount());
    Assert.assertEquals(6, DefUseCoverageFactory.getIntraMethodGoalsCount());
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 25 with TestSuiteChromosome

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

the class ImplicitExplicitExceptionsSystemTest method testExceptionFitness.

private void testExceptionFitness() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = ImplicitExplicitException.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.CRITERION = new Criterion[] { Properties.Criterion.EXCEPTION };
    Properties.OUTPUT_VARIABLES = "" + RuntimeVariable.Explicit_MethodExceptions + "," + RuntimeVariable.Explicit_TypeExceptions + "," + RuntimeVariable.Implicit_MethodExceptions + "," + RuntimeVariable.Implicit_TypeExceptions;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    double fitness = best.getFitness();
    /*
		 * there are 2 undeclared exceptions (both implicit and explicit),
		 * and 3 declared: so fit = 1 / (1+5)
		 */
    Assert.assertEquals("Wrong fitness: ", 1d / 6d, fitness, 0.0000001);
    Map<String, OutputVariable<?>> map = DebugStatisticsBackend.getLatestWritten();
    Assert.assertNotNull(map);
    Assert.assertEquals(1, map.get(RuntimeVariable.Explicit_MethodExceptions.toString()).getValue());
    Assert.assertEquals(1, map.get(RuntimeVariable.Explicit_TypeExceptions.toString()).getValue());
    Assert.assertEquals(1, map.get(RuntimeVariable.Implicit_MethodExceptions.toString()).getValue());
    Assert.assertEquals(1, map.get(RuntimeVariable.Implicit_TypeExceptions.toString()).getValue());
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) OutputVariable(org.evosuite.statistics.OutputVariable)

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