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

adding few more custom generators

parent 06a8767e
......@@ -32,10 +32,12 @@ import Sequenic.T3.Generator.Generator;
import Sequenic.T3.Generator.Value.*;
import Sequenic.T3.JavaType.JTfun;
import Sequenic.T3.JavaType.JType;
import Sequenic.T3.JavaType.JTypeUtils;
import Sequenic.T3.Reflection.Reflection;
import Sequenic.T3.Sequence.Datatype.*;
import Sequenic.T3.utils.FUN;
import Sequenic.T3.utils.Maybe;
import Sequenic.T3.utils.SomeObject;
/**
*
* Terminology: an SPrimitive value is either a Java primitive value or a string.
......@@ -77,6 +79,7 @@ public class G2ValueMG {
private EnumMG enumgens = new EnumMG();
private NullMG nullgens = new NullMG();
private ObjectMG constructorgens ;
private ClazzMG clazzgens = new ClazzMG() ;
private LamdaMG lambdagens ;
private CollectionLikeMG collectiongens ;
private REFMG refgens ;
......@@ -450,6 +453,51 @@ public class G2ValueMG {
return P ;
}
static Class[] serializableTypes = {
String.class,
Integer.class,
Double.class,
SomeObject.class
} ;
static Class[] ObjectSubclasses = {
String.class,
Integer.class,
Double.class,
SomeObject.class
} ;
private Generator<PARAM,STEP> serializableMG(FUN<Generator<PARAM,STEP>> recGen) {
return P -> {
if (!(P.ty instanceof JTfun)) return null ;
JTfun ty_ = (JTfun) P.ty ;
Class F = ty_.fun ;
if (F == Serializable.class) {
Class R = serializableTypes[T3Random.getRnd().nextInt(serializableTypes.length)] ;
PARAM Q = new PARAM(P.name,JTypeUtils.convert(R)) ;
return recGen.fun.generate(Q) ;
}
return null ;
} ;
}
// When asked to generate literally instance of Object
private Generator<PARAM,STEP> ObjectInstance_MG(FUN<Generator<PARAM,STEP>> recGen) {
return P -> {
if (!(P.ty instanceof JTfun)) return null ;
JTfun ty_ = (JTfun) P.ty ;
Class F = ty_.fun ;
if (F == Object.class) {
Class R = ObjectSubclasses[T3Random.getRnd().nextInt(ObjectSubclasses.length)] ;
PARAM Q = new PARAM(P.name,JTypeUtils.convert(R)) ;
return recGen.fun.generate(Q) ;
}
return null ;
} ;
}
/**
* This will construct the meta-generator to generate values.
*/
......@@ -461,11 +509,14 @@ public class G2ValueMG {
nullgens.constant().WithChance(0.04),
sprimitiveMG(),
enumgens.random(),
clazzgens.classMG(recGenerator),
lambdagens.constlambdaMG(recGenerator),
serializableMG(recGenerator),
ObjectInstance_MG(recGenerator).WithChance(0.3),
refgens.objectUnderTest().WithChance(0.4),
refgens.random().WithChance(0.4),
refgens.objectUnderTestIfSubclass().WithChance(0.1),
collectiongens.collectionlike(recGenerator),
lambdagens.constlambdaMG(recGenerator),
constructorgens.useConstructor(recGenerator).transformReq(P -> remapPARAM_with_InstanceOfConstants(P)).WithChance(0.4),
constructorgens.useConstructor(recGenerator),
constructorgens.useCreationyMethod(recGenerator).transformReq(P -> remapPARAM_with_InstanceOfConstants(P)).WithChance(0.4),
......@@ -492,7 +543,10 @@ public class G2ValueMG {
sprimitiveMG(),
enumgens.random(),
collectiongens.collectionlike(recGenerator),
clazzgens.classMG(recGenerator),
lambdagens.constlambdaMG(recGenerator),
serializableMG(recGenerator),
ObjectInstance_MG(recGenerator).WithChance(0.3),
constructorgens.useConstructor(recGenerator).transformReq(P -> remapPARAM_with_InstanceOfConstants(P)).WithChance(0.4),
constructorgens.useConstructor(recGenerator),
constructorgens.useCreationyMethod(recGenerator).transformReq(P -> remapPARAM_with_InstanceOfConstants(P)).WithChance(0.4),
......
......@@ -4,6 +4,8 @@ import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import Sequenic.T3.T3Random;
/**
* Providing some standard generators; not very sophisticated, but can be borrowed.
*
......@@ -21,6 +23,21 @@ public class SomeObjGenerators {
static public <A,B,C> BiFunction<A,B,C> genConstantBiFunction(A ignored, B alsoignored, C y) {
return (o,p) -> y ;
}
public static enum SomeEnum_ { X1, X2 } ;
static Class[] classes_ = {
String.class,
Integer.class,
Integer.TYPE,
SomeEnum_.class,
Object.class
} ;
static public Class genClass(int k) {
if (k<0) k = -k ;
return classes_[k % classes_.length] ;
}
}
package Sequenic.T3.Generator.Value;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.function.Predicate;
import Sequenic.T3.Generator.Generator;
import Sequenic.T3.Generator.SomeObjGenerators;
import Sequenic.T3.JavaType.JTfun;
import Sequenic.T3.JavaType.JType;
import Sequenic.T3.JavaType.Solver;
import Sequenic.T3.Sequence.Datatype.METHOD;
import Sequenic.T3.Sequence.Datatype.PARAM;
import Sequenic.T3.Sequence.Datatype.STEP;
import Sequenic.T3.utils.FUN;
import Sequenic.T3.utils.Maybe;
/**
* Providing meta-generators for generating instances of "Class".
*/
public class ClazzMG {
Method genclass_ = null ;
public ClazzMG() {
try {
genclass_ = SomeObjGenerators.class.getMethod("genClass", Integer.TYPE) ;
}
catch(Exception e) { }
}
public Generator<PARAM,STEP> classMG(FUN<Generator<PARAM,STEP>> recvalueMetaGenerator) {
return P -> {
if (genclass_ == null) return null ;
if (!(P.ty instanceof JTfun)) return null ;
JTfun ty_ = (JTfun) P.ty ;
Class F = ty_.fun ;
//Map<String, JType> myTySubsts = Solver.solveClassTyvars(ImpsMap, ty_) ;
if (Class.class.isAssignableFrom(F)) {
JType Kty = new JTfun(Integer.class) ;
Maybe<STEP> K = recvalueMetaGenerator.fun.generate(new PARAM("k",Kty)) ;
if (K==null) return null ;
STEP[] args = {K.val} ;
return new Maybe(new METHOD(genclass_, null , args)) ;
} ;
return null ;
} ;
}
static public void main(String[] args) throws NoSuchMethodException, SecurityException {
ClazzMG cmg = new ClazzMG() ;
}
}
......@@ -46,19 +46,6 @@ public class LamdaMG {
Class F = ty_.fun ;
Map<String, JType> myTySubsts = Solver.solveClassTyvars(ImpsMap, ty_) ;
if (Function.class.isAssignableFrom(F)) {
JType Aty = new JTfun(Object.class) ;
JType Bty = new JTfun(Object.class) ;
if (ty_.args.length>=1) Aty = ty_.args[0] ;
if (ty_.args.length>=2) Bty = ty_.args[1] ;
Maybe<STEP> A = recvalueMetaGenerator.fun.generate(new PARAM("ignored",Aty)) ;
if (A==null) return null ;
Maybe<STEP> B = recvalueMetaGenerator.fun.generate(new PARAM("y",Bty)) ;
if (B==null) return null ;
STEP[] args = {A.val,B.val} ;
return new Maybe(new METHOD(funcGen, null , args)) ;
}
if (Predicate.class.isAssignableFrom(F)) {
JType Aty = new JTfun(Object.class) ;
JType Bty = new JTfun(Boolean.class) ;
......@@ -88,6 +75,19 @@ public class LamdaMG {
return new Maybe(new METHOD(bifuncGen, null , args)) ;
}
if (Function.class.isAssignableFrom(F)) {
JType Aty = new JTfun(Object.class) ;
JType Bty = new JTfun(Object.class) ;
if (ty_.args.length>=1) Aty = ty_.args[0] ;
if (ty_.args.length>=2) Bty = ty_.args[1] ;
Maybe<STEP> A = recvalueMetaGenerator.fun.generate(new PARAM("ignored",Aty)) ;
if (A==null) return null ;
Maybe<STEP> B = recvalueMetaGenerator.fun.generate(new PARAM("y",Bty)) ;
if (B==null) return null ;
STEP[] args = {A.val,B.val} ;
return new Maybe(new METHOD(funcGen, null , args)) ;
}
return null ;
} ;
}
......
......@@ -47,6 +47,7 @@ public class ValueMG {
protected NullMG nullgens = new NullMG();
protected ObjectMG constructorgens ;
protected LamdaMG lambdagens ;
protected ClazzMG clazzgens = new ClazzMG() ;
protected CollectionLikeMG collectiongens ;
......@@ -115,6 +116,7 @@ public class ValueMG {
enumgens.random(),
primitiveLike1(),
stringgens.fromSamples(),
clazzgens.classMG(recGenerator),
refgens.objectUnderTest().WithChance(0.4),
refgens.random().WithChance(0.4),
refgens.objectUnderTestIfSubclass().WithChance(0.1),
......
......@@ -5,5 +5,7 @@ import java.io.Serializable;
public class SomeObject implements Serializable {
private static final long serialVersionUID = 1L;
public SomeObject() {}
}
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