Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix
Source:RegexDistanceUtils.java
...20/*21 Note: some of the code here as been refactored into the ".regex" module22 */23package org.evomaster.client.java.instrumentation.coverage.methodreplacement;24import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix;25import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexGraph;26import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexUtils;27import org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer;28import org.evomaster.client.java.utils.SimpleLogger;29import java.util.Map;30import java.util.Set;31import java.util.concurrent.ConcurrentHashMap;32import java.util.concurrent.CopyOnWriteArraySet;33import java.util.regex.Pattern;34/**35 * Class used to define the distance between a string and a regex36 */37public class RegexDistanceUtils {38 private static final Map<String, Pattern> patternCache = new ConcurrentHashMap<>();39 private static final Set<String> notSupported = new CopyOnWriteArraySet<>();40 /**41 * This is tricky... we could reasonably assume that in most cases the number of regex in the SUT42 * is finite, but that is not the case for the input strings, as those are parts of the generated43 * inputs by EM.44 * However, this code is really expensive... so any saving would be worthy.45 *46 */47 private static final Map<String, Map<String, RegexGraph>> graphCache = new ConcurrentHashMap<>();48 /**49 * <p>50 * Get the distance between the arg and the given regex.51 * All operations (insertion/deletion/replacement) cost 1.52 * There is no assumption on where and how the operations53 * can be done (ie all sequences are valid).54 * </p>55 *56 * Note that this quite expensive. If done too often, instrumentation can57 * decide to rather compute a flag.58 */59 public static int getStandardDistance(String arg, String regex) {60 if (!RegexUtils.isSupportedRegex(regex)61 || notSupported.contains(regex)62 || ExecutionTracer.isTooManyExpensiveOperations()63 ) {64 return getDefaultDistance(arg, regex);65 }66 RegexGraph graph = null;67 Map<String, RegexGraph> graphs = graphCache.get(regex);68 if(graphs != null){69 graph = graphs.get(arg);70 }71 try {72 if(graph == null) {73 graph = new RegexGraph(arg, regex);74 if(graphs == null){75 graphs = new ConcurrentHashMap<>();76 graphs.put(regex, graph);77 }78 graphs.put(arg, graph);79 }80 }catch (Exception e){81 SimpleLogger.uniqueWarn("Failed to build graph for regex: " + regex);82 notSupported.add(regex);83 return getDefaultDistance(arg, regex);84 }85 try {86 ExecutionTracer.increaseExpensiveOperationCount();87 return CostMatrix.calculateStandardCost(graph);88 }catch (Exception e){89 SimpleLogger.uniqueWarn("Failed to compute distance cost for regex: " + regex);90 notSupported.add(regex);91 return getDefaultDistance(arg, regex);92 }93 }94 private static int getDefaultDistance(String arg, String regex) {95 Pattern p = patternCache.get(regex);96 if(p == null) {97 p = Pattern.compile(regex);98 patternCache.put(regex, p);99 }100 if (p.matcher(arg).matches())101 return 0;...
CostMatrix
Using AI Code Generation
1package org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex;2import org.evomaster.client.java.instrumentation.coverage.methodreplacement.DistanceHelper;3import org.evomaster.client.java.instrumentation.shared.ReplacementType;4import org.evomaster.client.java.instrumentation.shared.StringSpecialization;5import org.evomaster.client.java.instrumentation.shared.StringSpecializationInfo;6import org.evomaster.client.java.instrumentation.shared.TaintInputName;7import org.evomaster.client.java.instrumentation.shared.StringSpecializationInfo;8import org.evomaster.client.java.instrumentation.shared.StringSpecialization;9import org.evomaster.client.java.instrumentation.shared.ReplacementType;10import org.evomaster.client.java.instrumentation.shared.TaintInputName;11import org.evomaster.client.java.instrumentation.coverage.methodreplacement.DistanceHelper;12import org.evomaster.client.java.instrumentation.coverage.methodreplacement.DistanceHelper;13import java.util.List;14import java.util.regex.Matcher;15import java.util.regex.Pattern;16public class RegexDistanceHelper {17 public static double getDistance(String regex, String input, StringSpecializationInfo info, List<TaintInputName> taintInputNames) {18 if (regex == null || input == null) {19 return 0.0;20 }21 if (info == null) {22 return 0.0;23 }24 if (taintInputNames == null || taintInputNames.isEmpty()) {25 return 0.0;26 }27 double distance = 0.0;28 int index = 0;29 for (TaintInputName taintInputName : taintInputNames) {30 if (taintInputName.getReplacementType() != ReplacementType.REGEX) {31 continue;32 }33 if (taintInputName.getSpecialization() == null) {34 continue;35 }36 if (taintInputName.getSpecialization().getDistance() == null) {37 continue;38 }39 StringSpecialization specialization = taintInputName.getSpecialization();40 String regexPart = regex.substring(index, index + taintInputName.getTaintInputName().length());41 if (specialization.getDistance().equals(DistanceHelper.EXACT_MATCH)) {42 if (!regexPart.equals(input.substring(index, index + taintInputName.getTaintInputName().length()))) {43 return 0.0;44 }45 } else if (specialization.getDistance().equals(DistanceHelper
CostMatrix
Using AI Code Generation
1import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix;2import org.evomaster.client.java.instrumentation.shared.Replacement;3import org.evomaster.client.java.instrumentation.shared.StringSpecialization;4import java.util.List;5import java.util.Map;6import java.util.regex.Matcher;7import java.util.regex.Pattern;8public class RegexReplacement {9 public static final String METHOD = "matches";10 @Replacement(className = "java.lang.String", method = METHOD)11 public static boolean replace(String target, String regex, CostMatrix matrix) {12 if (target == null) {13 throw new NullPointerException("Target string is null");14 }15 if (regex == null) {16 throw new NullPointerException("Regex is null");17 }18 if (matrix == null) {19 throw new NullPointerException("Matrix is null");20 }21 Pattern pattern = Pattern.compile(regex);22 Matcher matcher = pattern.matcher(target);23 boolean match = matcher.matches();24 if (match) {25 matrix.increment(0);26 }27 StringSpecialization specialization = new StringSpecialization(28 target, matrix, 0, regex, true);29 matrix.addSpecialization(specialization);30 return match;31 }32 @Replacement(className = "java.util.regex.Pattern", method = "matches")33 public static boolean replace(Pattern pattern, String input, CostMatrix matrix) {34 if (pattern == null) {35 throw new NullPointerException("Pattern is null");36 }37 if (input == null) {38 throw new NullPointerException("Input string is null");39 }40 if (matrix == null) {41 throw new NullPointerException("Matrix is null");42 }43 Matcher matcher = pattern.matcher(input);44 boolean match = matcher.matches();45 if (match) {46 matrix.increment(0);47 }48 StringSpecialization specialization = new StringSpecialization(49 input, matrix, 0, pattern.pattern(), true);50 matrix.addSpecialization(specialization);51 return match;52 }53 @Replacement(className = "java.util.regex.Matcher", method = "matches")54 public static boolean replace(Matcher matcher, CostMatrix matrix) {55 if (matcher == null) {56 throw new NullPointerException("Matcher is null");57 }58 if (matrix == null) {59 throw new NullPointerException("Matrix is null");60 }61 boolean match = matcher.matches();62 if (match) {63 matrix.increment(0);64 }
CostMatrix
Using AI Code Generation
1 import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix;2 import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexCoverageInfo;3 import org.evomaster.client.java.instrumentation.shared.StringSpecialization;4 import org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer;5 import org.evomaster.client.java.instrumentation.coverage.methodreplacement.MethodReplacementClass;6 import org.evomaster.client.java.instrumentation.coverage.methodreplacement.classes.StringClassReplacement;7 import org.evomaster.client.java.instrumentation.coverage.methodreplacement.classes.StringSpecializationUtils;8 import org.evomaster.client.java.instrumentation.coverage.methodreplacement.classes.StringSpecializationUtils$;9 import java.util.*;10 import java.util.function.Predicate;11 import java.util.regex.*;12 import static org.evomaster.client.java.instrumentation.coverage.methodreplacement.classes.StringSpecializationUtils.*;13 public class CostMatrixReplacement {14 private static final String CLASS_NAME = CostMatrix.class.getName();15 private static final String METHOD_NAME = "getCostMatrix";16 private static final String METHOD_DESCRIPTOR = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lorg/evomaster/client/java/instrumentation/coverage/methodreplacement/regex/CostMatrix;";17 private static final String METHOD_SIGNATURE = null;18 private static final String METHOD_TYPE = MethodReplacementClass.Type.STATIC.name();19 public static CostMatrix getCostMatrix(String regex, String text, String flags, String prefix) {20 if (regex == null || text == null || flags == null || prefix == null) {21 throw new IllegalArgumentException("Input parameters cannot be null");22 }23 if (!ExecutionTracer.isTaintInputEnabled()) {24 return CostMatrix.getCostMatrix(regex, text, flags, prefix);25 }26 String idTemplate = CLASS_NAME + "#" + METHOD_NAME + "(%s, %s, %s, %s)";27 String id = String.format(idTemplate, regex, text, flags, prefix);28 RegexCoverageInfo regexCoverageInfo = ExecutionTracer.getRegexCoverageInfo(id);29 if (regexCoverageInfo == null) {30 regexCoverageInfo = new RegexCoverageInfo();31 ExecutionTracer.addRegexCoverageInfo(id, regexCoverageInfo);32 }33 Pattern pattern = getPattern(regex, flags, prefix
CostMatrix
Using AI Code Generation
1import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix;2import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexCoverageInfo;3import java.util.HashMap;4import java.util.Map;5public class RegexCoverageInfo {6 private static final String REGEX = "REGEX";7 private static final String INPUT = "INPUT";8 private static final String TRUE = "TRUE";9 private static final String FALSE = "FALSE";10 private static final String START = "START";11 private static final String END = "END";12 private static final String MATCH = "MATCH";13 private static final String NO_MATCH = "NO_MATCH";14 private static final String MATCH_FOUND = "MATCH_FOUND";15 private static final String MATCH_NOT_FOUND = "MATCH_NOT_FOUND";16 private static final String MATCH_ANY = "MATCH_ANY";17 private static final String MATCH_ANY_NOT_FOUND = "MATCH_ANY_NOT_FOUND";18 private static final String MATCH_ANY_OF = "MATCH_ANY_OF";19 private static final String MATCH_ANY_OF_NOT_FOUND = "MATCH_ANY_OF_NOT_FOUND";20 private static final String MATCH_ANY_OF_ANY = "MATCH_ANY_OF_ANY";21 private static final String MATCH_ANY_OF_ANY_NOT_FOUND = "MATCH_ANY_OF_ANY_NOT_FOUND";22 private static final String MATCH_ANY_OF_ANY_ANY = "MATCH_ANY_OF_ANY_ANY";23 private static final String MATCH_ANY_OF_ANY_ANY_NOT_FOUND = "MATCH_ANY_OF_ANY_ANY_NOT_FOUND";24 private static final String MATCH_ANY_OF_ANY_ANY_ANY = "MATCH_ANY_OF_ANY_ANY_ANY";25 private static final String MATCH_ANY_OF_ANY_ANY_ANY_NOT_FOUND = "MATCH_ANY_OF_ANY_ANY_ANY_NOT_FOUND";26 private static final String MATCH_ANY_OF_ANY_ANY_ANY_ANY = "MATCH_ANY_OF_ANY_ANY_ANY_ANY";27 private static final String MATCH_ANY_OF_ANY_ANY_ANY_ANY_NOT_FOUND = "MATCH_ANY_OF_ANY_ANY_ANY_ANY_NOT_FOUND";28 private static final String MATCH_ANY_OF_ANY_ANY_ANY_ANY_ANY = "MATCH_ANY_OF_ANY_ANY_ANY_ANY_ANY";29 private static final String MATCH_ANY_OF_ANY_ANY_ANY_ANY_ANY_NOT_FOUND = "MATCH_ANY_OF_ANY_ANY_ANY_ANY_ANY_NOT_FOUND";30 private static final String MATCH_ANY_OF_ANY_ANY_ANY_ANY_ANY_ANY = "MATCH_ANY_OF_ANY_ANY_ANY_ANY_ANY_ANY";
CostMatrix
Using AI Code Generation
1public class CostMatrix {2 private int[][] matrix;3 private int row;4 private int col;5 private int size;6 private int cost;7 private int[][] costMatrix;8 private int[] path;9 private int pathLength;10 private int[] costPath;11 private int costPathLength;12 private int[][][] pathMatrix;13 private int[] pathLengthMatrix;14 private int[] costPathMatrix;15 private int costPathLengthMatrix;16 private int[] pathLengthMatrix2;17 private int[] costPathMatrix2;18 private int costPathLengthMatrix2;19 private int[] pathLengthMatrix3;20 private int[] costPathMatrix3;21 private int costPathLengthMatrix3;22 private int[] pathLengthMatrix4;23 private int[] costPathMatrix4;24 private int costPathLengthMatrix4;25 private int[] pathLengthMatrix5;26 private int[] costPathMatrix5;27 private int costPathLengthMatrix5;28 private int[] pathLengthMatrix6;29 private int[] costPathMatrix6;30 private int costPathLengthMatrix6;31 private int[] pathLengthMatrix7;32 private int[] costPathMatrix7;33 private int costPathLengthMatrix7;34 private int[] pathLengthMatrix8;35 private int[] costPathMatrix8;36 private int costPathLengthMatrix8;37 private int[] pathLengthMatrix9;38 private int[] costPathMatrix9;39 private int costPathLengthMatrix9;40 private int[] pathLengthMatrix10;41 private int[] costPathMatrix10;42 private int costPathLengthMatrix10;43 private int[] pathLengthMatrix11;44 private int[] costPathMatrix11;45 private int costPathLengthMatrix11;46 private int[] pathLengthMatrix12;47 private int[] costPathMatrix12;48 private int costPathLengthMatrix12;49 private int[] pathLengthMatrix13;50 private int[] costPathMatrix13;51 private int costPathLengthMatrix13;52 private int[] pathLengthMatrix14;53 private int[] costPathMatrix14;54 private int costPathLengthMatrix14;55 private int[] pathLengthMatrix15;56 private int[] costPathMatrix15;57 private int costPathLengthMatrix15;58 private int[] pathLengthMatrix16;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!