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());
}
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());
}
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);
}
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"));
}
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"));
}
Aggregations