package Sequenic.T2ext.Selection; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import junit.framework.Assert; import org.junit.Test; import Sequenic.T2.Coverage.BasicPath; import Sequenic.T2ext.Instrumenter.CFG; import Sequenic.T2ext.Instrumenter.MethodInstrumenter; import Sequenic.T2ext.Instrumenter.Node; import Sequenic.T2ext.Instrumenter.PrimePathCalculator; public class SyntheticCoverage { public Object exampleMethod() { int i=hashCode(); if(i==0) return this; else { while(i>0) i-=1000; } return null; } public Object exampleMethod2() { if(!Collections.EMPTY_SET.isEmpty()) throw new Error("Empty set is not empty."); else { while(Collections.EMPTY_SET.size()>0) System.err.println("Empty set still not empty"); } return Collections.EMPTY_SET; } /** * This test makes two CFGs of exampleMethod and renumbers one of them to create a 'modified' CFG. * It then checks whether all primepaths are correctly identified to be non-modification-traversing * and if the updated coverage information (i.e. renumbered paths) are correctly synthesized. */ @Test public void test1() { MethodInstrumenter mi = null; try { mi = MethodInstrumenter.createMethodInstrumenter( this.getClass().getDeclaredMethod("exampleMethod", new Class[] {}), this.getClass()); } catch (Exception e) { fail(e.getMessage()); e.printStackTrace(); } //get a CFG of exampleMethod. This is our base CFG. CFG cfgA = mi.doInstrumenting(false); //get a second CFG and renumber it. This is our modified CFG. CFG cfgB = mi.doInstrumenting(false); for(Node n : cfgB.getNodes()) n.setId(n.getId() * 2 ); ISelector selectorRH = new RegressionTestSelector(Collections.singleton(cfgA),Collections.singleton(cfgB), RegressionTestSelector.RothermelHarrold); System.out.println(selectorRH); ISelector selectorBP = new RegressionTestSelector(Collections.singleton(cfgA),Collections.singleton(cfgB), RegressionTestSelector.BallPartial); cfgA.simplePrint(); cfgB.simplePrint(); System.out.println(selectorBP); //We need BasicPaths for the selector LinkedList bps = new LinkedList(); //Get some paths in cfgA, we'll just use all prime paths. PrimePathCalculator.calculatePrimePath(cfgA); for(LinkedList origpath : cfgA.getTargetPaths()) { int[] path = new int[origpath.size()]; int i = 0; for(Node n : origpath) path[i++] = n.getId(); if(path[0]==0)//We are only interested in paths which start at the startnode bps.add(BasicPath.create(cfgA.getMethodName(), path)); } for(BasicPath path : bps) { assertFalse(selectorRH.isModificationTraversing(path)); assertFalse(selectorBP.isModificationTraversing(path)); int[] rhNodes = selectorRH.synthesizeCoverage(path).getNodes(); int[] bpNodes = selectorBP.synthesizeCoverage(path).getNodes(); int i = 0; for(int id : path.getNodes()) assertEquals(id*2, rhNodes[i++]); Assert.assertTrue(Arrays.equals(rhNodes, bpNodes)); } } }