use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class SymbolicObserver method readInstanceField.
private ReferenceExpressionPair readInstanceField(VariableReference source, Field field, Scope scope) {
String owner = field.getDeclaringClass().getName().replace('.', '/');
String name = field.getName();
Class<?> fieldClazz = field.getType();
String source_name = source.getName();
ReferenceConstant symb_receiver = (ReferenceConstant) symb_references.get(source_name);
try {
Object conc_receiver = source.getObject(scope);
if (fieldClazz.equals(int.class)) {
int conc_value = field.getInt(conc_receiver);
IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
ReferenceConstant newIntegerRef = newIntegerReference(conc_value, expr);
return new ReferenceExpressionPair(newIntegerRef, expr);
} else if (fieldClazz.equals(char.class)) {
char conc_value = field.getChar(conc_receiver);
IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
ReferenceConstant newCharacterRef = newCharacterReference(conc_value, expr);
return new ReferenceExpressionPair(newCharacterRef, expr);
} else if (fieldClazz.equals(long.class)) {
long conc_value = field.getLong(conc_receiver);
IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
ReferenceConstant newLongRef = newLongReference(conc_value, expr);
return new ReferenceExpressionPair(newLongRef, expr);
} else if (fieldClazz.equals(short.class)) {
short conc_value = field.getShort(conc_receiver);
IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
ReferenceConstant newShortRef = newShortReference(conc_value, expr);
return new ReferenceExpressionPair(newShortRef, expr);
} else if (fieldClazz.equals(byte.class)) {
byte conc_value = field.getByte(conc_receiver);
IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
ReferenceConstant newByteRef = newByteReference(conc_value, expr);
return new ReferenceExpressionPair(newByteRef, expr);
} else if (fieldClazz.equals(boolean.class)) {
boolean conc_value = field.getBoolean(conc_receiver);
IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value ? 1 : 0);
ReferenceConstant newBooleanRef = newBooleanReference(conc_value, expr);
return new ReferenceExpressionPair(newBooleanRef, expr);
} else if (fieldClazz.equals(float.class)) {
float conc_value = field.getFloat(conc_receiver);
RealValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
ReferenceConstant newFloatRef = newFloatReference(conc_value, expr);
return new ReferenceExpressionPair(newFloatRef, expr);
} else if (fieldClazz.equals(double.class)) {
double conc_value = field.getDouble(conc_receiver);
RealValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
ReferenceConstant newDoubleRef = newDoubleReference(conc_value, expr);
return new ReferenceExpressionPair(newDoubleRef, expr);
} else {
Object conc_value = field.get(conc_receiver);
if (conc_value instanceof String) {
String string = (String) conc_value;
StringValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, string);
ReferenceConstant newStringRef = newStringReference(string, 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 (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (CodeUnderTestException e) {
throw new RuntimeException(e);
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class TestStringSearch method testRegionMatchesICTrueConstant.
@Test
public void testRegionMatchesICTrueConstant() {
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
String var1 = "teXto";
String const2 = "rtestooo";
boolean ignore_case = true;
int offset1 = 0;
int offset2 = 1;
int len = 4;
StringVariable strVar = new StringVariable("test1", var1);
StringConstant strConst = new StringConstant(const2);
IntegerConstant len_expr = new IntegerConstant(len);
IntegerConstant offs_one = new IntegerConstant(offset1);
IntegerConstant offs_two = new IntegerConstant(offset2);
IntegerConstant ign_case = new IntegerConstant(ignore_case ? 1 : 0);
ArrayList<Expression<?>> other = new ArrayList<Expression<?>>();
other.add(offs_one);
other.add(offs_two);
other.add(len_expr);
other.add(ign_case);
StringMultipleComparison strComp = new StringMultipleComparison(strVar, Operator.REGIONMATCHES, strConst, other, 0L);
constraints.add(new StringConstraint(strComp, Comparator.NE, new IntegerConstant(0)));
EvoSuiteSolver skr = new EvoSuiteSolver();
Map<String, Object> result;
try {
result = solve(skr, constraints);
assertNotNull(result);
assertNotNull(result.get("test1"));
assertTrue((result.get("test1").toString()).regionMatches(ignore_case, offset1, const2, offset2, len));
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class TestStringSearch method testRegionMatchesICFalseConstant.
@Test
public void testRegionMatchesICFalseConstant() {
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
String var1 = "foTESTo";
String const2 = "rtestooo";
boolean ignore_case = true;
int offset1 = 2;
int offset2 = 1;
int len = 4;
StringVariable strVar = new StringVariable("test1", var1);
StringConstant strConst = new StringConstant(const2);
IntegerConstant len_expr = new IntegerConstant(len);
IntegerConstant offs_two = new IntegerConstant(offset2);
IntegerConstant offs_one = new IntegerConstant(offset1);
IntegerConstant ign_case = new IntegerConstant(ignore_case ? 1 : 0);
ArrayList<Expression<?>> other = new ArrayList<Expression<?>>();
other.add(offs_one);
other.add(offs_two);
other.add(len_expr);
other.add(ign_case);
StringMultipleComparison strComp = new StringMultipleComparison(strVar, Operator.REGIONMATCHES, strConst, other, 0L);
constraints.add(new StringConstraint(strComp, Comparator.EQ, new IntegerConstant(0)));
EvoSuiteSolver skr = new EvoSuiteSolver();
Map<String, Object> result;
try {
result = solve(skr, constraints);
assertNotNull(result);
assertNotNull(result.get("test1"));
assertFalse((result.get("test1").toString()).regionMatches(ignore_case, offset1, const2, offset2, len));
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class TestStringSearch method testStartsWithTrueConstant.
@Test
public void testStartsWithTrueConstant() {
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
String var1 = "foo";
String const2 = "test";
StringVariable strVar = new StringVariable("test1", var1);
StringConstant strConst = new StringConstant(const2);
IntegerConstant offs_expr = new IntegerConstant(2);
ArrayList<Expression<?>> other = new ArrayList<Expression<?>>();
other.add(offs_expr);
StringMultipleComparison strComp = new StringMultipleComparison(strVar, Operator.STARTSWITH, strConst, other, 0L);
constraints.add(new StringConstraint(strComp, Comparator.NE, new IntegerConstant(0)));
EvoSuiteSolver skr = new EvoSuiteSolver();
Map<String, Object> result;
try {
result = solve(skr, constraints);
assertNotNull(result);
assertNotNull(result.get("test1"));
assertTrue((result.get("test1").toString()).startsWith(const2, 2));
} catch (SolverTimeoutException e) {
fail();
}
}
Aggregations