Commit f374ed4e authored by ISWB Prasetya's avatar ISWB Prasetya
Browse files

Adding a convenient way to hook a custom primval generator to G2

parent ea4f0ef9
package Sequenic.T3.DerivativeSuiteGens.Gen2;
import static Sequenic.T3.Generator.GenCombinators.OneOfVal;
import java.io.Serializable;
import Sequenic.T3.Generator.Generator;
import Sequenic.T3.JavaType.JTypeUtils;
import Sequenic.T3.Sequence.Datatype.PARAM;
public class CustomGenDSL {
static boolean ofOfTheseTypes(PARAM P, Class... cls) {
Class C = JTypeUtils.getTopClass(P.ty) ;
for (Class D : cls) {
if(C == D) return true ;
}
return false ;
}
public static Generator<PARAM,Serializable> OneOfVal_(Float... f) {
Serializable[] f_ = f ;
Generator<PARAM,Serializable> gen = OneOfVal(f_) ;
return gen.If(P -> ofOfTheseTypes(P,Float.class, Float.TYPE)) ;
}
public static Generator<PARAM,Serializable> OneOfVal_(Integer... f) {
Serializable[] f_ = f ;
Generator<PARAM,Serializable> gen = OneOfVal(f_) ;
return gen.If(P -> ofOfTheseTypes(P,Integer.class, Integer.TYPE)) ;
}
public static Generator<PARAM,Serializable> OneOfVal_(String... f) {
Serializable[] f_ = f ;
Generator<PARAM,Serializable> gen = OneOfVal(f_) ;
return gen.If(P -> ofOfTheseTypes(P,String.class)) ;
}
}
......@@ -111,7 +111,7 @@ public class G2 {
*/
protected G2(String CUTname, G2Config config) throws Exception {
this.config = config ;
g2sg = new G2SuiteGen(CUTname,config.CUTrootDir, config.useCoverageGuidance,config.useStaticInfo,config.dirOfStaticInfo) ;
g2sg = new G2SuiteGen(CUTname,config.CUTrootDir, config.useCoverageGuidance,config.useStaticInfo,config.dirOfStaticInfo,config.customPrimitivesGenerator) ;
g2sg.regressionMode = config.regressionMode ;
g2sg.injectOracles = config.injectOracles ;
g2sg.maxSuffixLength = config.maxSuffixLength ;
......
package Sequenic.T3.DerivativeSuiteGens.Gen2;
import java.io.Serializable;
import Sequenic.T3.Generator.Generator;
import Sequenic.T3.Sequence.Datatype.PARAM;
public class G2Config {
public String CUTrootDir ;
......@@ -66,6 +71,14 @@ public class G2Config {
// controlling how violating sequences are shown:
public boolean showExcExecution = true ;
public int showLength = 4 ;
public int showDepth = 5 ;
public int showDepth = 5 ;
public Generator<PARAM,Serializable> customPrimitivesGenerator = null ;
public G2Config usePrimitiveGenerator(Generator<PARAM,Serializable> gen) {
customPrimitivesGenerator = gen ;
return this ;
}
}
......@@ -20,6 +20,7 @@ package Sequenic.T3.DerivativeSuiteGens.Gen2;
import static Sequenic.T3.Generator.GenCombinators.SequenceWhile;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
......@@ -82,13 +83,13 @@ public class G2SuiteGen {
public float fieldUpdateProbability = 0.3f ;
private G2SuiteGen() {}
//private G2SuiteGen() {}
public G2SuiteGen(String CUTname,
String CUTrootDir)
throws Exception
{
this(CUTname,CUTrootDir,true,true,null) ;
this(CUTname,CUTrootDir,true,true,null,null) ;
}
/**
......@@ -100,7 +101,9 @@ public class G2SuiteGen {
String CUTrootDir,
boolean useCoverageGuidance,
boolean useStaticInfo,
String staticInfoDir)
String staticInfoDir,
Generator<PARAM,Serializable> customPrimValGenerator
)
throws Exception
{
//t3log.info("** CUT = " + CUT.getName());
......@@ -137,11 +140,11 @@ public class G2SuiteGen {
}
}
configure(CUT,staticInfoDir) ;
configure(CUT,staticInfoDir,customPrimValGenerator) ;
}
// configuring other stuffs ; called from the constructor.
private void configure(Class CUT, String staticInfoDir) {
private void configure(Class CUT, String staticInfoDir, Generator<PARAM,Serializable> customPrimValGenerator) {
// Be careful with the order of setting up!
// setting up implementation-map and testing scope:
......@@ -176,7 +179,7 @@ public class G2SuiteGen {
}
// setup value generator, and custom pool:
gen2vmg = new G2ValueMG(imap) ;
gen2vmg = new G2ValueMG(imap,customPrimValGenerator) ;
resetSeededConstants() ;
pool = gen2vmg.pool ;
valueMG = gen2vmg.valueMG() ;
......
......@@ -27,6 +27,7 @@ import java.util.Map.Entry;
import Sequenic.T3.ImplementationMap;
import Sequenic.T3.Pool;
import Sequenic.T3.T3Random;
import Sequenic.T3.Generator.GenCombinators;
import Sequenic.T3.Generator.Generator;
import Sequenic.T3.Generator.Value.*;
import Sequenic.T3.JavaType.JTfun;
......@@ -43,6 +44,12 @@ public class G2ValueMG {
public G2Pool pool ;
/**
* Two ways to provide custom control on generating primitive values. One is by supplying
* an explicit generator (below). And the other is by providing a list of constants.
*/
public Generator<PARAM,Serializable> customPrimValGenerator = null ;
/**
* primitive values and strings that are seeded (more likely to be needed):
*/
......@@ -122,7 +129,7 @@ public class G2ValueMG {
}
}
public G2ValueMG(ImplementationMap ImpsMap) {
public G2ValueMG(ImplementationMap ImpsMap, Generator<PARAM,Serializable> customPrimValGenerator) {
String chars = "abcdefghijklmnopqrstuvwxyz" ;
chars = chars
+ chars.toUpperCase()
......@@ -137,6 +144,7 @@ public class G2ValueMG {
collectiongens = new CollectionLikeMG(collectionSize,ImpsMap) ;
lambdagens = new LamdaMG(ImpsMap) ;
refgens = new REFMG(pool) ;
this.customPrimValGenerator = customPrimValGenerator ;
}
/**
......@@ -384,7 +392,17 @@ public class G2ValueMG {
}
else {
toss = toss - chanceToPickPreviousPrimitive ;
if (toss < chanceToPickSeededPrimitive) o = getRandomSeededSPrimitive(ty) ;
if (toss < chanceToPickSeededPrimitive) {
o = null ;
if (customPrimValGenerator != null) {
if (rnd.nextFloat() <= 0.5) {
// 50% chance to pick using the custom generator
Maybe<Serializable> o_ = customPrimValGenerator.generate(P) ;
if(o_ != null) o = o_.val ;
}
}
if(o==null) o = getRandomSeededSPrimitive(ty) ;
}
else {
toss = toss - chanceToPickSeededPrimitive ;
if (toss < chanceToPickNeighbor) o = getRandomNeighbor(ty) ;
......@@ -436,7 +454,7 @@ public class G2ValueMG {
* This will construct the meta-generator to generate values.
*/
public Generator<PARAM,STEP> valueMG() {
FUN<Generator<PARAM,STEP>> recGenerator = new FUN<Generator<PARAM,STEP>>() ;
Generator<PARAM,STEP> g =
FirstOf(
......
......@@ -9,7 +9,7 @@ public class Test_G2SuiteGen_forCUTwith_constants {
static void genWithG2() throws Exception{
String staticInfoDir = "/Users/iswbprasetya/tmp/t3" ;
Class CUT = Sequenic.T3.Examples.IncomeTax.class ;
G2SuiteGen g2 = new G2SuiteGen(CUT.getName(),null,true,true,staticInfoDir) ;
G2SuiteGen g2 = new G2SuiteGen(CUT.getName(),null,true,true,staticInfoDir,null) ;
g2.maxSuffixLength = 0 ;
g2.regressionMode = true ;
int N0 = Math.min(500, Math.max(10, g2.staticInfo.allConstants.length * 20)) ;
......
package Sequenic.T3.DerivativeSuiteGens.Gen2;
import static Sequenic.T3.DerivativeSuiteGens.Gen2.CustomGenDSL.* ;
public class Test_G2_CustomPrimValGen {
static G2Config config() {
G2Config config = new G2Config() ;
config.regressionMode = true ;
config.CUTrootDir = "/Users/iswbprasetya/eclipseWorkspace/t3Workspace/t3/bin" ;
config.dirToSaveSuites = "/Users/iswbprasetya/tmp/t3" ;
config.dirOfStaticInfo = "/Users/iswbprasetya/tmp/t3" ;
config.generateJunitForEachSuite = false ;
config.maxPrefixLength = 6 ;
config.maxSuffixLength = 1 ;
config.numberOfPrefixes = 40 ;
// config.refinementHeuristic="evo" ;
return config ;
}
static void genWithG2() throws Exception {
G2Config config = config() ;
config.customPrimitivesGenerator = OneOfVal_(777f,707f) ;
G2.generateSuites("Sequenic.T3.Examples.Triangle1",config,60000) ;
}
static public void main(String[] args) throws Throwable {
genWithG2() ;
}
}
......@@ -60,6 +60,16 @@ public class GenCombinators {
} ;
}
/**
* Randomly chooses one of the values. The condition is ignored.
*/
public static <R,T> Generator<R,T> OneOfVal(T ... values) {
return R1 -> {
int k = T3Random.getRnd().nextInt(values.length) ;
return new Maybe(values[k]) ;
} ;
}
/**
* Just another name for OneOf
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment