Search in sources :

Example 46 with DefaultTestCase

use of org.evosuite.testcase.DefaultTestCase in project evosuite by EvoSuite.

the class TestDSETestCaseLocalSearch 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 = Foo.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);
    double fitnessBeforeLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsBeforeLocalSearch = suite.getNumOfCoveredGoals();
    // some goal was covered
    assertTrue(coveredGoalsBeforeLocalSearch > 0);
    DefaultTestCase duplicatedTestCase0 = buildTestCase0();
    TestChromosome duplicatedTestChromosome0 = new TestChromosome();
    duplicatedTestChromosome0.setTestCase(duplicatedTestCase0);
    suite.addTest(duplicatedTestChromosome0);
    TestSuiteLocalSearchObjective localSearchObjective = TestSuiteLocalSearchObjective.buildNewTestSuiteLocalSearchObjective(Collections.singletonList(branchCoverageSuiteFitness), suite, 1);
    DSETestCaseLocalSearch localSearch = new DSETestCaseLocalSearch();
    boolean improved = localSearch.doSearch(duplicatedTestChromosome0, localSearchObjective);
    assertTrue(improved);
    double fitnessAfterLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsAfterLocalSearch = suite.getNumOfCoveredGoals();
    assertTrue(fitnessAfterLocalSearch < fitnessBeforeLocalSearch);
    assertTrue(coveredGoalsAfterLocalSearch > coveredGoalsBeforeLocalSearch);
}
Also used : TestSuiteLocalSearchObjective(org.evosuite.testsuite.localsearch.TestSuiteLocalSearchObjective) Foo(com.examples.with.different.packagename.symbolic.Foo) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 47 with DefaultTestCase

use of org.evosuite.testcase.DefaultTestCase in project evosuite by EvoSuite.

the class TestSerialization method testSerializationNonEmptySuite.

@Test
public void testSerializationNonEmptySuite() throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    double fitness = 0.9950513142057124d;
    TestSuiteChromosome chromosome = new TestSuiteChromosome();
    TestChromosome testChromosome = new TestChromosome();
    TestCase test = new DefaultTestCase();
    PrimitiveStatement<?> statement = PrimitiveStatement.getPrimitiveStatement(test, int.class);
    test.addStatement(statement);
    testChromosome.setTestCase(test);
    testChromosome.setFitness(null, 3.14d);
    chromosome.setFitness(null, fitness);
    chromosome.setCoverage(null, 0.5);
    chromosome.updateAge(24);
    chromosome.setChanged(true);
    chromosome.addTest(testChromosome);
    oos.writeObject(chromosome);
    byte[] baSerialized = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(baSerialized);
    ObjectInputStream ois = new ObjectInputStream(bais);
    TestSuiteChromosome copy = (TestSuiteChromosome) ois.readObject();
    Assert.assertEquals(chromosome.getFitness(), copy.getFitness(), 0.0);
    Assert.assertEquals(chromosome.getAge(), copy.getAge());
    Assert.assertEquals(chromosome.getCoverage(), copy.getCoverage(), 0.0);
    Assert.assertEquals(chromosome.getCoveredGoals(), copy.getCoveredGoals());
    Assert.assertEquals(chromosome.isChanged(), copy.isChanged());
    Assert.assertEquals(chromosome.getTestChromosome(0).getFitness(), copy.getTestChromosome(0).getFitness(), 0.0);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) ObjectOutputStream(java.io.ObjectOutputStream) TestChromosome(org.evosuite.testcase.TestChromosome) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 48 with DefaultTestCase

use of org.evosuite.testcase.DefaultTestCase in project evosuite by EvoSuite.

the class TestDSETestSuiteCookie 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 = Foo.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);
    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();
    assertTrue(coveredGoalsAfterLocalSearch == 8);
    assertTrue(finalSuiteSize >= 5);
}
Also used : Foo(com.examples.with.different.packagename.symbolic.Foo) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 49 with DefaultTestCase

use of org.evosuite.testcase.DefaultTestCase in project evosuite by EvoSuite.

the class TestDSETestSuiteFoo method testAVMSolver.

@Test
public void testAVMSolver() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    Properties.DSE_SOLVER = Properties.SolverType.EVOSUITE_SOLVER;
    Properties.CRITERION = new Properties.Criterion[] { Criterion.BRANCH };
    Properties.TARGET_CLASS = Foo.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);
    double fitnessBeforeLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsBeforeLocalSearch = suite.getNumOfCoveredGoals();
    // some goal was covered
    assertTrue(coveredGoalsBeforeLocalSearch > 0);
    LocalSearchObjective<TestSuiteChromosome> localSearchObjective = new DefaultLocalSearchObjective<>();
    localSearchObjective.addFitnessFunction(branchCoverageSuiteFitness);
    TestSuiteLocalSearch localSearch = new TestSuiteLocalSearch();
    boolean improved;
    do {
        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();
    assertTrue(coveredGoalsAfterLocalSearch >= 7);
    assertTrue(finalSuiteSize >= 4);
}
Also used : DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) Foo(com.examples.with.different.packagename.symbolic.Foo) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 50 with DefaultTestCase

use of org.evosuite.testcase.DefaultTestCase in project evosuite by EvoSuite.

the class TestDSETestSuiteTriangle 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 = Triangle.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);
    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();
    assertEquals(18, coveredGoalsAfterLocalSearch);
    assertTrue(finalSuiteSize > 1);
}
Also used : BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) Triangle(com.examples.with.different.packagename.ncs.Triangle) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Aggregations

DefaultTestCase (org.evosuite.testcase.DefaultTestCase)347 Test (org.junit.Test)234 Constraint (org.evosuite.symbolic.expr.Constraint)101 TestCase (org.evosuite.testcase.TestCase)78 ArrayList (java.util.ArrayList)63 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)47 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)45 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)41 VariableReference (org.evosuite.testcase.variable.VariableReference)37 TestCaseStringAppendString (com.examples.with.different.packagename.solver.TestCaseStringAppendString)27 TestCaseStringIndexOfString (com.examples.with.different.packagename.solver.TestCaseStringIndexOfString)27 TestCaseStringLastIndexOfString (com.examples.with.different.packagename.solver.TestCaseStringLastIndexOfString)27 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)27 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)25 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)25 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)18 GenericMethod (org.evosuite.utils.generic.GenericMethod)17 TestChromosome (org.evosuite.testcase.TestChromosome)16 Method (java.lang.reflect.Method)14 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)14