use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class ReplaceFirst method executeFunction.
@Override
public Object executeFunction() {
// receiver
ReferenceConstant symb_receiver = this.getSymbReceiver();
String conc_receiver = (String) this.getConcReceiver();
// regex argument
ReferenceExpression symb_regex = this.getSymbArgument(0);
String conc_regex = (String) this.getConcArgument(0);
// replacement argument
ReferenceExpression symb_replacement = this.getSymbArgument(1);
String conc_replacement = (String) this.getConcArgument(1);
// return value
String conc_ret_val = (String) this.getConcRetVal();
ReferenceExpression symb_ret_val = this.getSymbRetVal();
StringValue stringReceiverExpr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_receiver, symb_receiver, conc_receiver);
if (symb_regex instanceof ReferenceConstant && symb_replacement instanceof ReferenceConstant) {
ReferenceConstant non_null_symb_regex = (ReferenceConstant) symb_regex;
StringValue regexExpr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_regex, non_null_symb_regex, conc_regex);
ReferenceConstant non_null_symb_replacement = (ReferenceConstant) symb_replacement;
StringValue replacementExpr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_replacement, non_null_symb_replacement, conc_replacement);
if (symb_ret_val instanceof ReferenceConstant) {
ReferenceConstant non_null_symb_ret_val = (ReferenceConstant) symb_ret_val;
StringMultipleExpression symb_value = new StringMultipleExpression(stringReceiverExpr, Operator.REPLACEFIRST, regexExpr, new ArrayList<Expression<?>>(Collections.singletonList(replacementExpr)), conc_ret_val);
env.heap.putField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_ret_val, non_null_symb_ret_val, symb_value);
}
}
return symb_ret_val;
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class ExprToZ3Str2Visitor method visit.
@Override
public SmtExpr visit(StringMultipleExpression e, Void arg) {
Expression<String> leftOperand = e.getLeftOperand();
Expression<?> rightOperand = e.getRightOperand();
Operator op = e.getOperator();
ArrayList<Expression<?>> othersOperands = e.getOther();
SmtExpr left = leftOperand.accept(this, null);
SmtExpr right = rightOperand.accept(this, null);
List<SmtExpr> others = new LinkedList<SmtExpr>();
for (Expression<?> otherOperand : othersOperands) {
SmtExpr other = otherOperand.accept(this, null);
others.add(other);
}
if (left == null || right == null) {
return null;
}
for (SmtExpr expr : others) {
if (expr == null) {
return null;
}
}
if (!left.isSymbolic() && !right.isSymbolic()) {
boolean isSymbolic = false;
for (SmtExpr smtExpr : others) {
if (smtExpr.isSymbolic()) {
isSymbolic = true;
}
}
if (!isSymbolic) {
String stringValue = e.getConcreteValue();
return mkStringConstant(stringValue);
}
}
switch(op) {
case REPLACECS:
{
SmtExpr string = left;
SmtExpr target = right;
SmtExpr replacement = others.get(0);
SmtExpr substringExpr = SmtExprBuilder.mkReplace(string, target, replacement);
return substringExpr;
}
case SUBSTRING:
{
SmtExpr string = left;
SmtExpr fromExpr = right;
SmtExpr toExpr = others.get(0);
SmtExpr substringExpr = SmtExprBuilder.mkSubstring(string, fromExpr, toExpr);
return substringExpr;
}
case REPLACEC:
case REPLACEALL:
case REPLACEFIRST:
{
String stringValue = e.getConcreteValue();
return mkStringConstant(stringValue);
}
default:
throw new UnsupportedOperationException("Not implemented yet! " + op);
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class ConstraintNormalizer method createStringConstraint.
private static Constraint<?> createStringConstraint(IntegerConstraint c) {
if (c.getLeftOperand() instanceof StringComparison) {
StringComparison string_comparison = (StringComparison) c.getLeftOperand();
@SuppressWarnings("unchecked") Expression<Long> number_expr = (Expression<Long>) c.getRightOperand();
IntegerConstant constant = new IntegerConstant(number_expr.getConcreteValue());
return new StringConstraint(string_comparison, c.getComparator(), constant);
} else {
assert c.getRightOperand() instanceof StringComparison;
StringComparison string_comparison = (StringComparison) c.getRightOperand();
@SuppressWarnings("unchecked") Expression<Long> number_expr = (Expression<Long>) c.getLeftOperand();
IntegerConstant constant = new IntegerConstant(number_expr.getConcreteValue());
return new StringConstraint(string_comparison, c.getComparator(), constant);
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class TestStringSearch method testStartsWithFalseConstant.
@Test
public void testStartsWithFalseConstant() {
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
String var1 = "footest";
String const2 = "test";
StringVariable strVar = new StringVariable("test1", var1);
StringConstant strConst = new StringConstant(const2);
IntegerConstant offs_expr = new IntegerConstant(3);
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.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()).startsWith(const2, 3));
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class SymbolicHeap method array_load.
public IntegerValue array_load(ReferenceExpression symb_array, int conc_index, long conc_value) {
Map<Integer, Expression<?>> symb_array_contents = getOrCreateSymbolicArray(symb_array);
IntegerValue symb_value = (IntegerValue) symb_array_contents.get(conc_index);
if (symb_value == null || ((Long) symb_value.getConcreteValue()).longValue() != conc_value) {
symb_value = ExpressionFactory.buildNewIntegerConstant(conc_value);
symb_array_contents.remove(conc_index);
}
return symb_value;
}
Aggregations