Search in sources :

Example 16 with TestFitnessFunction

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

the class IndividualTestStrategy method getAdditionallyCoveredGoals.

private Set<Integer> getAdditionallyCoveredGoals(List<? extends TestFitnessFunction> goals, Set<Integer> covered, TestChromosome best) {
    Set<Integer> r = new HashSet<Integer>();
    ExecutionResult result = best.getLastExecutionResult();
    assert (result != null);
    // if (result == null) {
    // result = TestCaseExecutor.getInstance().execute(best.test);
    // }
    int num = -1;
    for (TestFitnessFunction goal : goals) {
        num++;
        if (covered.contains(num))
            continue;
        if (goal.isCovered(best, result)) {
            r.add(num);
            if (Properties.PRINT_COVERED_GOALS)
                LoggingUtils.getEvoLogger().info("* Additionally covered: " + goal.toString());
        }
    }
    return r;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) HashSet(java.util.HashSet)

Example 17 with TestFitnessFunction

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

the class TestMethodNamingComplexExamples method testIDNamingWithSameMethodGoals2.

@Test
public void testIDNamingWithSameMethodGoals2() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // method goals
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LField;II)V");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "<init>(LFieldMatrix;)V");
    // exception goals
    TestFitnessFunction goal3 = new ExceptionCoverageTestFitness("FooClass", "<init>(LField;II)V", ArrayIndexOutOfBoundsException.class, ExceptionCoverageTestFitness.ExceptionType.IMPLICIT);
    // input goals
    TestFitnessFunction goal4 = new InputCoverageTestFitness(new InputCoverageGoal("FooClass", "<init>(LFieldMatrix;)V", 0, Type.getType("LFieldMatrix;"), REF_NULL));
    TestFitnessFunction goal5 = new InputCoverageTestFitness(new InputCoverageGoal("FooClass", "<init>(LFieldMatrix;)V", 0, Type.getType("LFieldMatrix;"), REF_NONNULL));
    TestFitnessFunction goal6 = new InputCoverageTestFitness(new InputCoverageGoal("FooClass", "<init>(LField;II)V", 0, Type.getType("LField;"), REF_NONNULL));
    TestFitnessFunction goal7 = new InputCoverageTestFitness(new InputCoverageGoal("FooClass", "<init>(LField;II)V", 1, Type.INT_TYPE, NUM_ZERO));
    TestFitnessFunction goal8 = new InputCoverageTestFitness(new InputCoverageGoal("FooClass", "<init>(LField;II)V", 1, Type.INT_TYPE, NUM_NEGATIVE));
    TestFitnessFunction goal9 = new InputCoverageTestFitness(new InputCoverageGoal("FooClass", "<init>(LField;II)V", 2, Type.INT_TYPE, NUM_ZERO));
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal3);
    test1.addCoveredGoal(goal6);
    // unique
    test1.addCoveredGoal(goal7);
    // unique
    test1.addCoveredGoal(goal9);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal2);
    test2.addCoveredGoal(goal3);
    // unique
    test2.addCoveredGoal(goal4);
    DefaultTestCase test3 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test3.addStatement(new IntPrimitiveStatement(test3, 3));
    test3.addCoveredGoal(goal1);
    test3.addCoveredGoal(goal2);
    test3.addCoveredGoal(goal3);
    // unique
    test3.addCoveredGoal(goal5);
    test3.addCoveredGoal(goal6);
    // unique
    test3.addCoveredGoal(goal8);
    ArrayList<TestCase> testCases = new ArrayList<>();
    testCases.add(test1);
    testCases.add(test2);
    testCases.add(test3);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
    String nameTest1 = naming.getName(test1);
    String nameTest2 = naming.getName(test2);
    String nameTest3 = naming.getName(test3);
    assertEquals("Generated test name differs from expected", "testCreatesFooClassTaking3ArgumentsWithZeroAndZero", nameTest1);
    assertEquals("Generated test name differs from expected", "testCreatesFooClassTakingFieldMatrixWithNull", nameTest2);
    // TODO: Why is Positive vs nonnull nondeterministically chosen?
    assertEquals("Generated test name differs from expected", "testCreatesFooClassTaking3ArgumentsWithNegative", nameTest3);
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) InputCoverageTestFitness(org.evosuite.coverage.io.input.InputCoverageTestFitness) InputCoverageGoal(org.evosuite.coverage.io.input.InputCoverageGoal) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 18 with TestFitnessFunction

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

the class TestMethodNamingComplexExamples method testExactSameTests.

@Test
public void testExactSameTests() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // method goals
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "values([B)[B");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "values()[B");
    // output goals
    TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([B)", Type.getType("[B"), ARRAY_EMPTY));
    TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values()", Type.getType("[B"), ARRAY_NONEMPTY));
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal2);
    test1.addCoveredGoal(goal4);
    test1.addCoveredGoal(goal5);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal2);
    test2.addCoveredGoal(goal4);
    test2.addCoveredGoal(goal5);
    ArrayList<TestCase> testCases = new ArrayList<>();
    testCases.add(test1);
    testCases.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
    String nameTest1 = naming.getName(test1);
    String nameTest2 = naming.getName(test2);
    assertEquals("Generated test name differs from expected", "testValuesTakingNoArgumentsReturningNonEmptyArray", nameTest1);
    assertEquals("Generated test name differs from expected", "testValuesTakingByteArrayReturningEmptyArray", nameTest2);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 19 with TestFitnessFunction

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

the class TestMethodNamingComplexExamples method testOverloadedMethodsNoArgs.

@Test
public void testOverloadedMethodsNoArgs() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // Method goals
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "values([B)[B");
    TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "values()[B");
    // Output goals for method values
    TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([B)", Type.getType("[B"), ARRAY_EMPTY));
    TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values()", Type.getType("[B"), ARRAY_NONEMPTY));
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal2);
    test1.addCoveredGoal(goal4);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal3);
    test2.addCoveredGoal(goal5);
    ArrayList<TestCase> testCases = new ArrayList<>();
    testCases.add(test1);
    testCases.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
    String nameTest1 = naming.getName(test1);
    String nameTest2 = naming.getName(test2);
    assertEquals("Generated test name differs from expected", "testValuesTakingByteArray", nameTest1);
    assertEquals("Generated test name differs from expected", "testValuesTakingNoArguments", nameTest2);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 20 with TestFitnessFunction

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

the class TestMethodNamingComplexExamples method testIDNamingWithSameMethodGoals.

@Test
public void testIDNamingWithSameMethodGoals() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // method goals
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>()V");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "getPublicID()Ljava/lang/String;");
    TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "setPublicID(Ljava/lang/String;)LFooClass;");
    // output goals
    TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "getPublicID()", Type.getType("Ljava.lang.String;"), REF_NONNULL));
    TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "setPublicID(Ljava/lang/String;)", Type.getType("LFooClass;"), REF_NONNULL));
    DefaultTestCase test1 = new DefaultTestCase();
    // any statement to fool hashcode function
    test1.addStatement(new IntPrimitiveStatement(test1, 1));
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal2);
    test1.addCoveredGoal(goal4);
    DefaultTestCase test2 = new DefaultTestCase();
    // any statement to fool hashcode function
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal3);
    test2.addCoveredGoal(goal5);
    ArrayList<TestCase> testCases = new ArrayList<>();
    testCases.add(test1);
    testCases.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
    String nameTest1 = naming.getName(test1);
    String nameTest2 = naming.getName(test2);
    // TODO: testGetPublicID ?
    assertEquals("Generated test name differs from expected", "testGetPublicIDTakingNoArguments", nameTest1);
    // TODO: testSetPublicID ?
    assertEquals("Generated test name differs from expected", "testSetPublicIDTakingString", nameTest2);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Aggregations

TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)73 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)26 TestCase (org.evosuite.testcase.TestCase)24 Test (org.junit.Test)22 TestChromosome (org.evosuite.testcase.TestChromosome)21 ArrayList (java.util.ArrayList)20 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)11 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)8 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)8 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)8 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)4 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)4 Properties (org.evosuite.Properties)3