diff --git a/derived_gen.go b/derived_gen.go index cc3f434..62fd09a 100644 --- a/derived_gen.go +++ b/derived_gen.go @@ -106,7 +106,7 @@ func DeriveGen(downstream interface{}, upstream interface{}, gens ...Gen) Gen { sieves := make([]func(interface{}) bool, len(gens)) shrinkers := make([]Shrinker, len(gens)) for i, gen := range gens { - result := gen(DefaultGenParameters()) + result := gen(DefaultGenParams) sieves[i] = result.Sieve shrinkers[i] = result.Shrinker } diff --git a/gen.go b/gen.go index ebee94c..78fbac0 100644 --- a/gen.go +++ b/gen.go @@ -16,6 +16,10 @@ import ( // If you just plug generators together you do not have to worry about this. type Gen func(*GenParameters) *GenResult +var ( + DefaultGenParams = DefaultGenParameters() +) + // Sample generate a sample value. // Depending on the state of the RNG the generate might fail to provide a sample func (g Gen) Sample() (interface{}, bool) { @@ -48,7 +52,7 @@ func (g Gen) SuchThat(f interface{}) Gen { if checkType.NumIn() != 1 { panic(fmt.Sprintf("Param of SuchThat has to be a func with one param, but is %v", checkType.NumIn())) } else { - genResultType := g(DefaultGenParameters()).ResultType + genResultType := g(DefaultGenParams).ResultType if !genResultType.AssignableTo(checkType.In(0)) { panic(fmt.Sprintf("Param of SuchThat has to be a func with one param assignable to %v, but is %v", genResultType, checkType.In(0))) } @@ -102,7 +106,7 @@ func (g Gen) Map(f interface{}) Gen { if mapperType.NumIn() != 1 { panic(fmt.Sprintf("Param of Map has to be a func with one param, but is %v", mapperType.NumIn())) } else { - genResultType := g(DefaultGenParameters()).ResultType + genResultType := g(DefaultGenParams).ResultType if !genResultType.AssignableTo(mapperType.In(0)) { panic(fmt.Sprintf("Param of Map has to be a func with one param assignable to %v, but is %v", genResultType, mapperType.In(0))) } diff --git a/gen_test.go b/gen_test.go index 5a04b3b..dc40668 100644 --- a/gen_test.go +++ b/gen_test.go @@ -22,6 +22,32 @@ func TestGenSample(t *testing.T) { } } +func BenchmarkMap(b *testing.B) { + for i := 0; i < b.N; i++ { + gen := constGen("sample") + var mappedWith string + mapper := func(v string) string { + mappedWith = v + return "other" + } + value, ok := gen.Map(mapper).Sample() + if !ok || value != "other" { + b.Errorf("Invalid gen sample: %#v", value) + } + if mappedWith != "sample" { + b.Errorf("Invalid mapped with: %#v", mappedWith) + } + + gen = gen.SuchThat(func(interface{}) bool { + return false + }) + value, ok = gen.Map(mapper).Sample() + if ok { + b.Errorf("Invalid gen sample: %#v", value) + } + } +} + func TestGenMap(t *testing.T) { gen := constGen("sample") var mappedWith string