use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.
the class DistanceCalculator method getDistanceIndexOfCIEqualsK.
private static long getDistanceIndexOfCIEqualsK(IntegerConstraint n, long leftVal, long rightVal) {
ExpressionExecutor exprExecutor = new ExpressionExecutor();
if (n.getLeftOperand() instanceof StringMultipleToIntegerExpression && n.getComparator() == Comparator.EQ && n.getRightOperand() instanceof IntegerConstant) {
IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
StringMultipleToIntegerExpression left_string_expr = (StringMultipleToIntegerExpression) n.getLeftOperand();
if (left_string_expr.getOperator() == Operator.INDEXOFCI) {
Expression<?> theSymbolicString = left_string_expr.getLeftOperand();
Expression<?> theSymbolicChar = left_string_expr.getRightOperand();
Expression<?> theSymbolicIndex = right_constant;
Expression<?> theOffset = left_string_expr.getOther().get(0);
Long theConcreteOffset = (Long) theOffset.accept(exprExecutor, null);
// check theString.lenght>0
String theConcreteString = (String) theSymbolicString.accept(exprExecutor, null);
Long theConcreteIndex = (Long) theSymbolicIndex.accept(exprExecutor, null);
if (theConcreteIndex > theConcreteString.length() - theConcreteOffset - 1) {
// there is no char at the index to modify
return Long.MAX_VALUE;
} else if (theConcreteIndex != -1) {
int theIndex = theConcreteIndex.intValue();
char theConcreteChar = (char) ((Long) theSymbolicChar.accept(exprExecutor, null)).longValue();
char theCurrentChar = theConcreteString.charAt(theIndex);
return Math.abs(theCurrentChar - theConcreteChar);
}
}
}
return -1;
}
use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.
the class DistanceCalculator method getDistanceStringIsInteger.
private static long getDistanceStringIsInteger(IntegerConstraint n, long leftVal, long rightVal) {
if (n.getLeftOperand() instanceof StringUnaryToIntegerExpression && n.getComparator() == Comparator.NE && n.getRightOperand() instanceof IntegerConstant) {
IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
StringUnaryToIntegerExpression left_string_expr = (StringUnaryToIntegerExpression) n.getLeftOperand();
if (right_constant.getConcreteValue().longValue() != 0L) {
return -1;
}
if (left_string_expr.getOperator() != Operator.IS_INTEGER) {
return -1;
}
String string = left_string_expr.getOperand().getConcreteValue();
if (string.length() > 0) {
char[] charArray = string.toCharArray();
int maxDistance = 0;
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
int distance;
if (!Character.isDigit(c)) {
if (c < '0') {
distance = '0' - c;
} else if (c > '9') {
distance = c - '9';
} else {
throw new RuntimeException("This branch is unreachable!");
}
if (maxDistance < distance) {
maxDistance = distance;
}
}
}
return maxDistance;
} else {
return Long.MAX_VALUE;
}
}
return -1;
}
use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.
the class DistanceCalculator method getDistanceIndexOfCIFound.
private static long getDistanceIndexOfCIFound(IntegerConstraint n, long leftVal, long rightVal) {
ExpressionExecutor exprExecutor = new ExpressionExecutor();
if (n.getLeftOperand() instanceof StringMultipleToIntegerExpression && n.getComparator() == Comparator.NE && n.getRightOperand() instanceof IntegerConstant) {
IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
StringMultipleToIntegerExpression left_string_expr = (StringMultipleToIntegerExpression) n.getLeftOperand();
if (left_string_expr.getOperator() == Operator.INDEXOFCI && right_constant.getConcreteValue() == -1L) {
Expression<?> theSymbolicString = left_string_expr.getLeftOperand();
Expression<?> theSymbolicChar = left_string_expr.getRightOperand();
Expression<?> theOffset = left_string_expr.getOther().get(0);
// check theString.lenght>0
String theConcreteString = (String) theSymbolicString.accept(exprExecutor, null);
Long theConcreteOffset = (Long) theOffset.accept(exprExecutor, null);
if (theConcreteOffset > theConcreteString.length() - 1) {
// satisfy the constraint
return Long.MAX_VALUE;
} else {
char theConcreteChar = (char) ((Long) theSymbolicChar.accept(exprExecutor, null)).longValue();
char[] charArray = theConcreteString.substring(theConcreteOffset.intValue(), theConcreteString.length()).toCharArray();
int min_distance_to_char = Integer.MAX_VALUE;
for (char c : charArray) {
if (Math.abs(c - theConcreteChar) < min_distance_to_char) {
min_distance_to_char = Math.abs(c - theConcreteChar);
}
}
return min_distance_to_char;
}
}
}
return -1;
}
use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.
the class TestIntegerSearch method testNEConstant.
@Test
public void testNEConstant() throws SolverEmptyQueryException {
// TODO: Currently, the model returned by the search is null if the
// constraint is already satisfied,
// so in this example the concrete value has to be the target initially
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new IntegerConstraint(new IntegerVariable("test1", 235082, -1000000, 1000000), Comparator.NE, new IntegerConstant(235082)));
try {
EvoSuiteSolver solver = new EvoSuiteSolver();
SolverResult solverResult = solver.solve(constraints);
assertTrue(solverResult.isSAT());
Map<String, Object> model = solverResult.getModel();
assertNotNull(model.get("test1"));
assertTrue(235082 != ((Number) model.get("test1")).intValue());
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.
the class TestStringEqualsIgnoreCase method testStringEqualsIgnoreCase.
@Test
public void testStringEqualsIgnoreCase() throws SecurityException, NoSuchMethodException, SolverTimeoutException {
IntegerConstant zero = new IntegerConstant(0);
StringVariable stringVar0 = new StringVariable("var0", "");
StringConstant strConst = new StringConstant("bar");
StringBinaryComparison cmp1 = new StringBinaryComparison(stringVar0, Operator.EQUALS, strConst, 0L);
StringConstraint constr1 = new StringConstraint(cmp1, Comparator.EQ, zero);
StringBinaryComparison cmp2 = new StringBinaryComparison(stringVar0, Operator.EQUALSIGNORECASE, strConst, 1L);
StringConstraint constr2 = new StringConstraint(cmp2, Comparator.NE, zero);
Collection<Constraint<?>> constraints = Arrays.<Constraint<?>>asList(constr1, constr2);
EvoSuiteSolver solver = new EvoSuiteSolver();
Map<String, Object> solution = solve(solver, constraints);
assertNotNull(solution);
String var0 = (String) solution.get("var0");
assertNotNull(var0);
assertTrue(!var0.equals("bar"));
assertTrue(var0.equalsIgnoreCase("bar"));
}
Aggregations