Skip to content

Instantly share code, notes, and snippets.

@vporoshok
Created June 11, 2018 18:17
Show Gist options
  • Save vporoshok/8dd79c4ab2443376c910426702de0094 to your computer and use it in GitHub Desktop.
Save vporoshok/8dd79c4ab2443376c910426702de0094 to your computer and use it in GitHub Desktop.
package coverage
import (
"github.com/leanovate/gopter"
)
func CoverageGen(mod uint32, count uint32) gopter.Gen {
return func(params *gopter.GenParameters) *gopter.GenResult {
coverage := make([][]uint32, count)
for i := range coverage {
n := int(uint64(mod/3) + params.NextUint64()%uint64(mod/3))
set := make(map[uint32]struct{}, n)
coverage[i] = make([]uint32, 0, n)
for len(set) < n {
x := uint32(params.NextUint64() % uint64(mod))
if _, ok := set[x]; !ok {
coverage[i] = append(coverage[i], x)
set[x] = struct{}{}
}
}
}
return gopter.NewGenResult(coverage, CoverageShrinker)
}
}
type coverageShrink struct {
coverage [][]uint32
}
func CoverageShrinker(coverage interface{}) gopter.Shrink {
shrink := coverageShrink{coverage.([][]uint32)}
return shrink.Next
}
func (cs *coverageShrink) Next() (interface{}, bool) {
cs.coverage = cs.coverage[:len(cs.coverage)/2]
if len(cs.coverage) == 0 {
return nil, false
}
return cs.coverage, true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment