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;
}
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;
}
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);
}
}
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;
}
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();
}
}
Aggregations