Search in sources :

Example 6 with ArrayReference

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

the class TestDefaultValue method testCharacter.

@Test
public void testCharacter() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference characterArray0 = builder.appendArrayStmt(Character[].class, 10);
    VariableReference character0 = builder.appendNull(Character.class);
    builder.appendAssignment(characterArray0, 0, character0);
    builder.appendAssignment(character0, characterArray0, 0);
    builder.appendMethod(character0, Character.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 7 with ArrayReference

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

the class TestDefaultValue method testDouble.

@Test
public void testDouble() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference doubleArray0 = builder.appendArrayStmt(Double[].class, 10);
    VariableReference double0 = builder.appendNull(Double.class);
    builder.appendAssignment(doubleArray0, 0, double0);
    builder.appendAssignment(double0, doubleArray0, 0);
    builder.appendMethod(double0, Double.class.getMethod("floatValue"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 8 with ArrayReference

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

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

the class TestConstantInliner method testArrayIndexInlining.

@Test
public void testArrayIndexInlining() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(Object.class.getConstructor(), Object.class), new ArrayList<VariableReference>());
    VariableReference objectVar = test.addStatement(cs);
    ArrayStatement as = new ArrayStatement(test, Object[].class, 3);
    test.addStatement(as);
    ArrayReference arrayVar = as.getArrayReference();
    ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
    ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
    ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
    test.addStatement(new AssignmentStatement(test, ai0, objectVar));
    test.addStatement(new AssignmentStatement(test, ai1, objectVar));
    test.addStatement(new AssignmentStatement(test, ai2, objectVar));
    ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(ObjectParameter.class.getConstructor(), ObjectParameter.class), new ArrayList<VariableReference>());
    VariableReference sut = test.addStatement(sutCS);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(ai0);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    parameters = new ArrayList<VariableReference>();
    parameters.add(ai1);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    parameters = new ArrayList<VariableReference>();
    parameters.add(ai2);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    assertFalse(code.contains("objectParameter0.testMe(objectArray0"));
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) ObjectParameter(com.examples.with.different.packagename.ObjectParameter) Test(org.junit.Test)

Example 10 with ArrayReference

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

the class TestConstantInliner method testNumericArrayIndexInlining.

@Test
public void testNumericArrayIndexInlining() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    PrimitiveStatement<?> primitiveStatement = PrimitiveStatement.getPrimitiveStatement(test, int.class);
    VariableReference intVar = test.addStatement(primitiveStatement);
    ArrayStatement as = new ArrayStatement(test, int[].class, 3);
    test.addStatement(as);
    ArrayReference arrayVar = as.getArrayReference();
    ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
    ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
    ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
    test.addStatement(new AssignmentStatement(test, ai0, intVar));
    test.addStatement(new AssignmentStatement(test, ai1, intVar));
    test.addStatement(new AssignmentStatement(test, ai2, intVar));
    ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(TrivialInt.class.getConstructor(), TrivialInt.class), new ArrayList<>());
    VariableReference sut = test.addStatement(sutCS);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(ai0);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    parameters = new ArrayList<>();
    parameters.add(ai1);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    parameters = new ArrayList<>();
    parameters.add(ai2);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    System.out.println(test.toCode());
    assertFalse(code.contains("trivialInt0.testMe(int"));
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) TrivialInt(com.examples.with.different.packagename.TrivialInt) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Test(org.junit.Test)

Aggregations

ArrayReference (org.evosuite.testcase.variable.ArrayReference)20 VariableReference (org.evosuite.testcase.variable.VariableReference)15 Test (org.junit.Test)10 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)8 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)7 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)7 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)6 ArrayList (java.util.ArrayList)3 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)3 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)3 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)3 GenericMethod (org.evosuite.utils.generic.GenericMethod)3 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)2 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)2 NullStatement (org.evosuite.testcase.statements.NullStatement)2 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)2 ObjectParameter (com.examples.with.different.packagename.ObjectParameter)1 TrivialInt (com.examples.with.different.packagename.TrivialInt)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1