How to use hasLinearCost method of org.evomaster.client.java.instrumentation.coverage.methodreplacement.classes.MapClassReplacement class

Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.coverage.methodreplacement.classes.MapClassReplacement.hasLinearCost

Source:MapClassReplacement.java Github

copy

Full Screen

...18 //should never happen...19 throw new RuntimeException(e);20 }21 }22 private static boolean hasLinearCost(Class<? extends Map> klass){23 Boolean isLinear = cacheIsLinearCost.get(klass);24 if(isLinear == null){25 try {26 Method m = klass.getMethod("containsKey",Object.class);27 isLinear = m.equals(linearCostContainsKey);28 } catch (NoSuchMethodException e) {29 isLinear = false;30 }31 cacheIsLinearCost.put(klass, isLinear);32 return isLinear;33 }34 return isLinear;35 }36 @Override37 public Class<?> getTargetClass() {38 return Map.class;39 }40 /*41 This is same code as in Collection.42 However, Map does not extend Collection...43 so, in theory, could have a Map that is not a Collection...44 */45 @Replacement(type = ReplacementType.BOOLEAN, category = ReplacementCategory.EXT_0)46 public static boolean isEmpty(Map caller, String idTemplate) {47 Objects.requireNonNull(caller);48 boolean result = caller.isEmpty();49 if (idTemplate == null) {50 return result;51 }52 int len = caller.size();53 Truthness t = TruthnessUtils.getTruthnessToEmpty(len);54 ExecutionTracer.executedReplacedMethod(idTemplate, ReplacementType.BOOLEAN, t);55 return result;56 }57 @Replacement(type = ReplacementType.BOOLEAN, category = ReplacementCategory.BASE)58 public static boolean containsKey(Map c, Object o, String idTemplate) {59 Objects.requireNonNull(c);60 if (c instanceof IdentityHashMap) {61 /*62 IdentityHashMap does not use .equals() for the comparisons63 */64 return c.containsKey(o);65 }66 /*67 keySet() returns a set instance that indirectly calls68 to containsKey() when doing a contains().69 In order to avoid a stack overflow in classes sub-classing JDK70 collections, we compute a fresh collection.71 An example is StringHashMap from RestAssured.72 The problem though is this can be come quickly very, very73 expensive...74 NOTE: due to changes in ReplacementList, this does not seem a problem75 anymore, due to how we handle subclasses. See comments in that class.76 */77 //Collection keyCollection = new HashSet(c.keySet());78 Collection keyCollection = c.keySet();79 CollectionsDistanceUtils.evaluateTaint(keyCollection, o);80 /*81 Even if we skip instrumenting subclass calls, we still have issues.82 For example, in Guava Maps, contains() is implemented as83 map().containsKey()84 which still leads to infinite recursion, due to map() returning85 a reference to Map, and also due to the keyset be a non-JDK subclass as well.86 So, we still need to look at the actual concrete class, and not just the ref.87 Skipping it is not so great, but is there any alternative?88 However, if still can evaluate taint, then should not be a big problem89 */90 if(!keyCollection.getClass().getName().startsWith("java.util")){91 return c.containsKey(o);92 }93 boolean result = keyCollection.contains(o);94 if (idTemplate == null) {95 return result;96 }97 Truthness t;98 if (result) {99 t = new Truthness(1d, DistanceHelper.H_NOT_NULL);100 } else {101 double h = CollectionsDistanceUtils.getHeuristicToContains(keyCollection, o);102 t = new Truthness(h, 1d);103 }104 ExecutionTracer.executedReplacedMethod(idTemplate, ReplacementType.BOOLEAN, t);105 return result;106 }107 @Replacement(type = ReplacementType.OBJECT, category = ReplacementCategory.EXT_0)108 public static Object get(Map map, Object key, String idTemplate){109 Objects.requireNonNull(map);110 if(! (map instanceof IdentityHashMap)) {111 try {112 //check Map.containsKey is not from AbstractMap, which is O(n). Case for Kotlin's ZipEntryMap113 if(! hasLinearCost(map.getClass())) {114 containsKey(map, key, idTemplate);115 }116 } catch (Exception e) {117 //do nothing118 //this does actually happen in Kotlin for ConcurrentRefValueHashMap throwing a IncorrectOperationException119 }120 }121 return map.get(key);122 }123 @Replacement(type = ReplacementType.OBJECT, category = ReplacementCategory.EXT_0)124 public static Object getOrDefault(Map map, Object key, Object defaultValue, String idTemplate) {125 get(map,key,idTemplate);//compute taint + heuristics126 return map.getOrDefault(key,defaultValue);127 }...

Full Screen

Full Screen

hasLinearCost

Using AI Code Generation

copy

Full Screen

1public class MapClassReplacementTest {2 public void testHasLinearCost() {3 Map<Integer, String> map = new HashMap<>();4 map.put(1, "1");5 map.put(2, "2");6 map.put(3, "3");7 map.put(4, "4");8 map.put(5, "5");9 map.put(6, "6");10 map.put(7, "7");11 map.put(8, "8");12 map.put(9, "9");13 map.put(10, "10");14 map.put(11, "11");15 map.put(12, "12");16 map.put(13, "13");17 map.put(14, "14");18 map.put(15, "15");19 map.put(16, "16");20 map.put(17, "17");21 map.put(18, "18");22 map.put(19, "19");23 map.put(20, "20");24 map.put(21, "21");25 map.put(22, "22");26 map.put(23, "23");27 map.put(24, "24");28 map.put(25, "25");29 map.put(26, "26");30 map.put(27, "27");31 map.put(28, "28");32 map.put(29, "29");33 map.put(30, "30");34 map.put(31, "31");35 map.put(32, "32");36 map.put(33, "33");37 map.put(34, "34");38 map.put(35, "35");39 map.put(36, "36");40 map.put(37, "37");41 map.put(38, "38");42 map.put(39, "39");43 map.put(40, "40");44 map.put(41, "41");45 map.put(42, "42");46 map.put(43, "43");47 map.put(44, "44");48 map.put(45, "45");49 map.put(46, "46");50 map.put(47, "47");51 map.put(48, "48");52 map.put(49, "49");53 map.put(50, "50");54 map.put(51, "51");55 map.put(52, "52

Full Screen

Full Screen

hasLinearCost

Using AI Code Generation

copy

Full Screen

1public void checkCoverage() {2 Map<String, String> map = new HashMap<>();3 map.put("1", "1");4 map.put("2", "2");5 map.put("3", "3");6 assertTrue(MapClassReplacement.hasLinearCost(map));7}8import com.foo.Foo;9import io.restassured.RestAssured;10import io.restassured.http.ContentType;11import org.evomaster.client.java.instrumentation.coverage.methodreplacement.classes.MapClassReplacement;12import org.junit.jupiter.api.Test;13import static io.restassured.RestAssured.given;14import static org.hamcrest.CoreMatchers.is;15import static org.junit.jupiter.api.Assertions.assertTrue;16public class FooEMTest {17 public void test0() throws Exception {18 Map<String, String> map = new HashMap<>();19 map.put("1", "1");20 map.put("2", "2");21 map.put("3", "3");22 assertTrue(MapClassReplacement.hasLinearCost(map));23 }24}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful