github.com/leanovate/gopter@v0.2.9/gen_parameters.go (about) 1 package gopter 2 3 import ( 4 "math/rand" 5 "time" 6 ) 7 8 // GenParameters encapsulates the parameters for all generators. 9 type GenParameters struct { 10 MinSize int 11 MaxSize int 12 MaxShrinkCount int 13 Rng *rand.Rand 14 } 15 16 // WithSize modifies the size parameter. The size parameter defines an upper bound for the size of 17 // generated slices or strings. 18 func (p *GenParameters) WithSize(size int) *GenParameters { 19 newParameters := *p 20 newParameters.MaxSize = size 21 return &newParameters 22 } 23 24 // NextBool create a random boolean using the underlying Rng. 25 func (p *GenParameters) NextBool() bool { 26 return p.Rng.Int63()&1 == 0 27 } 28 29 // NextInt64 create a random int64 using the underlying Rng. 30 func (p *GenParameters) NextInt64() int64 { 31 v := p.Rng.Int63() 32 if p.NextBool() { 33 return -v 34 } 35 return v 36 } 37 38 // NextUint64 create a random uint64 using the underlying Rng. 39 func (p *GenParameters) NextUint64() uint64 { 40 first := uint64(p.Rng.Int63()) 41 second := uint64(p.Rng.Int63()) 42 43 return (first << 1) ^ second 44 } 45 46 // CloneWithSeed clone the current parameters with a new seed. 47 // This is useful to create subsections that can rerun (provided you keep the 48 // seed) 49 func (p *GenParameters) CloneWithSeed(seed int64) *GenParameters { 50 return &GenParameters{ 51 MinSize: p.MinSize, 52 MaxSize: p.MaxSize, 53 MaxShrinkCount: p.MaxShrinkCount, 54 Rng: rand.New(NewLockedSource(seed)), 55 } 56 } 57 58 // DefaultGenParameters creates default GenParameters. 59 func DefaultGenParameters() *GenParameters { 60 seed := time.Now().UnixNano() 61 62 return &GenParameters{ 63 MinSize: 0, 64 MaxSize: 100, 65 MaxShrinkCount: 1000, 66 Rng: rand.New(NewLockedSource(seed)), 67 } 68 } 69 70 // MinGenParameters creates minimal GenParameters. 71 // Note: Most likely you do not want to use these for actual testing 72 func MinGenParameters() *GenParameters { 73 seed := time.Now().UnixNano() 74 75 return &GenParameters{ 76 MinSize: 0, 77 MaxSize: 0, 78 MaxShrinkCount: 0, 79 Rng: rand.New(NewLockedSource(seed)), 80 } 81 }