Search in sources :

Example 26 with StringValue

use of org.evosuite.symbolic.expr.str.StringValue in project evosuite by EvoSuite.

the class StringAVM method applyAVM.

/**
 * <p>
 * strLocalSearch
 * </p>
 *
 * @return a boolean.
 */
public boolean applyAVM() throws SolverTimeoutException {
    ExpressionExecutor exprExecutor = new ExpressionExecutor();
    // try to remove each
    log.debug("Trying to remove characters");
    boolean improvement = false;
    checkpointVar(DistanceEstimator.getDistance(cnstr));
    // First chop characters from the back until distance doesn't improve
    String oldString = strVar.getConcreteValue();
    boolean improved = true;
    while (improved && oldString.length() > 0) {
        if (isFinished()) {
            throw new SolverTimeoutException();
        }
        String newStr = oldString.substring(0, oldString.length() - 1);
        strVar.setConcreteValue(newStr);
        log.debug("Current attempt: " + newStr);
        improved = false;
        double newDist = DistanceEstimator.getDistance(cnstr);
        // if (distImpr(newDist)) {
        if (newDist <= checkpointDistance) {
            log.debug("Distance improved or did not increase, keeping change");
            checkpointVar(newDist);
            improvement = true;
            improved = true;
            oldString = newStr;
            if (newDist == 0) {
                return true;
            }
        } else {
            log.debug("Distance did not improve, reverting change");
            restoreVar();
        }
    }
    // next try to replace each character using AVM
    log.debug("Trying to replace characters");
    // Backup is done internally
    if (doStringAVM(oldString)) {
        improvement = true;
        oldString = strVar.getConcreteValue();
    }
    if (checkpointDistance == 0.0) {
        return true;
    }
    // try to add at the end
    log.debug("Trying to add characters");
    checkpointVar(DistanceEstimator.getDistance(cnstr));
    // Finally add new characters at the end of the string
    improved = true;
    while (improved) {
        if (isFinished()) {
            throw new SolverTimeoutException();
        }
        improved = false;
        char charToInsert = Randomness.nextChar();
        String newStr = oldString + charToInsert;
        strVar.setConcreteValue(newStr);
        double newDist = DistanceEstimator.getDistance(cnstr);
        log.debug("Adding: " + newStr + ": " + newDist);
        if (distImpr(newDist)) {
            improvement = true;
            improved = true;
            checkpointVar(newDist);
            if (checkpointDistance == 0.0) {
                log.debug("Search seems successful, stopping at " + checkpointDistance + "/" + newDist);
                return true;
            }
            doCharacterAVM(newStr.length() - 1);
            oldString = strVar.getConcreteValue();
        } else {
            restoreVar();
        }
    }
    // try to insert delimiters (if any)
    Set<StringValue> delimiters = getTokenDelimiters(cnstr);
    for (StringValue delimiter : delimiters) {
        if (isFinished()) {
            throw new SolverTimeoutException();
        }
        improved = true;
        String delimiterStr = (String) delimiter.accept(exprExecutor, null);
        while (improved) {
            if (isFinished()) {
                throw new SolverTimeoutException();
            }
            improved = false;
            char charToInsert = Randomness.nextChar();
            String newStr = oldString + delimiterStr + charToInsert;
            strVar.setConcreteValue(newStr);
            double newDist = DistanceEstimator.getDistance(cnstr);
            log.debug("Adding: " + newStr + ": " + newDist);
            if (distImpr(newDist)) {
                improvement = true;
                improved = true;
                checkpointVar(newDist);
                if (checkpointDistance == 0.0) {
                    log.debug("Search seems successful, stopping at " + checkpointDistance + "/" + newDist);
                    return true;
                }
                doCharacterAVM(newStr.length() - 1);
                oldString = strVar.getConcreteValue();
            } else {
                restoreVar();
            }
        }
    }
    return improvement;
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) ExpressionExecutor(org.evosuite.symbolic.expr.ExpressionExecutor) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 27 with StringValue

use of org.evosuite.symbolic.expr.str.StringValue in project evosuite by EvoSuite.

the class StringAVM method getTokenDelimiters.

private static Set<StringValue> getTokenDelimiters(Collection<Constraint<?>> constraints) {
    Set<StringValue> delimiters = new HashSet<StringValue>();
    for (Constraint<?> constraint : constraints) {
        if (constraint instanceof StringConstraint) {
            StringConstraint stringConstraint = (StringConstraint) constraint;
            if (stringConstraint.getLeftOperand() instanceof HasMoreTokensExpr) {
                HasMoreTokensExpr hasMoreTokensExpr = (HasMoreTokensExpr) stringConstraint.getLeftOperand();
                StringValue delimiter = hasMoreTokensExpr.getTokenizerExpr().getDelimiter();
                delimiters.add(delimiter);
            }
        }
    }
    return delimiters;
}
Also used : StringConstraint(org.evosuite.symbolic.expr.StringConstraint) HasMoreTokensExpr(org.evosuite.symbolic.expr.token.HasMoreTokensExpr) StringValue(org.evosuite.symbolic.expr.str.StringValue) HashSet(java.util.HashSet)

Example 28 with StringValue

use of org.evosuite.symbolic.expr.str.StringValue in project evosuite by EvoSuite.

the class DistanceCalculator method getStringDistance.

private static double getStringDistance(HasMoreTokensExpr hasMoreTokensExpr) {
    TokenizerExpr tokenizerExpr = hasMoreTokensExpr.getTokenizerExpr();
    StringValue string = tokenizerExpr.getString();
    StringValue delimiter = tokenizerExpr.getDelimiter();
    int nextTokenCount = tokenizerExpr.getNextTokenCount();
    ExpressionExecutor exprExecutor = new ExpressionExecutor();
    String concreteString = (String) string.accept(exprExecutor, null);
    String concreteDelimiter = (String) delimiter.accept(exprExecutor, null);
    if (concreteString.length() < concreteDelimiter.length() * nextTokenCount) {
        // nextToken operations
        return Double.MAX_VALUE;
    }
    StringTokenizer tokenizer = new StringTokenizer(concreteString, concreteDelimiter);
    Vector<String> tokens = new Vector<String>();
    while (tokenizer.hasMoreTokens()) {
        tokens.add(tokenizer.nextToken());
    }
    if (tokens.size() > nextTokenCount) {
        // we already have enough tokens to make n true
        return 0;
    } else {
        return StrEquals("", concreteDelimiter);
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) StringValue(org.evosuite.symbolic.expr.str.StringValue) TokenizerExpr(org.evosuite.symbolic.expr.token.TokenizerExpr) Vector(java.util.Vector)

Example 29 with StringValue

use of org.evosuite.symbolic.expr.str.StringValue in project evosuite by EvoSuite.

the class SymbolicHeap method getField.

/**
 * @param className
 * @param fieldName
 * @param conc_receiver
 * @param symb_receiver
 * @param conc_value
 * @return
 */
public StringValue getField(String className, String fieldName, Object conc_receiver, ReferenceExpression symb_receiver, String conc_value) {
    Map<ReferenceExpression, Expression<?>> symb_field = getOrCreateSymbolicField(className, fieldName);
    StringValue symb_value = (StringValue) symb_field.get(symb_receiver);
    if (symb_value == null || !((String) symb_value.getConcreteValue()).equals(conc_value)) {
        symb_value = ExpressionFactory.buildNewStringConstant(conc_value);
        symb_field.remove(symb_receiver);
    }
    return symb_value;
}
Also used : Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 30 with StringValue

use of org.evosuite.symbolic.expr.str.StringValue in project evosuite by EvoSuite.

the class Perl5Matcher_Matches method executeFunction.

@Override
public Object executeFunction() {
    // Perl5Matcher conc_matcher = (Perl5Matcher) this.getConcReceiver();
    // NonNullReference symb_matcher = (NonNullReference) this
    // .getSymbReceiver();
    boolean res = this.getConcBooleanRetVal();
    ReferenceConstant symb_string_ref = (ReferenceConstant) this.getSymbArgument(0);
    // Reference symb_pattern_ref = this.getSymbArgument(1);
    String conc_string = (String) this.getConcArgument(0);
    Pattern conc_pattern = (Pattern) this.getConcArgument(1);
    StringValue symb_string_value = env.heap.getField(org.evosuite.symbolic.vm.regex.Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_string, symb_string_ref, conc_string);
    if (symb_string_value != null && symb_string_value.containsSymbolicVariable()) {
        int concrete_value = res ? 1 : 0;
        String pattern_str = conc_pattern.getPattern();
        StringConstant symb_pattern_value = ExpressionFactory.buildNewStringConstant(pattern_str);
        StringBinaryComparison strComp = new StringBinaryComparison(symb_pattern_value, Operator.APACHE_ORO_PATTERN_MATCHES, symb_string_value, (long) concrete_value);
        return strComp;
    } else {
        return this.getSymbIntegerRetVal();
    }
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) Pattern(org.apache.oro.text.regex.Pattern) StringBinaryComparison(org.evosuite.symbolic.expr.bv.StringBinaryComparison) StringValue(org.evosuite.symbolic.expr.str.StringValue) StringConstant(org.evosuite.symbolic.expr.str.StringConstant)

Aggregations

StringValue (org.evosuite.symbolic.expr.str.StringValue)45 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)39 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)16 Expression (org.evosuite.symbolic.expr.Expression)10 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)8 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)8 RealValue (org.evosuite.symbolic.expr.fp.RealValue)4 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)4 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)4 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)4 ArrayList (java.util.ArrayList)3 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)3 StringReaderExpr (org.evosuite.symbolic.expr.reader.StringReaderExpr)3 StringMultipleExpression (org.evosuite.symbolic.expr.str.StringMultipleExpression)3 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)3 StringReader (java.io.StringReader)2 Matcher (java.util.regex.Matcher)2 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)2 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)2 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)2