Search in sources :

Example 21 with IntegerConstraint

use of org.evosuite.symbolic.expr.IntegerConstraint in project evosuite by EvoSuite.

the class JumpVM method IF_ICMPEQ.

/**
 * (left == right). (left != right) is just (not (left == right)).
 *
 * http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.
 * doc6.html#if_icmpcond
 */
@Override
public void IF_ICMPEQ(String className, String methName, int branchIndex, int left, int right) {
    IntegerValue rightOp = env.topFrame().operandStack.popBv32();
    IntegerValue leftOp = env.topFrame().operandStack.popBv32();
    IntegerConstraint cnstr;
    if (left == right)
        // "True" branch
        cnstr = ConstraintFactory.eq(leftOp, rightOp);
    else
        // "False" branch
        cnstr = ConstraintFactory.neq(leftOp, rightOp);
    // add branch condition iif local constraint is concrete
    if (cnstr.getLeftOperand().containsSymbolicVariable() || cnstr.getRightOperand().containsSymbolicVariable())
        pc.addBranchCondition(className, methName, branchIndex, cnstr);
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue)

Example 22 with IntegerConstraint

use of org.evosuite.symbolic.expr.IntegerConstraint in project evosuite by EvoSuite.

the class JumpVM method LOOKUPSWITCH.

/**
 * <b>switch</b> statement whose cases may not be numbered consecutively.
 * I.e., there may be holes (missing targets) between the lowest and highest
 * target.
 *
 * <p>
 * Very similar to {@link #TABLESWITCH}. The main difference is that here we
 * are given a list of explicit goals. Tableswitch defines its goals
 * implicitly, between min and max.
 *
 * http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.
 * doc8.html#lookupswitch
 */
@Override
public void LOOKUPSWITCH(String className, String methName, int branchIndex, int goalConcrete, int[] targetsConcrete) {
    // TODO: target array remains constant. Do we really need to create and
    // pass
    // this array every time as a paremeter?
    final IntegerValue goal = env.topFrame().operandStack.popBv32();
    Vector<IntegerConstraint> constraints = new Vector<IntegerConstraint>();
    for (int targetConcrete : targetsConcrete) {
        IntegerConstant integerConstant = ExpressionFactory.buildNewIntegerConstant(targetConcrete);
        IntegerConstraint constraint;
        if (goalConcrete == targetConcrete) {
            constraint = ConstraintFactory.eq(goal, integerConstant);
            constraints.add(constraint);
            break;
        } else {
            constraint = ConstraintFactory.neq(goal, integerConstant);
            constraints.add(constraint);
        }
    }
    for (int i = 0; i < constraints.size() - 1; i++) {
        IntegerConstraint cnstrnt = constraints.get(i);
        if (cnstrnt.getLeftOperand().containsSymbolicVariable() || cnstrnt.getRightOperand().containsSymbolicVariable())
            pc.addSupportingConstraint(cnstrnt);
    }
    // add branch condition iif local constraint is concrete
    IntegerConstraint cnstr = constraints.get(constraints.size() - 1);
    if (cnstr.getLeftOperand().containsSymbolicVariable() || cnstr.getRightOperand().containsSymbolicVariable()) {
        pc.addBranchCondition(className, methName, branchIndex, cnstr);
    }
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) Vector(java.util.Vector) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 23 with IntegerConstraint

use of org.evosuite.symbolic.expr.IntegerConstraint in project evosuite by EvoSuite.

the class JumpVM method IF_ICMPLT.

/**
 * (left < right). (left >= right) is just (not (left < right)).
 *
 * http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.
 * doc6.html#if_icmpcond
 */
@Override
public void IF_ICMPLT(String className, String methName, int branchIndex, int left, int right) {
    IntegerValue rightBv = env.topFrame().operandStack.popBv32();
    IntegerValue leftBv = env.topFrame().operandStack.popBv32();
    IntegerConstraint cnstr;
    if (left < right)
        // True Branch
        cnstr = ConstraintFactory.lt(leftBv, rightBv);
    else
        // False branch
        cnstr = ConstraintFactory.gte(leftBv, rightBv);
    // add branch condition iif local constraint is concrete
    if (cnstr.getLeftOperand().containsSymbolicVariable() || cnstr.getRightOperand().containsSymbolicVariable())
        pc.addBranchCondition(className, methName, branchIndex, cnstr);
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue)

Example 24 with IntegerConstraint

use of org.evosuite.symbolic.expr.IntegerConstraint in project evosuite by EvoSuite.

the class PathConditionCollector method addBranchCondition.

/**
 * Add a new constraint to a branch condition
 *
 * @param className
 *            the class name where the branch is
 * @param methName
 *            the method where the branch is
 * @param branchIndex
 *            the branch index
 * @param c
 *            the constraint for the branch condition
 */
public void addBranchCondition(String className, String methName, int branchIndex, IntegerConstraint c) {
    Constraint<?> normalizedConstraint = normalizeConstraint(c);
    LinkedList<Constraint<?>> branch_supporting_constraints = new LinkedList<Constraint<?>>(currentSupportingConstraints);
    BranchCondition new_branch = new BranchCondition(className, methName, branchIndex, normalizedConstraint, branch_supporting_constraints);
    branchConditions.add(new_branch);
    currentSupportingConstraints.clear();
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) BranchCondition(org.evosuite.symbolic.BranchCondition) LinkedList(java.util.LinkedList)

Example 25 with IntegerConstraint

use of org.evosuite.symbolic.expr.IntegerConstraint in project evosuite by EvoSuite.

the class DistanceEstimator method getDistance.

/**
 * <p>
 * getDistance
 * </p>
 *
 * @param constraints
 *            a {@link java.util.Collection} object.
 * @return normalized distance in [0,1]
 */
public static double getDistance(Collection<Constraint<?>> constraints) {
    double result = 0;
    DistanceCalculator distanceCalculator = new DistanceCalculator();
    try {
        for (Constraint<?> c : constraints) {
            if (c instanceof StringConstraint) {
                StringConstraint string_constraint = (StringConstraint) c;
                try {
                    double strD = (double) string_constraint.accept(distanceCalculator, null);
                    result += normalize(strD);
                    log.debug("S: " + string_constraint + " strDist " + strD);
                } catch (Throwable t) {
                    log.debug("S: " + string_constraint + " strDist " + t);
                    result += 1.0;
                }
            } else if (c instanceof IntegerConstraint) {
                IntegerConstraint integer_constraint = (IntegerConstraint) c;
                long intD = (long) integer_constraint.accept(distanceCalculator, null);
                result += normalize(intD);
                log.debug("C: " + integer_constraint + " intDist " + intD);
            } else if (c instanceof RealConstraint) {
                RealConstraint real_constraint = (RealConstraint) c;
                double realD = (double) real_constraint.accept(distanceCalculator, null);
                result += normalize(realD);
                log.debug("C: " + real_constraint + " realDist " + realD);
            } else {
                throw new IllegalArgumentException("DistanceCalculator: got an unknown constraint: " + c);
            }
        }
        log.debug("Resulting distance: " + result);
        return Math.abs(result);
    } catch (Exception e) {
        return Double.MAX_VALUE;
    }
}
Also used : DistanceCalculator(org.evosuite.symbolic.expr.DistanceCalculator) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) RealConstraint(org.evosuite.symbolic.expr.RealConstraint)

Aggregations

IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)43 Constraint (org.evosuite.symbolic.expr.Constraint)37 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)30 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)30 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)28 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)25 SolverResult (org.evosuite.symbolic.solver.SolverResult)25 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)24 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)9 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)8 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)7 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)7 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)6 StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)4 RealConstraint (org.evosuite.symbolic.expr.RealConstraint)3 LinkedList (java.util.LinkedList)2 Vector (java.util.Vector)2 Comparator (org.evosuite.symbolic.expr.Comparator)2 TestCaseStringAppendString (com.examples.with.different.packagename.solver.TestCaseStringAppendString)1