Search in sources :

Example 56 with DefaultTestCase

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

the class FunctionalMockStatementTest method testLimit.

@Test
public void testLimit() throws Exception {
    TestCase tc = new DefaultTestCase();
    final int LIMIT_5 = 5;
    Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT = LIMIT_5;
    final int LOOP_0 = 0, LOOP_3 = 3, LOOP_5 = 5, LOOP_7 = 7;
    IntPrimitiveStatement x = new IntPrimitiveStatement(tc, LOOP_3);
    VariableReference loop = tc.addStatement(x);
    VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, true));
    VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
    FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
    VariableReference mock = tc.addStatement(mockStmt);
    tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("limit", Foo.class, int.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock, loop)));
    // execute first time with default mock
    execute(tc);
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    List<Type> types = mockStmt.updateMockedMethods();
    Assert.assertEquals(LOOP_3, types.size());
    for (Type t : types) {
        Assert.assertEquals(boolean.class, t);
    }
    // add the 3 missing values
    mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef, boolRef));
    // before re-executing, change loops to the limit
    x.setValue(LOOP_5);
    execute(tc);
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    Assert.assertEquals(LOOP_5 - LOOP_3, types.size());
    for (Type t : types) {
        Assert.assertEquals(boolean.class, t);
    }
    // add the 2 missing values
    mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef));
    Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
    // before re-executing 3rd time, change loops above the limit
    x.setValue(LOOP_7);
    execute(tc);
    // no update should be required
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    Assert.assertEquals(0, types.size());
    Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
    // decrease, but to the limit, so still no required change
    x.setValue(LOOP_5);
    execute(tc);
    // no update should be required
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    Assert.assertEquals(0, types.size());
    Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
    // further decrease, but now we need to remove parameters
    x.setValue(LOOP_3);
    execute(tc);
    // do update
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    // but no new types to add
    Assert.assertEquals(0, types.size());
    Assert.assertEquals(LOOP_3, mockStmt.getNumParameters());
    // remove all
    x.setValue(LOOP_0);
    execute(tc);
    // do update
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    // but no new types to add
    Assert.assertEquals(0, types.size());
    Assert.assertEquals(LOOP_0, mockStmt.getNumParameters());
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Type(java.lang.reflect.Type) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) Test(org.junit.Test)

Example 57 with DefaultTestCase

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

the class FunctionalMockStatementTest method testPackageLevel_differentPackage_instrumentation_package.

@Test
public void testPackageLevel_differentPackage_instrumentation_package() throws Exception {
    TestCase tc = new DefaultTestCase();
    ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
    InstrumentingClassLoader loader = new InstrumentingClassLoader();
    Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePackageLevel");
    VariableReference ref = new VariableReferenceImpl(tc, example);
    try {
        FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
        fail();
    } catch (java.lang.IllegalArgumentException e) {
    // expected
    }
// tc.addStatement(mockStmt);
// execute(tc);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) NonInstrumentingClassLoader(org.evosuite.instrumentation.NonInstrumentingClassLoader) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 58 with DefaultTestCase

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

the class TestDSETestSuiteFlag 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 = Flag.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);
    branchCoverageSuiteFitness.getFitness(suite);
    DefaultTestCase testCase1 = buildTestCase1();
    TestChromosome testChromosome1 = new TestChromosome();
    testChromosome1.setTestCase(testCase1);
    suite.addTest(testChromosome1);
    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();
    /*
		 * Check at least 6 goals were covered
		 */
    assertTrue(coveredGoalsAfterLocalSearch >= 6);
    assertTrue(finalSuiteSize > 1);
}
Also used : BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) Flag(com.examples.with.different.packagename.ncs.Flag) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 59 with DefaultTestCase

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

the class TestZ3Str2HardConstraints method test0.

@Test
public void test0() throws SecurityException, NoSuchMethodException, SolverTimeoutException {
    Z3Str2Solver solver = new Z3Str2Solver();
    DefaultTestCase tc = buildTestCase0();
    Collection<Constraint<?>> constraints = DefaultTestCaseConcolicExecutor.execute(tc);
    assertTrue(!constraints.isEmpty());
    Map<String, Object> solution = solve(solver, constraints);
    assertNotNull(solution);
    Long int0 = (Long) solution.get("var0");
    assertEquals(13075, int0.intValue());
}
Also used : Constraint(org.evosuite.symbolic.expr.Constraint) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 60 with DefaultTestCase

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

the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.

@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
    Class<?> sut = ClassWithField.class;
    DefaultTestCase tc = new DefaultTestCase();
    // ClassWithField classWithField0 = new ClassWithField();
    GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
    ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
    VariableReference obj = tc.addStatement(constructorStatement);
    // classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
    FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
    Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
    GenericMethod gm = new GenericMethod(m, sut);
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    // classWithField0.BOOLEAN_FIELD = false;
    VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
    tc.addStatement(new AssignmentStatement(tc, field, boolRef));
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    Properties.TARGET_CLASS = sut.getCanonicalName();
    Properties.JUNIT_TESTS = true;
    TestSuiteChromosome testSuite = new TestSuiteChromosome();
    testSuite.addTest(tc);
    FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
    assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
    assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ClassWithField(com.examples.with.different.packagename.coverage.ClassWithField) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) GenericField(org.evosuite.utils.generic.GenericField) 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