Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.example.branches.BranchesInstrumentedTest.evalEq
Source:BranchesInstrumentedTest.java
...27 } catch (Exception e) {28 throw new RuntimeException(e);29 }30 }31 private int evalEq(int x, int y){32 try {33 return getInstance().eq(x,y);34 } catch (Exception e) {35 throw new RuntimeException(e);36 }37 }38 @BeforeAll39 public static void initClass(){40 ObjectiveRecorder.reset(true);41 }42 @BeforeEach43 public void init(){44 ObjectiveRecorder.reset(false);45 ExecutionTracer.reset();46 assertEquals(0 , ExecutionTracer.getNumberOfObjectives());47 }48 @Test49 public void testPosX(){50 int res = evalPos(10, 0);51 //first branch should had been taken52 assertEquals(0, res);53 //so far, seen only first "if", of which the else is not covered54 assertEquals(2, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));55 assertEquals(1, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));56 String elseBranch = ExecutionTracer.getNonCoveredObjectives(ObjectiveNaming.BRANCH).iterator().next();57 assertTrue(elseBranch.contains(ObjectiveNaming.FALSE_BRANCH));58 assertFalse(elseBranch.contains(ObjectiveNaming.TRUE_BRANCH));59 double first = ExecutionTracer.getValue(elseBranch);60 assertTrue(first < 1d); // not covered61 evalPos(15, 0); //worse value, should have no impact62 double second = ExecutionTracer.getValue(elseBranch);63 assertTrue(second < 1d); // still not covered64 assertEquals(first, second, 0.001);65 evalPos(8, 0); //better value66 double third = ExecutionTracer.getValue(elseBranch);67 assertTrue(third < 1d); // still not covered68 assertTrue(third > first);69 }70 @Test71 public void testPosY() {72 assertEquals(0d, ObjectiveRecorder.computeCoverage(ObjectiveNaming.BRANCH));73 int res;74 res = evalPos(10, 0);75 assertEquals(0, res);76 res = evalPos(-5, 4);77 assertEquals(1, res);78 //seen 2 "if", but returned on the second "if"79 assertEquals(4, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));80 assertEquals(1, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));81 String elseBranch = ExecutionTracer.getNonCoveredObjectives(ObjectiveNaming.BRANCH).iterator().next();82 assertTrue(elseBranch.contains(ObjectiveNaming.FALSE_BRANCH));83 assertFalse(elseBranch.contains(ObjectiveNaming.TRUE_BRANCH));84 double first = ExecutionTracer.getValue(elseBranch);85 assertTrue(first < 1d); // not covered86 evalPos(-8, 8); //worse value, should have no impact87 double second = ExecutionTracer.getValue(elseBranch);88 assertTrue(second < 1d); // still not covered89 assertEquals(first, second, 0.001);90 evalPos(-8, 0); //better value, but still not covered91 double third = ExecutionTracer.getValue(elseBranch);92 assertTrue(third < 1d); // still not covered93 assertTrue(third > second);94 //all branches covered95 res = evalPos(-89, -45);96 assertEquals(2, res);97 assertEquals(4, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));98 assertEquals(0, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));99 assertTrue(ObjectiveRecorder.computeCoverage(ObjectiveNaming.BRANCH) > 0);100 }101 @Test102 public void testNegX(){103 int res = evalNeg(-10, 0);104 //first branch should had been taken105 assertEquals(3, res);106 //so far, seen only first "if", of which the else is not covered107 assertEquals(2, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));108 assertEquals(1, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));109 String elseBranch = ExecutionTracer.getNonCoveredObjectives(ObjectiveNaming.BRANCH).iterator().next();110 assertTrue(elseBranch.contains(ObjectiveNaming.FALSE_BRANCH));111 assertFalse(elseBranch.contains(ObjectiveNaming.TRUE_BRANCH));112 double first = ExecutionTracer.getValue(elseBranch);113 assertTrue(first < 1d); // not covered114 evalNeg(-15, 0); //worse value, should have no impact115 double second = ExecutionTracer.getValue(elseBranch);116 assertTrue(second < 1d); // still not covered117 assertEquals(first, second, 0.001);118 evalNeg(-8, 0); //better value119 double third = ExecutionTracer.getValue(elseBranch);120 assertTrue(third < 1d); // still not covered121 assertTrue(third > first);122 }123 @Test124 public void testNegY() {125 int res;126 res = evalNeg(-10, 0);127 assertEquals(3, res);128 res = evalNeg(5, -4);129 assertEquals(4, res);130 //seen 2 "if", but returned on the second "if"131 assertEquals(4, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));132 assertEquals(1, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));133 String elseBranch = ExecutionTracer.getNonCoveredObjectives(ObjectiveNaming.BRANCH).iterator().next();134 assertTrue(elseBranch.contains(ObjectiveNaming.FALSE_BRANCH));135 assertFalse(elseBranch.contains(ObjectiveNaming.TRUE_BRANCH));136 double first = ExecutionTracer.getValue(elseBranch);137 assertTrue(first < 1d); // not covered138 evalNeg(8, -8); //worse value, should have no impact139 double second = ExecutionTracer.getValue(elseBranch);140 assertTrue(second < 1d); // still not covered141 assertEquals(first, second, 0.001);142 evalNeg(8, 0); //better value, but still not covered143 double third = ExecutionTracer.getValue(elseBranch);144 assertTrue(third < 1d); // still not covered145 assertTrue(third > second);146 //all branches covered147 res = evalNeg(89, 45);148 assertEquals(5, res);149 assertEquals(4, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));150 assertEquals(0, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));151 }152 @Test153 public void testEq(){154 int res;155 res = evalEq(0, 0);156 assertEquals(6, res);157 assertEquals(2, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));158 assertEquals(1, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));159 res = evalEq(2, 5);160 assertEquals(7, res);161 assertEquals(4, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));162 assertEquals(1, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));163 res = evalEq(2, 0);164 assertEquals(8, res);165 assertEquals(4, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));166 assertEquals(0, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));167 }168 @Test169 public void testAll(){170 evalPos(1,1);171 evalPos(-1, 1);172 evalPos(-1, -1);173 evalNeg(-1, -1);174 evalNeg(1, -1);175 evalNeg(1, 1);176 evalEq(0, 0);177 evalEq(4, 0);178 evalEq(5, 5);179 assertEquals(12, ExecutionTracer.getNumberOfObjectives(ObjectiveNaming.BRANCH));180 assertEquals(0, ExecutionTracer.getNumberOfNonCoveredObjectives(ObjectiveNaming.BRANCH));181 }182}...
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!!