Search in sources :

Example 76 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testArray.

@Test
public void testArray() throws Exception {
    TestCase tc = new DefaultTestCase();
    /*
            String s = "...";
            String[] array = new String[1];
            array[0] = s;
            Foo foo = mock(Foo.class);
            getFirstInArray(foo);
         */
    final String MOCKED_VALUE = "Hello 42!!!";
    VariableReference aString = tc.addStatement(new StringPrimitiveStatement(tc, MOCKED_VALUE));
    ArrayReference mockedArray = (ArrayReference) tc.addStatement(new ArrayStatement(tc, String[].class, 1));
    ArrayIndex arrayIndex = new ArrayIndex(tc, mockedArray, 0);
    AssignmentStatement stmt = new AssignmentStatement(tc, arrayIndex, aString);
    tc.addStatement(stmt);
    FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, Foo.class, Foo.class);
    VariableReference mock = tc.addStatement(mockStmt);
    VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("getFirstInArray", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
    // if not executed, should be no way to tell if needs new inputs
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    Assert.assertEquals(0, mockStmt.getNumParameters());
    // execute first time with default mock
    Scope scope = execute(tc);
    Object obj = scope.getObject(result);
    // default mock value should be null for objects/arrays
    Assert.assertNull(obj);
    // after execution, there should be one variable to provide
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    List<Type> types = mockStmt.updateMockedMethods();
    Assert.assertEquals(1, types.size());
    Assert.assertEquals(String[].class, types.get(0));
    // add int variable to list of mock expected returns
    mockStmt.addMissingInputs(Arrays.asList(mockedArray));
    Assert.assertEquals(1, mockStmt.getNumParameters());
    Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedArray));
    // re-execute with initialized mock
    scope = execute(tc);
    String val = (String) scope.getObject(result);
    Assert.assertEquals(MOCKED_VALUE, val);
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Type(java.lang.reflect.Type) Scope(org.evosuite.testcase.execution.Scope) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 77 with VariableReference

use of org.evosuite.testcase.variable.VariableReference 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 78 with VariableReference

use of org.evosuite.testcase.variable.VariableReference 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 79 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestTestSuiteMinimizer method minimizeSuiteHalfCoverageWithTwoFitnessFunctions.

@Test
public void minimizeSuiteHalfCoverageWithTwoFitnessFunctions() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    testFactory.addConstructor(test, gc, 0, 0);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    for (int i = 0; i < 10; i++) {
        IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
        VariableReference vr = test.addStatement(ips, i + 1);
    }
    ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
    GenericMethod method = new GenericMethod(m, sut);
    testFactory.addMethod(test, method, 11, 0);
    assertEquals(12, test.size());
    TestSuiteChromosome tsc = new TestSuiteChromosome();
    tsc.addTest(test);
    TestSuiteFitnessFunction branch = new BranchCoverageSuiteFitness();
    double previous_branch_fitness = branch.getFitness(tsc);
    tsc.setFitness(branch, previous_branch_fitness);
    assertEquals(previous_branch_fitness, 2.0, 0.0);
    TestSuiteFitnessFunction defuse = new DefUseCoverageSuiteFitness();
    double previous_defuse_fitness = defuse.getFitness(tsc);
    tsc.setFitness(defuse, previous_defuse_fitness);
    assertEquals(previous_defuse_fitness, 0.0, 0.0);
    List<TestFitnessFactory<? extends TestFitnessFunction>> factories = new ArrayList<TestFitnessFactory<? extends TestFitnessFunction>>();
    factories.add(new BranchCoverageFactory());
    factories.add(new DefUseCoverageFactory());
    TestSuiteMinimizer minimizer = new TestSuiteMinimizer(factories);
    minimizer.minimize(tsc, false);
    assertEquals(1, tsc.getTests().size());
    assertEquals(3, tsc.getTests().get(0).size());
    // assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nflagExample1_0.testMe(int0);\n"));
    double branch_fitness = branch.getFitness(tsc);
    assertEquals(previous_branch_fitness, branch_fitness, 0.0);
    double defuse_fitness = defuse.getFitness(tsc);
    assertEquals(previous_defuse_fitness, defuse_fitness, 0.0);
}
Also used : DefUseCoverageSuiteFitness(org.evosuite.coverage.dataflow.DefUseCoverageSuiteFitness) ArrayList(java.util.ArrayList) GenericMethod(org.evosuite.utils.generic.GenericMethod) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) DefUseCoverageFactory(org.evosuite.coverage.dataflow.DefUseCoverageFactory) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) FlagExample1(com.examples.with.different.packagename.FlagExample1) Test(org.junit.Test)

Example 80 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestZ3Str2HardConstraints method buildTestCase0.

private DefaultTestCase buildTestCase0() throws NoSuchMethodException, SecurityException {
    TestCaseBuilder tc = new TestCaseBuilder();
    VariableReference int0 = tc.appendIntPrimitive(13075);
    Method method = HardConstraints.class.getMethod("test0", int.class);
    tc.appendMethod(null, method, int0);
    return tc.getDefaultTestCase();
}
Also used : TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) Method(java.lang.reflect.Method)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14