Created
June 11, 2018 18:17
-
-
Save vporoshok/8dd79c4ab2443376c910426702de0094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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