github.com/leanovate/gopter@v0.2.9/gen/integers_shrink.go (about) 1 package gen 2 3 import ( 4 "github.com/leanovate/gopter" 5 ) 6 7 type int64Shrink struct { 8 original int64 9 half int64 10 } 11 12 func (s *int64Shrink) Next() (interface{}, bool) { 13 if s.half == 0 { 14 return nil, false 15 } 16 value := s.original - s.half 17 s.half /= 2 18 return value, true 19 } 20 21 type uint64Shrink struct { 22 original uint64 23 half uint64 24 } 25 26 func (s *uint64Shrink) Next() (interface{}, bool) { 27 if s.half == 0 { 28 return nil, false 29 } 30 value := s.original - s.half 31 s.half >>= 1 32 return value, true 33 } 34 35 // Int64Shrinker is a shrinker for int64 numbers 36 func Int64Shrinker(v interface{}) gopter.Shrink { 37 negShrink := int64Shrink{ 38 original: -v.(int64), 39 half: -v.(int64), 40 } 41 posShrink := int64Shrink{ 42 original: v.(int64), 43 half: v.(int64) / 2, 44 } 45 return gopter.Shrink(negShrink.Next).Interleave(gopter.Shrink(posShrink.Next)) 46 } 47 48 // UInt64Shrinker is a shrinker for uint64 numbers 49 func UInt64Shrinker(v interface{}) gopter.Shrink { 50 shrink := uint64Shrink{ 51 original: v.(uint64), 52 half: v.(uint64), 53 } 54 return shrink.Next 55 } 56 57 // Int32Shrinker is a shrinker for int32 numbers 58 func Int32Shrinker(v interface{}) gopter.Shrink { 59 return Int64Shrinker(int64(v.(int32))).Map(int64To32) 60 } 61 62 // UInt32Shrinker is a shrinker for uint32 numbers 63 func UInt32Shrinker(v interface{}) gopter.Shrink { 64 return UInt64Shrinker(uint64(v.(uint32))).Map(uint64To32) 65 } 66 67 // Int16Shrinker is a shrinker for int16 numbers 68 func Int16Shrinker(v interface{}) gopter.Shrink { 69 return Int64Shrinker(int64(v.(int16))).Map(int64To16) 70 } 71 72 // UInt16Shrinker is a shrinker for uint16 numbers 73 func UInt16Shrinker(v interface{}) gopter.Shrink { 74 return UInt64Shrinker(uint64(v.(uint16))).Map(uint64To16) 75 } 76 77 // Int8Shrinker is a shrinker for int8 numbers 78 func Int8Shrinker(v interface{}) gopter.Shrink { 79 return Int64Shrinker(int64(v.(int8))).Map(int64To8) 80 } 81 82 // UInt8Shrinker is a shrinker for uint8 numbers 83 func UInt8Shrinker(v interface{}) gopter.Shrink { 84 return UInt64Shrinker(uint64(v.(uint8))).Map(uint64To8) 85 } 86 87 // IntShrinker is a shrinker for int numbers 88 func IntShrinker(v interface{}) gopter.Shrink { 89 return Int64Shrinker(int64(v.(int))).Map(int64ToInt) 90 } 91 92 // UIntShrinker is a shrinker for uint numbers 93 func UIntShrinker(v interface{}) gopter.Shrink { 94 return UInt64Shrinker(uint64(v.(uint))).Map(uint64ToUint) 95 }