use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.
the class SymbolicObserver method readArray.
private ReferenceExpressionPair readArray(ArrayIndex rhs, Scope scope) {
ArrayReference arrayReference = rhs.getArray();
ReferenceConstant symb_array = (ReferenceConstant) symb_references.get(arrayReference.getName());
int conc_index = rhs.getArrayIndex();
Class<?> componentClass = arrayReference.getComponentClass();
try {
Object conc_array = arrayReference.getObject(scope);
if (componentClass.equals(int.class)) {
int conc_value = Array.getInt(conc_array, conc_index);
IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
ReferenceConstant newIntegerRef = newIntegerReference(conc_value, expr);
return new ReferenceExpressionPair(newIntegerRef, expr);
} else if (componentClass.equals(char.class)) {
char conc_value = Array.getChar(conc_array, conc_index);
IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
ReferenceConstant newCharacterRef = newCharacterReference(conc_value, expr);
return new ReferenceExpressionPair(newCharacterRef, expr);
} else if (componentClass.equals(boolean.class)) {
boolean conc_value = Array.getBoolean(conc_array, conc_index);
IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value ? 1 : 0);
ReferenceConstant newBooleanRef = newBooleanReference(conc_value, expr);
return new ReferenceExpressionPair(newBooleanRef, expr);
} else if (componentClass.equals(byte.class)) {
byte conc_value = Array.getByte(conc_array, conc_index);
IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
ReferenceConstant newByteRef = newByteReference(conc_value, expr);
return new ReferenceExpressionPair(newByteRef, expr);
} else if (componentClass.equals(short.class)) {
short conc_value = Array.getShort(conc_array, conc_index);
IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
ReferenceConstant newShortRef = newShortReference(conc_value, expr);
return new ReferenceExpressionPair(newShortRef, expr);
} else if (componentClass.equals(long.class)) {
long conc_value = Array.getLong(conc_array, conc_index);
IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
ReferenceConstant newLongRef = newLongReference(conc_value, expr);
return new ReferenceExpressionPair(newLongRef, expr);
} else if (componentClass.equals(float.class)) {
float conc_value = Array.getFloat(conc_array, conc_index);
RealValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
ReferenceConstant newFloatRef = newFloatReference(conc_value, expr);
return new ReferenceExpressionPair(newFloatRef, expr);
} else if (componentClass.equals(double.class)) {
double conc_value = Array.getDouble(conc_array, conc_index);
RealValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
ReferenceConstant newDoubleRef = newDoubleReference(conc_value, expr);
return new ReferenceExpressionPair(newDoubleRef, expr);
} else {
Object conc_value = Array.get(conc_array, conc_index);
if (conc_value instanceof String) {
StringValue expr = env.heap.array_load(symb_array, conc_index, (String) conc_value);
ReferenceConstant newStringRef = newStringReference((String) conc_value, expr);
return new ReferenceExpressionPair(newStringRef, expr);
} else {
ReferenceExpression ref = env.heap.getReference(conc_value);
if (conc_value != null && isWrapper(conc_value)) {
ReferenceConstant nonNullRef = (ReferenceConstant) ref;
Expression<?> expr = findOrCreate(conc_value, nonNullRef);
return new ReferenceExpressionPair(ref, expr);
} else {
return new ReferenceExpressionPair(ref, null);
}
}
}
} catch (CodeUnderTestException e) {
throw new RuntimeException(e);
}
}
use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.
the class SymbolicObserver method writeArray.
private void writeArray(ArrayIndex lhs, ReferenceExpressionPair readResult, Scope scope) {
ArrayReference arrayReference = lhs.getArray();
int conc_index = lhs.getArrayIndex();
Object conc_array;
try {
conc_array = arrayReference.getObject(scope);
} catch (CodeUnderTestException e) {
throw new EvosuiteError(e);
}
Type arrayType = Type.getType(conc_array.getClass());
Type elementType = arrayType.getElementType();
if (isValue(elementType) || elementType.equals(Type.getType(String.class))) {
Expression<?> symb_value = readResult.getExpression();
symb_value = castIfNeeded(elementType, symb_value);
String array_name = arrayReference.getName();
ReferenceExpression symb_ref = symb_references.get(array_name);
ReferenceConstant symb_array = (ReferenceConstant) symb_ref;
env.heap.array_store(conc_array, symb_array, conc_index, symb_value);
} else {
/* ignore storing references (we use objects to find them) */
}
}
use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.
the class TestCaseExpander method visitArrayStatement.
public void visitArrayStatement(TestCase test, ArrayStatement statement) {
ArrayReference arrRef = (ArrayReference) statement.getReturnValue();
Set<Integer> assignments = new HashSet<Integer>();
int position = statement.getPosition() + 1;
while (position < test.size()) {
Statement st = test.getStatement(position);
if (st instanceof AssignmentStatement) {
if (st.getReturnValue() instanceof ArrayIndex) {
ArrayIndex arrayIndex = (ArrayIndex) st.getReturnValue();
if (arrayIndex.getArray().equals(arrRef)) {
assignments.add(arrayIndex.getArrayIndex());
}
}
} else if (st instanceof PrimitiveStatement) {
// OK, ignore
} else {
break;
}
position++;
}
position = statement.getPosition() + 1;
for (int i = 0; i < statement.size(); i++) {
if (assignments.contains(i))
continue;
ArrayIndex index = new ArrayIndex(test, arrRef, i);
VariableReference retVal = null;
if (index.isPrimitive()) {
PrimitiveStatement<?> primitive = PrimitiveStatement.getPrimitiveStatement(test, index.getGenericClass());
retVal = test.addStatement(primitive, position++);
} else {
NullStatement nullStatement = new NullStatement(test, index.getType());
retVal = test.addStatement(nullStatement, position++);
}
AssignmentStatement assignment = new AssignmentStatement(test, index, retVal);
test.addStatement(assignment, position++);
}
}
use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.
the class TestDefaultValue method testLong.
@Test
public void testLong() throws SecurityException, NoSuchMethodException {
TestCaseBuilder builder = new TestCaseBuilder();
ArrayReference longArray0 = builder.appendArrayStmt(Long[].class, 10);
VariableReference long0 = builder.appendNull(Long.class);
builder.appendAssignment(longArray0, 0, long0);
builder.appendAssignment(long0, longArray0, 0);
builder.appendMethod(long0, Long.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 testFloat.
@Test
public void testFloat() throws SecurityException, NoSuchMethodException {
TestCaseBuilder builder = new TestCaseBuilder();
ArrayReference floatArray0 = builder.appendArrayStmt(Float[].class, 10);
VariableReference float0 = builder.appendNull(Float.class);
builder.appendAssignment(floatArray0, 0, float0);
builder.appendAssignment(float0, floatArray0, 0);
builder.appendMethod(float0, Float.class.getMethod("toString"));
DefaultTestCase tc = builder.getDefaultTestCase();
System.out.println(tc.toCode());
ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
assertNotNull(ret_val);
assertFalse(ret_val.explicitExceptions.isEmpty());
}
Aggregations