How to use HeuristicsForJumps class of org.evomaster.client.java.instrumentation.heuristic package

Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps

Source:ExecutionTracer.java Github

copy

Full Screen

2import org.evomaster.client.java.instrumentation.Action;3import org.evomaster.client.java.instrumentation.AdditionalInfo;4import org.evomaster.client.java.instrumentation.shared.*;5import org.evomaster.client.java.instrumentation.TargetInfo;6import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;7import org.evomaster.client.java.instrumentation.heuristic.Truthness;8import java.util.*;9import java.util.concurrent.ConcurrentHashMap;10import java.util.stream.Collectors;11/**12 * Methods of this class will be injected in the SUT to13 * keep track of what the tests do execute/cover.14 */15public class ExecutionTracer {16 /*17 Careful if you change the signature of any of the18 methods in this class, as they are injected in the19 bytecode instrumentation.20 Fortunately, unit tests should quickly find such21 type of issues.22 */23 /**24 * Key -> the unique descriptive id of the coverage objective25 */26 private static final Map<String, TargetInfo> objectiveCoverage =27 new ConcurrentHashMap<>(65536);28 /**29 * A test case can be composed by 1 or more actions, eg HTTP calls.30 * When we get the best distance for a testing target, we might31 * also want to know which action in the test led to it.32 */33 private static int actionIndex = 0;34 /**35 * A set of possible values used in the tests, needed for some kinds36 * of taint analyses37 */38 private static Set<String> inputVariables = new HashSet<>();39 /**40 * Besides code coverage, there might be other events that we want to41 * keep track during test execution.42 * We keep track of it separately for each action43 */44 private static final List<AdditionalInfo> additionalInfoList = new ArrayList<>();45 static {46 reset();47 }48 public static void reset() {49 objectiveCoverage.clear();50 actionIndex = 0;51 additionalInfoList.clear();52 additionalInfoList.add(new AdditionalInfo());53 inputVariables = new HashSet<>();54 }55 public static void setAction(Action action){56 if(action.getIndex() != actionIndex) {57 actionIndex = action.getIndex();58 additionalInfoList.add(new AdditionalInfo());59 }60 if(action.getInputVariables() != null && !action.getInputVariables().isEmpty()){61 inputVariables = action.getInputVariables();62 }63 }64 /**65 * Check if the given input represented a tainted value from the test cases.66 * This could be based on static info of the input (eg, according to a precise67 * name convention given by TaintInputName), or dynamic info given directly by68 * the test itself (eg, the test at action can register a list of values to check69 * for)70 */71 public static boolean isTaintInput(String input){72 return TaintInputName.isTaintInput(input) || inputVariables.contains(input);73 }74 public static TaintType getTaintType(String input){75 if(input == null){76 return TaintType.NONE;77 }78 if(isTaintInput(input)){79 return TaintType.FULL_MATCH;80 }81 if(TaintInputName.includesTaintInput(input)82 || inputVariables.stream().anyMatch(v -> input.contains(v))){83 return TaintType.PARTIAL_MATCH;84 }85 return TaintType.NONE;86 }87 public static List<AdditionalInfo> exposeAdditionalInfoList() {88 return additionalInfoList;89 }90 public static void addQueryParameter(String param){91 additionalInfoList.get(actionIndex).addQueryParameter(param);92 }93 public static void addHeader(String header){94 additionalInfoList.get(actionIndex).addHeader(header);95 }96 public static void addStringSpecialization(String taintInputName, StringSpecializationInfo info){97 additionalInfoList.get(actionIndex).addSpecialization(taintInputName, info);98 }99 public static void markLastExecutedStatement(String lastLine, String lastMethod){100 additionalInfoList.get(actionIndex).pushLastExecutedStatement(lastLine, lastMethod);101 }102 public static final String COMPLETED_LAST_EXECUTED_STATEMENT_NAME = "completedLastExecutedStatement";103 public static final String COMPLETED_LAST_EXECUTED_STATEMENT_DESCRIPTOR = "()V";104 public static void completedLastExecutedStatement(){105 additionalInfoList.get(actionIndex).popLastExecutedStatement();106 }107 public static Map<String, TargetInfo> getInternalReferenceToObjectiveCoverage() {108 return objectiveCoverage;109 }110 /**111 * @return the number of objectives that have been encountered112 * during the test execution113 */114 public static int getNumberOfObjectives() {115 return objectiveCoverage.size();116 }117 public static int getNumberOfObjectives(String prefix) {118 return (int) objectiveCoverage119 .entrySet().stream()120 .filter(e -> prefix == null || e.getKey().startsWith(prefix))121 .count();122 }123 /**124 * Note: only the objectives encountered so far can have125 * been recorded. So, this is a relative value, not based126 * on the code of the whole SUT (just the parts executed so far).127 * Therefore, it is quite useless for binary values (ie 0 or 1),128 * like current implementation of basic line coverage.129 *130 * @param prefix used for string matching of which objectives types131 * to consider, eg only lines or only branches.132 * Use "" or {@code null} to pick up everything133 * @return134 */135 public static int getNumberOfNonCoveredObjectives(String prefix) {136 return getNonCoveredObjectives(prefix).size();137 }138 public static Set<String> getNonCoveredObjectives(String prefix) {139 return objectiveCoverage140 .entrySet().stream()141 .filter(e -> prefix == null || e.getKey().startsWith(prefix))142 .filter(e -> e.getValue().value < 1)143 .map(e -> e.getKey())144 .collect(Collectors.toSet());145 }146 public static Double getValue(String id) {147 return objectiveCoverage.get(id).value;148 }149 private static void updateObjective(String id, double value) {150 if (value < 0d || value > 1d) {151 throw new IllegalArgumentException("Invalid value " + value + " out of range [0,1]");152 }153 /*154 In the same execution, a target could be reached several times,155 so we should keep track of the best value found so far156 */157 if (objectiveCoverage.containsKey(id)) {158 double previous = objectiveCoverage.get(id).value;159 if(value > previous){160 objectiveCoverage.put(id, new TargetInfo(null, id, value, actionIndex));161 }162 } else {163 objectiveCoverage.put(id, new TargetInfo(null, id, value, actionIndex));164 }165 ObjectiveRecorder.update(id, value);166 }167 public static void executedNumericComparison(String idTemplate, double lt, double eq, double gt){168 updateObjective(ObjectiveNaming.numericComparisonObjectiveName(idTemplate, -1), lt);169 updateObjective(ObjectiveNaming.numericComparisonObjectiveName(idTemplate, 0), eq);170 updateObjective(ObjectiveNaming.numericComparisonObjectiveName(idTemplate, +1), gt);171 }172 public static void executedReplacedMethod(String idTemplate, ReplacementType type, Truthness t){173 String idTrue = ObjectiveNaming.methodReplacementObjectiveName(idTemplate, true, type);174 String idFalse = ObjectiveNaming.methodReplacementObjectiveName(idTemplate, false, type);175 updateObjective(idTrue, t.getOfTrue());176 updateObjective(idFalse, t.getOfFalse());177 }178 public static final String EXECUTED_LINE_METHOD_NAME = "executedLine";179 public static final String EXECUTED_LINE_DESCRIPTOR = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V";180 /**181 * Report on the fact that a given line has been executed.182 */183 public static void executedLine(String className, String methodName, String descriptor, int line) {184 //for targets to cover185 String lineId = ObjectiveNaming.lineObjectiveName(className, line);186 String classId = ObjectiveNaming.classObjectiveName(className);187 updateObjective(lineId, 1d);188 updateObjective(classId, 1d);189 //to calculate last executed line190 String lastLine = className + "_" + line + "_" + methodName;191 String lastMethod = className + "_" + methodName + "_" + descriptor;192 markLastExecutedStatement(lastLine, lastMethod);193 }194 public static final String EXECUTING_METHOD_METHOD_NAME = "executingMethod";195 public static final String EXECUTING_METHOD_DESCRIPTOR = "(Ljava/lang/String;IIZ)V";196 /**197 * Report on whether method calls have been successfully completed.198 * Failures can happen due to thrown exceptions.199 *200 * @param className201 * @param line202 * @param index as there can be many method calls on same line, need to differentiate them203 * @param completed whether the method call was successfully completed.204 */205 public static void executingMethod(String className, int line, int index, boolean completed){206 String id = ObjectiveNaming.successCallObjectiveName(className, line, index);207 if(completed) {208 updateObjective(id, 1d);209 } else {210 updateObjective(id, 0.5);211 }212 }213 //---- branch-jump methods --------------------------214 private static void updateBranch(String className, int line, int branchId, Truthness t) {215 /*216 Note: when we have217 if(x > 0){}218 the "jump" to "else" branch is done if that is false.219 So, the actual evaluated condition is the negation, ie220 x <= 0221 */222 String forThen = ObjectiveNaming.branchObjectiveName(className, line, branchId, true);223 String forElse = ObjectiveNaming.branchObjectiveName(className, line, branchId, false);224 updateObjective(forElse, t.getOfTrue());225 updateObjective(forThen, t.getOfFalse());226 }227 public static final String EXECUTING_BRANCH_JUMP_METHOD_NAME = "executingBranchJump";228 public static final String JUMP_DESC_1_VALUE = "(IILjava/lang/String;II)V";229 public static void executingBranchJump(230 int value, int opcode, String className, int line, int branchId) {231 Truthness t = HeuristicsForJumps.getForSingleValueJump(value, opcode);232 updateBranch(className, line, branchId, t);233 }234 public static final String JUMP_DESC_2_VALUES = "(IIILjava/lang/String;II)V";235 public static void executingBranchJump(236 int firstValue, int secondValue, int opcode, String className, int line, int branchId) {237 Truthness t = HeuristicsForJumps.getForValueComparison(firstValue, secondValue, opcode);238 updateBranch(className, line, branchId, t);239 }240 public static final String JUMP_DESC_OBJECTS =241 "(Ljava/lang/Object;Ljava/lang/Object;ILjava/lang/String;II)V";242 public static void executingBranchJump(243 Object first, Object second, int opcode, String className, int line, int branchId) {244 Truthness t = HeuristicsForJumps.getForObjectComparison(first, second, opcode);245 updateBranch(className, line, branchId, t);246 }247 public static final String JUMP_DESC_NULL =248 "(Ljava/lang/Object;ILjava/lang/String;II)V";249 public static void executingBranchJump(250 Object obj, int opcode, String className, int line, int branchId) {251 Truthness t = HeuristicsForJumps.getForNullComparison(obj, opcode);252 updateBranch(className, line, branchId, t);253 }254}...

Full Screen

Full Screen

Source:DateClassReplacement.java Github

copy

Full Screen

...78 Objects.requireNonNull(when);79 final long a = caller.getTime();80 final long b = when.getTime();81 /*82 * We use the same gradient that HeuristicsForJumps.getForValueComparison()83 * used for IF_ICMPLT, ie, a < b84 */85 return TruthnessUtils.getLessThanTruthness(a, b);86 }87 /**88 * Tests if this date is after the specified date.89 *90 * @param caller91 * @param when92 * @param idTemplate93 * @return94 */95 @Replacement(type = ReplacementType.BOOLEAN, category = ReplacementCategory.BASE)96 public static boolean after(Date caller, Date when, String idTemplate) {...

Full Screen

Full Screen

HeuristicsForJumps

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.instrumentation.example.heuristics;2import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;3import org.evomaster.client.java.instrumentation.shared.ObjectiveNaming;4public class HeuristicsExample {5 public static void main(String[] args) {6 HeuristicsForJumps heuristics = new HeuristicsForJumps();7 int x = 0;8 int y = 0;9 if (x > 0) {10 y++;11 }12 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:0:0", 0.5);13 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:1:0", 0.5);14 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:1:1", 0.5);15 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:2:0", 0.5);16 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:2:1", 0.5);17 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:2:2", 0.5);18 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:3:0", 0.5);19 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:3:1", 0.5);20 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:3:2", 0.5);21 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:3:3", 0.5);22 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:4:0", 0.5);23 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:4:1", 0.5);24 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:4:2", 0.5);25 heuristics.addHeuristic(ObjectiveNaming.METHOD_REACHED, "2:4:3", 0.5);26 heuristics.addHeuristic(ObjectiveNaming

Full Screen

Full Screen

HeuristicsForJumps

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.instrumentation.example;2import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;3public class 2 {4 public static int foo(int i) {5 int j = 0;6 if (i == 0) {7 j = 1;8 } else if (i == 1) {9 j = 2;10 } else if (i == 2) {11 j = 3;12 } else if (i == 3) {13 j = 4;14 } else if (i == 4) {15 j = 5;16 } else if (i == 5) {17 j = 6;18 } else if (i == 6) {19 j = 7;20 } else if (i == 7) {21 j = 8;22 } else if (i == 8) {23 j = 9;24 } else if (i == 9) {25 j = 10;26 } else if (i == 10) {27 j = 11;28 } else if (i == 11) {29 j = 12;30 } else if (i == 12) {31 j = 13;32 } else if (i == 13) {33 j = 14;34 } else if (i == 14) {35 j = 15;36 } else if (i == 15) {37 j = 16;38 } else if (i == 16) {39 j = 17;40 } else if (i == 17) {41 j = 18;42 } else if (i == 18) {43 j = 19;44 } else if (i == 19) {45 j = 20;46 } else if (i == 20) {47 j = 21;48 } else if (i == 21) {49 j = 22;50 } else if (i == 22) {51 j = 23;52 } else if (i == 23) {53 j = 24;54 } else if (i == 24) {55 j = 25;56 } else if (i == 25) {57 j = 26;58 } else if (i ==

Full Screen

Full Screen

HeuristicsForJumps

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.instrumentation.example;2import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;3import org.junit.jupiter.api.Test;4public class HeuristicsForJumpsExample {5 public void test() {6 HeuristicsForJumps heuristics = new HeuristicsForJumps();7 heuristics.addHeuristic("id", "1", true);8 heuristics.addHeuristic("id", "2", false);9 heuristics.addHeuristic("id", "3", false);10 System.out.println(heuristics.getHeuristicsAsJson());11 }12}13package org.evomaster.client.java.instrumentation.example;14import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;15import org.junit.jupiter.api.Test;16public class HeuristicsForJumpsExample {17 public void test() {18 HeuristicsForJumps heuristics = new HeuristicsForJumps();19 heuristics.addHeuristic("id", "1", false);20 heuristics.addHeuristic("id", "2", false);21 heuristics.addHeuristic("id", "3", true);22 System.out.println(heuristics.getHeuristicsAsJson());23 }24}25package org.evomaster.client.java.instrumentation.example;26import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;27import org.junit.jupiter.api.Test;28public class HeuristicsForJumpsExample {29 public void test() {30 HeuristicsForJumps heuristics = new HeuristicsForJumps();31 heuristics.addHeuristic("id", "1", false);32 heuristics.addHeuristic("id", "2", true);33 heuristics.addHeuristic("id", "3", false);

Full Screen

Full Screen

HeuristicsForJumps

Using AI Code Generation

copy

Full Screen

1public class Main {2 public static void main(String[] args) {3 HeuristicsForJumps heuristics = new HeuristicsForJumps();4 heuristics.addHeuristic("heuristic1", 0.5, 0.5, 0.5);5 heuristics.addHeuristic("heuristic2", 0.5, 0.5, 0.5);6 heuristics.addHeuristic("heuristic3", 0.5, 0.5, 0.5);7 heuristics.addHeuristic("heuristic4", 0.5, 0.5, 0.5);8 }9}10public class Main {11 public static void main(String[] args) {12 HeuristicsForJumps heuristics = new HeuristicsForJumps();13 heuristics.addHeuristic("heuristic1", 0.5, 0.5, 0.5);14 heuristics.addHeuristic("heuristic2", 0.5, 0.5, 0.5);15 heuristics.addHeuristic("heuristic3", 0.5, 0.5, 0.5);16 heuristics.addHeuristic("heuristic4", 0.5, 0.5, 0.5);17 }18}19public class Main {20 public static void main(String[] args) {21 HeuristicsForJumps heuristics = new HeuristicsForJumps();22 heuristics.addHeuristic("heuristic1", 0.5, 0.5, 0.5);23 heuristics.addHeuristic("heuristic2", 0.5, 0.5, 0.5);24 heuristics.addHeuristic("heuristic3", 0.5, 0.5, 0.5);25 heuristics.addHeuristic("heuristic4", 0.5, 0.5, 0.5);26 }27}

Full Screen

Full Screen

HeuristicsForJumps

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.instrumentation.heuristic.*;2import java.util.List;3import java.util.ArrayList;4import java.util.Arrays;5import java.util.Collections;6import java.util.HashMap;7import java.util.Map;8import java.util.Map.Entry;9public class HeuristicsForJumpsExample {10 public static void main(String[] args) {11 HeuristicsForJumps heuristics = new HeuristicsForJumps();12 List<Integer> jumps = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));13 List<Integer> jumps2 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));14 List<Integer> jumps3 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));15 List<Integer> jumps4 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));16 List<Integer> jumps5 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));17 List<Integer> jumps6 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));18 List<Integer> jumps7 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));19 List<Integer> jumps8 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));20 List<Integer> jumps9 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

Full Screen

Full Screen

HeuristicsForJumps

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;2import org.evomaster.client.java.instrumentation.heuristic.Truthness;3import org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer;4public class Main {5 public static void main(String[] args) {6 int a = 5;7 int b = 10;8 int c = 15;9 int d = 20;10 int e = 25;11 int f = 30;12 int g = 35;13 int h = 40;14 int i = 45;15 int j = 50;16 int k = 55;17 int l = 60;18 int m = 65;19 int n = 70;20 int o = 75;21 int p = 80;22 int q = 85;23 int r = 90;24 int s = 95;25 int t = 100;26 int u = 105;27 int v = 110;28 int w = 115;29 int x = 120;30 int y = 125;31 int z = 130;32 int aa = 135;33 int bb = 140;34 int cc = 145;35 int dd = 150;36 int ee = 155;37 int ff = 160;38 int gg = 165;39 int hh = 170;40 int ii = 175;41 int jj = 180;42 int kk = 185;43 int ll = 190;44 int mm = 195;45 int nn = 200;46 int oo = 205;47 int pp = 210;48 int qq = 215;49 int rr = 220;50 int ss = 225;51 int tt = 230;52 int uu = 235;53 int vv = 240;54 int ww = 245;55 int xx = 250;56 int yy = 255;57 int zz = 260;58 int aaa = 265;59 int bbb = 270;60 int ccc = 275;61 int ddd = 280;

Full Screen

Full Screen

HeuristicsForJumps

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;2import java.util.ArrayList;3import java.util.List;4public class HeuristicTest {5 public static void main(String[] args) {6 HeuristicsForJumps heuristicsForJumps = new HeuristicsForJumps();7 List<Boolean> jumps = new ArrayList<>();8 jumps.add(true);9 jumps.add(false);10 jumps.add(false);11 jumps.add(false);12 double heuristic = heuristicsForJumps.calculateHeuristic(jumps);13 System.out.println(heuristic);14 }15}16import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;17import java.util.ArrayList;18import java.util.List;19public class HeuristicTest {20 public static void main(String[] args) {21 HeuristicsForJumps heuristicsForJumps = new HeuristicsForJumps();22 List<Boolean> jumps = new ArrayList<>();23 jumps.add(true);24 jumps.add(false);25 jumps.add(true);26 jumps.add(false);27 double heuristic = heuristicsForJumps.calculateHeuristic(jumps);28 System.out.println(heuristic);29 }30}31import org.evomaster.client.java.instrumentation.heuristic.HeuristicsForJumps;32import java.util.ArrayList;33import java.util.List;34public class HeuristicTest {35 public static void main(String[] args) {

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run EvoMaster automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful