Skip to content

Commit

Permalink
Merge pull request #30 from talgendler/master
Browse files Browse the repository at this point in the history
Improve performance of Map, SuchThat and DeriveGen - #29
  • Loading branch information
untoldwind authored Mar 16, 2018
2 parents bd58674 + 7f08f0b commit f778776
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion derived_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)))
}
Expand Down Expand Up @@ -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)))
}
Expand Down
26 changes: 26 additions & 0 deletions gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f778776

Please sign in to comment.